package RPS::Publisher::Command::CA::Delete;

use lib '/app/tools/rps/lib';
use RPS::CommandLog;

use lib '/app/tools/common/lib';
use Common::FormObject;
use RSApache::Response::XML;
use RSApache::Response::XSLT;
use RSApache::Response::Redirect;

use RPS::Payee::Command::Main;
use RPS::Publisher::Command::CA::Edit;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::DB::Item::CAPublisher;
use RPS::DB::Item::CAMechanicalRun;
use RPS::DB::Item::CAPublisherAccount;
use RPS::DB::Item::FinanceTransaction;

use parent 'RPS::Command';

sub cmd  { 'delete' }
sub area { 'ca_publisher' }

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $id = $self->getParam('PublisherID') || 0;
    unless ( $id && $id =~ /^\d+$/ ) {
        return $self->_redirectToPayeeForm();
    }

    if ( my $oPublisher = RPS::DB::Item::CAPublisher->Lookup( ca_publisher_id => $id ) ) {
        if ( $self->_isActionAllowed($oPublisher) ) {
            $oPublisher->delete();
            return $self->_redirectToPayeeForm('success');
        } else {
            return $self->_redirectToEditForm('error', $id);
        }
    }

    return $self->_redirectToPayeeForm();
}

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

    my $restricted = 0;
    $restricted += $self->_isAdminOrAgent($oPublisher);
    $restricted += $self->_hasAttachedLicenses($oPublisher);
    $restricted += $self->_hasRunActivity($oPublisher);
    $restricted += $self->_areActiveRoyaltyRuns();
    $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 _areActiveRoyaltyRuns {
    my $self = shift;

    my $hData = RPS::RoyaltyRun::RoyaltyRunStatusList->new( caMechanical => 1 );
    return !$hData->{CanEdit};
}

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->GetTransactionsHistoryByPublisher( $oPublisher->ca_publisher_id );
    while ( my $oItem = $oCollection->next() ) {
        $res += $oItem->finance_account_id;
        last;
    }

    return $res;
}


1;
