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

use Carp;

use Data::Dumper;
use Date::Calc;

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

#use Common::DiskCache;

use lib '/app/tools/rps/lib';

use RPS::Label;
use RPS::Payor::Payor;
use RPS::Statement::Mechanical::StatementTrackList;
use RPS::Statement::Mechanical::StatementTransactionList;
use RPS::Statement::Mechanical::StatementPublisherList;
use RPS::DB::Item::MechanicalStatement;

use RPS::Statement::Mechanical::BaseStatement;
use base 'RPS::Statement::Mechanical::BaseStatement';

sub _init {
    my ( $self, %args ) = @_;
    my $timer = Common::Timer->new();

    $self->{_hfa}              = $args{hfa};
    $self->{_focusPublisherID} = $args{focusPublisherID};

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

    #    $self->_postProcess();

    return $self;
}

# !!! This absolutely seems pointless now..
#
#sub _postProcess
#{
#    if ($self->{_hfa})
#    {
#        my $digitalTotal = 0;
#        my $physicalTotal = 0;
#
#        if ($self->{Publisher}->IsAdmin()
#         || $self->{Publisher}->IsAgency())
#        {
#            my $pubList = $self->{MechanicalStatementPublisherList}->getList();
#            foreach my $publisher (@$pubList)
#            {
#                my $trackList = $publisher->getTrackList();
#                my ($pubDigitalTotal, $pubPhysicalTotal) = _getTrackListTotal($trackList);
#                $digitalTotal += $pubDigitalTotal;
#                $physicalTotal += $pubPhysicalTotal;
#            }
#        }
#        else
#        {
#            my $trackList = $self->{MechanicalStatementTrackList}->getList();
#            ($digitalTotal, $physicalTotal) = _getTrackListTotal($trackList);
#        }
#
#
#        # Now replace the previously calculated totals with these.
#        #
#        $self->{PhysicalSubtotal}       = Common::FormObject::Scalar::Decimal->new(value => $digitalTotal, format => '.4');
#        $self->{DigitalSubtotal}        = Common::FormObject::Scalar::Decimal->new(value => $physicalTotal, format => '.4');
#    }
#}
#
#
#sub _getTrackListTotal
#{
#    my ($trackList) = @_;
#
#    my $physicalTotal;
#    my $digitalTotal;
#
#    foreach my $track (@$trackList)
#    {
#        my ($trackD, $trackP) = $track->getDigitalPhysicalTotals();
#    }
#    return ($digitalTotal, $physicalTotal);
#}
#

sub _initProperties {
    my ( $self, $props ) = @_;
    my $timer = Common::Timer->new();

    $self->SUPER::_initProperties($props);

    # jpk - the ManufacturerNumber is assigned by hfa, and is based on client id.
    # !!! Why look this up now?  why not put this in the mechanical_statement table?
    #
    my $manufacturerCode = Common::Client::Current()->HFAManufacturerCode();
    $self->{ManufacturerNumber} = Common::FormObject::Scalar::String->new( value => $manufacturerCode, readOnly => 1 );

    # jpk - transaction date is supposed to be the date this statement is submitted to hfa.
    #      So, we'll just use the current date.
    #
    my ( undef, undef, undef, $mday, $mon, $year, undef, undef, $isdst ) = localtime(time);
    my $now = sprintf( "%4d%02d%02d", $year + 1900, $mon + 1, $mday );
    $self->{TransactionDate} = Common::FormObject::Scalar->new( value => $now, readOnly => 1 );

    # jpk - report the number of items
    #
    my $numLines;
    if ( $self->{_hfa} ) {
        $numLines = RPS::DB::Item::MechanicalStatementItem->GetNumPositiveItemsByStatementID( $props->{mechanical_statement_id} );
    } else {
        $numLines = RPS::DB::Item::MechanicalStatementItem->GetNumItemsByStatementID( $props->{mechanical_statement_id} );
    }
    $self->{NumLines} = Common::FormObject::Scalar->new( value => $numLines );

    if (   $self->{Publisher}->IsAdmin()
        || $self->{Publisher}->IsAgency() ) {
        $self->{MechanicalStatementPublisherList} = RPS::Statement::Mechanical::StatementPublisherList->new(
            mechanicalStatementID => $props->{mechanical_statement_id},
            hfa                   => $self->{_hfa},
            focusPublisherID      => $self->{_focusPublisherID},
        );
    } else {

        # Load the list of statement lines.
        #
        $self->{MechanicalStatementTrackList} = RPS::Statement::Mechanical::StatementTrackList->new(
            mechanicalStatementID => $props->{mechanical_statement_id},
            hfa                   => $self->{_hfa}
        );
    }

    $self->{MechanicalStatementTransactionList} =
      RPS::Statement::Mechanical::StatementTransactionList->new( mechanicalStatementID => $props->{mechanical_statement_id} );

    $self->{Payor} = RPS::Payor::Payor->new( payorID => $props->{payor_id}, loadSubs => 0 );
}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub _calculateReportingPeriod {
    my ( $startDate, $endDate ) = @_;

    my ( $startYear, $startMonth, $startDay ) = split( '-', $startDate );
    my ( $endYear,   $endMonth,   $endDay )   = split( '-', $endDate );

    my ( $dy, $dm, $dd ) = Date::Calc::Delta_YMD( $startYear, $startMonth, $startDay, $endYear, $endMonth, $endDay );

    my $months = ( 12 * $dy ) + $dm;
    if ( $dd > 0 ) {
        $months++;
    }

    # Good ole' harry fox assumes the year is >= 2000.
    #
    my $period = sprintf( "%02d%02d%02d", $months, $endMonth, ( $endYear - 2000 ) );
    return $period;
}

###
1;    #
###
