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

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

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

use constant kParentAlbumContract => 1;
use constant kParentTrackContract => 2;


# A note about this 'processedFlag':
# Don't pass anything if you want both processed and unprocessed.
# Pass '0' if you want only unprocessed.
# Pass '1' if you want only processed.
sub GetByParent
{
    my ($class, $id, $type, $processedFlag) = @_;

    my $sql = "SELECT * FROM ".kTable." WHERE parent_type=$type AND parent_id=$id";
    if (defined $processedFlag)
    {
        $sql .= " AND processed=$processedFlag";
    }

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

sub GetByType
{
    my ($class, $type) = @_;

    my $sql = "SELECT * FROM ".kTable." WHERE expense_type_id=$type";

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

sub GetAllUnprocessed
{
    my ($class) = @_;

    my $sql = "SELECT * FROM " . kTable . " WHERE processed=0";

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

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

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

    my $sql = "SELECT COUNT(*) FROM ".kTable." WHERE parent_type=$type AND parent_id=$id AND processed>0";
    my $sth = $dbo->DoCmd($sql);
    my $hr = $sth->fetchrow_hashref();
    my $count = $hr->{'COUNT(*)'};
    return $count;
}

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

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

    my $sql = "SELECT COUNT(*) FROM ".kTable." WHERE parent_type=$type AND parent_id=$id AND pre_process = 1 AND processed=0";
    my $sth = $dbo->DoCmd($sql);
    my $hr = $sth->fetchrow_hashref();
    my $count = $hr->{'COUNT(*)'};
    return $count;
}

1;

