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

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 => 'distributor_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 distributor_payee_id FROM " . kTable . " WHERE status=" . kStatusInactive;
    my $sth = $dbo->DoCmd($sql);
    while ( my $hr = $sth->fetchrow_hashref() ) {
        $map{ $hr->{distributor_payee_id} } = 1;
    }

    return \%map;
}

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

    my $sql =
        "SELECT * FROM "
      . kTable
      . " WHERE distributor_payee_id IN (SELECT payee_id FROM distribution_statement WHERE distribution_run_id = " . $class->quote($runID) . ")";

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

1;

