#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Mechanical::UK::Command::Show;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use RSApache::Response::XSLTDownload;
use Common::FormObject;

use lib '/app/tools/rps/lib';
use RPS::Mechanical::UK::UKMechanicalRun;
use RPS::Command;
use RPS::Payor::Payor;
use RPS::DB::Item::UKMechanicalRun;
use base 'RPS::Command';


sub cmd  { return 'show'; }
sub area { return 'uk_mech'; }

use constant kTemplate      => "/app/tools/rps/templates/uk_mech/show.xsl";
use constant kTextTemplate  => "/app/tools/rps/templates/uk_mech/text/show.xsl";

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

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

    my $runID = $self->getParam('UKMechanicalRunID');
    my $filterBy = $self->getParam('filterBy');
    my $download = $self->getParam('download');


	$self->{xml}{UKMechanicalRun} = RPS::Mechanical::UK::UKMechanicalRun->new
    (
        ukMechanicalRunID => $runID, 
        filterBy => $filterBy,
    );
    
    my $payorID = $self->{xml}{UKMechanicalRun}->PayorID();
    $self->{xml}{Payor} = RPS::Payor::Payor->new(payorID => $payorID);   

    if ($download)
    {
        my $run = RPS::DB::Item::UKMechanicalRun->Lookup(uk_mechanical_run_id => $runID);
        my $cleanRunLabel = RPS::Mechanical::UK::UKMechanicalRun::GenerateFileLabel($run->start_date, $run->end_date);

        my $filename = "Summary_$cleanRunLabel.txt";
        $response = RSApache::Response::XSLTDownload->new($self->{xml}, kTextTemplate, $filename);
    }
    else
    {
	    $response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
    }
	return $response;
}



1;
