#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
# $Id$
#------------------------------------------------------------
package RPS::Mechanical::US::CreateRunForm;

use Sys::Hostname;

use lib '/app/tools/common/lib';
use Common::FormObject;
use Common::RSApp;

use lib '/app/tools/job/lib';
use Job::Queue;

use lib '/app/tools/raptor/lib';
use Raptor::DB::Item::Period;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::MechanicalRun;
use RPS::Mechanical::US::MechanicalRun;
use RPS::Payor::PayorList::WithCommitDate;
use RPS::Mechanical::US::Job::RunMechanicalRoyalties;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::RoyaltyRun::Status;

use base 'Common::FormObject';

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    $self->{PayorList} = RPS::Payor::PayorList::WithCommitDate->new( runType => RPS::DB::Item::SaleRunMap::kRunTypeMechanical );

    $self->{PayorID}        = Common::FormObject::Scalar->new( required => 1 );
    $self->{EndingSaleDate} = Common::FormObject::Scalar::Date->new();
    $self->{Label}          = Common::FormObject::Scalar::String->new( required => 1 );
    $self->{LogLevel}       = Common::FormObject::Scalar->new( value => 2, required => 1 );

    $runStatus = RPS::RoyaltyRun::RoyaltyRunStatusList->new( usMechanical => 1 )->USMechanicalRuns();
    $self->{HasRunError} = $runStatus->Error();

    # We need the date that the last period was closed.
    my $periodDate = Raptor::DB::Item::Period->GetLatestPeriodDate();
    $self->{PeriodCloseDate} = Common::FormObject::Scalar::Date->new( value => $periodDate );

    return $self;
}

sub validate {
    my $self = shift;

    return 0 if ( $self->HasRunError() );

    $self->{RoyaltyRunStatus} = RPS::RoyaltyRun::RoyaltyRunStatusList->new(
        usMechanical => 1,
        payorID      => $self->PayorID()
    );

    return 0 unless ( $self->{RoyaltyRunStatus}->CanEdit() );

    return $self->SUPER::validate();
}

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

    my $run = $self->_createRunInDatabase();
    my $job = $self->_enqueueJob($run);
    $self->_updateRunWithJob( $job, $run );
}

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

    # First, create the run entry.
    #
    my $newRun = RPS::DB::Item::MechanicalRun->Create(
        label            => $self->Label(),
        ending_sale_date => $self->EndingSaleDate(),
        payor_id         => $self->PayorID(),
        status           => RPS::RoyaltyRun::Status::kWaitingToRun,
    );

    $newRun->save();
    return $newRun;
}

sub _enqueueJob {
    my ( $self, $run ) = @_;

    # Create the new job, and put it in the queue.
    #
    my $jobArgs = RPS::Mechanical::US::Job::RunMechanicalRoyalties->new(
        runID    => $run->mechanical_run_id,
        logLevel => $self->LogLevel(),
    );
    my $job = $jobArgs->enqueue();

    return $job;
}

sub _updateRunWithJob {
    my ( $self, $job, $run ) = @_;

    # Stash the job id in the run table.
    #
    $run->job_id( $job->id() );
    $run->save();
}

###
1;    #
###
