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

package RPS::ArtistRoyalty::Fast::Static::PaidSales;

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 DB_File;

use Common::Log;

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

# JPK - We're using the new artist_royalty_paid_sales table, but this could easily be
# switched back to using the ol' sale_run_map.
#
# !!! And that's exactly what I'm going to do for now.

# !!! Note that since this depends on tables that we _write to_ during a normal run,
# !!! we can effectively NEVER cache this data between runs.

sub FileName { 'paid_sales' }

sub CacheQueryTableList {
    "'sale_run_map','artist_royalty_income_item','artist_royalty_album','artist_royalty_statement','artist_royalty_run'";
}

sub _Create {
    my ( $class, $filename ) = @_;

    my %hash;
    $DB_BTREE->{'flags'} = R_DUP;
    tie %hash, "DB_File", $filename, O_RDWR | O_CREAT, 0666, $DB_BTREE or die "Error opening $filename: $!\n";

    my $dbo = Common::RSApp::GetClientDB();

    # Enable non-buffered mysql_use_result processing
    my $direct = $dbo->DirectIO();

#    my $sql = qq/
#        SELECT
#            artist_royalty_paid_sales.sale_id,
#            artist_royalty_income_item.artist_contract_term_id,
#            artist_royalty_income_item.track_id
#        FROM artist_royalty_paid_sales
#        JOIN artist_royalty_income_item ON (artist_royalty_income_item.artist_royalty_income_item_id = artist_royalty_paid_sales.artist_royalty_income_item_id)
#        JOIN artist_royalty_album USING (artist_royalty_album_id)
#        JOIN artist_royalty_statement USING (artist_royalty_statement_id)
#        JOIN artist_royalty_run USING (artist_royalty_run_id)
#        WHERE artist_royalty_run.status in (2,5)
#    /;

    # JPK - This query sure looks fine, and it works.  EXCEPT in the somewhat anomolous circumstance where a term
    # we've paid on has been subsequently _deleted_.   In that case, we don't make the mapping, and we'd pay out again.
    # So, we'll use a query that is closer to what the old code does, and not try and refer to the contract term.
    # Instead, we'll look at artist_royalty_album to get the contract_id.
    #
    #    my $sql = qq/
    #        SELECT
    #            sale_run_map.sale_id,
    #            artist_royalty_income_item.artist_contract_term_id,
    #            new_artist_contract_term.artist_contract_id,
    #            artist_royalty_income_item.track_id
    #        FROM sale_run_map
    #        JOIN artist_royalty_income_item ON (artist_royalty_income_item.artist_royalty_income_item_id = sale_run_map.statement_item_id)
    #        JOIN new_artist_contract_term USING (artist_contract_term_id)
    #        JOIN artist_royalty_album USING (artist_royalty_album_id)
    #        JOIN artist_royalty_statement USING (artist_royalty_statement_id)
    #        JOIN artist_royalty_run USING (artist_royalty_run_id)
    #        WHERE artist_royalty_run.status in (2,5) AND sale_run_map.run_type='ARTR' AND sale_run_map.status='paid'
    #    /;

    my $sql = qq/
        SELECT
            sale_run_map.sale_id,
            artist_royalty_income_item.artist_contract_term_id,
            artist_royalty_album.artist_contract_id,
            artist_royalty_income_item.track_id
        FROM sale_run_map
        JOIN artist_royalty_income_item ON (artist_royalty_income_item.artist_royalty_income_item_id = sale_run_map.statement_item_id)
        JOIN artist_royalty_album USING (artist_royalty_album_id)
        JOIN artist_royalty_statement USING (artist_royalty_statement_id)
        JOIN artist_royalty_run USING (artist_royalty_run_id)
        WHERE artist_royalty_run.status in (2,5) AND sale_run_map.run_type='ARTR' AND sale_run_map.status='paid'
    /;

    my $productSTH = $dbo->DoCmd($sql);
    while ( my @data = $productSTH->fetchrow_array() ) {

        # For this first implementation, we are mapping sale_id => (contract,track) over and over again.
        # Just for simplicity, we'll use a comma-delimited string.
        #
        $hash{ $data[0] } = $data[2] . ',' . $data[3];
    }

    untie %hash;
}

my $gHash;

sub GetPaidSalesData {
    my ( $class, $dataPath ) = @_;
    my $filename = "$dataPath/" . $class->FileName();

    my %hash;
    my $dbObj = tie %hash, "DB_File", $filename, O_RDONLY, 0666, $DB_BTREE or die "Error opening $filename: $!\n";

    # JPK - This is ugly, but I honestly don't know if I can let the hash go out of scope.
    #
    $gHash = \%hash;

    return $dbObj;
}

1;

