#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Raptor::Tracker::Command::QwikClose;
use strict;
use warnings;

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

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ClientOptions;

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

sub cmd  { return "qwikclose"; }
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;

    $self->{xml}{GrossRevenueEnabled} = RPS::DB::Item::ClientOptions->Get( 'gross_revenue' );

    my $submit = $self->getParam("formSubmit");
    my $files  = $self->getParam("file");
    if ( 'ARRAY' ne ref($files) ) {
        my @fileArray;
        push @fileArray, $files;
        $files = \@fileArray;
    }

    foreach my $file_id (@$files) {
        ## not checking for errors... if a file fails, it'll stay open
        $self->finishImport( $file_id, 1 );
    }
    $self->{xml}->{ActionCompleted} = "QwikClose";

    # !!! jpk - This is a hack - The goofy finishImport method doesn't really return any indication
    # of whether anything succeeded.  However, it does create Messages when something failed.
    # So, I am going to make the assumption that no news is good news...
    #
    if ( !$self->hasMessages() ) {
        $self->addMessageXML( type => 'success', code => 'qwikclose_succeeded' );
    }

    my $period      = new Period::RoyaltyPeriod();
    my $periodParam = $self->getParam('period');
    if ( !$periodParam ) {
        $period->LoadCurrent();
    } else {
        $period->Load( period_id => $periodParam );
    }

    $self->doFileListing($period);
    $self->{xml}{Report}{Period} = $period->GetObjectXML();
    $self->setExceptionsFileStatus();

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

    return $response;
}

1;
