#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package RPS::Report::MissingLicenseAccrual;
use strict;
use warnings;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::MechanicalRunUnlicensedTrackLog;

use base 'RPS::Report';

sub _baseXMLName { 'Track' }

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

    my $runID = $args{runID};

	my $labelList = RPS::DB::Item::MechanicalRunUnlicensedTrackLog->GetLabelsByRunID($runID);
    while (my $label = $labelList->next())
    {
        my $hash = $self->_fillInLabelHash($label);

        push @{$self->{LabelList}{Label}}, $hash;
    }

	my $run = RPS::DB::Item::MechanicalRun->Lookup(mechanical_run_id => $runID);
    $self->{MechanicalRun}{Label} = $run->label;

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

sub _getDBItem() {
    my $self = shift;
    my %args = @_;

	my $runID = $args{runID};

    return RPS::DB::Item::MechanicalRunUnlicensedTrackLog->GetByRunID($runID);
}

sub _reportColumns {
    my $self = shift;

    return (
             "RunName"               => "Run Name",
             "LabelName"             => "Label",
             "AlbumTitle"            => "Album Title",
             "CatalogNumber"         => "Catalog #",
             "UPC"                   => "UPC",
             "TrackArtistName"       => "Artist",
             "TrackTitle"            => "Track Title",
             "ISRC"                  => "ISRC",
             "UnlicensedShare"       => "Share",
             "ProductType"           => "Config",
             "Year"                  => "Year",
             "Rate"                  => "Rate",
             "Units"                 => "Units",
             "Accrual"               => "Cumulative Accrual USD",
    );
}

sub _fillInHash
{
    my ($self, $logLine) = @_;

    my %data;

    $data{LabelName}        = $logLine->label_name;
    $data{CatalogNumber}    = $logLine->catalog_number;
    $data{UPC}              = $logLine->upc_ean;
    $data{TrackTitle}       = $logLine->track_title;
    $data{TrackArtistName}  = $logLine->track_artist_name;
    $data{ISRC}             = $logLine->isrc;
    $data{AlbumID}          = $logLine->album_id;
    $data{TrackID}          = $logLine->track_id;
    $data{ProductID}        = $logLine->product_id;    
    $data{ProductTitle}     = $logLine->product_title;
    $data{ProductType}      = $logLine->product_type;
    $data{UnlicensedShare}  = Common::Client::Current()->Locale()->formatNumber($logLine->unlicensed_share,.2);
    
    # If we have a product title, we'll use that here.
    # Otherwise, fall back to the album title
    if ($logLine->product_title) {
        $data{AlbumTitle}   = $logLine->product_title;
    } else {
        $data{AlbumTitle}   = $logLine->album_title;
    }

    $data{Year}             = $logLine->year;

    $data{Rate}             = ($logLine->rate != 0) ? Common::Client::Current()->Locale()->formatNumber($logLine->rate,.4) : '';
    $data{Units}            = $logLine->units;
    $data{Accrual}          = Common::Client::Current()->Locale()->formatNumber($logLine->accrual,.2);

	$data{RunName}          = $self->{MechanicalRun}{Label};

    return \%data;
}

sub _fillInLabelHash
{
    my ($self, $label) = @_;

    my %data;

    $data{LabelID}          = $label->label_id;
    $data{LabelName}        = $label->label_name;

    return \%data;
}


###
1;#
###
