#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package RPS::Mechanical::CA::MechanicalRun;

use Data::Dumper;

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

use lib '/app/tools/rps/lib';
use RPS::Statement::Mechanical::CA::StatementList;
use RPS::DB::Item::CAMechanicalRun;
use RPS::DB::Item::CAMechanicalRunUnlicensedTrackLog;
use RPS::Statement::Mechanical::CA::Text;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::DB::Item::ReportQueries::CAMechanicalReservePipeline;


use Common::FormObject;
use base 'Common::FormObject';


sub _init
{
    my ($self, %args) = @_;

    $self->SUPER::_init(%args);

    # !!! This can probably go away soon.
    #
    $self->{_payorID} = $args{payorID};
    $self->{_filterBy} = $args{filterBy};

    my %properties;
    if ($args{_dbItem})
    {
        $self->_propertiesFromDBItem(\%properties, $args{_dbItem});
    }
    elsif ($args{mechanicalRunID})
    {
        $self->_propertiesFromDB(\%properties, $args{mechanicalRunID});
    }
    else
    {
        $self->_propertiesFromDefaults(\%properties);
    }

    $self->_initProperties(\%properties);
    $self->_initSubs(\%properties);

    return $self;
}


sub _propertiesFromDB
{
    my ($self, $hrProperties, $id) = @_;

    my $dbItem = RPS::DB::Item::CAMechanicalRun->Lookup(ca_mechanical_run_id => $id);
    return $self->_propertiesFromDBItem($hrProperties, $dbItem);
}


sub _propertiesFromDBItem
{
	my ($self, $hrProperties, $dbItem) = @_;

    $hrProperties->{ca_mechanical_run_id}  = $dbItem->ca_mechanical_run_id;
    $hrProperties->{ending_sale_date}	= $dbItem->ending_sale_date;
    #$hrProperties->{label}				= $dbItem->label;
    $hrProperties->{start_date}         = $dbItem->start_date;
    $hrProperties->{end_date}           = $dbItem->end_date;
    $hrProperties->{start_time}         = $dbItem->start_time;
    $hrProperties->{end_time}           = $dbItem->end_time;
    $hrProperties->{status}             = $dbItem->status;
    $hrProperties->{pid}                = $dbItem->pid;
    $hrProperties->{payor_id}           = $dbItem->payor_id;
    $hrProperties->{job_id}             = $dbItem->job_id;
    $hrProperties->{hostname}           = $dbItem->hostname;
    $hrProperties->{created_by}         = $dbItem->created_by;
}


sub _initProperties
{
    my ($self, $props) = @_;

    $self->{MechanicalRunID}    = Common::FormObject::Scalar->new(value => $props->{ca_mechanical_run_id}, readOnly => 1);
    $self->{EndingSaleDate}		= Common::FormObject::Scalar::Date->new(value => $props->{ending_sale_date}, readOnly => 1);
    #$self->{Label}				= Common::FormObject::Scalar::String->new(value => $props->{label}, readOnly => 1);
    $self->{StartDate}          = Common::FormObject::Scalar::Date->new(value => $props->{start_date}, readOnly => 1);
    $self->{EndDate}            = Common::FormObject::Scalar::Date->new(value => $props->{end_date}, readOnly => 1);
    $self->{StartTime}          = Common::FormObject::Scalar::DateTime->new(value => $props->{start_time}, readOnly => 1);
    $self->{EndTime}            = Common::FormObject::Scalar::DateTime->new(value => $props->{end_time}, readOnly => 1);
    $self->{Status}             = Common::FormObject::Scalar->new(value => $props->{status}, readOnly => 1);
    $self->{PID}                = Common::FormObject::Scalar->new(value => $props->{pid}, readOnly => 1);
    $self->{PayorID}            = Common::FormObject::Scalar->new(value => $props->{payor_id}, readOnly => 1);
    $self->{JobID}              = Common::FormObject::Scalar->new(value => $props->{job_id}, readOnly => 1);
    $self->{Hostname}           = Common::FormObject::Scalar::String->new(value => $props->{hostname}, readOnly => 1, maxlength => 64);
    $self->{CreatedBy}          = Common::FormObject::Scalar::UserName->new(value => $props->{created_by}, readOnly => 1);

    # We have to construct the label from the start and end dates...
    #
    my $label = $self->{StartDate}->formatMDY() . " - " . $self->{EndDate}->formatMDY();
    $self->{Label} = Common::FormObject::Scalar::String->new(value => $label, readOnly => 1);

    my $count = RPS::DB::Item::CAMechanicalStatement->GetActiveCountByMechanicalRunID($props->{ca_mechanical_run_id});
    $self->{NumStatements} = $count;

    # Determine whether we can download the zip file.
    # Basically, if the directory is there, and there's at least
    # 1 file in it, then they can download it.
    #
    my $canDownloadPDF;
    my $canDownloadText;
    my $canDownloadCMRRA;
    my $accrualReportFlag;
    my $canDownloadExportText;
    my $canDownloadExportExcel;
    my $canDownloadAgentExcel;
    my $canDownloadAgentSummaryExcel;

    my $pipelineReportFlag;

    if ($props->{ca_mechanical_run_id})
    {
        my $downloadDir = RPS::Statement::Mechanical::CA::Text->StatementPathFromRunID($props->{ca_mechanical_run_id});
        #my $downloadDir = '/app/data/royalty_statements/ca_mechanical/'.Common::RSApp::GetClientID().'/'.$props->{ca_mechanical_run_id};

        if (-d $downloadDir)
        {
            # JPK - Over-anxious customers are downloading the files while they are still being regenerated
            # after a commit.   We need to use a semaphore instead.
            #
            if (-f "$downloadDir/COMPLETE")
            {
                $canDownloadPDF = 1;
            }

            if (-f "$downloadDir/TEXT_COMPLETE")
            {
                $canDownloadText = 1;
            }

            if (-f "$downloadDir/CMRRA_COMPLETE")
            {
                $canDownloadCMRRA = 1;
            }

            if (-f "$downloadDir/EXPORT_TEXT_COMPLETE")
            {
                $canDownloadExportText = 1;
            }

            if (-f "$downloadDir/EXPORT_EXCEL_COMPLETE")
            {
                $canDownloadExportExcel = 1;
            }

            if (-f "$downloadDir/CMRRA_AGENT_REPORT_COMPLETE")
            {
                $canDownloadAgentExcel = 1;
            }

            if (-f "$downloadDir/CMRRA_AGENT_SUMMARY_COMPLETE")
            {
                $canDownloadAgentSummaryExcel = 1;
            }
        }

        $accrualReportFlag = RPS::DB::Item::CAMechanicalRunUnlicensedTrackLog->ReportAvailable($props->{ca_mechanical_run_id});

        $pipelineReportFlag = RPS::DB::Item::ReportQueries::CAMechanicalReservePipeline->ReportAvailable($props->{ca_mechanical_run_id});
    }

    $self->{CanDownload} = $canDownloadPDF;
    $self->{CanDownloadText} = $canDownloadText;
    $self->{CanDownloadCMRRA} = $canDownloadCMRRA;
    $self->{AccrualReportAvailable} = $accrualReportFlag;
    $self->{PipelineReportAvailable} = $pipelineReportFlag;
    $self->{CanDownloadExportText} = $canDownloadExportText;
    $self->{CanDownloadExportExcel} = $canDownloadExportExcel;
    $self->{CanDownloadAgentExcel} = $canDownloadAgentExcel;
    $self->{CanDownloadAgentSummaryExcel} = $canDownloadAgentSummaryExcel;

    $self->{RoyaltyRunStatus} = RPS::RoyaltyRun::RoyaltyRunStatusList->new( caMechanical => 1, payorID => $self->PayorID );

}


sub _initSubs
{
    my ($self, $props) = @_;

	if($self->loadSubs())
	{
        # Load the list of statement lines.
        #
        $self->{MechanicalStatementList} = RPS::Statement::Mechanical::CA::StatementList->new
        (
            mechanicalRunID => $props->{ca_mechanical_run_id},
            payorID => $self->{_payorID},
            filterBy => $self->{_filterBy},
        );
	}

    return $self;
}


sub _propertiesFromDefaults
{
    my ($self, $hrProperties) = @_;
}


sub GenerateFileLabel
{
    my ($run) = @_;

    my $fileLabel = Common::Util::clean_name($run->start_date) . "_" . Common::Util::clean_name($run->end_date);

    return $fileLabel;
}

###
1;#
###
