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

package RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyLicenseIncomeItem;

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::Final::SaleRunMap;
use RPS::ArtistRoyalty::Fast::Mapped::Data;
use RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum;

use Common::Log;

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

use constant kArtistRoyaltyLicenseIncomeItemID => 0;
use constant kLicenseIncomeID                  => 1;
use constant kArtistRoyaltyStatementID         => 2;
use constant kAlbumID                          => 3;
use constant kTrackID                          => 4;
use constant kArtistContractID                 => 5;
use constant kLicenseIncomeTypeID              => 6;
use constant kRate                             => 7;
use constant kUnits                            => 8;
use constant kRevenue                          => 9;
use constant kNetRevenue                       => 10;
use constant kMemo                             => 11;

use constant kAlbumRef => 12;

my @gFields = (
    'artist_royalty_license_income_item_id',
    'license_income_id', 'artist_royalty_statement_id', 'album_id', 'track_id', 'artist_contract_id', 'license_income_type_id', 'rate',
    'units', 'revenue', 'net_revenue', 'memo',
);

sub Fields { return \@gFields; }

sub FileName  { 'artist_royalty_license_income_item' }
sub _MaxIDSql { 'SELECT MAX(artist_royalty_license_income_item_id) FROM artist_royalty_license_income_item' }

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

    # We get passed the $album, since this is sort of a line-item entity.
    # But we really need the statement_id (although I find that... weird).
    #

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

    # 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->[kArtistRoyaltyLicenseIncomeItemID] = $class->_CurrentID();

    $item->[kLicenseIncomeID]          = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeID];
    $item->[kArtistRoyaltyStatementID] = $album->[RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum::kArtistRoyaltyStatementID];
    $item->[kAlbumID]                  = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kAlbumID];
    $item->[kTrackID]                  = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kTrackID];
    $item->[kArtistContractID]         = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kArtistContractID];
    $item->[kLicenseIncomeTypeID]      = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeTypeID];
    $item->[kRate]                     = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeRate];
    $item->[kUnits]                    = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeUnits];
    $item->[kRevenue]                  = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeRevenue];
    $item->[kNetRevenue]               = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeNetRevenue];
    $item->[kMemo]                     = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kLicenseIncomeMemo];

    push @$item, $album;

    # Create a sale_run_map entry to associate this sale with this run (so we know it was paid on subsequent runs).
    #
    my $saleID = $data->[RPS::ArtistRoyalty::Fast::Mapped::Data::kSaleID];
    if ( 0 != $saleID ) {
        my $saleRunMap = RPS::ArtistRoyalty::Fast::Final::SaleRunMap->new( $saleID, $item->[kArtistRoyaltyLicenseIncomeItemID] );
        #
        # Note that we let the reference immediately go out of scope, which _writes it out_.
    }

    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.
    #
    # !!! What if the album_id was 0?
    # !!! I believe we still have an Album object, so this works.
    # !!! It will then be up to the Album to pass along any license income
    # !!! not associated with an album (aka contract level license income) to
    # !!! the statement.
    #
    $album->[RPS::ArtistRoyalty::Fast::Final::ArtistRoyaltyAlbum::kLicenseIncomeSubtotal] += $self->[kNetRevenue];

    Log->warn( "+++ Outputting this license income item: ", $self, " passing the album this net revenue: " . $self->[kNetRevenue] );

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

1;

