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

use Carp;

use Data::Dumper;

use lib '/app/tools/common/lib';

use lib '/app/tools/rps/lib';
use RPS::Statement::Mechanical::CA::StatementListWithPayments;
use RPS::Payor::Payor;
use RPS::RoyaltyRun::Status;

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

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

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

    $self->{_fillAll}  = $args{fillAll};
    $self->{_clearAll} = $args{clearAll};
    $self->{_runID}    = $args{runID};
    $self->{_payorID}  = $args{payorID};
    $self->{_filterBy} = $args{filterBy};

    $self->{OnHoldCount} = RPS::DB::Item::CAMechanicalStatement->GetOnHoldCount(
        runID    => $args{runID},
        payorID  => $args{payorID},
        filterBy => $args{filterBy}
    );

    $self->{MechanicalStatementList} = RPS::Statement::Mechanical::CA::StatementListWithPayments->new(
        mechanicalRunID      => $args{runID},
        payorID              => $args{payorID},
        filterBy             => $args{filterBy},
        page                 => $args{page},
        pageSize             => $args{pageSize},
        skipTransactionsList => 1,
    );

    $self->{Payor} = RPS::Payor::Payor->new( payorID => $args{payorID} );
    $self->{MechanicalRun} = RPS::Mechanical::CA::MechanicalRun->new(
        mechanicalRunID => $args{runID},
        loadSubs        => 0,
    );

    $self->{RoyaltyRunStatus} = new RPS::RoyaltyRun::RoyaltyRunStatusList( caMechanical => 1, payorID => $args{payorID} );

    # Sanity check - the run has to have the state of 'committed', or we can't edit payments.
    #
    die "ERROR - cannot edit payments for uncommitted runs"
      unless ( RPS::RoyaltyRun::Status::kCommitted == $self->{MechanicalRun}->Status() );

    return $self;
}

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

    # If one of these params was passed in, we need to apply the action to
    # all statetememts _except_ the ones on the current page.
    if ( $self->{_fillAll} || $self->{_clearAll} ) {

        # Collect the statement IDs for the current page.
        my $pageStatements = $self->{MechanicalStatementList};
        my %excludedStatementIDs;
        foreach my $statement ( @{ $pageStatements->{MechanicalStatement} } ) {
            $excludedStatementIDs{ $statement->MechanicalStatementID() } = 1;
        }

        my $collection = RPS::Statement::Mechanical::CA::StatementListWithPayments->new(
            mechanicalRunID => $self->{_runID},
            payorID         => $self->{_payorID},
            filterBy        => $self->{_filterBy},
        );

        if ( $self->{_fillAll} ) {
            $collection->setPaymentsToFullPayment(%excludedStatementIDs);
        } elsif ( $self->{_clearAll} ) {
            $collection->clearPayments(%excludedStatementIDs);
        }

        # And then we have to save the results of that action.
        $collection->save();
    }

    # Now update the payments for statements on the current page
    $self->{MechanicalStatementList}->save();
}

###
1;    #
###
