#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Statement::Artist::PDF;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util;
use Common::RSApp;
use Common::DB::Item::Client;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ArtistRoyaltyRun;
use RPS::DB::Item::ArtistRoyaltyStatement;
use RPS::DB::Item::ArtistPayee;
use RPS::Config;


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

    my $config = RPS::Config->new();
    my $path = $config->get('artist_royalty_statement_dir');
    return $path;
}



sub StatementFileNameFromID
{
    my ($id) = @_;
    assert($id);

    my $statement = RPS::DB::Item::ArtistRoyaltyStatement->Lookup(artist_royalty_statement_id => $id);
    return StatementFilePathFromDBItem($statement);
}


sub StatementFileNameFromDBItem
{
    my ($statementDBItem) = @_;
    assert($statementDBItem);


    my $artistPayeeID = $statementDBItem->payee_id;

    my $fileName = "$artistPayeeID.pdf";

    return $fileName;
}

sub PrettyStatementFileNameFromID
{
    my ($id) = @_;
    assert($id);

    my $statement = RPS::DB::Item::ArtistRoyaltyStatement->Lookup(artist_royalty_statement_id => $id);
    return PrettyStatementFilePathFromDBItem($statement);
}


sub PrettyStatementFileNameFromDBItem
{
    my ($statementDBItem) = @_;
    assert($statementDBItem);

    my $runID = $statementDBItem->artist_royalty_run_id;
    my $cleanRunLabel = _runLabelClean($runID);

    my $artistPayeeID = $statementDBItem->payee_id;
    my $artistNameClean = _artistNameClean($artistPayeeID);

    my $fileName = $artistNameClean.'_'.$artistPayeeID.'_'."$cleanRunLabel.pdf";

    return $fileName;
}



# This returns the directory in which statement files can be found.
#
sub StatementPathFromRunID
{
    my ($runID) = @_;
    assert($runID);

    my $clientName = Common::Client::Current()->ClientNameClean();

    return _BasePath() . "/$clientName/$runID/";
}


# Going to create a little ram cache, since this code will often get called in a loop.
#
my %gRunLabel;
sub _runLabelClean
{
    my ($runID) = @_;

    my $clientID = Common::RSApp::GetClientID();
    if (! defined $gRunLabel{$clientID}{$runID})
    {
        my $run = RPS::DB::Item::ArtistRoyaltyRun->Lookup(artist_royalty_run_id => $runID);
        assert($run, "ERROR - runID $runID is invalid");

        $gRunLabel{$clientID}{$runID} = Common::Util::clean_name($run->label);
    }

    return $gRunLabel{$clientID}{$runID};
}


my %gArtistName;
sub _artistNameClean
{
    my ($artistID) = @_;

    my $clientID = Common::RSApp::GetClientID();
    if (! defined $gArtistName{$clientID}{$artistID})
    {
        my $artist = RPS::DB::Item::ArtistPayee->Lookup(artist_payee_id => $artistID);
        assert($artist, "ERROR - artist payee id $artistID is invalid");

        my $artistName = $artist->name;

        # Replace '&' with 'and' - the normal clean function doesn't do that.
        #
        $artistName =~ s/ \& / and /g;
        $gArtistName{$clientID}{$artistID} = Common::Util::clean_name($artistName);
    }

    return $gArtistName{$clientID}{$artistID};
}

1;
