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

package RPS::ArtistRoyalty::Fast::Static::OtherPayorsProducts;

use strict;
use Data::Dumper;
use File::Path;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';
use Common::RSApp;

use Common::Log;

# This is not a typical, staged data class.
# Instead this just wraps around a ram-cached query.
#
use base 'RPS::ArtistRoyalty::Fast::Static';

# This gives us a list of product IDs that are covered by active contracts
# for other payors.
sub GetOtherPayorsProducts {
    my ($class, $payorID) = @_;

    my $sql = qq/
    SELECT DISTINCT product.product_id FROM product
    JOIN track_contract ON (product.asset_id = track_contract.track_id)
    JOIN new_artist_contract ON (track_contract.artist_contract_id = new_artist_contract.artist_contract_id)
    WHERE track_contract.status = 1
    AND new_artist_contract.payor_id != $payorID
    AND product.product_type_id = 4
    UNION
    SELECT DISTINCT product.product_id FROM product
    JOIN track ON (product.asset_id = track.track_id)
    JOIN album_contract ON (track.album_id = album_contract.album_id)
    JOIN new_artist_contract ON (album_contract.artist_contract_id = new_artist_contract.artist_contract_id)
    WHERE album_contract.status = 1
    AND new_artist_contract.payor_id != $payorID
    AND product.product_type_id = 4
    UNION
    SELECT DISTINCT product.product_id FROM product
    JOIN album_contract ON (product.asset_id = album_contract.album_id)
    JOIN new_artist_contract ON (album_contract.artist_contract_id = new_artist_contract.artist_contract_id)
    WHERE album_contract.status = 1
    AND new_artist_contract.payor_id != $payorID
    AND product.product_type_id <> 4
    /;

    my %data;
    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd($sql);
    while ( my @result = $sth->fetchrow_array() ) {
        $data{ $result[0] } = 1;
    }

    return \%data;
}

1;

