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

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;

use lib '/app/tools/rps/lib';
use RPS::Publisher::CA::EditForm;
use RPS::Publisher::Command::CA::Show;
use RPS::DB::Item::CAMechanicalRun;
use RPS::DB::Item::CAPublisher;
use RPS::DB::Item::CAPublisherAccount;
use RPS::DB::Item::FinanceTransaction;

use base 'RPS::Publisher::Command::BaseEdit';

sub area { return "ca_publisher"; }

sub _template    { return "/app/tools/rps/templates/ca_publisher/edit.xsl"; }
sub _editForm    { return "RPS::Publisher::CA::EditForm"; }
sub _editCommand { return "RPS::Publisher::Command::CA::Edit"; }
sub _showCommand { return "RPS::Publisher::Command::CA::Show"; }

sub _openRunCount {
    my ( $self, $publisherID ) = @_;

    # If there are any "open" (completed, running, waiting to run, waiting to commit) runs
    # that include this publisher, we don't want to allow any changes.
    my $openRuns = 0;
    $openRuns = RPS::DB::Item::CAMechanicalRun->GetOpenRunCountForPublisher($publisherID);

    return $openRuns;

}

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

    return 0 unless $id && $id =~ /^\d+$/;

    my $oPublisher = RPS::DB::Item::CAPublisher->Lookup( ca_publisher_id => $id );
    return $self->_isActionAllowed($oPublisher);
}

sub _isActionAllowed {
    my ($self, $oPublisher) = @_;

    my $restricted = 0;
    $restricted += $self->_isAdminOrAgent($oPublisher);
    $restricted += $self->_hasAttachedLicenses($oPublisher);
    $restricted += $self->_hasRunActivity($oPublisher);
    $restricted += $self->_hasPendingOrHistoryTransactions($oPublisher);

    return !$restricted;
}

sub _isAdminOrAgent {
    my ($self, $oPublisher) = @_;

    my $res = 0;

    my $oCollection = RPS::DB::Item::CAPublisher->GetByAdminID( $oPublisher->ca_publisher_id );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->ca_publisher_id;
        last;
    }

    $oCollection = RPS::DB::Item::CAPublisher->GetByAgentID( $oPublisher->ca_publisher_id );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->ca_publisher_id;
        last;
    }

    return $res;
}

sub _hasAttachedLicenses {
    my ($self, $oPublisher) = @_;

    my $res = 0;

    my $oCollection = RPS::DB::Item::CAPublisher->GetAllWithLicenses( $oPublisher->ca_publisher_id );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->ca_publisher_id;
        last;
    }

    return $res;
}

sub _hasRunActivity {
    my ($self, $oPublisher) = @_;

    # check for any run activity
    return RPS::DB::Item::CAPublisher->HasRunActivity( $oPublisher->ca_publisher_id );
}

sub _redirectToEditForm {
    my ($self, $status, $id) = @_;

    my %showParams;
    if ( $status && $status =~ /^error/ ) {
        my $oSuccessMessage = RPS::Message->new( type => "error", code => Common::FormObject::kErrFormSubmit );
        %showParams = ( msg => $oSuccessMessage->toString() );
    }

    $showParams{PublisherID} = $id;
    my $oCommand = RPS::Publisher::Command::CA::Edit->new( %showParams );
    return RSApache::Response::Redirect->new( $oCommand->getRedirect() );
}

sub _redirectToPayeeForm {
    my ($self, $status) = @_;

    my %showParams;
    if ( $status && $status =~ /^success$/ ) {
        my $oSuccessMessage = RPS::Message->new( type => $status, code => Common::FormObject::kSuccessDelete );
        %showParams = ( msg => $oSuccessMessage->toString() );
    }

    $showParams{payeeType}     = 'caPublisher';
    $showParams{viewByType}    = 'publisher';
    $showParams{publisherType} = 'caPublisher';
    my $oCommand = RPS::Payee::Command::Main->new( %showParams );
    return RSApache::Response::Redirect->new( $oCommand->getRedirect() );
}

sub _hasPendingOrHistoryTransactions {
    my ($self, $oPublisher) = @_;

    my $res = 0;

    # pending
    my $oCollection = RPS::DB::Item::CAPublisherAccount->GetAllWithPendingTransactions(
        undef, $oPublisher->ca_publisher_id
    );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->finance_account_id;
        last;
    }

    # history
    $oCollection = RPS::DB::Item::FinanceTransaction->GetTransactionsHistoryByCAPublisher( $oPublisher->ca_publisher_id );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->finance_account_id;
        last;
    }

    return $res;
}

1;

