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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::Price::ProductMarketPriceList;
use BookPub::DB::Item::ProductMarket;
use BookPub::DB::Item::ProductMarketPrice;
use BookPub::DB::Item::rNativeListPrice;
use BookPub::DB::Item::SalePriceException;

use Common::Assert;

use base 'Common::FormObject';

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

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

    $self->_initProperties( \%args );

    return $self;
}

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

    my $sale = $props->{sale};

    my $saleCurrencyCode;

    # First check for a native list price entry.
    my $nativePrice = BookPub::DB::Item::rNativeListPrice->Lookup( sale_id => $sale->sale_id );
    if ($nativePrice) {
        $saleCurrencyCode = $nativePrice->r_native_list_price_currency;
    } else {

        # Then check for the list price currency.
        $saleCurrencyCode = $sale->r_list_price_currency;
        if ( !$saleCurrencyCode ) {

            # If nothing else, we should have a currency code we can use.
            $saleCurrencyCode = $sale->currency_code;
        }
    }

    $self->{CurrencyCode} = $saleCurrencyCode;
    $self->{CountryCode}  = $sale->country_code;
    $self->{PriceTypeID}  = $sale->price_type_id;
    $self->{ProductID}    = $sale->product_id;

    if ( defined $sale->price_type_qualifier_id ) {
        $self->{PriceTypeQualifierID} = $sale->price_type_qualifier_id;
    } else {
        $self->{PriceTypeQualifierID} = 0;
    }

    my $regionIDs       = $self->_getRegionIDs($self);
    my $productMarketID = $self->_getProductMarketID( $sale->sale_id );

    if ( $regionIDs && $productMarketID && $self->CurrencyCode && $self->PriceTypeID ) {
        my $history = BookPub::Price::ProductMarketPriceList->new(
            productMarketID      => $productMarketID,
            regionID             => $regionIDs,
            currencyCode         => $self->CurrencyCode(),
            priceTypeID          => $self->PriceTypeID,
            priceTypeQualifierID => $self->PriceTypeQualifierID
        );

        $self->{Prices} = $history if ( $history->size );
    }

    return $self;
}

sub _getRegionIDs {
    my $self = shift;

    my $countryCode = $self->CountryCode() || return;
    my @regions;

    my $collection = BookPub::DB::Item::Region->GetAllRegions( country_code => $countryCode );

    while ( my $row = $collection->next() ) {
        push( @regions, $row->region_id );
    }

    return @regions ? \@regions : undef;
}

sub _getProductMarketID {
    my $self = shift;
    my $saleID = shift || return;

    # We store the id for the price with the sale_price_exception, so let's just use that
    # and work our way back to the product_market.
    my $salePriceException = BookPub::DB::Item::SalePriceException->Lookup( sale_id => $saleID );
    if ($salePriceException) {
        my $productMarketPriceID = $salePriceException->product_market_price_id;
        if ($productMarketPriceID) {
            my $productMarketPrice = BookPub::DB::Item::ProductMarketPrice->Lookup( product_market_price_id => $productMarketPriceID );
            if ($productMarketPrice) {
                return $productMarketPrice->product_market_id;
            }
        }
    } else {
        return undef;
    }
}
###
1;    #
###
