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

use strict;
use warnings;

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

use constant kTypePublishingLicense     => 1;
use constant kTypeControlledComposition => 2;
use constant kTypeContractUnitBased     => 3;
use constant kTypeContractNetBased      => 4;
use constant kTypeCAPublishingLicense   => 5;
use constant kTypeRingtoneLicense       => 6;

# this is a replacement for UnitBased
use constant kTypeArtistContract => 3;

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

sub GetByIDType {
    my ( $class, $id, $type ) = @_;
    assert($id);
    my $sql = "SELECT * FROM reserve_liquidation WHERE id=$id AND type=$type ORDER BY period";
    return $class->GetAll($sql);
}

sub GetByIDTypePeriod {
    my ( $class, $id, $type, $period ) = @_;
    assert($id);
    my $sql        = "SELECT * FROM reserve_liquidation WHERE id=$id AND type=$type and period = $period";
    my $collection = $class->GetAll($sql);
    return $collection->next();

}

sub CopyFromPatternIDAndType {
    my ($class, $patternID, $patternType, $newID) = @_;

    $patternID   = $class->quote($patternID);
    $patternType = $class->quote($patternType);
    $newID       = $class->quote($newID);

    my $sql = qq/
        INSERT INTO reserve_liquidation (
            id,
            `type`,
            period,
            percent,
            date_created ,
            created_by,
            date_modified,
            modified_by
        )
        SELECT
            $newID,
            `type`,
            period,
            percent,
            CURRENT_TIMESTAMP(),
            created_by,
            CURRENT_TIMESTAMP(),
            modified_by
        FROM reserve_liquidation
        WHERE id = $patternID
            AND `type` = $patternType
    /;

    my $dbo = Common::RSApp::GetClientDB();
    my $sth = $dbo->DoCmd($sql);

    return 1;
}


1;


