#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::PendingTransaction;
use strict;
use warnings;
use Carp;

use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use base 'Common::DB::Item';
use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::FinanceAccount;

# !!! Need to define currency types someplace
#

use constant kTypeOpeningBalance => 1;
use constant kTypeAdjustment     => 2;
use constant kTypeAdvance        => 3;
use constant kTypePayment        => 4;
use constant kTypeClosingBalance => 5;

use constant kTable => 'pending_transaction';
use constant kDB    => Common::DB::Item::kClientDB();

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

    # Fetch the account's currency type for a quick sanity check.
    # We can't mix currencies in an account...
    #
    # !!! If it wasn't specified, default to the account's currency_code
    #
    my $accountID = $self->finance_account_id;
    my $accountDBItem = RPS::DB::Item::FinanceAccount->Lookup( finance_account_id => $accountID );
    die "ERROR - invalid account ID $accountID" unless $accountDBItem;

    my $accountCurrency = $accountDBItem->currency_code;

    if ( !$self->currency_code() ) {
        $self->currency_code($accountCurrency);
    } else {
        die "ERROR - account currency_code $accountCurrency does not match transaction currency_code " . $self->currency_code()
          unless $self->currency_code() eq $accountCurrency;
    }

    return $self->SUPER::save();
}

sub GetPendingBalance {
    my ( $class, $accountID ) = @_;

    my $sql     = "SELECT SUM(amount) FROM " . kTable . " WHERE finance_account_id=$accountID";
    my $dbo     = Common::RSApp::GetClientDB();
    my $sth     = $dbo->DoCmd($sql);
    my $balance = $sth->fetchrow_arrayref()->[0] if ($sth);

    return ( defined $balance ) ? $balance : 0;
}

sub GetLastAccountTransaction {
    my ( $class, $accountID ) = @_;
    assert($accountID);

    # Going to use the collection interface, even though I only expect 1 result.
    # Just seems easier this way, oddly enough.
    #
    my $sql        = "SELECT * FROM " . kTable . " WHERE finance_account_id=$accountID ORDER BY pending_transaction_id DESC LIMIT 1";
    my $collection = $class->GetAll($sql);

    return $collection->next();
}

sub GetAccountTransactions {
    my ( $class, %args ) = @_;

    my $accountID = $args{finance_account_id};
    assert($accountID);

    my $allFlag          = $args{all};
    my $paymentsOnlyFlag = $args{show_payments_only};

    # These dates _must_ be in datetime format,
    # YYYY-MM-DD HH-MM-SS
    #
    my $startDate = $args{start_date};
    my $endDate   = $args{end_date};

    my $sql = "SELECT * FROM " . kTable . " WHERE finance_account_id=$accountID";
    if ($startDate) {
        $sql .= " AND date >= '$startDate'";
    }
    if ($endDate) {
        $sql .= " AND date <= '$endDate'";
    }

    if ( !$allFlag ) {
        $sql .= " AND processed=0";
    }

    if ($paymentsOnlyFlag) {
        $sql .= " AND type_code=" . kTypePayment;
    }

    $sql .= " ORDER BY date";

    return $class->GetAll($sql);
}

1;

