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

use Data::Dumper;
use Getopt::Std;
use POSIX qw(ceil);
use Carp;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/raptor/lib';
use Raptor::DB::Item::Sale;

use lib '/app/tools/rps/lib';
use RPS::RoyaltyRun::Status;
use RPS::Statement::Label::Report;
use RPS::DB::Item::LabelRoyaltyRun;
use RPS::DB::Item::SaleRunMap;
use RPS::DB::Item::LabelContract;
use RPS::DB::Item::LabelContractTerm;
use RPS::DB::Item::LabelPayee;
use RPS::DB::Item::LabelPayeeAccount;
use RPS::DB::Item::FinanceAccount;
use RPS::DB::Item::PendingTransaction;
use RPS::DB::Item::LabelRoyaltyLabelItem;
use RPS::DB::Item::LabelRoyaltyTransactionItem;
use RPS::DB::Item::LabelRoyaltyStatement;

my $gVerbosityLevel = 1;

# Get the id from the command-line
#
my %options;
_parseCommandLine( \%options );
_deleteRun( $options{clientID}, $options{runID} );

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

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

    if ( !$opt{r} || !$opt{c} ) {
        _usage();
        exit(1);
    }
    $settings->{runID}    = $opt{r};
    $settings->{clientID} = $opt{c};
    if ( $opt{V} ) {
        $gVerbosityLevel = $opt{V};
    }
}

sub _usage {
    print "\nusage: $0 -c client_id -r run_id\n";
    print "\n";
    print "Arguments:\n";
    print "\t-c <client_id>\t\tThe client_id of the client to process\n";
    print "\t-r <run_id>\t\tThe label_royalty_run_id of the label royalty run to delete\n";
}

sub _deleteRun {
    my ( $clientID, $runID ) = @_;
    assert($clientID);
    assert($runID);

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

    # Get the run - we need to check it's status to make sure we can delete it.
    #
    my $run = RPS::DB::Item::LabelRoyaltyRun->Lookup( label_royalty_run_id => $runID );
    if ( !$run ) {
        die "ERROR - cannot find label royalty run with id $runID\n";
    }

    if ( RPS::RoyaltyRun::Status::kWaitingToDelete != $run->status ) {
        die "ERROR - run $runID cannot be deleted\n";
    }

    # Get all of the statements created by this run,
    # and delete them.
    #
    my $statements = RPS::DB::Item::LabelRoyaltyStatement->GetByLabelRoyaltyRunID($runID);

    while ( $statements->hasNext() ) {
        my $statement = $statements->next();
        _deleteStatement($statement);
    }

    # Delete the sales report files
    #
    my $reportPath = RPS::Statement::Label::Report::ReportPathFromRunID($runID);
    if ( -d $reportPath ) {
        system("rm -rf $reportPath");
    }

    # Delete the pdf statements
    #
    #    my $pdfPath = RPS::Statement::Artist::PDF::StatementPathFromRunID($runID);
    #    if (-d $pdfPath)
    #    {
    #        system("rm -rf $pdfPath");
    #    }

    # Delete this run from the run table.
    #
    _report("deleting from label royalty run table");
    $run->delete();

    # Delete sale map entries
    #
    _report("deleting from sale map");
    RPS::DB::Item::SaleRunMap->DeleteByLabelRoyaltyRunID($runID);
}

sub _deleteStatement {
    my ($statement) = @_;

    my $statementID = $statement->label_royalty_statement_id;

    _report("deleting statement $statementID");
    $statement->delete();

    # Delete all the label items
    #
    _report( "  ... deleting label royalty label items", 2 );
    RPS::DB::Item::LabelRoyaltyLabelItem->DeleteByLabelRoyaltyStatementID($statementID);

    # Delete the transaction items
    _report( "  ... deleting label royalty transactions", 2 );
    RPS::DB::Item::LabelRoyaltyTransactionItem->DeleteByLabelRoyaltyStatementID($statementID);
}

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

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

