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

use strict;
use warnings;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::CATrackLicense;
use RPS::DB::Item::PendingTransaction;
use RPS::DB::Item::CAPublisherAccount;
use RPS::DB::Item::CAPublisherIndirectBalanceAccount;
use RPS::Publisher::CA::Publisher;
use RPS::Payor::Payor;
use RPS::Payor::PayorList;
use base 'RPS::Payor::PayorList';

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

    # In this mode, we _need_ a publisher id.
    #
    my $publisherID = $args{publisherID};
    assert($publisherID);

    $self->{_publisherID} = $publisherID;

    $self->{Payor} = [];

    my $payors = RPS::DB::Item::Payor->GetAll();

    while ( $payors->hasNext() ) {
        my $data = $payors->next();

        # We want to show up to 2 lines per payor, so doubling the list allows me
        # to simly loop over them in the template.
        #
        push @{ $self->{Payor} }, $self->_getPayorFromDBItem( $data, 'direct' );
        push @{ $self->{Payor} }, $self->_getPayorFromDBItem( $data, 'indirect' );
    }

    return $self;
}

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

    my $payorObj = $self->SUPER::_getPayorFromDBItem($dbItem);

    my $publisher = RPS::Publisher::CA::Publisher->new(
        publisherID => $self->{_publisherID},
        loadSubs    => 0,
        readOnly    => 1,
    );

    my $forcePublisherDirect = 1;

    if ( $publisher->AdminID() || $publisher->AgentID ) {
        $forcePublisherDirect = 0;
    }

    # Going to 'annotate' the publisher with the count.
    # This is a bit of a hack, but I really don't want to bother
    # subclassing Payor for this purpose.

    my $licenseCount;

    if ( $type eq 'direct' ) {

        my $totalLicenses = RPS::DB::Item::CATrackLicense->GetByPublisherIDPayorID( $self->{_publisherID}, $payorObj->PayorID() );

        if ( $forcePublisherDirect == 1 ) {
            $licenseCount = $totalLicenses->size();
        } else {
            my $directLicenses =
              RPS::DB::Item::CATrackLicense->GetDirectByPublisherIDPayorID( $self->{_publisherID}, $payorObj->PayorID() );
            $licenseCount = $directLicenses->size();
        }
    }

    if ( $type eq 'indirect' ) {
        if ( $forcePublisherDirect == 1 ) {
            $licenseCount = 0;
        } else {
            my $indirectLicenses =
              RPS::DB::Item::CATrackLicense->GetIndirectByPublisherIDPayorID( $self->{_publisherID}, $payorObj->PayorID() );
            $licenseCount = $indirectLicenses->size();
        }
    }

    $payorObj->{LicenseCount} = $licenseCount;

    # We need to know if there are any accounts (direct or indirect).
    #

    my $accounts;

    if ( $type eq 'direct' ) {
        $accounts = RPS::DB::Item::CAPublisherAccount->GetByPayorPublisherID( $payorObj->PayorID(), $self->{_publisherID} );
    }
    if ( $type eq 'indirect' ) {
        $accounts = RPS::DB::Item::CAPublisherIndirectBalanceAccount->GetByPayorPublisherID( $payorObj->PayorID(), $self->{_publisherID} );

        # For indirect accounts, we need to total up all of the pending transactions.
        #
        my $pendingBalance;
        while ( my $account = $accounts->next() ) {
            $pendingBalance += RPS::DB::Item::PendingTransaction->GetPendingBalance( $account->{finance_account_id} );
        }

        $payorObj->{PendingBalance} =
          Common::FormObject::Scalar::Money->new( value => $pendingBalance, precision => 0.2, currencyCode => 'USD' );

    }

    $payorObj->{HasAccount} = $accounts->size();

    # Let's add an element so that we can be sure things are lining up correctly.
    #

    if ( $type eq 'direct' ) {
        $payorObj->{Direct} = 1;
    }
    if ( $type eq 'indirect' ) {
        $payorObj->{Indirect} = 1;
    }

    return $payorObj;
}

###
1;    #
###
