#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::DB::Item::ArtistRoyaltyExpenseItem;
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 => 'artist_royalty_expense_item';
use constant kDB    => Common::DB::Item::kClientDB();

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

    my $sql = "DELETE FROM " . kTable . " WHERE artist_royalty_album_id=$id";
    my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd($sql);
}

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

    my $sql = "SELECT * from " . kTable . " WHERE artist_royalty_album_id=$id";

    return $class->GetAll($sql);

}

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

    assert($statementID);

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

    my $sql = "SELECT IFNULL(SUM(cost), 0) as cost_sum, IFNULL(SUM(total), 0) as total_sum ";
    $sql .=   "FROM " . kTable . " WHERE artist_royalty_album_id IN ";
    $sql .=     "( SELECT artist_royalty_album_id FROM artist_royalty_album ";
    $sql .=     "WHERE artist_royalty_statement_id = " . $class->quote($statementID) . ")";

    my $sth = $dbo->DoCmd($sql);
    my $hr  = $sth->fetchrow_hashref();
    return ( $hr->{cost_sum}, $hr->{total_sum} );
}
1;
