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

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::LabelContract::LabelContract;
use RPS::LabelList;
use RPS::Payor::PayorList;
use RPS::RoyaltyRun::RoyaltyRunStatusList;
use RPS::CountryList;
use RPS::ServiceList;
use RPS::DB::Item::LabelContract;
use RPS::DB::Item::LabelContractGroupTerm;
use RPS::DB::Item::LabelContractGroupTermLabel;

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

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

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

	if($args{labelContractGroupTermID})
	{
		$self->{GroupTerm}
			= RPS::LabelContract::GroupTerm->new(
				labelContractGroupTermID => $args{labelContractGroupTermID}
			);

	}
	elsif($args{labelContractID})
	{
		$self->{GroupTerm}
			= RPS::LabelContract::GroupTerm->new(
				labelContractID => $args{labelContractID}
			);

	}	
	else
	{
		die "Unable to create group term";
	}

	$self->{LabelList} = RPS::LabelList->new();
	$self->{CountryList} = RPS::CountryList->new();
	$self->{ServiceList} = RPS::ServiceList->new(fileOnly => 1);
	

	$self->{RoyaltyRunStatus} = new RPS::RoyaltyRun::RoyaltyRunStatusList( label => 1 );

	return $self;
}

sub _propertyIsEditable
{
	my ($self, $propertyName) = @_;

	# The Product (and contained objects) can be edited.
	#
	# if ($propertyName eq 'Album')
	# {
		# return 1;
	# }
	if ($propertyName eq 'GroupTerm')
	{
		return 1;
	}
	return 0;
}

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

    my $valid = $self->{GroupTerm}->validate();

    # We need to make sure that the labels for this term aren't already associated with the payee
    # on this contract or another contract.	  
    my $term = $self->{GroupTerm};
       
    my $labelContract = RPS::DB::Item::LabelContract->Lookup(label_contract_id => $term->LabelContractID);
    my $labelPayeeID = $labelContract->label_payee_id;  
    my %labelsSeen;        
    
    # Let's prefill this with the labels from the _other_ terms on this contract.
    my $groupTerms = RPS::DB::Item::LabelContractGroupTerm->GetByLabelContractID($term->LabelContractID);
    while (my $groupTerm = $groupTerms->next()) {
        if ($groupTerm->label_contract_group_term_id != $term->LabelContractGroupTermID) {
            my $groupTermLabels = RPS::DB::Item::LabelContractGroupTermLabel->GetByLabelContractGroupTermID($groupTerm->label_contract_group_term_id);   
            while (my $groupTermLabel = $groupTermLabels->next()) {    
                $labelsSeen{$groupTermLabel->label_id} = 1;
            }
        }
    }    
    
	my $termLabelList = $term->GroupTermLabelList();
    foreach my $termLabelID (keys %{$termLabelList->{GroupTermLabel}})
    {
        my $termLabel = $termLabelList->{GroupTermLabel}{$termLabelID};
		next if (!defined $termLabel);		

        # We need to make sure that this label is not already associated
        # with the same payee through a different contract.
        if ($termLabel->LabelID() && ! $termLabel->Delete())
        {            
            # We also want to check for dupes on the same contract.
            #
            if ($labelsSeen{$termLabel->LabelID()})
            {
                $termLabel->{LabelID}->setError(Common::FormObject::kErrFieldDuplicate);
                $valid = 0;
            } 
            else
            {
                $labelsSeen{$termLabel->LabelID()} = 1;
            }        
            
            my $contractTerms = RPS::DB::Item::LabelContractGroupTermLabel->GetByLabelID($termLabel->LabelID());
            while (my $contractTerm = $contractTerms->next())
            {   
                my $otherGroupTerm = RPS::DB::Item::LabelContractGroupTerm->Lookup(label_contract_group_term_id => $contractTerm->label_contract_group_term_id);
                if ($otherGroupTerm->label_contract_id != $term->LabelContractID())
                {    
                    my $otherContract = RPS::DB::Item::LabelContract->Lookup(label_contract_id => $otherGroupTerm->label_contract_id);
                    if ($otherContract->label_payee_id == $labelPayeeID)
                    {
                        $termLabel->{LabelID}->setError(Common::FormObject::kErrFieldInvalid);
                        $valid = 0;
                    }
                }
            }                                                 
        }		
    }
    
    if (!$term->DistributionFee)
    {
        $term->{DistributionFee}->setError(Common::FormObject::kErrFieldBlank);
        $valid = 0;
    }
    		
	return $valid;
}

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

	$self->{GroupTerm}->save();
}

sub delete
{
	my ($self) = @_;
	my $term = $self->{GroupTerm};
	
    # Let's make sure this isn't the only term attached to the contract.
    my $groupTermCount = 0;
    my $groupTerms = RPS::DB::Item::LabelContractGroupTerm->GetByLabelContractID($term->LabelContractID);
    while (my $groupTerm = $groupTerms->next()) {	
        $groupTermCount++;
    }
    
    if ($groupTermCount > 1) {
	    $self->{GroupTerm}->delete();
    } else {
        return undef;   
    }
}

sub labelContractGroupTermID
{
	my ($self) = @_;
	return $self->{GroupTerm}->LabelContractGroupTermID();
}


###
1;#
###
