#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::DB::Item::CAPublisherTrackLicense;

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

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

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


# We are joining some tables, so we need a custom configuration.
#
sub _config
{
    my ($class) = @_;

    return
    {
        'track_license_id'  => {},
        'album_title'       => {},
        'album_id'          => {},
        'track_title'       => {},
        'track_id'          => {},
        'track_order'       => {},
        'crossed'           => {},
        'region_id'         => {},
        'region_name'       => {},
        'share'             => {},
        'admin_id'          => {},
        'agent_id'          => {},
        'admin_name'        => {},
        'agent_name'        => {},
        'type'              => {},
        'product_type_id'   => {},
        'inactive'          => {},
    };
}


sub GetListSorted
{
    my ($class, $publisherID, $payorID, $directOrIndirect) = @_;

    assert($publisherID);
    assert($payorID);

    my @select;
    push @select, "ca_track_license.ca_track_license_id AS track_license_id";
    push @select, "album.title AS album_title";
    push @select, "album.album_id AS album_id";
    push @select, "track.title AS track_title";
    push @select, "track.track_id AS track_id";
    push @select, "track.track_order AS track_order";
    push @select, "ca_track_license.cross_collateralized AS crossed";
    push @select, "ca_track_license.share AS share";
    push @select, "ca_track_license.type AS type";
    push @select, "ca_track_license.inactive AS inactive";
    push @select, "ca_track_license.product_type_id AS product_type_id";
    push @select, "region.region_id AS region_id";
    push @select, "track_license_publisher.admin_id AS admin_id";
    push @select, "track_license_publisher.agent_id AS agent_id";
    push @select, "publisher_admin.publisher_name as admin_name";
    push @select, "publisher_agent.publisher_name as agent_name";
   
    my @join;
    push @join, "LEFT JOIN track ON (ca_track_license.track_id = track.track_id)";
    push @join, "LEFT JOIN album ON (track.album_id = album.album_id)";
    push @join, "LEFT JOIN region ON (ca_track_license.region_id = region.region_id)";
    push @join, "LEFT JOIN ca_publisher AS track_license_publisher ON (ca_track_license.ca_publisher_id = track_license_publisher.ca_publisher_id)";
    push @join, "LEFT JOIN ca_publisher AS publisher_admin ON (track_license_publisher.admin_id = publisher_admin.ca_publisher_id)";
    push @join, "LEFT JOIN ca_publisher AS publisher_agent ON (track_license_publisher.agent_id = publisher_agent.ca_publisher_id)";

    my @where;
    push @where, "ca_track_license.ca_publisher_id=".$class->quote($publisherID);
    push @where, "ca_track_license.payor_id=".$class->quote($payorID);
    if ('direct' eq $directOrIndirect)
    {
        push @where, "publisher_direct = 1";
    }
    if ('indirect' eq $directOrIndirect)
    {
        push @where, "publisher_direct = 0";
    }

    my $sql = "SELECT " . join(',', @select) . " FROM ca_track_license " . join(' ', @join) 
     . " WHERE " . join(' AND ', @where)
     . " ORDER BY album_title,track_order,product_type_id,region_id";

    return $class->GetAll($sql);
}

1;
