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

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 RPS::DB::Item::MechanicalStatementTrack;
use RPS::DB::Item::Track;
use RPS::DB::Item::Album;
use RPS::Song::Song;

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{mechanicalStatementTrackID})
    {
        $self->_propertiesFromDB(\%properties, $args{mechanicalStatementTrackID});
    }
    else
    {
        $self->_propertiesFromDefaults(\%properties);
    }

    $self->_initProperties(\%properties);

    return $self;
}


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


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

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


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

    my $currencyCode = $self->_getCurrencyCode();

    $self->{MechanicalStatementTrackID} = Common::FormObject::Scalar->new(value => $props->{mechanical_statement_track_id}, readOnly => 1);
    $self->{MechanicalStatementID}      = Common::FormObject::Scalar->new(value => $props->{mechanical_statement_id});
    $self->{TrackID}                    = Common::FormObject::Scalar->new(value => $props->{track_id});
    $self->{AlbumID}                    = Common::FormObject::Scalar->new(value => $props->{album_id});
    $self->{PublisherID}                = Common::FormObject::Scalar->new(value => $props->{publisher_id});
    $self->{Subtotal}                   = Common::FormObject::Scalar::Money->new(value => $props->{subtotal}, currencyCode => $currencyCode);
    $self->{SongTitle}                  = Common::FormObject::Scalar::String->new(value => $props->{song_title});
    $self->{CrossedTransactionSubtotal} = Common::FormObject::Scalar::Money->new(value => $props->{crossed_transaction_subtotal}, currencyCode => $currencyCode);
    $self->{CrossedSubtotal}            = Common::FormObject::Scalar::Money->new(value => $props->{crossed_subtotal}, currencyCode => $currencyCode);
    $self->{CrossedPreviousAdvanceBalance} = Common::FormObject::Scalar::Money->new(value => $props->{crossed_previous_advance_balance}, currencyCode => $currencyCode);
    $self->{CrossedAppliedToAdvance}    = Common::FormObject::Scalar::Money->new(value => $props->{crossed_applied_to_advance}, currencyCode => $currencyCode);
    $self->{CrossedAdvanceBalance}      = Common::FormObject::Scalar::Money->new(value => $props->{crossed_advance_balance}, currencyCode => $currencyCode);
    $self->{CrossedAdjustedSubtotal}    = Common::FormObject::Scalar::Money->new(value => $props->{crossed_adjusted_subtotal}, currencyCode => $currencyCode);

    # We're going to want to have the track title at this level.
    # The other stuff, like album title and product code, will be the responsibility
    # of each individual statement item
    #

    my $trackID = $props->{track_id};
    my $statementID = $props->{mechanical_statement_id};
    my $publisherID = $props->{publisher_id};

        
    $self->{MechanicalStatementCrossedLicenseList} = $self->_getCrossedLicenseList($statementID, $trackID, $publisherID);
    $self->{MechanicalStatementCrossedLicenseTransactionList} = $self->_getCrossedLicenseTransactionList($statementID, $trackID, $publisherID);
    $self->{MechanicalStatementLicenseList} = $self->_getLicenseList($statementID, $trackID, $publisherID);


    # I'm calling ISRC a 'sub', since I need to query the Master table to find it.
    #
    my $track = RPS::DB::Item::Track->Lookup(track_id => $props->{track_id});
    if ($track)
    {
        my $master = RPS::DB::Item::Master->Lookup(master_id => $track->master_id);
        if ($master)
        {
            $self->{ISRC} = Common::FormObject::Scalar::String->new(value => $master->isrc, readOnly => 1);
        }
    }

    # Need to display the catalog number for HFA...
    #
    my $catalogNumber;
    my $albumName;
    if ($props->{album_id})
    {
        my $album = RPS::DB::Item::Album->Lookup(album_id => $props->{album_id});
        if ($album)
        {
            $catalogNumber = $album->catalog_number;
            $albumName = $album->title;
        }
    }
    $self->{CatalogNumber} = Common::FormObject::Scalar::String->new(value => $catalogNumber);
    $self->{AlbumName} = Common::FormObject::Scalar::String->new(value => $albumName);

}

sub _getCrossedLicenseList
{
    my ($self, $statementID, $trackID, $publisherID) = @_;
    assert(0, 'override this');
}


sub _getCrossedLicenseTransactionList
{
    my ($self, $statementID, $trackID, $publisherID) = @_;
    assert(0, 'override this');
}

sub _getLicenseList
{
    my ($self, $statementID, $trackID, $publisherID) = @_;
    assert(0, 'override this');
}


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

}

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

    my $digital;
    my $physical;

    my $licenseList = $self->{MechanicalStatementLicenseList}->getList();
    foreach my $license (@$licenseList)
    {
        my ($d, $p) = $license->getDigitalPhysicalTotals();
        $digital += $d;
        $physical += $p;
    }

    return ($digital, $physical);
}

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

    my $numItems = 0;
    my $licenseList = $self->{MechanicalStatementLicenseList}->getList();
    foreach my $license (@$licenseList)
    {
        $numItems += $license->numLineItems();
    }

    return ($numItems > 0);
}


###
1;#
###



