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

use Carp;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::WriteXML;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::LabelPayeeAccount;
use RPS::Finance::Account;

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

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

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

    my %properties;
    $self->{_showPendingPaymentsOnly} = $args{showPendingPaymentsOnly};
    $self->{_skipTransactionsList}    = $args{skipTransactionsList};

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

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

    return $self;
}

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

    my $dbItem = RPS::DB::Item::LabelPayeeAccount->Lookup( label_payee_id => $labelPayeeID, payor_id => $payorID );

    if ($dbItem) {
        $self->_propertiesFromDBItem( $hrProperties, $dbItem );
    } else {
        $self->_propertiesFromDefaults( $hrProperties, $labelPayeeID, $payorID );
    }
}

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

    #    assert($dbItem->finance_account_id);
    $self->{_dbItem} = $dbItem;

    $hrProperties->{label_payee_id}     = $dbItem->label_payee_id;
    $hrProperties->{payor_id}           = $dbItem->payor_id;
    $hrProperties->{finance_account_id} = $dbItem->finance_account_id;
    $hrProperties->{min_payment}        = $dbItem->min_payment;
}

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

    $hrProperties->{label_payee_id} = $labelPayeeID if $labelPayeeID;
    $hrProperties->{payor_id}       = $payorID      if $payorID;
}

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

    $self->{LabelPayeeID}     = Common::FormObject::Scalar->new( value => $props->{label_payee_id},     readOnly => 1 );
    $self->{PayorID}          = Common::FormObject::Scalar->new( value => $props->{payor_id},           readOnly => 1 );
    $self->{FinanceAccountID} = Common::FormObject::Scalar->new( value => $props->{finance_account_id}, readOnly => 1 );
    $self->{MinPayment} = Common::FormObject::Scalar::Decimal->new( value => $props->{min_payment}, precision => '0.2' );
}

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

    if ( $self->loadSubs() ) {
        if ( $props->{finance_account_id} ) {
            $self->{FinanceAccount} = RPS::Finance::Account->new(
                accountID               => $props->{finance_account_id},
                showPendingPaymentsOnly => $self->{_showPendingPaymentsOnly},
                skipTransactionsList    => $self->{_skipTransactionsList},
            );
        } else {
            $self->{FinanceAccount} = RPS::Finance::Account->new();
        }
    }
}

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

    $self->{FinanceAccount}->clearPendingPayments();
}

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

    $self->{FinanceAccount}->setFirstPendingPayment($amount);
}

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

    if ( !$self->{FinanceAccount}->Description() ) {
        $self->{FinanceAccount}->Description( "account for label payee " . $self->LabelPayeeID() . " payor " . $self->PayorID() );
    }
    if ( !$self->{FinanceAccount}->TypeCode() ) {
        $self->{FinanceAccount}->TypeCode(RPS::DB::Item::FinanceAccount::kAccountTypeHoldover);
    }

    $self->{FinanceAccount}->save();
    my $financeAccountID = $self->{FinanceAccount}->AccountID();

    $dbItem = $self->{_dbItem} unless $dbItem;

    if ( !$dbItem ) {
        $dbItem = RPS::DB::Item::LabelPayeeAccount->Create( finance_account_id => $financeAccountID, );
    }

    if ( $dbItem->finance_account_id() != $financeAccountID ) {
        $dbItem->finance_account_id($financeAccountID);
    }
    $dbItem->label_payee_id( $self->LabelPayeeID() );
    $dbItem->payor_id( $self->PayorID() );
    $dbItem->min_payment( $self->MinPayment() );

    $dbItem->save();

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

###
1;    #
###

