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

use lib '/app/tools/rps/lib';
use RPS::Command;

use base 'RPS::Command';

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

sub _template { assert(0, 'override this'); }
sub _getMechanicalPayorList
{
    my ($self, $runID) = @_;
    assert(0, 'override this');
}

sub _getEditPaymentsCommand
{
    my ($self, $runID, $payorID) = @_;
    assert(0, 'override this');
}

sub _getShowCommand
{   
    my ($self, $runID, $payorID) = @_;
    assert(0, 'override this');
}

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

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

    my $runID = $self->getParam('MechanicalRunID');
    my $payorList = $self->_getMechanicalPayorList($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 = $self->_getEditPaymentsCommand($runID, $payorID);
        }
        else
        {
            $command = $self->_getShowCommand($runID, $payorID);
        }
        $response = RSApache::Response::Redirect->new($command->getRedirect());
    }
    else
    {
	    $self->{xml}{MechanicalPayorList} = $payorList;
	    $response = RSApache::Response::XSLT->new($self->{xml}, $self->_template());
    }

	return $response;
}



1;
