#------------------------------------------------------------
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::ArtistRoyaltyRunMissedSaleLog;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Util qw(escape_mysql_regexp escape_mysql_like);
use Common::DB::ItemCollection;
use Common::Assert;
use Common::RSApp;

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

use constant kTable => 'artist_royalty_run_missed_sale_log';
use constant kDB    => Common::DB::Item::kClientDB();

sub GetByRunID {
    my ( $class, $id ) = @_;

    my $sql =
        "SELECT * FROM "
      . kTable
      . " WHERE artist_royalty_run_id = "
      . $class->quote($id)
      . " ORDER BY catalog_number, album_title, track_title, reason, details, units DESC";
    return $class->SUPER::GetAll($sql);
}

sub DeleteByRunID {
    my ( $class, $id ) = @_;

    my $dbo = Common::RSApp::GetClientDB();
    my $sql = "DELETE FROM " . kTable . " WHERE artist_royalty_run_id = ?";
    $dbo->DoCmdWithPlaceholders($sql, [$id]);
}

sub DeleteByRunIDAndSaleID {
    my ( $class, $runID, $saleID ) = @_;

    my $dbo = Common::RSApp::GetClientDB();
    my $sql = "DELETE FROM " . kTable . " WHERE artist_royalty_run_id = ? and sale_id = ?";
    $dbo->DoCmdWithPlaceholders($sql, [$runID, $saleID]);
}

sub DeleteByRunIDSaleIDAndReason {
    my ( $class, $runID, $saleID, $reason ) = @_;

    my $dbo = Common::RSApp::GetClientDB();
    my $sql = "DELETE FROM " . kTable . " WHERE artist_royalty_run_id = ? and sale_id = ? and reason = ?";
    $dbo->DoCmdWithPlaceholders($sql, [$runID, $saleID, $reason]);
}

sub GetSaleIDsByRunID {
    my ( $class, $id ) = @_;

    my $sql = "SELECT distinct sale_id FROM " . kTable . " WHERE artist_royalty_run_id = " . $class->quote($id);
    return $class->SUPER::GetAll($sql);
}

1;
