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

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

use BookPub::Tracker::File;
use BookPub::Tracker::FileList::ClosedFiles;
use BookPub::Tracker::Period;
use BookPub::DB::Item::File;
use BookPub::DB::Item::Period;

use BookPub::Tracker::Command::Main;
use BookPub::Tracker::Job::DumpRoyaltyReport;
use BookPub::Tracker::Job::DumpReconciliationReport;

use base 'BookPub::Command';

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

    # Make sure we have at least 1 closed file in the current period.
    #
    my $closedFiles = BookPub::Tracker::FileList::ClosedFiles->new( periodID => 0 );
    die "ERROR - Cannot close current period - No closed files!" unless $closedFiles->size() > 0;

    my $currentPeriod = BookPub::Tracker::Period->new( periodID => 0 );

    # Make sure the period has been given a name.
    #
    if ( !$currentPeriod->Name ) {
        return RSApache::Response::Redirect->new( "/bookpub/tracker?c=edit_period&period=0&redirect=" . $self->getRedirect(1) );
    }

    # Closing a period means:
    #   1. Setting EndDate for this period object
    #   2. creating a new period object with start date of today
    #   3. associate finished files with new period
    #
    my $today = Common::Util::today();

    my $maxPeriodID = BookPub::DB::Item::Period->GetMaxPeriodID();
    my $newPeriodID = $maxPeriodID + 1;

    my $newPeriod = BookPub::Tracker::Period->new();
    $newPeriod->PeriodID($newPeriodID);
    $newPeriod->StartDate( $currentPeriod->StartDate() );
    $newPeriod->EndDate($today);
    $newPeriod->Name( $currentPeriod->Name() );
    $newPeriod->Notes( $currentPeriod->Notes() );
    $newPeriod->save();

    # This call will assign the new periodID to the closed files.
    #
    BookPub::DB::Item::File->StartPeriod($newPeriodID);

    # 'reset' the current period
    #
    $currentPeriod->StartDate($today);
    $currentPeriod->Name("");
    $currentPeriod->Notes("");
    $currentPeriod->save();

    # Queue up the royalty report job, and the recon report job.
    #
    my $jobArgs = BookPub::Tracker::Job::DumpRoyaltyReport->new( periodID => $newPeriodID );
    $jobArgs->enqueue();

    $jobArgs = BookPub::Tracker::Job::DumpReconciliationReport->new( periodID => $newPeriodID );
    $jobArgs->enqueue();

    return RSApache::Response::Redirect->new("/bookpub/tracker?period=$newPeriodID");
}

1;
