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

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Util qw(escape_mysql_regexp);
use Common::DB::ItemCollection;

use Common::DB::Item;
use base 'Common::DB::Item';

use constant kModeUnit              => 1;
use constant kModeNetRevenue        => 2;
use constant kModeUnitDigital       => 3;    # albums
use constant kModeUnitDigitalSingle => 4;    # single downloads

use constant kRateTypePercent => 1;
use constant kRateTypeFlat    => 2;

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

# !!! THIS IS DEPRECATED

sub GetAll {
    my %args = @_;

    my $order = "artist_contract.title, artist_payee.name";

    my $sql = "SELECT artist_contract.* FROM artist_contract"
      . " LEFT JOIN artist_payee ON artist_payee.artist_payee_id=artist_contract.artist_payee_id ORDER BY $order";

    my $collection = Common::DB::ItemCollection->new(
        class => "RPS::DB::Item::ArtistContract",
        query => $sql,
        dbID  => $gConfig->{_db},
    );

    return $collection;
}

sub Search {
    my ($searchTerm) = @_;

    my $where;
    if ($searchTerm) {
        $where = Common::DB::Item->SearchSyntax( field => "artist_payee.name", value => $searchTerm ) . " OR "
          . Common::DB::Item->SearchSyntax( field => "artist_contract.title", value => $searchTerm );
    } else {
        die "no valid param passed to artist_contract search";
    }

    my $order = "artist_contract.title, artist_payee.name";

    my $sql =
        "SELECT artist_contract.* FROM artist_contract"
      . " LEFT JOIN artist_payee ON artist_payee.artist_payee_id=artist_contract.artist_payee_id"
      . " WHERE $where"
      . " ORDER BY $order";

    my $collection = Common::DB::ItemCollection->new(
        class => "RPS::DB::Item::ArtistContract",
        query => $sql,
        dbID  => $gConfig->{_db},
    );

    return $collection;
}

sub GetByPayorID {
    my ($payorID) = @_;

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

    my $sql = "SELECT * FROM " . kTable . " WHERE payor_id = " . $dbo->DBQuote($payorID);

    my $collection = Common::DB::ItemCollection->new(
        class => "RPS::DB::Item::ArtistContract",
        query => $sql,
        dbID  => $gConfig->{_db},
    );

    return $collection;
}

1;
