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

package RPS::ArtistRoyalty::Fast::Final::ConsolidatedArtistRoyaltyRunMissedSaleLog;

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 RPS::DB::Item::ProductType;

use Data::Dumper;
use DB_File;

use RPS::ArtistRoyalty::Fast::Mapped::MissedSales;
use RPS::ArtistRoyalty::Fast::Static::ContractTitleArtists;
use RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts;

use Common::Log;

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

# We never actually _render_ the 'total revenue' column, so why bother with it at all?
#

use constant kArtistRoyaltyRunID        => 0;
use constant kReason                    => 1;
use constant kDetails                   => 2;
use constant kAlbumID                   => 3;
use constant kTrackID                   => 4;
use constant kProductID                 => 5;
use constant kArtistContractID          => 6;
use constant kUnits                     => 7;
use constant kTotalRevenue              => 8;
use constant kArtistContractTitle       => 9;
use constant kArtistName                => 10;
use constant kAlbumTitle                => 11;
use constant kCatalogNumber             => 12;
use constant kTrackTitle                => 13;
use constant kProductTypeID             => 14;
use constant kProductType               => 15;


my @gFields = qw/
artist_royalty_run_id
reason
details
album_id
track_id
product_id
artist_contract_id
units
total_revenue
artist_contract_title
artist_name
album_title
catalog_number
track_title
product_type_id
product_type
/;


sub Fields { return \@gFields; }



sub FileName { 'consolidated_artist_royalty_run_missed_sale_log' }


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

    return 0;
}


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

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


    $gRunID = $runID;
}



sub new
{
    my ($class, $data, $runID, $artistContractData, $allProductData) = @_;

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

# JPK - I had been under the impression that we needed to quote our strings, but it appears
# that this step is not necessary when using the LOAD DATA INFILE scheme.
#    my $dbo = Common::RSApp::GetClientDB();

    my $productData = $allProductData->{int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kProductID])};
    my $contractData = $artistContractData->{int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kArtistContractID])};

    $item->[kArtistRoyaltyRunID] = $gRunID;

    # Ultimately this will be a string, but we'll store the integer code in here for now to make comparison fast.
    #
    $item->[kReason] = int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kReasonCode]),

    $item->[kDetails] = $data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kDetails],
    $item->[kAlbumID] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kAlbumID] : 0,
    $item->[kTrackID] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kTrackID] : 0,
    $item->[kProductID] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kProductID] : 0,
    $item->[kArtistContractID] = $contractData ? $contractData->[RPS::ArtistRoyalty::Fast::Static::ContractTitleArtists::kArtistContractID] : 0,
    $item->[kUnits] = $data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kUnits],
    $item->[kTotalRevenue] = 0,
    $item->[kArtistContractTitle] = $contractData ? $contractData->[RPS::ArtistRoyalty::Fast::Static::ContractTitleArtists::kArtistContractTitle] : '""',
    $item->[kArtistName] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kArtistName] : '""',
    $item->[kAlbumTitle] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kAlbumTitle] : '""',
    $item->[kCatalogNumber] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kCatalogNumber] : '""',
    $item->[kTrackTitle] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kTrackTitle] : '""',
    $item->[kProductTypeID] = $productData ? $productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kProductTypeID] : '""',
    $item->[kProductType] = $productData ? _idToProductType($productData->[RPS::ArtistRoyalty::Fast::Static::MissedSaleProducts::kProductTypeID]) : '""',


    return bless($item, $class);
}


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

    # Test whether we're still looking at data for 'this' object.
    #
    if ($self->[kProductID] != int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kProductID])
     || $self->[kArtistContractID] != int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kArtistContractID])
     || $self->[kReason] != int($data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kReasonCode])
     || $self->[kDetails] ne $data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kDetails])
    {
        return undef;
    }

    $self->[kUnits] += $data->[RPS::ArtistRoyalty::Fast::Mapped::MissedSales::kUnits];

    return 1;
}



sub _output
{
    my ($self, $fd) = @_;
    $self->[kReason] = RPS::ArtistRoyalty::Fast::Mapped::MissedSales::Reason($self->[kReason]);
    $self->SUPER::_output($fd);
}



my $gProductTypeIDMap;
sub _idToProductType
{
    my ($id) = @_;

    if (! $gProductTypeIDMap)
    {    
        $gProductTypeIDMap = {};

        my $all = RPS::DB::Item::ProductType->GetAll();
        while (my $pt = $all->next())
        {    
            $gProductTypeIDMap->{$pt->product_type_id} = $pt->description;
        }    
    }    

    my $productTypeName = '(Missing)';

    if ($gProductTypeIDMap->{$id})
    {    
        $productTypeName = $gProductTypeIDMap->{$id};
    }    

    return $productTypeName;
}
1;

