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

use strict;
use warnings;
use Carp;

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

use lib '/app/tools/rps/lib';
use Common::FormObject;
use RPS::DB::Item::ProductPrice;

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{productPriceID} ) {
        $self->_propertiesFromDB( \%properties, $args{productPriceID} );
    } elsif ( $args{productID} ) {
        $self->_propertiesFromDefaults( \%properties, $args{productID} );
    }

    $self->_initProperties( \%properties );

    return $self;
}

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

    my $dbItem = RPS::DB::Item::ProductPrice->Lookup( product_price_id => $productPriceID );
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

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

    $self->{_inDB}                    = 1;
    $hrProperties->{product_price_id} = $dbItem->product_price_id;
    $hrProperties->{product_id}       = $dbItem->product_id;
    $hrProperties->{price_level_id}   = $dbItem->price_level_id;
    $hrProperties->{price_id}         = $dbItem->price_id;
}

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

    $self->{ProductPriceID} = Common::FormObject::Scalar->new( value => $props->{product_price_id} );
    $self->{ProductID}      = Common::FormObject::Scalar->new( value => $props->{product_id} );
    $self->{PriceLevelID}   = Common::FormObject::Scalar->new( value => $props->{price_level_id} );
    $self->{PriceID}        = Common::FormObject::Scalar->new( value => $props->{price_id} );
}

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

    $self->{_inDB}              = 0;
    $hrProperties->{product_id} = $productID;
}

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

    if ( !$self->PriceID() ) {
        $self->delete();
        return 1;
    }

    my $dbItem;
    if ( $self->{_inDB} ) {
        $dbItem = RPS::DB::Item::ProductPrice->Lookup( product_price_id => $self->ProductPriceID() );
    } else {
        $dbItem = RPS::DB::Item::ProductPrice->Create( product_id => $self->ProductID() );
    }

    $dbItem->price_level_id( $self->PriceLevelID() );
    $dbItem->price_id( $self->PriceID() );

    $dbItem->save();

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

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

    if ( $self->{_inDB} ) {
        my $dbItem = RPS::DB::Item::ProductPrice->Lookup( product_price_id => $self->ProductPriceID() );
        $dbItem->delete();
        $self->{_inDB} = 0;
    }

    return 1;
}

# Adding some static methods for various purposes.
#
sub GetWholesaleProductPrice {
    my ( $productID, $priceLevelID ) = @_;
    assert($productID);

    my $priceData = _GetProductPriceData( $productID, $priceLevelID );
    return undef unless $priceData;
    my $price = $priceData->wholesale;

    return $price;
}

sub GetRetailProductPrice {
    my ( $productID, $priceLevelID ) = @_;
    assert($productID);

    my $priceData = _GetProductPriceData( $productID, $priceLevelID );
    return undef unless $priceData;
    my $price = $priceData->retail;

    return $price;
}

sub _GetProductPriceData {
    my ( $productID, $priceLevelID ) = @_;

    assert($productID);

    my $product = RPS::DB::Item::Product->Lookup( product_id => $productID );

    # Use the product's default price_level if the sale does not specify one...
    #
    $priceLevelID = $priceLevelID || $product->default_price_level_id || 0;
    my $productPrice = RPS::DB::Item::ProductPrice->GetByProductIDPriceLevelID( $productID, $priceLevelID );

    # Return undef if we don't have this price level.
    #
    return undef unless $productPrice;

    my $priceID = $productPrice->price_id;
    my $priceData = RPS::DB::Item::Price->Lookup( price_id => $priceID );
    return $priceData;
}

###
1;    #
###

