#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package RPS::Statement::Mechanical::StatementLicense;

use strict;
use warnings;
use Carp;

use Data::Dumper;

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

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

use Common::FormObject;
use base 'Common::FormObject';

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

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

    my %properties;
    if ( $args{_dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{_dbItem} );
    } elsif ( $args{mechanicalStatementLicenseID} ) {
        $self->_propertiesFromDB( \%properties, $args{mechanicalStatementLicenseID} );
    } else {
        $self->_propertiesFromDefaults( \%properties );
    }

    $self->_initProperties( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $id ) = @_;
    assert( 0, 'override this' );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;
    assert( 0, 'override this' );
}

sub _getCurrencyCode {
    assert( 0, 'override this' );
}

sub _initProperties {
    my ( $self, $props ) = @_;

    # !!! Assume USD for now...
    #
    my $currencyCode = $self->_getCurrencyCode();

    $self->{MechanicalStatementLicenseID} =
      Common::FormObject::Scalar->new( value => $props->{mechanical_statement_license_id}, readOnly => 1 );
    $self->{MechanicalStatementID} = Common::FormObject::Scalar->new( value => $props->{mechanical_statement_id} );
    $self->{TrackLicenseID}        = Common::FormObject::Scalar->new( value => $props->{track_license_id} );
    $self->{TransactionSubtotal} =
      Common::FormObject::Scalar::Money->new( value => $props->{transaction_subtotal}, currencyCode => $currencyCode );
    $self->{Subtotal} = Common::FormObject::Scalar::Money->new( value => $props->{subtotal}, currencyCode => $currencyCode );
    $self->{PreviousAdvanceBalance} =
      Common::FormObject::Scalar::Money->new( value => $props->{previous_advance_balance}, currencyCode => $currencyCode );
    $self->{AppliedToAdvance} =
      Common::FormObject::Scalar::Money->new( value => $props->{applied_to_advance}, currencyCode => $currencyCode );
    $self->{AdvanceBalance} = Common::FormObject::Scalar::Money->new( value => $props->{advance_balance}, currencyCode => $currencyCode );
    $self->{AdjustedSubtotal} =
      Common::FormObject::Scalar::Money->new( value => $props->{adjusted_subtotal}, currencyCode => $currencyCode );
    $self->{RegionID} = Common::FormObject::Scalar->new( value => $props->{region_id} );
    $self->{Crossed} = Common::FormObject::Scalar::Boolean->new( value => $props->{crossed} );

    my $statementID    = $props->{mechanical_statement_id};
    my $trackLicenseID = $props->{track_license_id};

    $self->{MechanicalStatementItemList} = $self->_getStatementItemList( $statementID, $trackLicenseID );

    $self->{MechanicalStatementAdjustmentItemList} = $self->_getStatementAdjustmentItemList( $statementID, $trackLicenseID );

    $self->{MechanicalStatementLicenseTransactionList} = $self->_getLicenseTransactionList( $props->{mechanical_statement_license_id} );

    # We also want to display the license info here.
    #
    # jpk - the schema has changed...

    # !!! WTF?  This is not an XML object...
    #        my $trackLicense = RPS::DB::Item::TrackLicense->Lookup(track_license_id => $props->{track_license_id});
    #
    #        $self->{TrackLicense} = $trackLicense;
}

sub _getStatementItemList {
    my ( $self, $statementID, $trackLicenseID ) = @_;
    assert( 0, 'override this' );
}

sub _getStatementAdjustmentItemList {
    my ( $self, $statementID, $trackLicenseID ) = @_;
    assert( 0, 'override this' );
}

sub _getLicenseTransactionList {
    my ( $self, $statementLicenseID ) = @_;
    assert( 0, 'override this' );
}

sub numLineItems {
    my ($self) = @_;
    return $self->{MechanicalStatementItemList}->numItems;
}

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

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

    my $digital;
    my $physical;

    my $itemList = $self->{MechanicalStatementItemList}->getList();
    foreach my $item (@$itemList) {
        my $amount = $item->Amount();

        # Is this a physical or digital product?
        #
        if ( 'CD' eq $item->ConfigurationCode() ) {
            $physical += $amount;
        } else {
            $digital += $amount;
        }
    }

    return ( $digital, $physical );
}

###
1;    #
###
