#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::LabelRoyaltyStatement;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::Assert;
use Common::DB::Item;
use Common::DB::ItemCollection;
use base 'Common::DB::Item';

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

use constant kFilterByTotalLessThanZero => 1;
use constant kFilterByTotalGreaterThanZero => 2;
use constant kFilterByTotalGreaterThatMinPayment => 3;
use constant kFilterByOnHold => 4;
use constant kFilterByNone => 5;


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

    my @from;
    push @from, "label_royalty_statement as s";

    my @where;
    if ($args{runID})
    {
        push @where, "s.label_royalty_run_id = ".$args{runID};
    }
    if ($args{payorID})
    {
        push @where, "s.payor_id = ".$args{payorID};
    }

    if ($args{filterBy})
    {
        my $filter = $args{filterBy};
        if (kFilterByTotalLessThanZero == $filter)
        {
            push @where, "(balance <= 0 && amount_due <= 0)";
        }
        elsif (kFilterByTotalGreaterThanZero == $filter)
        {
            push @where, "(balance > 0 || amount_due > 0)";
        }
        elsif (kFilterByTotalGreaterThatMinPayment == $filter)
        {
            push @where, "(s.balance >= s.min_payment || s.amount_due >= s.min_payment)";
        }
        elsif (kFilterByOnHold == $filter)
        {
            push @where, "s.on_hold = 1";
        }
        #
        # else... kFilterByNone means no filter
    }

    my $sortString;
    if ($args{sortByPayeeName})
    {
        push @from, "label_payee as ap";
        push @where, "s.payee_id = ap.label_payee_id";
        $sortString = "ORDER BY ap.name";
    }

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

    my $fromString;
    if (scalar @from)
    {
        $fromString = " FROM " . join(',', @from);
    }


    my $sql = "SELECT s.* $fromString $whereString $sortString";

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


sub GetByLabelRoyaltyRunID
{
    my ($class, $runID, $payorID) = @_;
    assert($runID);

    my $sql = "SELECT * from " . kTable . " WHERE label_royalty_run_id=$runID";
    if ($payorID)
    {
        $sql .= " AND payor_id=$payorID";
    }

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


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

    my @payorIDs;

    my $sql = "SELECT distinct payor_id from " . kTable . " where label_royalty_run_id=$runID";
    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd($sql);
    while (my $hr = $sth->fetchrow_hashref())
    {
        push @payorIDs, $hr->{payor_id};
    }

    return @payorIDs;
}




sub GetCountByLabelRoyaltyRunID
{
    my ($class, $runID, $payorID) = @_;
    assert($runID);
    my $sql = "SELECT COUNT(*) from " . kTable . " where label_royalty_run_id=$runID";
    if ($payorID)
    {
        $sql .=" and payor_id=$payorID";
    }
    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd($sql);
    my $hr = $sth->fetchrow_hashref();
    return $hr->{'COUNT(*)'};
}


1;
