#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Finance::PendingTransactionList;

use strict;
use Data::Dumper;

use lib '/app/tools/rps/lib';

use RPS::DB::Item::PendingTransaction;
use RPS::Finance::PendingTransaction;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::FormObject;
use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    my $accountID            = $args{accountID};
    my $startDate            = $args{startDate};
    my $endDate              = $args{endDate};
    my $showPaymentsOnlyFlag = $args{showPendingPaymentsOnly};

    $self->SUPER::_init(%args);

    $self->{PendingTransaction} = [];
    return $self if !defined $accountID;
    $self->{_accountID} = $accountID;

    my $collection = RPS::DB::Item::PendingTransaction->GetAccountTransactions(
        finance_account_id => $accountID,
        start_date         => $startDate,
        end_date           => $endDate,
        show_payments_only => $showPaymentsOnlyFlag,
    );

    while ( $collection->hasNext() ) {
        push @{ $self->{PendingTransaction} }, new RPS::Finance::PendingTransaction( _dbItem => $collection->next() );
    }

    return $self;
}

sub validate {
    my ($self) = @_;

    my $valid = 1;
    foreach my $trans ( @{ $self->{PendingTransaction} } ) {
        next if ( !defined $trans || $trans->Delete() );

        if ( !$trans->validate() ) {
            $valid = 0;
        }
    }

    return $valid;
}

sub save {
    my ( $self, %args ) = @_;
    foreach my $trans ( @{ $self->{PendingTransaction} } ) {
        if ( defined $trans ) {
            $trans->save();
        }
    }
}

sub _listProperties {
    return { PendingTransaction => 1 };
}

sub PendingTransaction {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

    if ( 0 == scalar @$methodAddressList ) {
        if ( defined $newValue ) {
            $self->{PendingTransaction}[$index] = $newValue;
        }
        return 1;
    }

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{PendingTransaction}[$index] ) {
        $self->{PendingTransaction}[$index] = RPS::Finance::PendingTransaction->new( accountID => $self->{_accountID} );
    }

    return $self->{PendingTransaction}[$index]->_accessProperty( $methodAddressList, $newValue );
}

sub assignAccountID {
    my ( $self, $accountID, $currencyCode ) = @_;

    assert( $accountID, 'missing accountID' );

    $self->{_accountID} = $accountID;

    foreach my $trans ( @{ $self->{PendingTransaction} } ) {
        next if ( !$trans );
        $trans->AccountID($accountID);
        $trans->CurrencyCode($currencyCode) if $currencyCode;
    }

}

sub getPendingTransactionArray {
    my ($self) = @_;

    return $self->{PendingTransaction};
}

###
1;    #
###
