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

package RPS::ArtistRoyalty::Fast::Static::ArtistPayeePendingTransactions;

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 kPendingTransactionID => 0;
use constant kArtistPayeeID        => 1;
use constant kAmount               => 2;
use constant kMemo                 => 3;
use constant kCheckNumber          => 4;
use constant kTypeCode             => 5;
use constant kDate                 => 6;

sub FileName            { 'artist_payee_pending_transactions' }
sub CacheQueryTableList { "'pending_transaction','artist_payee_account','artist_payee'" }

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

    # I think this data will just be spewed forth to a text file.
    #
    open OUTFILE, "> $filename" or die "ERROR: Unable to open $filename for writing: $!";

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

    my $sql = qq/
    SELECT 
    pending_transaction.pending_transaction_id,
    artist_payee_account.artist_payee_id,
    pending_transaction.amount,
    pending_transaction.memo,
    pending_transaction.check_number,
    pending_transaction.type_code,
    pending_transaction.transaction_date
    FROM pending_transaction
    JOIN artist_payee_account USING (finance_account_id)
    JOIN artist_payee USING (artist_payee_id)
    WHERE payor_id=$payorID
    AND artist_payee.status in (1,2)
    /;

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

    while ( my @array = $sth->fetchrow_array() ) {
        my $escapedArray = $class->EscapeArray( \@array );
        print OUTFILE join( "\t", @$escapedArray ) . "\n";
    }

    close OUTFILE;
}

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

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

    my @array;
    tie @array, "DB_File", $filename, O_RDONLY, 0666, $DB_RECNO or die "Error opening $filename: $!\n";

    return \@array;
}

1;

