#!/usr/bin/perl
#------------------------------------------------------------
# Copyright (C) 2006-2012 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
use strict;

#use warnings;

use Data::Dumper;
use Getopt::Std;
use Carp;

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

use lib '/app/tools/rps/lib';
use RPS::Statement::Mechanical::CA::StatementFull;
use RPS::Statement::Mechanical::CA::StatementLicenseTransactionList;

use RPS::Payor::Payor;
use RPS::Publisher::CA::Publisher;
use RPS::Mechanical::CA::MechanicalRun;
use RPS::DB::Item::ContractRateType;
use RPS::DB::Item::IncomeSource;
use RPS::DB::Item::Region;
use RPS::DB::Item::Channel;
use RPS::DB::Item::PriceLevel;
use RPS::DB::Item::Product;
use RPS::DB::Item::ProductType;
use RPS::DB::Item::CATrackLicense;

use lib '/app/tools/rps/bin/statements';
use Formats;

$SIG{__DIE__} = \&Carp::confess;



# This determines how much output we spew forth to STDERR.
# The user can set this with the -V command-line argument.
#
my $gVerbosityLevel = 1;

# Parse the command-line, then create the CSV!
#
my %options;
parseCommandLine( \%options );

processMechanicalRunCSV( $options{clientID}, $options{mechanicalRunID},
    $options{mechanicalStatementID}, $options{outputPath} );

# ----------------------------------------------------------------------------------------------

sub processMechanicalRunCSV
{
    my($clientID, $runID, $statementID, $outFilePath) = @_;

    # Instantiate the application singleton object.
    #
    my $appSingleton = Common::RSApp->new( clientID => $clientID );

    # This report is intended to be run after the statements have been
    # generated, so the output path should already exist.

    # Create the output path if it isn't there already.
    #
    if( '/' ne substr($outFilePath, -1, 1) )
    {
        $outFilePath .= "/";
    }

    if(! -d $outFilePath )
    {
        mkpath($outFilePath) or die "ERROR: Unable to create path $outFilePath: $!\n";
    }


    # Get rid of the semaphore file
    my $semaphore = $outFilePath . "EXPORT_TEXT_COMPLETE";
    unlink($semaphore);

    print STDERR "DEBUG: outFilePath($outFilePath) semaphore($semaphore)!!! \n";

    my $targetFile = $outFilePath . "ca_mechanical_export_run_$runID.txt";

    # Open export file for writing
    #open(my $outFile,">".$outFilePath);
    open(my $outFile,">".$targetFile);
    binmode($outFile, ':utf8'); 


    # Write out column headings.
    my @headings = (
        'publisher-name',            # A
        'client-no',                 # B
        'publisher-type',            # C
        'publisher-admin',           # D
        'publisher-agent',           # E
        'track-title',               # F
        'ISRC',                      # G
        'catalog-no',                # H
        'album-title',               # I
        'upc',                       # J
        'license-config',            # K
        'region',                    # L
        'share',                     # M
        'total-gross-units',         # N
        'percent-of-sales',          # O
        'free-goods',                # P
        'misc',                      # Q
        'total-reserves',            # R
        'total-liquidations',        # S
        'total-returns',             # T
        'total-carryover',           # U
        'total-net-units',           # V
        'total-cc-adjustments',      # W
        'license-subtotal',          # X
        'total-adjustments',         # Y
        'total-advances',            # Z
        'previous-license-balance',  # AA
        'license-balance-forward',   # AB
        'license-adjusted-subtotal', # AC
        'rs-license-id',             # AD
        'license-date-modified',     # AE

    );

    my $header = join("\t", @headings);
    print $outFile "$header\n";

    # Get the collection of statements for this run.
    #
    my $statements = RPS::DB::Item::CAMechanicalStatement->GetByMechanicalRunID($runID);
    while (my $statement = $statements->next())
    {

        my $_stmtID = $statement->ca_mechanical_statement_id;

        next if ( $statementID && $_stmtID != $statementID );

        processStatementCSV( $outFile,  $_stmtID );
    }


    # All done, close it up.
    #
    close $outFile;

    # Create the semaphore
    #
    open SEMAPHORE, "> $semaphore";
    print SEMAPHORE "done\n";
    close SEMAPHORE;

}#processMechanicalRunCSV


sub processStatementCSV
{
    my ( $outFile, $mechanicalStatementID ) = @_;

    # Instantiate the MechanicalStatement object.
    # This object will contain all the info we need to output the statement document.
    #
    my $statement = RPS::Statement::Mechanical::CA::StatementFull->new(
        mechanicalStatementID => $mechanicalStatementID,
        focusPublisherID      => 'all'
    );

    # Determine if this is an admin or regular publisher.
    if (  $statement->Publisher()->IsAdmin() == 1 || $statement->Publisher()->IsAgency() == 1 )
    {
        # Loop through all publishers for admins.
        my $publisherList = $statement->MechanicalStatementPublisherList()->getList();
        foreach my $publisher (@$publisherList)
        {
            details( $publisher, $outFile);
        }
    }
    else
    {
        # Not an admin, so just do this once.
        details( $statement, $outFile );
    }

}


sub details
{
    my ( $statement, $outFile ) = @_;

    my %gExport; # will hold our royalty data export

    my $trackList = $statement->MechanicalStatementTrackList()->getList();
    foreach my $track (@$trackList)
    {

        my %licenseData; # will contain all license-centric report data for the current track


        # Go through the crossed license list
        #
        my $crossedLicenseList = $track->MechanicalStatementCrossedLicenseList()->getList();

        my $crossedAdvanceTotal    = 0;
        my $crossedAdjustmentTotal = 0;

        if ( @$crossedLicenseList > 0 )
        {

            my $balanceTotal = 0;

            # Add the transactions up into two buckets, adjustments and advances.
            #
            my $advanceTotal = 0;
            my $adjustmentTotal = 0;
 
            foreach my $license (@$crossedLicenseList) # Crossed licenses !!!
            {
                my $trackLicenseData = RPS::DB::Item::CATrackLicense->Lookup(ca_track_license_id => $license->TrackLicenseID());
                my $issuerLicenseID = $trackLicenseData->issuer_license_id;
                my $licenseID = $trackLicenseData->ca_track_license_id;

                $licenseData{$licenseID}{cross_collateralized} = $trackLicenseData->cross_collateralized;

#
                $licenseData{$licenseID}{crossed_previous_advance_balance} = $track->CrossedPreviousAdvanceBalance();
                $licenseData{$licenseID}{crossed_subtotal}                 = $track->CrossedSubtotal();
                $licenseData{$licenseID}{crossed_adjusted_subtotal}        = $track->CrossedAdjustedSubtotal();
                $licenseData{$licenseID}{crossed_advance_balance}          = $track->CrossedAdvanceBalance();

                $licenseData{$licenseID}{adjusted_subtotal}          = $license->AdjustedSubtotal();
                $licenseData{$licenseID}{subtotal}                   = $license->Subtotal();

                # Get the license transactions tied to this license
                #
                my $xmlObj = $license->MechanicalStatementLicenseTransactionList();
                my $licenseTransactionList  = $xmlObj->getList();
                my $licenseTransactionCount = scalar(@$licenseTransactionList);

                if ( $licenseTransactionCount != 0 )
                {
                    foreach my $licenseTransaction (@$licenseTransactionList)
                    {
                        if ( $licenseTransaction->TypeCode == 3 )
                        {
                            $advanceTotal += $licenseTransaction->Amount();
                        }
                        else
                        {
                            $adjustmentTotal += $licenseTransaction->Amount();
                        }
                    }
                }

                $licenseData{$licenseID}{advance_total}    = $advanceTotal;
                $licenseData{$licenseID}{adjustment_total} = $adjustmentTotal;


                my $incomeItemList = $license->MechanicalStatementItemList()->getList();
                my $incomeItemListLength = scalar(@$incomeItemList);

                if ( $incomeItemListLength != 0 )
                {
                    # If there are income items, process them all and aggregate
                    # the following data:
                    #  - gross units
                    #  - reserves held
                    #  - reserves liquidated
                    #  - returns
                    #  - carryover
                    #  - net units
                    #  - cc adjustments (crossed adjustments)
                    #

                    foreach my $incomeItem (@$incomeItemList)
                    {
                        $licenseData{$licenseID}{net_units}   += $incomeItem->NetUnits();

                        #$licenseData{$licenseID}{gross_units} += $incomeItem->GrossUnits();
                        $licenseData{$licenseID}{gross_units} += $incomeItem->Sales();

                        $licenseData{$licenseID}{reserved}    += $incomeItem->Reserved();
                        $licenseData{$licenseID}{liquidated}  += $incomeItem->Liquidated();
                        $licenseData{$licenseID}{carryover}   += $incomeItem->Carryover();
                        $licenseData{$licenseID}{returns}     += $incomeItem->Returns();

                        $licenseData{$licenseID}{upc}  = $incomeItem->UPC() if ( $incomeItem->UPC() );
                        $licenseData{$licenseID}{product_id} = $incomeItem->ProductID() if ( $incomeItem->ProductID() );

                    }#income item loop

                }#income item list block


            }# license loop

        }#crossedLicenseList loop


        # Calculate the crossed advances and/or adjustments
        #
        # Note: these transactions are for ALL crossed licenses attached
        # to the track.
        #
        my $xmlObj = $track->MechanicalStatementCrossedLicenseTransactionList();
        my $licenseTransactionList  = $xmlObj->getList();
        my $licenseTransactionCount = scalar(@$licenseTransactionList);

        if ( $licenseTransactionCount != 0 )
        {
            foreach my $licenseTransaction (@$licenseTransactionList)
            {
                if ( $licenseTransaction->TypeCode == 3 )
                {
                    $crossedAdvanceTotal += $licenseTransaction->Amount();
                }
                else
                {
                    $crossedAdjustmentTotal += $licenseTransaction->Amount();
                }
            }
        }



        #-------------------------------------------------------
        #
        #  U N C R O S S E D    L I C E N S E    S E C T I O N
        #
        #-------------------------------------------------------


        # Go through the uncrossed license list
        #
        my $licenseList = $track->MechanicalStatementLicenseList()->getList();
        foreach my $license (@$licenseList)
        {

            my $licenseID = $license->TrackLicenseID;

            my $trackLicenseData = RPS::DB::Item::CATrackLicense->Lookup(ca_track_license_id => $license->TrackLicenseID());
            my $issuerLicenseID = $trackLicenseData->issuer_license_id;


            # Get the mechanical statement license totals for the license
            #

            $licenseData{$licenseID}{cross_collateralized} = $trackLicenseData->cross_collateralized;

            $licenseData{$licenseID}{previous_advance_balance} = $license->PreviousAdvanceBalance();
            $licenseData{$licenseID}{subtotal}                 = $license->Subtotal();
            $licenseData{$licenseID}{adjusted_subtotal}        = $license->AdjustedSubtotal();
            $licenseData{$licenseID}{advance_balance}          = $license->AdvanceBalance();


            # Get the income items associated with this license
            #
            my $incomeItemList       = $license->MechanicalStatementItemList()->getList();
            my $incomeItemListLength = scalar(@$incomeItemList);

            # If there are income items, aggregate some totals
            #
            if ( $incomeItemListLength != 0 )
            {

                # Process the mechanical statement items tied to this license,
                # aggregating totals along the way.
                #

                foreach my $incomeItem (@$incomeItemList)
                {
                    my $licenseID   = $incomeItem->TrackLicense->TrackLicenseID;
                    $licenseData{$licenseID}{net_units}   += $incomeItem->NetUnits();

                    #$licenseData{$licenseID}{gross_units} += $incomeItem->GrossUnits();
                    $licenseData{$licenseID}{gross_units} += $incomeItem->Sales();

                    $licenseData{$licenseID}{reserved}    += $incomeItem->Reserved();
                    $licenseData{$licenseID}{liquidated}  += $incomeItem->Liquidated();
                    $licenseData{$licenseID}{carryover}   += $incomeItem->Carryover();
                    $licenseData{$licenseID}{returns}     += $incomeItem->Returns();

                    $licenseData{$licenseID}{upc}  = $incomeItem->UPC() if ( $incomeItem->UPC() );
                    $licenseData{$licenseID}{product_id} = $incomeItem->ProductID() if ( $incomeItem->ProductID() );
                }
            }# income item list block


            my $xmlObj = $license->MechanicalStatementLicenseTransactionList();
            my $licenseTransactionList  = $xmlObj->getList();
            my $licenseTransactionCount = scalar(@$licenseTransactionList);

            # Add the transactions up into two buckets, adjustments and advances.
            #
            my $advanceTotal    = 0;
            my $adjustmentTotal = 0;

            if ( $licenseTransactionCount != 0 )
            {
                foreach my $licenseTransaction (@$licenseTransactionList)
                {
                    my $transactionType;
                    if ( $licenseTransaction->TypeCode == 3 )
                    {
                        $advanceTotal += $licenseTransaction->Amount();
                    }
                    else
                    {
                        $adjustmentTotal += $licenseTransaction->Amount();
                    }
                }
            }

            $licenseData{$licenseID}{advance_total}    = $advanceTotal;
            $licenseData{$licenseID}{adjustment_total} = $adjustmentTotal;


        }# uncrossed license list block


        # Output the report lines
        #
        foreach my $licenseID (keys %licenseData)
        {
            my $licenseObj        = RPS::DB::Item::CATrackLicense->Lookup( ca_track_license_id => $licenseID );
            my $productTypeID     = $licenseObj->product_type_id;
            my $share             = $licenseObj->share;
            my $regionID          = $licenseObj->region_id;
            my $dateModified      = $licenseObj->date_modified;
            my $percentageOfSales = $licenseObj->percentage_of_sales;
            my $freeGoods         = $licenseObj->free_goods || 0;
            my $miscDeduction     = $licenseObj->misc_deduction || 0;

            my $publisherObj = RPS::DB::Item::CAPublisher->Lookup( ca_publisher_id => $licenseObj->ca_publisher_id );

            my $publisherClientAccountID = $publisherObj->client_account_id;
            my $adminName                = _getAdminName( $publisherObj );
            my $agentName                = _getAgentName( $publisherObj );
            my $publisherType            = _getPublisherType( $publisherObj );

            my $cross_collateralized     = $licenseData{$licenseID}{cross_collateralized};

            my $previousAdvanceBalance;
            my $subtotal;
            my $adjustedSubtotal;
            my $licenseBalanceForward;

            if ( $cross_collateralized )
            {
                $previousAdvanceBalance  = $licenseData{$licenseID}{crossed_previous_advance_balance};

                my $crossedSubtotal          = $licenseData{$licenseID}{crossed_subtotal};
                my $crossedAdjustedSubtotal  = $licenseData{$licenseID}{crossed_adjusted_subtotal};

                $subtotal                    = $licenseData{$licenseID}{subtotal};

                $adjustedSubtotal            = $licenseData{$licenseID}{adjusted_subtotal};

                $licenseBalanceForward       = $licenseData{$licenseID}{crossed_advance_balance};

                if ( ($previousAdvanceBalance != 0) || ( $crossedSubtotal != $crossedAdjustedSubtotal ) )
                {
                    $adjustedSubtotal = $crossedAdjustedSubtotal;
                }
            }
            else
            {
                $previousAdvanceBalance  = $licenseData{$licenseID}{previous_advance_balance};
                $subtotal                = $licenseData{$licenseID}{subtotal};
                $adjustedSubtotal        = $licenseData{$licenseID}{adjusted_subtotal};
                $licenseBalanceForward   = $licenseData{$licenseID}{advance_balance};
            }

            my $advanceTotal             = $licenseData{$licenseID}{advance_total};
            my $adjustmentTotal          = $licenseData{$licenseID}{adjustment_total};

            my $licenseConfig = RPS::DB::Item::Product->ProductTypeIDToString( $productTypeID );

            my $albumTitle;
            my $productID = $licenseData{$licenseID}{product_id};
            if ($productID) 
            {
                my $product = RPS::DB::Item::Product->Lookup( product_id => $productID );
                $albumTitle = _getAlbumTitle($product, $track->AlbumName());
            } 
            else
            {
                $albumTitle = $track->AlbumName();    
            } 

            my @reportRow = (
                $statement->Publisher()->PublisherName(),  # A - publisher-name
                $publisherClientAccountID,                 # B - client-no
                $publisherType,                            # C - publisher-type
                $adminName,                                # D - publisher-admin
                $agentName,                                # E - publisher-agent
                $track->SongTitle(),                       # F - track-title
                $track->ISRC(),                            # G - isrc
                $track->CatalogNumber(),                   # H - catalog-no
                $albumTitle,                               # I - album-title
                $licenseData{$licenseID}{upc},             # J - upc
                $licenseConfig,                            # K - license-config
                regionIDToName( $regionID ),               # L - region
                formatPercent( $share ),                   # M - share
                $licenseData{$licenseID}{gross_units},     # N - total-gross-units
                formatPercent( $percentageOfSales ),       # O - percent-of-sales
                formatPercent( $freeGoods ),               # P - free-goods
                formatPercent( $miscDeduction ),           # Q - misc
                $licenseData{$licenseID}{reserved},        # R - total-reserves
                $licenseData{$licenseID}{liquidated},      # S - total-liquidations
                $licenseData{$licenseID}{returns},         # T - total-returns
                $licenseData{$licenseID}{carryover},       # U - total-carryover
                $licenseData{$licenseID}{net_units},       # V - total-net-units
                $crossedAdjustmentTotal,                   # W - total-cc-adjustments (NB: we don't show crossedAdvanceTotal)
                formatMoney( $subtotal ),                  # X - license-subtotal (current period license subtotal)
                formatMoney( $adjustmentTotal ),           # Y - total-adjustments
                formatMoney( $advanceTotal ),              # Z - total-advances
                formatMoney( $previousAdvanceBalance ),    # AA - previous-license-balance
                formatMoney( $licenseBalanceForward ),     # AB - license-balance-foward
                formatMoney( $adjustedSubtotal ),          # AC - license-adjusted-subtotal
                $licenseID,                                # AD - rs-license-id
                $dateModified,                             # AE - date-modified

            );
            my $joinedReportRow = join("\t", @reportRow);
            print $outFile "$joinedReportRow\n";
        }#license aggregated data loop


    }# track loop
}#details


# For now, we are not realy going to be doing any formatting of numbers.
#

sub formatNumber
{
    my ($value) = @_;

    #$value = Formats::formatNumber($value);

    return $value;
}

sub formatMoney
{
    my ($value) = @_;

    #$value = Formats::formatMoney($value);

    return $value;
}

sub formatPercent
{
    my ($value) = @_;

    #$value = Formats::formatPercent($value);

    return $value;
}

# These functions map ids to their text representations.
# Basically, the first time we ask for a particular id->string mapping, we
# will query the correct database, and build a hash.  Subsequent calls just
# hit the hash.
#
# Because these tables are so similar, I abstracted the guts into the _genericMapAccessor
# method (to save myself some typing).
#
my %idMaps;

sub _genericMapAccessor
{
    my ( $collectionAccessor, $idName, $id ) = @_;

    if ( !defined $idMaps{$collectionAccessor} )
    {
        $idMaps{$collectionAccessor} = {};

       # This is a very naughty thing to do, but it works great in this context.
       #
       #        no strict 'refs';
       #        my $c = &$collectionAccessor();
       #        use strict 'refs';
        my $c = $collectionAccessor->GetAll();

        while ( my $item = $c->next() )
        {
            if ( $idName ne 'product_type_id' )
            {
                $idMaps{$collectionAccessor}{ $item->$idName() } = $item->name;
            }
            else
            {
                $idMaps{$collectionAccessor}{ $item->$idName() } =
                  $item->description;
            }
        }
    }

    return $idMaps{$collectionAccessor}{$id};
}

sub regionIDToName
{
    my ($id) = @_;

    # we don't want to show Rest of World here
    if ( $id != 0 )
    {
        return _genericMapAccessor( 'RPS::DB::Item::Region', 'region_id', $id );
    }
    else
    {
        return ' ';
    }
}

sub _getAlbumTitle 
{
    my ($product, $albumTitle) = @_;
    
    my $title;
    
    # If this is a track product, we need to get the title from the parent.
    if ($product->product_type_id == RPS::DB::Item::Product::kProductTypeDigitalTrack && $product->parent_product_id)
    {
        my $parentProduct = RPS::DB::Item::Product->Lookup(product_id => $product->parent_product_id);
        $title = $parentProduct->title;
    }
    else 
    {
        $title = $product->title;
    }	
    
    # Just in case we don't find a product title, let's fall back to the album title.
    if (!$title) 
    {
        $title = $albumTitle;
    }
    
    return $title;	       
}

sub _getPublisherType
{
    my($publisher) = @_;
    assert($publisher);

    if( $publisher->is_agency )
    {
       return "Agent";
    }
    elsif( $publisher->is_admin )
    {
       return "Admin";
    }
    else
    {
       return "Standard";
    }
}

sub _getAdminName
{
    my($publisher) = @_;
    assert($publisher);
    my $name;
    if( $publisher->admin_id )
    {
       my $o = RPS::DB::Item::CAPublisher->Lookup(
           ca_publisher_id => $publisher->admin_id,
       );
       $name = $o->publisher_name;
    }
    return $name;
}

sub _getAgentName
{
    my($publisher) = @_;
    assert($publisher);
    my $name;
    if( $publisher->agent_id )
    {
       my $o = RPS::DB::Item::CAPublisher->Lookup(
           ca_publisher_id => $publisher->agent_id,
       );
       $name = $o->publisher_name;
    }
    return $name;
}

#
# Boring script stuff below...
#

sub parseCommandLine
{
    my ($settings) = @_;

    my %opt;
    getopts( 's:r:c:p:V:', \%opt );

    if ( !$opt{r} || !$opt{c} )
    {
        usage();
        exit(1);
    }
    $settings->{mechanicalRunID} = $opt{r};
    $settings->{clientID}        = $opt{c};
    $settings->{outputPath}      = $opt{p};
    $settings->{mechanicalStatementID} = $opt{s};

    if ( defined $opt{V} )
    {
        $gVerbosityLevel = $opt{V};
    }
}

sub usage
{
    print STDERR
"\nusage: $0 -c <client_id> -r <ca_mechanical_run_id> [-p <output path>]\n";
    print STDERR "\n";
    print STDERR "Arguments:\n";
    print STDERR
      "\t-c <client_id>\t\t\tThe client_id of the client to process\n";
    print STDERR
"\t-r <ca_mechanical_run_id>\tThe id of the CA mechanical run to generate export data for\n";
    print STDERR
"\t-p <output path>\tThe output path. Optional. If not provided, use current directory.\n";
}

sub report
{
    my ( $string, $verbosity ) = @_;
    $verbosity = 1 unless defined $verbosity;

    if ( $gVerbosityLevel >= $verbosity )
    {
        print STDERR $string . "\n";
    }
}

