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

use strict;
use warnings;
use Carp;

use lib '/app/tools/rps/lib';
use RPS::DistributorContract::TermHash;
use RPS::DistributorPayee::DistributorPayee;

use RPS::DB::Item::DistributorContract;

use Common::FormObject;
use base 'Common::FormObject';


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

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

    my %properties;
    if ($args{_dbItem})
    {
        $self->_propertiesFromDBItem(\%properties, $args{_dbItem});
    }
    elsif ($args{distributorContractID})
    {
        $self->_propertiesFromDB(\%properties, $args{distributorContractID});
    }
    else
    {
        $self->_propertiesFromDefaults(\%properties);
    }

    $self->_initProperties(\%properties);
    $self->_initSubs(\%properties);

    return $self;
}


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

    my $dbItem = RPS::DB::Item::DistributorContract->Lookup(distributor_contract_id => $id);
    $self->_propertiesFromDBItem($hrProperties, $dbItem);
}


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

    $hrProperties->{distributor_contract_id} = $dbItem->distributor_contract_id;
    $hrProperties->{title} = $dbItem->title;
    $hrProperties->{distributor_payee_id} = $dbItem->distributor_payee_id;
    $hrProperties->{payor_id} = $dbItem->payor_id;
    $hrProperties->{client_contract_id} = $dbItem->client_contract_id;
    $hrProperties->{issue_date} = $dbItem->issue_date;

    $hrProperties->{date_created} = $dbItem->date_created;
    $hrProperties->{date_modified} = $dbItem->date_modified;
    $hrProperties->{created_by} = $dbItem->created_by;
    $hrProperties->{modified_by} = $dbItem->modified_by;

}


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

    $self->{DistributorContractID}	= Common::FormObject::Scalar->new(value => $props->{distributor_contract_id}, readOnly => 1);
    $self->{Title}				= Common::FormObject::Scalar::String->new(value => $props->{title}, required => 1);
    $self->{DistributorPayeeID}		= Common::FormObject::Scalar->new(value => $props->{distributor_payee_id}, required => 1);
    $self->{PayorID}		    = Common::FormObject::Scalar->new(value => $props->{payor_id}, required => 1);
    $self->{ClientContractID}	= Common::FormObject::Scalar::String->new(value => $props->{client_contract_id});
    $self->{IssueDate}			= Common::FormObject::Scalar::Date->new(value => $props->{issue_date});

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

    # !!! jpk - I don't agree with this at all - this stuff really should be in load subs.
    # There _is_ a use for a contract object without all this extra state.
    # But... I'll leave it alone for now.
    #

	# -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->{DistributorPayee} = RPS::DistributorPayee::DistributorPayee->new(distributorPayeeID => $props->{distributor_payee_id}, loadSubs => 0, readOnly => 1);

}


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

	if($self->loadSubs())
	{
        my $distributorContractID = $props->{distributor_contract_id};

        $self->{TermList} = RPS::DistributorContract::TermHash->new(distributorContractID => $distributorContractID);

	}
}


sub _propertiesFromDefaults
{
    my ($self, $hrProperties) = @_;

    my $defaultPayor = RPS::DB::Item::Payor->GetDefault();
    if ($defaultPayor)
    {
        $hrProperties->{payor_id} = $defaultPayor->payor_id;
    }
    # JPK - Don't have any canned defaults (yet)
    #
}


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

	my $dbObj;
	if($self->DistributorContractID())
	{
		$dbObj = RPS::DB::Item::DistributorContract->Lookup(distributor_contract_id => $self->DistributorContractID());
	}
	else
	{
		$dbObj = RPS::DB::Item::DistributorContract->Create();
	}

	# set fields here
	#
	$dbObj->distributor_payee_id($self->DistributorPayeeID());
	$dbObj->client_contract_id($self->ClientContractID());
	$dbObj->title($self->Title());
	$dbObj->issue_date($self->IssueDate());
	$dbObj->payor_id($self->PayorID());

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

	# save the term list
	#
	
	print STDERR "looking for terms...\n";
	
	foreach my $termKey (keys %{$self->{TermList}->getHash()})
	{
		print STDERR "found a term!\n";
		
        my $term = $self->{TermList}->getHash()->{$termKey};
		if(defined $term && $term->DistributionFee())
		{
			$term->DistributorContractID($dbObj->distributor_contract_id);
		}
	}
#	foreach my $term (@{$self->{TermList}->getList()})
#	{
#		if(defined $term && $term->Rate())
#		{
#			$term->ArtistContractID($dbObj->artist_contract_id);
#		}
#	}
  $self->{TermList}->save();

	$self->_init(_dbItem => $dbObj);
}

sub delete
{
	my $self = shift;

	# TODO: we'll have to delete all the terms, reserve, escalation, attached albums, tracks, etc...
	#
	# my $myDBObj = RPS::DB::Item::NewArtistContract->Lookup(artist_contract_id => $self->ArtistContractID());
	# $myDBObj->delete();

	return 1;
}


###
1;#
###

