#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Statement::Command::ShowCAMechanicalPDF;
use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::CAMechanicalStatement;
use RPS::Payor::Payor;
use RPS::Mechanical::CA::MechanicalRun;
use RPS::Publisher::CA::PublisherSimple;
use RPS::Statement::Mechanical::CA::PDF;

use lib '/app/tools/common/lib';
use Common::FormObject;
use Common::WriteXML;
use RSApache::Response::XSLT;
use RSApache::Response::XSLTDownload;
use RPS::Statement::Mechanical::CA::StatementDisplay;

use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

sub cmd  { return 'show_ca_mechanical_pdf'; }
sub area { return 'statement'; }

use constant kTemplate => "/app/tools/rps/templates/statement/show_ca_mechanical_pdf.xsl";

sub execute {
    my ($self) = @_;

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $id = $self->getParam('MechanicalStatementID');

    my $focusPublisherID = $self->getParam('focusPublisherID');
    my $commandName      = 'ShowCAMechanical';

    if ($focusPublisherID) {
        $commandName .= '_focusPublisherID_' . $focusPublisherID;
    }

    my $statementDB         = RPS::DB::Item::CAMechanicalStatement->Lookup( ca_mechanical_statement_id => $id );
    my $payorID             = $statementDB->payor_id;
    my $payor               = RPS::Payor::Payor->new( payorID => $payorID, loadSubs => 0 );
    my $runID               = $statementDB->ca_mechanical_run_id;
    my $run                 = RPS::Mechanical::CA::MechanicalRun->new( mechanicalRunID => $runID, payorID => $payorID, loadSubs => 0 );
    my $publisherID         = $statementDB->ca_publisher_id;
    my $publisher           = RPS::Publisher::CA::PublisherSimple->new( publisherID => $publisherID );
    my $prettyFileName      = RPS::Statement::Mechanical::CA::PDF->PrettyStatementFileNameFromDBItem($statementDB);
    my $prettyLabelFileName = RPS::Statement::Mechanical::CA::PDF->PrettyLabelStatementFileNameFromDBItem($statementDB);

    $self->{xml}{Statement}{MechanicalRun}              = $run;
    $self->{xml}{Statement}{MechanicalStatement}{Payor} = $payor;
    $self->{xml}{Statement}{Publisher}                  = $publisher;
    $self->{xml}{Statement}{MechanicalStatementID}      = $id;
    $self->{xml}{Statement}{PublisherViewPDF}           = $prettyFileName;
    $self->{xml}{Statement}{LabelViewPDF}               = $prettyLabelFileName;

    my $downloadDir = RPS::Statement::Mechanical::CA::Text->StatementPathFromRunID($runID);

    my $canDownloadAgentExcel;
    my $canDownloadAgentSummaryExcel;

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

    $self->{xml}{Statement}{CanDownloadAgentExcel}        = $canDownloadAgentExcel;
    $self->{xml}{Statement}{CanDownloadAgentSummaryExcel} = $canDownloadAgentSummaryExcel;

    my $template = kTemplate;
    RPS::CommandLog::Log( $self, $id );
    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );

    return $response;
}

1;
