#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::ArtistRoyalty::Fast::Static::Terms;

use strict;
use Data::Dumper;
use File::Path;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';
use Common::RSApp;

use Data::Dumper;
use DB_File;

use Common::Log;

use base 'RPS::ArtistRoyalty::Fast::Static';

use constant kArtistContractTermID => 0;
use constant kArtistContractID     => 1;
use constant kPriority             => 2;
use constant kIncomeSourceID       => 3;
use constant kContractTermSourceID => 4;
use constant kRegionID             => 5;
use constant kChannelID            => 6;
use constant kPriceLevelID         => 7;
use constant kContractRateTypeID   => 8;
use constant kRate                 => 9;
use constant kRateReduction        => 10;
use constant kPercentageOfSales    => 11;
use constant kPackagingDeduction   => 12;
use constant kFreeGoodsDeduction   => 13;
use constant kArtistPayeeID        => 14;
use constant kReserveRate          => 15;

sub FileName            { 'contract_terms' }
sub CacheQueryTableList { "'new_artist_contract','new_artist_contract_term','artist_payee'" }

sub _Create {
    my ( $class, $filename, $payorID ) = @_;

    my %hash;

    my $dbo = Common::RSApp::GetClientDB();

    my $sql = qq/
    SELECT 
    artist_contract_term_id,
    artist_contract_id,
    priority,
    income_source_id,
    contract_term_source_id,
    region_id,
    channel_id,
    price_level_id,
    contract_rate_type_id,
    rate,
    rate_reduction,
    percentage_of_sales,
    packaging_deduction,
    free_goods_deduction,
    artist_payee_id,
    reserve_rate
    FROM new_artist_contract_term
    JOIN new_artist_contract USING (artist_contract_id)
    JOIN artist_payee USING (artist_payee_id)
    WHERE (new_artist_contract_term.inactive = 0 OR new_artist_contract_term.inactive is null)
    AND artist_payee.status IN (1,2)
    AND payor_id=$payorID
    /;

    my $productSTH = $dbo->DoCmd($sql);
    while ( my @data = $productSTH->fetchrow_array() ) {
        $hash{ $data[0] } = \@data;
    }

    $Data::Dumper::Terse = 1;
    open OUTFH, "> $filename" or die "ERROR: $!";
    print OUTFH Dumper( \%hash );
    close OUTFH;
}

sub GetContractTermData {
    my ( $class, $dataPath ) = @_;
    my $filename = "$dataPath/" . $class->FileName();

    my $data = do $filename or die "ERROR: $!";

    return $data;
}

1;

