package Distribution::DB::Item::ProductServicePricing;
use strict;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;

use base 'Common::DB::Item';

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

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

    assert($class);

    assert( defined( $args{service_id} ) );
    assert( defined( $args{product_id} ) );

    my $dbo = Common::RSApp::GetClientDB();

    my $sql =
        'SELECT * '
      . 'FROM product_service_pricing '
      . 'WHERE service_id = '
      . $dbo->DBQuote( $args{service_id} ) . " AND "
      . ' product_id = '
      . $dbo->DBQuote( $args{product_id} );

    Common::Log::Debug("QUERY: $sql");
    my $collection = $class->SUPER::GetAll($sql);

    return $collection->next();
}

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

    assert($class);

    assert( defined( $args{service_id} ) );
    assert( defined( $args{product_id} ) );

    my $dbo = Common::RSApp::GetClientDB();

    my $sql =
        'DELETE  '
      . 'FROM product_service_pricing '
      . 'WHERE service_id = '
      . $dbo->DBQuote( $args{service_id} ) . " AND "
      . ' product_id = '
      . $dbo->DBQuote( $args{product_id} );

    Common::Log::Debug("QUERY: $sql");
    $dbo->DoCmd($sql);
}

sub UpdateTier {
    my $class = shift;
    my %args  = @_;
    my $dbo   = Common::RSApp::GetClientDB();

    assert( $args{service_id} );
    assert( $args{product_id} );
    assert( $args{service_price_tier_id} );

    my $sql =
        "UPDATE "
      . kTable
      . " SET service_price_tier_id = "
      . $dbo->DBQuote( $args{service_price_tier_id} )
      . " WHERE "
      . "service_id = "
      . $dbo->DBQuote( $args{service_id} ) . " AND "
      . "product_id = "
      . $dbo->DBQuote( $args{product_id} );

    Common::Log::Debug("Price Tier: $sql");

    my $collection = $class->GetAll($sql);

    return $collection->hasNext() ? $collection->next() : undef;
}
1;
