#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Price::Command::AjaxPriceHistory;
use strict;
use warnings;

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

use BookPub::Catalog::Book::Extended;
use BookPub::Catalog::ImprintList;
use BookPub::Catalog::ContributorRoleList;
use BookPub::Catalog::Product::Book::WithFormat;
use BookPub::Catalog::Region::RegionWithCountriesList;
use BookPub::DB::Item::Sale;
use BookPub::Price::PriceHistory;
use BookPub::Price::PriceTypeList;

use RSApache::Response::XSLT;
use RSApache::Response::JSON;
use RSApache::Command::XMLCommand;

use base 'RSApache::Command::XMLCommand';

sub cmd      { return 'price_history'; }
sub area     { return 'price'; }
sub pageName { return 'Price History'; }

use constant kTemplate => "/app/tools/bookpub/templates/price/ajax_price_history.xsl";

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $saleID = $self->getParam("SaleID");
    assert($saleID);

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

    if ($sale) {

        if ( $sale->product_id ) {
            my $product = BookPub::Catalog::Product::Book::WithFormat->new( productID => $sale->product_id );
            $self->{xml}{Product} = $product;
            if ( $product->BookID ) {
                $self->{xml}{Book} = BookPub::Catalog::Book::Extended->new( bookID => $product->BookID );
            }
        }

        $self->{xml}{Regions}             = BookPub::Catalog::Region::RegionWithCountriesList->new( productID => $sale->product_id );
        $self->{xml}{PriceTypes}          = BookPub::Price::PriceTypeList->new();
        $self->{xml}{ImprintList}         = BookPub::Catalog::ImprintList->new();
        $self->{xml}{ContributorRoleList} = BookPub::Catalog::ContributorRoleList->new();
        $self->{xml}{PriceHistory}        = BookPub::Price::PriceHistory->new( sale => $sale );
    }

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;
}

1;
