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

package RPS::ArtistRoyalty::Fast::Static::ArtistAlbumContractAccountBalances;

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 constant kMinPayment        => 0;
use constant kPreviousBalance   => 1;

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

sub FileName { 'artist_album_contract_balances' }
sub CacheQueryTableList { "'artist_royalty_album_balance_account','artist_payee','finance_transaction'"};

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


    my $balanceSql = 'select balance from finance_transaction where finance_transaction_id = (select MAX(finance_transaction_id) from finance_transaction where finance_account_id=?)';

    my %hash;
    $DB_BTREE->{'flags'} = R_DUP;
    tie %hash, "DB_File", $filename, O_RDWR | O_CREAT, 0666, $DB_HASH or die "Error opening $filename: $!\n";


    # This configures Dumper to use a format that can be eval'd directly into a variable.
    # Otherwise it lards it up with additional notation.
    #
    $Data::Dumper::Terse = 1;

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


    my $sql = qq/
    SELECT 
    artist_royalty_album_balance_account.artist_payee_id,
    artist_royalty_album_balance_account.album_id,
    artist_royalty_album_balance_account.account_id,
    artist_royalty_album_balance_account.artist_contract_id
    FROM artist_royalty_album_balance_account
    JOIN artist_payee USING (artist_payee_id)
    JOIN new_artist_contract USING (artist_contract_id)
    WHERE new_artist_contract.payor_id=$payorID
    AND artist_royalty_album_balance_account.payor_id=$payorID
    AND artist_payee.status in (1,2)
    ORDER BY artist_payee_id
    /;

    $Data::Dumper::Terse = 1;

    my $sth = $dbo->DoCmd($sql);

    my $hashRef;
    my $lastPayeeID;
    while (my @array = $sth->fetchrow_array())
    {
        if ($lastPayeeID != $array[0])
        {
            if ($hashRef)
            {
                $hash{$lastPayeeID} = Dumper($hashRef);
            }
            $hashRef = {};
            $lastPayeeID = $array[0];
        }
        my $innerSTH = $dbo->DoCmdWithPlaceholders($balanceSql, [ $array[2] ]);
        my $balance = $innerSTH->fetchrow_array();

        Log->info($lastPayeeID.','.$array[1].','.$array[3].'='.$balance);
        $hashRef->{$array[1]}{$array[3]} = $balance;
    }

    if ($hashRef && $lastPayeeID)
    {
        $hash{$lastPayeeID} = Dumper($hashRef);
    }

    untie %hash;
}


sub GetAccountBalances
{
    my ($class, $dataPath) = @_;

    my $filename = "$dataPath/" . $class->FileName();

    my %hash;
    my $dbObj = tie %hash, "DB_File", $filename, O_RDONLY, 0666, $DB_HASH or die "Error opening $filename: $!\n";

    return \%hash;
}



1;

