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

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 => 'ca_mechanical_run_unlicensed_track_log';
use constant kDB => Common::DB::Item::kClientDB();


sub _reload { }

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

    my $sql = "SELECT * FROM " . kTable . " WHERE ca_mechanical_run_id=$runID";
    
    if (defined($labelID))
    {
    	$sql .= " AND label_id=$labelID";
    }
    
    # These should be sorted by label, and then album title, and then config (and then maybe track title...).
    #    
    $sql .= " ORDER BY label_name, album_title, product_type, track_title, year";
    
    return $class->SUPER::GetAll($sql);
}

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

    my $sql = "DELETE FROM " . kTable . " WHERE ca_mechanical_run_id=$id";
    my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd($sql);
}

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

    my $sql = "SELECT DISTINCT label_id, label_name FROM " . kTable . " WHERE ca_mechanical_run_id=$id";
    return $class->SUPER::GetAll($sql);
}

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

    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd("SELECT COUNT(*) from " . kTable . " where ca_mechanical_run_id=$id");
    my $hr = $sth->fetchrow_hashref();
    if ($hr->{'COUNT(*)'} > 0)
    {
        return 1;
    }
    return 0;
}


1;
