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

use lib '/app/tools/rps/lib';
use RPS::ArtistRoyalty::ArtistRoyaltyPayorList;
use RPS::ArtistRoyalty::Command::Show;
use RPS::ArtistRoyalty::Command::EditPayments;
use RPS::Command;
use base 'RPS::Command';

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

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

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

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

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

    my $runID = $self->getParam('RoyaltyRunID');

    my $payorList = RPS::ArtistRoyalty::ArtistRoyaltyPayorList->new(royaltyRunID => $runID);


    # I want to re-use this command to present the payor list both when viewing the 
    # 'normal' statement list view, and the new statement payment list.
    #
    my $editPayments = $self->getParam('Payments');

    # If we only have 1 thing in the list, we really don't need to list it... just skip to the
    # detail screen.
    #
    if (1 == $payorList->size())
    {
        my $onlyPayor = ${$payorList->getArray()}[0];
        my $payorID = $onlyPayor->{PayorID};
        my $command;
        if ($editPayments)
        {
            $command = RPS::ArtistRoyalty::Command::EditPayments->new(RoyaltyRunID => $runID, PayorID => $payorID);
        }
        else
        {
            $command = RPS::ArtistRoyalty::Command::Show->new(RoyaltyRunID => $runID, PayorID => $payorID);
        }
        $response = RSApache::Response::Redirect->new($command->getRedirect());
    }
    else
    {
	    $self->{xml}{ArtistRoyaltyPayorList} = $payorList;
	    $response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
    }
	return $response;
}



1;
