#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::DB::Item::ProductMonitorItemPrice::JoinedWithProductMonitorItem;

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;
use Common::DB::Item;
use Common::Log;

use base 'BookPub::DB::Item::ProductMonitorItemPrice';

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

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

    my $config = $class->SUPER::_GenerateClassConfig();
    $config->{date_created} = { _readOnly => 1 };

    return $config;
}

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

    assert( $args{product_id}, "Product ID Required" );
    assert( $args{service_id}, "Service ID Required" );

    # product_id can be either a single id, or a reference to an array of ids.
    # We'll convert the single ID to an array of one
    #
    my @quotedProductIDs;
    if ( 'ARRAY' ne ref( $args{product_id} ) ) {
        push @quotedProductIDs, $class->quote( $args{product_id} );
    } else {
        foreach my $id ( @{ $args{product_id} } ) {
            push @quotedProductIDs, $class->quote($id);
        }
    }
    my $productIDString = join( ',', @quotedProductIDs );

    my $sql =
        "SELECT * FROM "
      . $class->kTable
      . " INNER JOIN product_monitor_item USING (product_monitor_item_id) WHERE "
      . " product_id IN ( $productIDString ) AND "
      . " service_id = "
      . $class->quote( $args{service_id} )
      . " ORDER BY date_created";

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

1;
