#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyExpenseItem;

use strict;
use Data::Dumper;
use File::Path;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';
use Common::RSApp;
use Common::RSDB;

use Data::Dumper;
use DB_File;

use RPS::ArtistRoyalty::Fast::Mapped::Data;
use RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum;

use Common::Log;

use base 'RPS::ArtistRoyalty::Fast::Final';

# Pretty sure term_rate is irrelevant now.
# !!! Well, in the old code we never set it, and presumably
# !!! just look up the term and do the math at the end.
# !!! But we might as well make use of it, and save a lookup here.
#
use constant kArtistRoyaltyAlbumID => 0;
use constant kTrackID              => 1;
use constant kExpenseID            => 2;
use constant kExpenseName          => 3;
use constant kMemo                 => 4;
use constant kUsesDefaultNetRate   => 5;
use constant kCost                 => 6;
use constant kRate                 => 7;
use constant kTotal                => 8;
use constant kTermRate             => 9;

my @gFields = (
    'artist_royalty_album_id', 'track_id', 'expense_id', 'expense_name', 'memo', 'uses_default_net_rate', 'cost', 'rate', 'total',
    'term_rate'
);

sub Fields { return \@gFields; }

sub FileName { 'artist_royalty_expense_item' }

sub GetCurrentMaxID {
    my ($class) = @_;

    return 0;
}

sub Init {
    my ( $class, $outputDirectory ) = @_;

    # Call the inherited method first.
    #
    $class->SUPER::Init($outputDirectory);

}

sub new {
    my ( $class, $data, $album ) = @_;

    my $item = $class->BlankItem();

    # So, really, we don't 'accumulate' Expenses.
    # Each line of data maps to a line in the output file.

    # Meaning, we'll do essentially everything here in the constructor.
    # process will _always_ return undef.
    #
    $item->[kArtistRoyaltyAlbumID] = $album->id();
    $item->[kTrackID]              = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kTrackID];
    $item->[kExpenseID]            = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseID];
    $item->[kExpenseName]          = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseName];
    $item->[kMemo]                 = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseMemo];

    # !!! This is not working. getting 1 always.  So I'm getting a termID each time?
    #
    $item->[kUsesDefaultNetRate] = ( $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseDefaultTermRate] == 0 ? 0 : 1 );

    $item->[kCost]     = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseCost];
    $item->[kRate]     = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseRate];
    $item->[kTotal]    = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseTotal];
    $item->[kTermRate] = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kExpenseDefaultTermRate];

    push @$item, $album;

    return bless( $item, $class );
}

sub process {
    my ( $self, $data ) = @_;

    return undef;
}

# Override _output to send our totals up to the Album
#
sub _output {
    my ( $self, $ITEMS ) = @_;
    my $album = pop @$self;

    # Update expense summary data.
    #
    if ( $self->[kUsesDefaultNetRate] ) {
        $album->[RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum::kNetRevenueExpenses] += $self->[kTotal];
        $album->[RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum::kDefaultNetRevenueRate] = $self->[kTermRate];
    } else {
        $album->[RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum::kRecoupableExpenses] += $self->[kTotal];
    }

    $self->SUPER::_output($ITEMS);
}

1;

