#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::Price::SalePriceExceptionRule;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::SalePriceExceptionRule;
use BookPub::DB::Item::Sale;
use BookPub::DB::Item::File;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;

use base 'Common::FormObject';

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

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

    my %properties;
    if ( $args{dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{dbItem} );
    } elsif ( $args{saleID} ) {
        $self->_propertiesFromSaleID( \%properties, $args{saleID} );
    } elsif ( $args{salePriceExceptionRuleID} ) {
        $self->_propertiesFromDB( \%properties, $args{salePriceExceptionRuleID} );
    } elsif ( $args{serviceID} ) {
        $self->_propertiesFromNew( \%properties, %args );
    } else {

        #$self->_propertiesFromDefaults(\%properties);
        die "no default ctor";
    }

    $self->_initProperties( \%properties );

    return $self;
}

sub _propertiesFromDB {
    my ( $self, $hrProperties, $id ) = @_;

    my $dbItem = BookPub::DB::Item::SalePriceExceptionRule->Lookup( sale_price_exception_rule_id => $id );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;
    assert($dbItem);

    $hrProperties->{sale_price_exception_rule_id} = $dbItem->sale_price_exception_rule_id;
    $hrProperties->{service_id}                   = $dbItem->service_id;
    $hrProperties->{version}                      = $dbItem->version;
    $hrProperties->{currency_code}                = $dbItem->currency_code;
    $hrProperties->{list_price}                   = $dbItem->list_price;
    $hrProperties->{price_type_id}                = $dbItem->price_type_id;
    $hrProperties->{price_type_qualifier_id}      = $dbItem->price_type_qualifier_id;
    $hrProperties->{product_id}                   = $dbItem->product_id;
    $hrProperties->{country_code}                 = $dbItem->country_code;
    $hrProperties->{date_created}                 = $dbItem->date_created;
    $hrProperties->{date_modified}                = $dbItem->date_modified;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    $self->{SalePriceExceptionRuleID} = $props->{sale_price_exception_rule_id};
    $self->{ServiceID}                = $props->{service_id};
    $self->{Version}                  = $props->{version};
    $self->{CurrencyCode}             = $props->{currency_code};
    $self->{ListPrice}                = $props->{list_price};
    $self->{PriceTypeID}              = $props->{price_type_id};
    $self->{PriceTypeQualifierID}     = $props->{price_type_qualifier_id};
    $self->{ProductID}                = $props->{product_id};
    $self->{CountryCode}              = $props->{country_code};
    $self->{DateCreated}              = $props->{date_created};
    $self->{DateModified}             = $props->{date_modified};
}

sub _propertiesFromDefaults {
    my ( $self, $hrProperties ) = @_;
}

sub _propertiesFromNew {
    my ( $self, $hrProperties, %args ) = @_;
    assert(%args);

    $hrProperties->{service_id}              = $args{serviceID};
    $hrProperties->{version}                 = $args{version};
    $hrProperties->{currency_code}           = $args{currencyCode};
    $hrProperties->{list_price}              = $args{listPrice};
    $hrProperties->{price_type_id}           = $args{priceTypeID};
    $hrProperties->{price_type_qualifier_id} = $args{priceTypeQualifierID};
    $hrProperties->{product_id}              = $args{productID};
    $hrProperties->{country_code}            = $args{countryCode};
}

sub _propertiesFromSaleID {
    my ( $self, $hrProperties, $saleID ) = @_;
    assert($saleID);

    Common::Log::Debug("Create exception from sale id: $saleID");

    my $sale = BookPub::DB::Item::Sale->Lookup( sale_id => $saleID );
    my $file = BookPub::DB::Item::File->Lookup( file_id => $sale->file_id ) if ($sale);

    assert( $sale, "Could not find sale $saleID" );
    assert( $file, "Could not find file " . $sale->file_id );

    if ( $sale && $file ) {

        # We need to check for a list price currency first,
        # and then fall back to the sale's currency code if we have to.
        my $currencyCode = $sale->r_list_price_currency;
        if ( !$currencyCode ) {
            $currencyCode = $sale->currency_code;
        }

        $hrProperties->{service_id}              = $file->service_id;
        $hrProperties->{version}                 = $file->version_num;
        $hrProperties->{currency_code}           = $currencyCode;
        $hrProperties->{list_price}              = $sale->list_price;
        $hrProperties->{price_type_id}           = $sale->price_type_id;
        $hrProperties->{price_type_qualifier_id} = $sale->price_type_qualifier_id;
        $hrProperties->{product_id}              = $sale->product_id;
        $hrProperties->{country_code}            = $sale->country_code;
    }

    $self->_initProperties($hrProperties);
}

sub save {
    my $self = shift;
    my %args = (
        service_id              => $self->ServiceID,
        version                 => $self->Version,
        currency_code           => $self->CurrencyCode,
        list_price              => $self->ListPrice,
        price_type_id           => $self->PriceTypeID,
        price_type_qualifier_id => $self->PriceTypeQualifierID,
        product_id              => $self->ProductID,
        country_code            => $self->CountryCode,
    );

    assert( $self->ServiceID, Dumper $self);
    assert( $self->Version );
    assert( $self->CurrencyCode );
    assert( $self->ProductID );

    my $dbItem = BookPub::DB::Item::SalePriceExceptionRule->FindRule(%args);

    unless ($dbItem) {
        $dbItem = BookPub::DB::Item::SalePriceExceptionRule->Create();
        foreach my $key ( keys(%args) ) {
            $dbItem->$key( $args{$key} );
        }
        $dbItem->save;
    }

    $self->_init( dbItem => $dbItem );

}

sub delete {
    my $self = shift;
    my %args = (
        service_id              => $self->ServiceID,
        version                 => $self->Version,
        currency_code           => $self->CurrencyCode,
        list_price              => $self->ListPrice,
        price_type_id           => $self->PriceTypeID,
        price_type_qualifier_id => $self->PriceTypeQualifierID,
        product_id              => $self->ProductID,
        country_code            => $self->CountryCode,
    );

    my $dbItem = BookPub::DB::Item::SalePriceExceptionRule->Delete(%args);
}

sub match {
    my $self = shift;
    my %args = (
        service_id              => $self->ServiceID,
        version                 => $self->Version,
        currency_code           => $self->CurrencyCode,
        list_price              => $self->ListPrice,
        price_type_id           => $self->PriceTypeID,
        price_type_qualifier_id => $self->PriceTypeQualifierID,
        product_id              => $self->ProductID,
        country_code            => $self->CountryCode,
    );

    my $dbItem = BookPub::DB::Item::SalePriceExceptionRule->FindRule(%args);

    if ($dbItem) {
        $self->_init( dbItem => $dbItem );
        return 1;
    } else {
        return;
    }

}

###
1;    #
###
