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

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

use constant kAdjustmentTypeCCompNewLicense => 1;
    #
    # This code is used for adjustments triggered by a new license
    # being added to a product, causing previously unallocated
    # funds to be adjusted.




sub GetLastStatementAdjustment
{
    my ($class, $statementItemID) = @_;

    # Get a definitive list (well, we'll build a hash for fast lookups) of the run ids worth considering.
    # I'm including status 0 (running) runs in here as well, since the current code relies on this to
    # avoid re-adjusting the same previous adjustment over and over again.
    # Since that logic is driven by license id, and we won't have simultaneous runs that hit the same license
    # (licenses are tied to individual payors), this should be safe.
    #
    my %goodRuns;
    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd("SELECT mechanical_run_id FROM mechanical_run WHERE status IN (0,2,5)");
    while (my $id = $sth->fetchrow_array())
    {
        $goodRuns{$id} = 1;
    }

    my $goodRunIDs = join(",", keys %goodRuns);


    # JPK - We appear to have a bug that prevents the 'previous_mechaniacl_statement_adjustment_item_id' from getting filled in.
    # I am going to re-write this logic to no longer rely on that, but to instead simply sort items by the run_id.
    #
#    my $sql = "select * from mechanical_statement_adjustment_item where original_mechanical_statement_item_id=$statementItemID "
#            . " and mechanical_statement_adjustment_item_id NOT IN "
#            . " ("
#            . "  select previous_mechanical_statement_adjustment_item_id from mechanical_statement_adjustment_item "
#            . "  where original_mechanical_statement_item_id=$statementItemID"
#            . " )"
#            . "  AND mechanical_statement_id IN (select mechanical_statement_id from mechanical_statement where mechanical_run_id in ($goodRunIDs))";
    my $sql = " select mechanical_statement_adjustment_item.*, mechanical_run.mechanical_run_id from mechanical_statement_adjustment_item"
            . " join mechanical_statement using (mechanical_statement_id)"
            . " join mechanical_run using (mechanical_run_id)"
            . "where original_mechanical_statement_item_id=$statementItemID"
            . " and mechanical_run.status in (0,2,5)"
            . " order by mechanical_run.mechanical_run_id desc";

    my $collection = $class->GetAll($sql);

    return undef unless $collection->hasNext();

# JPK - Don't need this assertion anymore.
#    assert($collection->size == 1);

    my $item = $collection->next;
    return $item;
}



sub GetByMechanicalStatementID
{
    my ($class, $statementID) = @_;

    my $sql = "SELECT * FROM mechanical_statement_adjustment_item WHERE mechanical_statement_id=$statementID";
    return $class->GetAll($sql);
}


sub GetByMechanicalStatementIDPublisherID
{
    my ($class, $statementID, $publisherID) = @_;

    my $sql = "SELECT * FROM mechanical_statement_adjustment_item WHERE mechanical_statement_id=$statementID"
     . " AND track_license_id in (select track_license_id from track_license where publisher_id=$publisherID)";
    return $class->GetAll($sql);
}


sub GetByMechanicalStatementIDTrackLicenseID
{
    my ($class, $statementID, $trackLicenseID) = @_;

    my $sql = "SELECT * FROM mechanical_statement_adjustment_item"
            . " WHERE mechanical_statement_id=$statementID"
            . " AND track_license_id=$trackLicenseID";

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

sub DeleteByMechanicalStatementID
{
    my ($class, $id) = @_;

	my $sql = "DELETE from mechanical_statement_adjustment_item WHERE mechanical_statement_id=$id";
	my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd($sql);
}


1;
