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

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Log;

use lib '/app/tools/rps/lib';
use RPS::ArtistContract::ArtistContractList;
use RPS::Payor::Payor;
use RPS::CountryList;
use RPS::StateList;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::Statement::Settings::ArtistSettings;
use RPS::Statement::Settings::ArtistSettingsHistory;
use RPS::DB::Item::ArtistRoyaltyStatementSettings;

use Common::FormObject;
use base 'Common::FormObject';

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

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

    # We're going to do this in a slightly different fashion than the usual edit command.
    # We will _never_ want to actually 'edit' an existing settings.
    # So we'll always be presenting a new one.  However, we do want to have this new
    # setting reflect the current settings.
    #
    my $currentItem = RPS::DB::Item::ArtistRoyaltyStatementSettings->GetCurrentSettings();
    $self->{Settings} = RPS::Statement::Settings::ArtistSettings->new();
    if ($currentItem) {

        # Override the default settings with the current values.
        #
        #        $self->{Settings}->ShowSalesPrice($currentItem->show_sales_price());
        #        $self->{Settings}->ShowRoyaltyRate($currentItem->show_royalty_rate());

        $self->{CurrentSettings} = RPS::Statement::Settings::ArtistSettings->new( _dbItem => $currentItem );
    } else {
        $self->{CurrentSettings} = RPS::Statement::Settings::ArtistSettings->new();
    }

    # Restrict the status list to artist runs.
    #
    $self->{RoyaltyRunStatus} = RPS::RoyaltyRun::RoyaltyRunStatusList->new( artist => 1 );

    return $self;
}

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    if ( $propertyName eq 'Settings' ) {
        return 1;
    }

    return 0;
}

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

    Common::Log::Print("--- in save");
    Common::Log::Print($self);

    # We're not going to 'blindly' save the new item.
    #
    # First, if we're not supposed to be able to edit (because of the state of the last run),
    # blow up - We shouldn't be here.
    #
    if ( !$self->{RoyaltyRunStatus}->CanEdit() ) {
        die "ERROR - Cannot save - Artist Royalty Runs are not in an editable state";
    }

    # Next, if the setting is _identical_ to the current settings, don't do anything.
    #
    if (
           $self->{CurrentSettings}
        && $self->{Settings}->ShowSalesPrice() == $self->{CurrentSettings}->ShowSalesPrice()
        && $self->{Settings}->ShowRoyaltyRate() == $self->{CurrentSettings}->ShowRoyaltyRate()

        && $self->{Settings}->ShowProration() == $self->{CurrentSettings}->ShowProration()
        && $self->{Settings}->ShowProratedRate() == $self->{CurrentSettings}->ShowProratedRate()
        && $self->{Settings}->ShowRateReduction() == $self->{CurrentSettings}->ShowRateReduction()
        && $self->{Settings}->ShowPackaging() == $self->{CurrentSettings}->ShowPackaging()
        && $self->{Settings}->ShowGrossUnits() == $self->{CurrentSettings}->ShowGrossUnits()
        && $self->{Settings}->ShowGrossSales() == $self->{CurrentSettings}->ShowGrossSales()
        && $self->{Settings}->ShowPercentageOfSales() == $self->{CurrentSettings}->ShowPercentageOfSales()
        && $self->{Settings}->ShowFreeGoods() == $self->{CurrentSettings}->ShowFreeGoods()
        && $self->{Settings}->ShowReserves() == $self->{CurrentSettings}->ShowReserves()
        && $self->{Settings}->ShowLiquidations() == $self->{CurrentSettings}->ShowLiquidations()
        && $self->{Settings}->ShowReturns() == $self->{CurrentSettings}->ShowReturns()
        && $self->{Settings}->ShowRateType() == $self->{CurrentSettings}->ShowRateType()
        && $self->{Settings}->ShowEffectiveRate() == $self->{CurrentSettings}->ShowEffectiveRate()
        && $self->{Settings}->ShowNetSales() == $self->{CurrentSettings}->ShowNetSales()
      ) {
        Common::Log::Print("--- identical - Not doing nothing.");
        return 1;
    }

    # So, if we ARE actually saving something, first see whether the current settings have EVER appeared in a run.
    # If not, delete that record first.
    #
    if ( $self->{CurrentSettings} ) {
        Common::Log::Print("--- calling deleteIfUnused.");
        $self->{CurrentSettings}->deleteIfUnused();
    }

    # One last scenario - They created and saved some new settings, never committed any runs, then
    # set the settings back to the _previous_ settings.
    # If that's the case, then we don't really want to save a new identical settings set.
    #
    my $currentItem = RPS::DB::Item::ArtistRoyaltyStatementSettings->GetCurrentSettings();
    if (
           $currentItem
        && $self->{Settings}->ShowSalesPrice() == $currentItem->show_sales_price
        && $self->{Settings}->ShowRoyaltyRate() == $currentItem->show_royalty_rate

        && $self->{Settings}->ShowProration() == $currentItem->show_proration
        && $self->{Settings}->ShowProratedRate() == $currentItem->show_prorated_rate
        && $self->{Settings}->ShowRateReduction() == $currentItem->show_rate_reduction
        && $self->{Settings}->ShowPackaging() == $currentItem->show_packaging
        && $self->{Settings}->ShowGrossUnits() == $currentItem->show_gross_units
        && $self->{Settings}->ShowGrossSales() == $currentItem->show_gross_sales
        && $self->{Settings}->ShowPercentageOfSales() == $currentItem->show_percent_sales
        && $self->{Settings}->ShowFreeGoods() == $currentItem->show_free_goods
        && $self->{Settings}->ShowReserves() == $currentItem->show_reserves
        && $self->{Settings}->ShowLiquidations() == $currentItem->show_liquidations
        && $self->{Settings}->ShowReturns() == $currentItem->show_returns
        && $self->{Settings}->ShowRateType() == $currentItem->show_rate_type
        && $self->{Settings}->ShowEffectiveRate() == $currentItem->show_effective_rate
        && $self->{Settings}->ShowNetSales() == $currentItem->show_net_sales

      ) {
        return 1;
    }

    Common::Log::Print("--- calling save on the Settings object.");
    $self->{Settings}->save();
}

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

    return 1;
}

###
1;    #
###
