#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::DB::Item::Sale::AllPriceExceptionsReport;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::DB::Item;
use base 'BookPub::DB::Item::Sale';

use constant kTable => 'sale';
use constant kDB    => Common::DB::Item::kClientDB;

sub _GenerateClassConfig {
    my ($class) = @_;

    # Going to extend the base 'sale' schema and add in the many columns we want to join.
    #
    my $config = BookPub::DB::Item::Sale->_GenerateClassConfig();

    $config->{orig_file_name}       = { _readOnly => 1 };
    $config->{service}              = { _readOnly => 1 };
    $config->{approved}             = { _readOnly => 1 };
    $config->{variance}             = { _readOnly => 1 };
    $config->{effective_price}      = { _readOnly => 1 };
    $config->{price_currency}       = { _readOnly => 1 };
    $config->{price_effective_date} = { _readOnly => 1 };
    $config->{onix_code}            = { _readOnly => 1 };
    $config->{product_isbn13}       = { _readOnly => 1 };
    $config->{product_isbn10}       = { _readOnly => 1 };
    $config->{work_id}              = { _readOnly => 1 };
    $config->{book_title}           = { _readOnly => 1 };
    $config->{book_subtitle}        = { _readOnly => 1 };
    $config->{book_id}              = { _readOnly => 1 };
    return $config;
}

sub ReportQuery {
    my ( $class, %args ) = @_;

    my $sql =
"SELECT file.orig_file_name as orig_file_name, service.service_name as service, sale_price_exception.approved as approved, sale_price_exception.variance as variance, sale_price_exception.price as effective_price, sale_price_exception.start_date as price_effective_date, sale_price_exception.currency_code as price_currency, book_format_type.onix_code_id as onix_code, book_product.work_id as work_id, book_product.isbn13 as product_isbn13, book_product.isbn10 as product_isbn10, book.title as book_title, book.subtitle as book_subtitle, book.book_id as book_id, sale.* from file, sale, service, sale_price_exception, book_product, book, book_format, book_format_type"
      . " WHERE file.file_id=sale.file_id"
      . " AND file.service_id=service.service_id"
      . " AND sale_price_exception.sale_id = sale.sale_id"
      . " AND book_product.product_id = sale.product_id"
      . " AND book.book_id = book_product.book_id"
      . " AND book_product.book_format_id = book_format.book_format_id"
      . " AND book_format.book_format_type_id = book_format_type.book_format_type_id";

    if ( $args{fileID} ) {
        $sql .= " AND file.file_id=" . $args{fileID};
    }

    if ( $args{serviceID} ) {
        $sql .= " AND file.service_id=" . $args{serviceID};
    }

    $sql .= " ORDER BY file.service_id,sale.file_id,sale.line_num";

    return $class->GetAll($sql);
}

1;
