#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::Tracker::Command::DistributionFee;
use strict;
use warnings;
use URI::Escape;

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

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

use lib '/app/tools/raptor/lib';
use Raptor::Tracker::Command::Main;

use Raptor::Command;
use base 'Raptor::Tracker::Command::Base';

sub cmd  { return "distributionfee"; }
sub area { return "tracker"; }

use constant kTemplate => '/app/tools/raptor/templates/tracker.xsl';

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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $fileID = $self->getParam('file');
    my $file = RPS::File::File->new( file_id => $fileID );

    $file->{InputDistributionFee} = 1;

    assert($file);

    # Adding a hack here so that we can edit the distribution fees for files in closed periods
    # because customers keep forgetting.
    if ( !$self->getParam('closed') ) {
        assert( $file->FileStatus != File::File::STATUS_CLOSED );
    }

    # Are we setting this, or just displaying the current value?
    #
    my $distributionfeeParam      = $self->getParam('distributionfee');
    my $has_multiple_distribution = $self->getParam('has_multiple_formats');
    if ( $has_multiple_distribution ne "" ) {

        my @formats = split( ",", $has_multiple_distribution );
        for ( my $x = 0 ; $x < @formats ; $x++ ) {
            my $this_distribution_param = $self->getParam( "format_" . $formats[$x] );

            #$this_distribution_param =~ s/\W//g;
            my $all_distribution_fee = $self->getParam("format_all");
            $this_distribution_param = $all_distribution_fee if ( $this_distribution_param eq "" && $all_distribution_fee > 0 );
            $file->SetDistributionFee( $this_distribution_param, $formats[$x] );
        }
        my $redirect = $self->getParam('redirect');
        if ( !$redirect ) {
            $redirect = '/app/tracker?page=' . $self->getParam('page');
        }
        return RSApache::Response::Redirect->new($redirect);
    } else {
        $distributionfeeParam = $self->getParam('format_all');
        if ( $distributionfeeParam =~ /\./ ) {
            $self->addMessageXML( type => 'error', code => 'invalid_distributionfee' );
        }

        #$distributionfeeParam =~ s/\W//g;
        if ( defined $distributionfeeParam ) {
            if ( $file->SetDistributionFee($distributionfeeParam) ) {

                # Redirect back to whence we came, if requested.
                #
                my $redirect = $self->getParam('redirect');
                if ( !$redirect ) {
                    $redirect = '/app/tracker?page=' . $self->getParam('page');
                }
                return RSApache::Response::Redirect->new($redirect);
            } else {
                $self->addMessageXML( type => 'error', code => 'invalid_distributionfee' );
            }
        }
    }
    $self->{xml}{File} = $file->GetObjectXML();

    # Add the 'date range' value to the XML.
    #
    $self->{xml}{File}{DateSalesFilePeriod} = Common::Client::Current()->Locale()->formatDate( $file->SalesFileDateBegin ) . " - "
      . Common::Client::Current()->Locale()->formatDate( $file->SalesFileDateEnd );

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

1;
