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

package RPS::LabelRoyalty::Fast::Static::LabelPayeePendingTransactions;

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::LabelRoyalty::Fast::Static';


use constant kPendingTransactionID      => 0;
use constant kLabelPayeeID             => 1;
use constant kAmount                    => 2;
use constant kMemo                      => 3;
use constant kCheckNumber               => 4;
use constant kTypeCode                  => 5;
use constant kDate                      => 6;


sub FileName { 'label_payee_pending_transactions' }
sub CacheQueryTableList { "'pending_transaction','label_payee_account','label_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,
    label_payee_account.label_payee_id,
    pending_transaction.amount,
    pending_transaction.memo,
    pending_transaction.check_number,
    pending_transaction.type_code,
    pending_transaction.transaction_date
    FROM pending_transaction
    JOIN label_payee_account USING (finance_account_id)
    JOIN label_payee USING (label_payee_id)
    WHERE payor_id=$payorID
    AND label_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 GetLabelPayeePendingTransactions
{
    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;

