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

use Data::Dumper;
use Carp;

use lib '/app/tools/common/lib';
use Common::Client;
use Common::Locale;
use Common::CurrencyFormat;

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

use RPS::DB::Item::FinanceTransaction;

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

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

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

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

    $self->_initProperties( \%properties );

    return $self;
}

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

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

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

    $hrProperties->{finance_trancsaction_id} = $dbItem->finance_transaction_id;
    $hrProperties->{finance_account_id}      = $dbItem->finance_account_id;
    $hrProperties->{label_id}                = $dbItem->label_id;
    $hrProperties->{amount}                  = $dbItem->amount;
    $hrProperties->{balance}                 = $dbItem->balance;
    $hrProperties->{memo}                    = $dbItem->memo;
    $hrProperties->{check_number}            = $dbItem->check_number;
    $hrProperties->{type_code}               = $dbItem->type_code;
    $hrProperties->{currency_code}           = $dbItem->currency_code;
    $hrProperties->{date}                    = $dbItem->date;
    $hrProperties->{transaction_date}        = $dbItem->transaction_date;
}

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

    # Set the fields from the finance_account db item.
    #
    $self->{TransactionID} = Common::FormObject::Scalar->new( value => $props->{finance_transaction_id}, readOnly => 1 );
    $self->{AccountID}     = Common::FormObject::Scalar->new( value => $props->{finance_account_id} );
    $self->{LabelID}       = Common::FormObject::Scalar->new( value => $props->{label_id} );
    $self->{Amount} =
      Common::FormObject::Scalar::Money->new( value => $props->{amount}, precision => 0.2, currencyCode => $props->{currency_code} );
    $self->{Balance} =
      Common::FormObject::Scalar::Money->new( value => $props->{balance}, precision => 0.2, currencyCode => $props->{currency_code} );
    $self->{Memo}         = Common::FormObject::Scalar::String->new( value => $props->{memo} );
    $self->{CheckNumber}  = Common::FormObject::Scalar::String->new( value => $props->{check_number} );
    $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 => 3 );
    $self->{Date} = Common::FormObject::Scalar::DateTime->new( value => $props->{date} );
    $self->{TransactionDate} = Common::FormObject::Scalar::Date->new( value => $props->{transaction_date} );

}

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

}

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

    my $dbObj;
    if ( $self->TransactionID() ) {

        # JPK - Current strategy is to NOT ALLOW a transaction to be modified once created.
        #
        confess "Error - Attempt to modify existing transaction";
    }

    $dbObj = RPS::DB::Item::FinanceTransaction->Create();

    $dbObj->finance_account_id( $self->AccountID() );
    $dbObj->label_id( $self->LabelID() );
    $dbObj->amount( $self->Amount() );
    $dbObj->type_code( $self->TypeCode() );
    $dbObj->memo( $self->Memo() );
    $dbObj->check_number( $self->CheckNumber() );
    $dbObj->transaction_date( $self->TransactionDate() );

    # If we didn't specify a CurrencyCode, we'll default to the client's base currency.
    # !!! This may cause the DB::Item to blow up if this transaction gets applied to an
    # !!! account with a different currency code...
    #
    my $currencyCode = $self->CurrencyCode();
    if ( !$currencyCode ) {
        $currencyCode = Common::Client::Current()->Locale()->currencyFormat()->currencyCode();
    }
    $dbObj->currency_code($currencyCode);

    #
    # Note that the _balance_ will be adjusted auto-magically by the DB::Item...
    #

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

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

###
1;    #
###
