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

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

use lib '/app/tools/rps/lib';
use RPS::ArtistRoyalty::ArtistRoyaltyRun;
use RPS::Command;
use RPS::Payor::Payor;
use base 'RPS::Command';

use RSApache::Response::XSLT;
use RSApache::Response::XSLTDownload;

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

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

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

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

    my $runID = $self->getParam('RoyaltyRunID');
    my $payorID = $self->getParam('PayorID');
    my $filterBy = $self->getParam('filterBy');
    my $download = $self->getParam('download');
    
    my $artistRun = RPS::ArtistRoyalty::ArtistRoyaltyRun->new
    (
        artistRoyaltyRunID => $runID, 
        payorID => $payorID,
        filterBy => $filterBy,
    );
    
	$self->{xml}{ArtistRoyaltyRun} = $artistRun;
	
	# I want to be able to link to this page without passing in a payor id...
	#
	if (! $payorID)
	{
	    $payorID = $artistRun->PayorID();   
	}
	    
    my $payor = RPS::Payor::Payor->new(payorID => $payorID);
    $self->{xml}{Payor} = $payor;	
	

    if ($download)
    {
        my $cleanRunLabel = Common::Util::clean_name($self->{xml}{ArtistRoyaltyRun}->Label());

        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;
