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

use lib '/app/tools/commmon/lib';

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

use RPS::DB::Item::FinanceAccount;
use RPS::DB::Item::FinanceTransaction;
use RPS::DB::Item::PendingTransaction;

use RPS::Finance::TransactionList;
use RPS::Finance::PendingTransactionList;

use Common::Client;
use Common::FormObject;
use base 'Common::FormObject';


# Account will have an id, a name, and a current balance.  Additionally, they _may_ contain
# a transaction list, based on a start and end date.
#
# The current balance is taken from the transaction table.
#

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

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


    # These _optional_ arguments can be passed to constrain within a date
    # range the transactions we will present.
    #
    $self->{_startDate} = $args{startDate};
    $self->{_endDate} = $args{endDate};

    $self->{_showPendingPaymentsOnly} = $args{showPendingPaymentsOnly};

    my %properties;
    if ($args{_dbItem})
    {
        $self->_propertiesFromDBItem(\%properties, $args{_dbItem});
    }
    elsif ($args{accountID})
    {
        $self->_propertiesFromDB(\%properties, $args{accountID});
    }
    else
    {
        $self->_propertiesFromDefaults(\%properties);
    }

    $self->_initProperties(\%properties);
    $self->_initSubs(\%properties);

    return $self;
}


sub _propertiesFromDB
{
    my ($self, $hrProperties, $id) = @_;

    my $dbItem = RPS::DB::Item::FinanceAccount->Lookup(finance_account_id => $id);
    $self->_propertiesFromDBItem($hrProperties, $dbItem);
}


sub _propertiesFromDBItem
{
	my ($self, $hrProperties, $dbItem) = @_;

    $hrProperties->{finance_account_id} = $dbItem->finance_account_id;
    $hrProperties->{description}        = $dbItem->{description};
    $hrProperties->{type_code}          = $dbItem->{type_code};
    $hrProperties->{currency_code}      = $dbItem->{currency_code};
    $hrProperties->{date_created}       = $dbItem->{date_created};
    $hrProperties->{date_modified}      = $dbItem->{date_modified};
}

sub _initProperties
{
    my ($self, $props) = @_;

    # Set the fields from the finance_account db item.
    #
    $self->{AccountID}      = Common::FormObject::Scalar->new(value => $props->{finance_account_id}, readOnly => 1);
    $self->{Description}    = Common::FormObject::Scalar::String->new(value => $props->{description});
    $self->{TypeCode}       = Common::FormObject::Scalar::String->new(value => $props->{type_code}, maxlength => 4);
    $self->{CurrencyCode}   = Common::FormObject::Scalar::String->new(value => $props->{currency_code}, maxlength => 4);
    $self->{DateCreated}    = Common::FormObject::Scalar::DateTime->new(value => $props->{date_created}, readOnly => 1);
    $self->{DateModified}   = Common::FormObject::Scalar::DateTime->new(value => $props->{date_modified}, readOnly => 1);

    # Now fetch the last transaction, so we know the current balance.
    #
    my $transaction;
	if($props->{finance_account_id})
	{
		$transaction = RPS::DB::Item::FinanceTransaction->GetLastAccountTransaction($props->{finance_account_id})
	}

    if ($transaction)
    {
        $self->{_hasTransactions} = 1;
        $self->{Balance} = Common::FormObject::Scalar::Money->new(value => $transaction->balance, precision => 0.2, currencyCode => $props->{currency_code});
    }
    else
    {
        $self->{_hasTransactions} = 0;
        $self->{Balance} = Common::FormObject::Scalar::Money->new(precision => 0.2, currencyCode => $props->{currency_code});
    }

    # sum up any pending transactions
	if ($props->{finance_account_id})
	{
        $self->{PendingBalance} = Common::FormObject::Scalar::Money->new(value => RPS::DB::Item::PendingTransaction->GetPendingBalance($props->{finance_account_id}), precision => 0.2, currencyCode => $props->{currency_code});
	}
    else
    {
        $self->{PendingBalance} = Common::FormObject::Scalar::Money->new(precision => 0.2, currencyCode => $props->{currency_code});
    }
}


sub _initSubs
{
    my ($self, $props) = @_;

	if($self->loadSubs())
	{
		# Create the transaction list.
		#
		$self->{TransactionList} = RPS::Finance::TransactionList->new(
		    accountID => $self->AccountID(),
		    startDate => $self->{_startDate},
		    endDate => $self->{_endDate},
		);

		$self->{PendingTransactionList} = RPS::Finance::PendingTransactionList->new(
		    accountID => $self->AccountID(),
		    startDate => $self->{_startDate},
		    endDate => $self->{_endDate},
            showPendingPaymentsOnly => $self->{_showPendingPaymentsOnly},
		);
	}
}


sub _propertiesFromDefaults
{
    my ($self, $hrProperties) = @_;


    # We want to use their default currency_code
    #
    $hrProperties->{currency_code} = Common::Client::Current()->Locale()->currencyFormat()->currencyCode();
}


sub hasTransactions
{
    my ($self) = @_;
    return $self->{_hasTransactions};
}


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

    my $dbObj;
    my $accountID = $self->AccountID();

    if($self->AccountID())
    {
        $dbObj = RPS::DB::Item::FinanceAccount->Lookup(finance_account_id => $self->AccountID());
    }
    else
    {
        $dbObj = RPS::DB::Item::FinanceAccount->Create();
    }

    # set fields here
    #
    $dbObj->description($self->Description());
    $dbObj->type_code($self->TypeCode());
    $dbObj->currency_code($self->CurrencyCode());


    # save db item
    #
    $dbObj->save();

    if (! $accountID)
    {
        $accountID = $dbObj->finance_account_id();
        $self->{PendingTransactionList}->assignAccountID($accountID, $dbObj->currency_code);
    }

    $self->{PendingTransactionList}->save();

    # reload object
    #
    $self->_init(_dbItem => $dbObj);
}


sub clearPendingPayments
{
    my ($self) = @_;
    
    my $pendingTransactions = $self->{PendingTransactionList}->getPendingTransactionArray();
    foreach my $pendingTransaction (@$pendingTransactions)
    {
        $pendingTransaction->Delete(1);
    }
}


sub setFirstPendingPayment
{
    my ($self, $amount) = @_;

    my $deleteThis = 0;
    my $pendingTransactions = $self->{PendingTransactionList}->getPendingTransactionArray();
    if ($pendingTransactions)
    {
        my $firstTransaction = $$pendingTransactions[0];
        if ($firstTransaction)
        {
            $firstTransaction->Delete(0);

            $firstTransaction->{Amount}->setRawValue($amount);
        }
    }
}



###
1;#
###
