#!/usr/bin/perl -w
#
# This is a utility to scan over contracts and determine if there
# are any that don't have terms, as these cause errors in the
# mechanical runs.

use strict;

use Getopt::Std;
use File::Basename;

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

my %opt;
usage() unless getopts( 'n:c:qhldv', \%opt );

if ( $opt{l} ) {
    list_clients( $ARGV[0] );
}

if ( $opt{h} ) {
    usage();
}

my $checker = new Checker(
    client_id => $opt{c},
    quiet     => $opt{q},
    verbose   => $opt{v},
    debug     => $opt{d}
);

$checker->run();

sub usage {
    my $prog = basename $0;

    print <<EofUSAGE;

usage: $prog [-c client_id] [-l client_name/pattern] [-h] [-d]
            -c - Client ID (if not specified then run for all clients)
            -l - List client ids (host pattern is optional)
            -d - display debug information to STDERR
            -v - display verbose debug information to STDERR
            -q - Do not output summary report
            -h - display this screen
EofUSAGE

    exit 1;
}

sub list_clients {
    my $host = shift;
    my $key;
    foreach $key ( sort { $a <=> $b } keys(%Common::RSDB::CLIENT_DB) ) {
        printf( "%-20s %-10s ID: %s\n", $Common::RSDB::CLIENT_DB{$key}->{db_name}, "($Common::RSDB::CLIENT_DB{$key}->{host_id})", $key )
          if ( !defined($host) || $Common::RSDB::CLIENT_DB{$key}->{db_name} =~ /$host/i );
    }
    exit 1;
}

package GetNoTermContracts;

use lib '/app/tools/common/lib';

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

sub _findThem {
    my $class = shift;

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

    my $sql = "SELECT DISTINCT(c.artist_contract_id)
                   FROM new_artist_contract c
                   INNER JOIN new_artist_contract_term t
                       ON c.artist_contract_id = t.artist_contract_id
                   LEFT JOIN expense_artist_payee_map m
                       ON c.artist_contract_id = m.artist_contract_id
                   LEFT JOIN expense e
                       ON m.expense_id = e.expense_id
                   WHERE ((t.rate = 0 AND t.priority = 0)
                       OR (c.artist_contract_id
                       NOT IN (select artist_contract_id
                                   FROM new_artist_contract_term
                                   WHERE priority = 0)))
                   AND e.expense_id IS NOT NULL
                   AND e.processed = 0
                   AND c.deleted = 0
                   ORDER BY c.artist_contract_id";

    Common::Log::Debug("SQL: $sql");

    my $collection = RPS::DB::Item::NewArtistContract->GetAll($sql);
    return $collection;

}

package Checker;

use lib '/app/tools/common/lib';
use Common::RSApp;
use Common::Util;
use Common::Client;
use Common::Assert;
use Common::WriteXML;
use Common::DB::Item::Client;

use lib '/app/tools/common/lib';
use base 'Common::Script';
use base 'Common::DB::Item';

use Data::Dumper;

sub _process {
    my $self = shift;

    my $client = Common::Client::Current();
    return if ( $client->isClientType(Common::Client::kBookPublishing) );

    my $clientID = Common::RSApp::GetClientID();
    assert($clientID);

    my $clientData = Common::DB::Item::Client->Lookup( client_id => $clientID );

    my $collection = GetNoTermContracts->_findThem();

    while ( my $row = $collection->next() ) {
        print "Client: $clientData->{client_name} Contract ID: " . $row->artist_contract_id . "\n";
    }

}
