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

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

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

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

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

sub _table { kTable }

sub requireUpdate {
    my $class = shift;
    my %args  = @_;

    assert( %args, "No args passed" );
    assert( $args{product_id} || $args{book_id}, "Product or Book ID Required" );
    assert( $args{service_id},    "Service ID Required" );
    assert( $args{catalog_price}, "Catalog Price Required" );

    my $sql =
        "SELECT * FROM "
      . $class->_table()
      . " INNER JOIN product_monitor_alert USING (product_monitor_alert_id) "
      . " INNER JOIN product_monitor_item USING (product_monitor_item_id) "
      . "WHERE "
      . " product_id = "
      . $class->quote( $args{product_id} )
      . " AND service_id = "
      . $class->quote( $args{service_id} )
      . " AND date_resolved IS NULL "
      . " AND catalog_price = "
      . $class->quote( $args{catalog_price} );

    Log->debug("SQL: $sql");

    my $collection = $class->SUPER::GetAll($sql);
    return !$collection->hasNext();
}

1;
