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

use Carp;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::ControlledComposition::ControlledComposition;
use RPS::Payee::ArtistPayee;
use RPS::DB::Item::ArtistContract;
use RPS::DB::Item::Artist;

use RPS::XMLObject;
use base 'RPS::XMLObject';


sub _init
{
    my ($self, %args) = @_;

    $self->SUPER::_init(%args);

    if ($args{_dbItem})
    {   
        return $self->_initFromDBItem($args{_dbItem});
    }
    elsif ($args{artistContractID})
    {   
        return $self->_initFromDB($args{artistContractID});
    }
    else
    {   
        return $self->_initWithDefaults();
    }
}


sub _initFromDB
{   
    my ($self, $id) = @_;

    my $dbItem = RPS::DB::Item::ArtistContract->Lookup(artist_contract_id => $id);
    return $self->_initFromDBItem($dbItem);
}


sub _initFromDBItem
{
	my ($self, $dbItem) = @_;

	return $self if(! $dbItem);

	$self->_initProperties($dbItem);

	if($self->loadSubs())
	{
		$self->_initSubs($dbItem);
	}
	
	return $self;
}


sub _initProperties
{
    my ($self, $dbItem) = @_;

    $self->{ArtistContractID}	= RPS::XMLObject::Scalar::Integer->new(value => $dbItem->artist_contract_id, readOnly => 1);
    $self->{ArtistPayeeID}		= RPS::XMLObject::Scalar::Integer->new(value => $dbItem->artist_payee_id);
    $self->{ClientContractID}	= RPS::XMLObject::Scalar::String->new(value => $dbItem->client_contract_id);
    $self->{Title}				= RPS::XMLObject::Scalar::String->new(value => $dbItem->title);
    $self->{Notes}				= RPS::XMLObject::Scalar::String->new(value => $dbItem->notes);
    $self->{IssueDate}			= RPS::XMLObject::Scalar::Date->new(value => $dbItem->issue_date);
    $self->{TermStart}			= RPS::XMLObject::Scalar::Date->new(value => $dbItem->term_start);
    $self->{TermEnd}			= RPS::XMLObject::Scalar::Date->new(value => $dbItem->term_end);
    $self->{Status}				= RPS::XMLObject::Scalar::Integer->new(value => $dbItem->status);

    $self->{CreatedDate}		= RPS::XMLObject::Scalar::DateTime->new(value => $dbItem->date_created, readOnly => 1);
    $self->{ModifiedDate}		= RPS::XMLObject::Scalar::DateTime->new(value => $dbItem->date_modified, readOnly => 1);
    $self->{CreatedBy}			= RPS::XMLObject::Scalar::UserName->new(value => $dbItem->created_by, readOnly => 1);
    $self->{ModifiedBy}			= RPS::XMLObject::Scalar::UserName->new(value => $dbItem->modified_by, readOnly => 1);

	# -jff- the artist contract by itself is fairly useless,
	# so I feel like ArtistPayee is a pretty integral part of this object.
	# It was in the _initSubs, but I moved it here.
	#
	$self->{ArtistPayee} = RPS::Payee::ArtistPayee->new(artistPayeeID => $dbItem->artist_payee_id, loadSubs => 0);

	# We need to fetch the artist name, given the artist id.
	#
	my $artistName;
	if ($dbItem->artist_id)
	{
		my $artistDBItem = RPS::DB::Item::Artist->Lookup(artist_id => $dbItem->artist_id);
		$artistName = $artistDBItem->name;
	}
	$self->{ArtistName} = RPS::XMLObject::Scalar::String->new(value => $artistName);
	$self->{ArtistID} = RPS::XMLObject::Scalar::Integer->new(value => $dbItem->artist_id);

    return $self;
}


sub _initSubs
{
    my ($self, $dbItem) = @_;


	if($self->loadSubs())
	{
        # Load all the contract clauses.
        #
        $self->{ControlledComposition} = RPS::ControlledComposition::ControlledComposition->new(contractID => $dbItem->artist_contract_id);
	}

    return $self;
}


sub _initWithDefaults
{
    my ($self) = @_;

    $self->{ArtistContractID}	= RPS::XMLObject::Scalar::Integer->new(readOnly => 1);
    $self->{ArtistID}			= RPS::XMLObject::Scalar::Integer->new();
    $self->{ArtistPayeeID}		= RPS::XMLObject::Scalar::Integer->new();
    $self->{ClientContractID}	= RPS::XMLObject::Scalar::String->new();
    $self->{Title}				= RPS::XMLObject::Scalar::String->new();
    $self->{Notes}				= RPS::XMLObject::Scalar::String->new();
    $self->{IssueDate}			= RPS::XMLObject::Scalar::Date->new();
    $self->{TermStart}			= RPS::XMLObject::Scalar::Date->new();
    $self->{TermEnd}			= RPS::XMLObject::Scalar::Date->new();
    $self->{Status}				= RPS::XMLObject::Scalar::Integer->new();

    $self->{CreatedDate}		= RPS::XMLObject::Scalar::DateTime->new(readOnly => 1);
    $self->{ModifiedDate}		= RPS::XMLObject::Scalar::DateTime->new(readOnly => 1);
    $self->{CreatedBy}			= RPS::XMLObject::Scalar::UserName->new(readOnly => 1);
    $self->{ModifiedBy}			= RPS::XMLObject::Scalar::UserName->new(readOnly => 1);

	$self->{ArtistPayee}		= RPS::Payee::ArtistPayee->new();
	$self->{ArtistName}			= RPS::XMLObject::Scalar::String->new();

	if($self->loadSubs())
	{
        $self->{ControlledComposition} = RPS::ControlledComposition::ControlledComposition->new();
	}

    return $self;
}


sub validate
{
    my ($self) = @_;

	# my $valid = $self->SUPER::validate();
	my $valid = 1;

	if(!$self->ArtistPayeeID())
	{
		$self->{ArtistPayeeID}->setError(RPS::XMLObject::kErrFieldBlank);
		$valid = 0;
	}

	if ($self->ArtistName())
	{
		# check for duplicate artist name:
		#   we're not actually going to flag an error here,
		#   just use the existing artist_id if it exists.
		#
		if(! $self->ArtistID)
		{
			my $tmpArtist = RPS::DB::Item::Artist->Lookup(name => $self->ArtistName());
			if(defined $tmpArtist && lc($tmpArtist->name) eq lc($self->ArtistName()))
			{
				$self->ArtistID($tmpArtist->artist_id);
			}
		}
	}

    return $valid;
}

sub save
{
    my ($self) = @_;

	# -------------------------------
	# Save or create the artist
	# -------------------------------
	#
	my $artistObj;
	if($self->ArtistID())
	{
		$artistObj = RPS::DB::Item::Artist->Lookup(artist_id => $self->ArtistID());

		# changing the artist? see if it's a different artist
		if($self->ArtistName())
		{
			if(lc($artistObj->name) ne lc($self->ArtistName()))
			{
				$artistObj = RPS::DB::Item::Artist->Create(name => $self->ArtistName());
				$artistObj->save();
			}
		}
	}
	elsif($self->ArtistName())
	{
		# Create a new artist.
		$artistObj = RPS::DB::Item::Artist->Create(name => $self->ArtistName());
		$artistObj->save();
	}

	my $dbObj;
	if($self->ArtistContractID())
	{
		$dbObj = RPS::DB::Item::ArtistContract->Lookup(artist_contract_id => $self->ArtistContractID());
	}
	else
	{
		$dbObj = RPS::DB::Item::ArtistContract->Create();
	}

	# set fields here
	#
	$dbObj->artist_id($artistObj->artist_id);
	$dbObj->artist_payee_id($self->ArtistPayeeID());
	$dbObj->client_contract_id($self->ClientContractID());
	$dbObj->title($self->Title());
	$dbObj->notes($self->Notes());
	$dbObj->issue_date($self->IssueDate());
	$dbObj->term_start($self->TermStart());
	$dbObj->term_end($self->TermEnd());
	$dbObj->status($self->Status());

	# save db item
	#
	$dbObj->save();

	# reload object
	#
	$self->_initProperties($dbObj);
}

sub delete
{
	my $self = shift;

	my $myDBObj = RPS::DB::Item::ArtistContract->Lookup(artist_contract_id => $self->ArtistContractID());
	$myDBObj->delete();

	return 1;
}


###
1;#
###



