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

use strict;
use warnings;
use Data::Dumper;
use Carp;
use Date::Calc qw( Date_to_Days Decode_Date_US );

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util qw(decodeDateMysql);
use Common::Log;

use lib '/app/tools/rps/lib';
use RPS::License::BaseLicense;
use RPS::DB::Item::ReserveLiquidation;
use RPS::DB::Item::RegionCountryMap;
use RPS::DB::Item::Product;
use RPS::ControlledComposition::ControlledComposition;

use base 'RPS::License::BaseLicense';


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


    $self->SUPER::_propertiesFromDefaults($hrProperties, %args);

	my $ccID = $args{controlledCompositionID};
    $hrProperties->{type} = $self->_type();
    $hrProperties->{controlled_composition_id} = $ccID;
}


# It looks like controlled comp licenses are 'forced' to be in a particular country.
# So we need an abstract method to determne that that is.
#
sub _homeCountryCode
{
    my ($self) = @_;

    # override this to return the 'home' country code.
    # i.e. 'US' for US controlled comps
    assert(0, "ERROR - you must override _homeCountryCode");
}


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


    # The region id will be forced to be the 'home country' - only region.
    # Which means that there _must_ be a 'home country' only region...
    #
    my $homeOnlyRegionID;
    my $regionsWithHome = RPS::DB::Item::RegionCountryMap->GetByCountryCode($self->_homeCountryCode());
    while (my $region = $regionsWithHome->next())
    {
        my $countries = RPS::DB::Item::RegionCountryMap->GetByRegionID($region->region_id);
        if (1 == $countries->size())
        {
            $homeOnlyRegionID = $region->region_id;
            last;
        }
    }

    if ($homeOnlyRegionID)
    {
        $props->{region_id} = $homeOnlyRegionID;
    }
    else
    {
        $props->{_noValidHomeOnlyRegion} = 1;
    }

	$self->SUPER::_initProperties($props);

    $self->{ControlledCompositionID} = Common::FormObject::Scalar->new(value => $props->{controlled_composition_id});


    # We don't allow 0% rate percentage.  This should get caught earlier, but just in case...
    #
    my $ratePercentage = $props->{rate_percentage};
    $ratePercentage = 100 unless (defined $ratePercentage && $ratePercentage > 0);
    $self->{RatePercentage}     = Common::FormObject::Scalar::Percent->new(value => $ratePercentage);


    # Set a distinct flag if there simply isn't a valid region to use.
    #
    if ($props->{_noValidHomeOnlyRegion})
    {
        $self->{NoValidRegion} = 1;
    }
}


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

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


	return $valid;
}


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

	$self->SUPER::_setDBValues($dbItem);

	$dbItem->controlled_composition_id($self->ControlledCompositionID());
}


###
1;#
###


