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

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::DB::Item;
use base 'Common::DB::Item';

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



use constant kCycleQuarterly  => 1;
use constant kCycleSemiAnnual => 2;
use constant kCycleAnnual     => 3;

use constant kStatusInactive    => 0;
use constant kStatusActive      => 1;
use constant kStatusOnHold      => 2;



sub Get
{
    my ($class, %args) = @_;

    my @where;

    # Are we paging alphanumerically?
    #
    my $alpha = $args{alpha};
    if (defined $alpha)
    {
        if ('0' eq uc($alpha))
        {
            push @where, "name < 'A'";
        }
        elsif ('Z' eq $alpha)
        {
            push @where, "name >= 'Z'";
        }
        else
        {
            push @where, "name >= '".uc($alpha)."'";
            push @where, "name < '".chr( ord(uc($alpha)) + 1)."'";
        }
    }

    my $whereClause;
    if (scalar @where)
    {
        $whereClause = "WHERE " . join(' AND ', @where);
    }


	my $sql = "SELECT * FROM ".kTable." $whereClause ORDER BY name";
    return $class->GetAll($sql);
}



sub Search
{
    my ($class, $searchTerm) = @_;

	my $dbo = Common::RSApp::GetClientDB();

	my $sql = "SELECT * FROM ".kTable
		. " WHERE " . $class->SearchSyntax(field => "name", value => $searchTerm)
		. " OR " . $class->SearchSyntax(field => "client_payee_id", value => $searchTerm)
		. " OR " . $class->SearchSyntax(field => "client_account_id", value => $searchTerm)
		. " ORDER BY name";

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


sub Match
{
    my ($class, $lookupValue) = @_;

	my $dbo = Common::RSApp::GetClientDB();

	my $sql = "SELECT * FROM ".kTable
		. " WHERE client_account_id=".$dbo->DBQuote($lookupValue)
		. " OR client_payee_id=".$dbo->DBQuote($lookupValue)
		. " OR name=".$dbo->DBQuote($lookupValue);

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


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

	my $dbo = Common::RSApp::GetClientDB();

    my %map;

    my $sql = "SELECT artist_payee_id FROM " . kTable . " WHERE status=".kStatusInactive;
    my $sth = $dbo->DoCmd($sql);
    while (my $hr = $sth->fetchrow_hashref())
    {
        $map{$hr->{artist_payee_id}} = 1;
    }

    return \%map;
}


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

    my $sql = "SELECT * FROM " . kTable . " WHERE artist_payee_id IN (SELECT payee_id FROM artist_royalty_statement WHERE artist_royalty_run_id=$runID) ORDER BY name";

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


sub GetAllActiveOrOnHoldForPayor
{
    my ($class, $payorID) = @_;

    # We need all of the payees who are have contracts and/or finance accounts with the payor. 
    #

	my $sql = "SELECT * FROM ".kTable." WHERE (status=1 OR status=2)"
     . " AND (artist_payee_id IN (select artist_payee_id from new_artist_contract where payor_id=$payorID) OR artist_payee_id IN (select artist_payee_id from artist_payee_account where payor_id=$payorID))";

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


1;


