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

use Sys::Hostname;

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

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ArtistRoyaltyRun;
use RPS::DB::Item::SaleRunMap;
use RPS::ArtistRoyalty::ArtistRoyaltyRun;
use RPS::Statement::Artist::StatementListWithPayments;
use RPS::Payor::PayorList::WithCommitDate;
use RPS::RoyaltyRun::Status;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::DB::Item::ClientOptions;

use RPS::ArtistRoyalty::Job::RunArtistRoyalties;

use base 'Common::FormObject';

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

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

    # I want:
    # A list of payors
    # a place to enter the start date
    # a place to enter the end date
    # ? A way to set the debugging level ?
    #
    $self->{PayorList} = RPS::Payor::PayorList::WithCommitDate->new( runType => RPS::DB::Item::SaleRunMap::kRunTypeArtistRoyalty );

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

    $runStatus = RPS::RoyaltyRun::RoyaltyRunStatusList->new( artist => 1 )->ArtistRuns();
    $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 );

    if ( 1 == RPS::DB::Item::ClientOptions->Get( 'show_abacus_data' ) ) {
        $self->{ShowAbacusData} = 1;
        $self->{AbacusPeriodID} = Common::FormObject::Scalar->new( required => 1, maxLength => 3 );
    }

    return $self;
}

sub validate {
    my $self = shift;

    if ( 1 == RPS::DB::Item::ClientOptions->Get( 'show_abacus_data' ) ) {
        if ( $self->AbacusPeriodID() !~ /^\d{3}$/ ) {
            $self->{AbacusPeriodID}->setError(1);
            $self->{AbacusPeriodID}->setErrorString('must be 3 digits');
            return 0;
        }
    }

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

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

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

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

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

    # First, create the run entry.
    #
    my %args = (
        label            => $self->Label(),
        ending_sale_date => $self->EndingSaleDate(),
        payor_id         => $self->PayorID(),
        status           => RPS::RoyaltyRun::Status::kWaitingToRun,
    );
    $args{abacus_period_id} = $self->AbacusPeriodID() if ( exists $self->{AbacusPeriodID} );

    my $newRun = RPS::DB::Item::ArtistRoyaltyRun->Create( %args );
    $newRun->save();

    # Create the new job.
    #
    my $jobArgs = RPS::ArtistRoyalty::Job::RunArtistRoyalties->new(
        runID    => $newRun->artist_royalty_run_id,
        logLevel => $self->LogLevel(),
    );
    my $job = $jobArgs->enqueue();

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

###
1;    #
###
