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

package RPS::ArtistRoyalty::Fast::Static::LiquidationSchedules;

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

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

use RPS::DB::Item::ReserveLiquidation;

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            { 'liquidation_schedules' }
sub CacheQueryTableList { "'reserve_liquidation'" }

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

    my %hash;

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

    my $type = RPS::DB::Item::ReserveLiquidation::kTypeArtistContract;

    my $sql = qq/
    SELECT 
    id,
    period,
    percent
    FROM reserve_liquidation
    WHERE type=$type
    ORDER BY id, period
    /;

    my $sth = $dbo->DoCmd($sql);
    while ( my @data = $sth->fetchrow_array() ) {
        my @periodPercent;
        $periodPercent[0] = $data[1];
        $periodPercent[1] = $data[2];
        push @{ $hash{ $data[0] } }, \@periodPercent;
    }

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

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

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

    return $data;
}

1;

