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

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::FormObject;

use lib '/app/tools/rps/lib';
use RPS::ArtistRoyalty::ArtistRoyaltyRun;
use RPS::Statement::Artist::StatementListWithPayments;
use RPS::RoyaltyRun::Status;
use RPS::Payor::Payor;

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};

    # We may skip some unnecessary information here such as Transactions List to speed up rendering time
    $self->{ArtistRoyaltyStatementList} = RPS::Statement::Artist::StatementListWithPayments->new(
        artistRoyaltyRunID          => $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->{ArtistRoyaltyRun} = RPS::ArtistRoyalty::ArtistRoyaltyRun->new(
        artistRoyaltyRunID => $args{runID},
        loadSubs           => 0,
    );

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

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

    die "ERROR - cannot edit payments for uncommitted runs"
      unless ( RPS::RoyaltyRun::Status::kCommitted == $self->{ArtistRoyaltyRun}->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->{ArtistRoyaltyStatementList};
        my %excludedStatementIDs;
        foreach my $statement ( @{ $pageStatements->{ArtistStatement} } ) {
            $excludedStatementIDs{ $statement->ArtistRoyaltyStatementID() } = 1;
        }

        my $collection = RPS::Statement::Artist::StatementListWithPayments->new(
            artistRoyaltyRunID => $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->{ArtistRoyaltyStatementList}->save();
}

###
1;    #
###

