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

use lib '/app/tools/rps/lib';
use RPS::LabelRoyalty::Command::Show;
use RPS::Command;
use RPS::Payor::Payor;
use RPS::LabelRoyalty::EditPaymentsForm;
use base 'RPS::Command';

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

sub cmd  { return 'editpay'; }
sub area { return 'label_royalty'; }

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

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

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

    my $runID = $self->getParam('LabelRoyaltyRunID');
    my $payorID = $self->getParam('PayorID');
    my $filterBy = $self->getParam('filterBy');


    my $form = RPS::LabelRoyalty::EditPaymentsForm->new
    (
        runID => $runID,
        payorID => $payorID,
        filterBy => $filterBy,
    );


    # !!! debugging
#    my @params = split('&', $self->getParamsAsString());
#    foreach my $p (@params)
#    {
#        print STDERR "PARAM: $p\n";
#    }

    # !!! 
    # So, having a 'formSubmit' parameter has been our convention.
    # In the same vein, I have 'clearPayments' and 'setMin' parameters.
    # ... I think we need to change this.
    # The problem is this makes it pretty hard to assign permissions to the
    # various activities.   Now, I don't think we need to make a big deal about
    # clearPayments or setMin, but I can easily see a distinction between
    # viewing these payments, and being allowed to make changes.
    # 
    # But, for now, let's stick with this and see if it works.
    #
	if ($self->getParam('formSubmit'))
    {
	    # Apply cgi parameters, then save changes if all went well.
	    #
	    if ($form->assignCGIParams('Form', $self->getParams()) && $form->validate())
	    {
	    	$form->save();

            # on a 'regular' save, we will refresh back to the Detail page.
            # on a 'save and new', we refresh back to a blank Edit screen (with a success message).
            #
            my $successMessage = RPS::Message->new(type => "success", code => Common::FormObject::kSuccessFormSubmit);

            my $command = RPS::LabelRoyalty::Command::EditPayments->new
            (
                msg => $successMessage->toString, 
                LabelRoyaltyRunID => $runID,
                PayorID => $payorID,
                filterBy => $filterBy,
            );

            my $redirectResponse = RSApache::Response::Redirect->new($command->getRedirect());
            return $redirectResponse;

#            $self->addMessageXML(type => "success", code => Common::FormObject::kSuccessFormSubmit);

#            # We want to change views, so we want to execute the Detail command now.
#            #
#            my $successMessage = RPS::Message->new(type => "success", code => Common::FormObject::kSuccessFormSubmit);
#
#            my $command = RPS::LabelRoyalty::Command::Show->new
#            (
#                msg => $successMessage->toString, 
#                RoyaltyRunID => $runID,
#                PayorID => $payorID,
#                filterBy => $filterBy,
#            );
#
#            my $redirectResponse = RSApache::Response::Redirect->new($command->getRedirect());
#            return $redirectResponse;
	    }
	    else
	    {
            # We stay on the same page, and display our errors.
            #
		    $form->setError(Common::FormObject::kErrFormSubmit);
		    $self->addMessageXML(type => "error", code => Common::FormObject::kErrFormSubmit);
        }
    }


    if ($self->getParam('clearPayments'))
    {
	    $form->assignCGIParams('Form', $self->getParams());
        $form->clearPayments();
    }
    elsif ($self->getParam('setMin'))
    {
	    $form->assignCGIParams('Form', $self->getParams());
        $form->setPaymentsToMin();
    }
    elsif ($self->getParam('fullPayments'))
    {
	    $form->assignCGIParams('Form', $self->getParams());
        $form->setPaymentsToFull();
    }


    $self->{xml}{Form} = $form;

	$response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
	return $response;
}



1;
