#------------------------------------------------------------
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------

package RPS::DB::Item::ReportQueries::CATrackWithoutSongTimings;

use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;
use Common::Client;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ReportQueries;

use base 'RPS::DB::Item::ReportQueries';

#use constant kTable => 'track';

sub _config
{
    return 
    {
        'label_name'        => {},
        'catalog_number'    => {},
        'album_title'       => {},
        'track_title'       => {},
        'artist_name'       => {},
        'track_order'       => {},
        'track_id'          => {},
        'album_id'          => {},
    };
}


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

    my @selectFields;
    push @selectFields, "label.label_name as label_name";
    push @selectFields, "album.catalog_number as catalog_number";
    push @selectFields, "album.title as album_title";
    push @selectFields, "track.title as track_title";
    push @selectFields, "artist.name as artist_name";
    push @selectFields, "track.track_order as track_order";
    push @selectFields, "track.track_id as track_id";
    push @selectFields, "album.album_id as album_id";


    my $selectClause = join(',', @selectFields);
    my $fromClause = "label, album, track, artist";

    # Note the big nasty implicit join.
    #
    my $whereClause = "track.track_id in (SELECT track_id from ca_track_license where inactive=0 and ( (type=1 and rate_type=1) or ( type=2 and track_id in (select track_id from track where album_id in (select album_id from album_contract where artist_contract_id in (select artist_contract_id from controlled_composition where rate_type=1)))))) AND master_id in (select master_id from master where duration IS NULL or duration=0) AND track.album_id = album.album_id and label.label_id = album.label_id and track.artist_id = artist.artist_id";
#    my $whereClause = "track.track_id in (SELECT track_id from track_license where inactive=0 and ( (type=1 and rate_type=1) or (type=2 and track_id in (".join(',',@ccTracks).")))) AND master_id in (select master_id from master where duration IS NULL or duration=0) AND track.album_id = album.album_id and label.label_id = album.label_id and track.artist_id = artist.artist_id";

    my $orderByClause = 'label_name, album_title, track_order';

    my $sql = "SELECT $selectClause FROM $fromClause WHERE $whereClause ORDER BY $orderByClause";
    
    return $class->SUPER::GetAll($sql);
}


1;
