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

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

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::McpsLicense;
use RPS::DB::Item::Product;
use RPS::DB::Item::McpsLicenseRun;
use RPS::Mechanical::UK::McpsLicenseRetentionList;

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{productID})
    {
        $self->_propertiesFromDB(\%properties, $args{productID});
    }
	else
	{
		$self->_propertiesFromDefaults(\%properties);
	}


	$self->_initProperties(\%properties);

    return $self;
}


sub _propertiesFromDB
{
    my ($self, $hrProperties, $productID) = @_;
    
    my $dbItem = RPS::DB::Item::McpsLicense->Lookup(product_id => $productID);
	$self->_propertiesFromDBItem($hrProperties, $dbItem);
}

# this gets called after we update/insert so
# we can refresh our properties
sub _setPropertiesFromDBItem
{
	my ($self, $dbItem) = @_;

	my %properties;
	$self->_propertiesFromDBItem(\%properties, $dbItem);
	$self->_initProperties(\%properties);
}

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

    if ($dbItem) 
    {
        
    	$hrProperties->{mcps_license_id}			        = $dbItem->mcps_license_id,
    	$hrProperties->{product_id}					        = $dbItem->product_id,
    	$hrProperties->{mcps_id}					        = $dbItem->mcps_id,
    	$hrProperties->{payor_id}				            = $dbItem->payor_id,
    	$hrProperties->{retention_category}	                = $dbItem->retention_category,
    	$hrProperties->{max_retention_periods}				= $dbItem->max_retention_periods,
    	$hrProperties->{initial_periods_completed}		    = $dbItem->initial_periods_completed,
    	$hrProperties->{dvd_category}				        = $dbItem->dvd_category,
        $hrProperties->{date_created}           	        = $dbItem->date_created;
        $hrProperties->{date_modified}          	        = $dbItem->date_modified;    	
    }

}


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


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

    return $self->McpsLicenseID();
}


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



    $self->{McpsLicenseID}			        = Common::FormObject::Scalar->new(value => $props->{mcps_license_id}, readOnly => 1);
    $self->{ProductID}				        = Common::FormObject::Scalar->new(value => $props->{product_id});
    $self->{McpsID}					        = Common::FormObject::Scalar::String->new(value => $props->{mcps_id});
    $self->{PayorID}	                    = Common::FormObject::Scalar->new(value => $props->{payor_id}); 
    $self->{RetentionCategory}              = Common::FormObject::Scalar->new(value => $props->{retention_category});
    $self->{MaxRetentionPeriods}	        = Common::FormObject::Scalar->new(value => $props->{max_retention_periods});
    $self->{InitialPeriodsCompleted}	    = Common::FormObject::Scalar->new(value => $props->{initial_periods_completed});
    $self->{DvdCategory}	                = Common::FormObject::Scalar->new(value => $props->{dvd_category});
    $self->{DateCreated}        	= Common::FormObject::Scalar::DateTime->new(value => $props->{date_created}, readOnly => 1);
    $self->{DateModified}       	= Common::FormObject::Scalar::DateTime->new(value => $props->{date_modified}, readOnly => 1);    
    if ($props->{mcps_license_id})
    {
        my $numCommittedRuns = RPS::DB::Item::McpsLicenseRun->NumCommittedRuns($props->{mcps_license_id});
	    $self->{Committed} = ($numCommittedRuns > 0 ? 1 : 0);
        $self->{PeriodsCompleted}	        = $props->{initial_periods_completed} + $numCommittedRuns;	    
	}

    # This is an editable section of the mcps license, so it
	# needs to be loaded here.
    #
    $self->{McpsLicenseRetentionList} = RPS::Mechanical::UK::McpsLicenseRetentionList->new
    (
        mcps_license_id => $props->{mcps_license_id},
        product_id => $props->{product_id}
    );

}



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

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

    return $valid if ($skipRequiredFields);

	return $valid;
}



sub _setDBValues
{
	my ($self, $dbItem) = @_;
	
	$dbItem->mcps_license_id($self->McpsLicenseID());
	$dbItem->product_id($self->ProductID());
	$dbItem->mcps_id($self->McpsID());
    $dbItem->payor_id($self->PayorID());
    $dbItem->retention_category($self->RetentionCategory());
    $dbItem->dvd_category($self->DvdCategory());
    
    # We don't want to allow this to be changed if the license has been included in a committed run.
    my $numCommittedRuns = 0;
    
    if ($self->McpsLicenseID())
    {
        $numCommittedRuns = RPS::DB::Item::McpsLicenseRun->NumCommittedRuns($self->McpsLicenseID());
    }    
        
    if ($numCommittedRuns == 0)
    {   
        $dbItem->initial_periods_completed($self->InitialPeriodsCompleted());
    }
}

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

	my $dbItem;
	if ($self->McpsLicenseID())
	{
		$dbItem = RPS::DB::Item::McpsLicense->Lookup(mcps_license_id => $self->McpsLicenseID());
	}
	else
	{
		$dbItem = RPS::DB::Item::McpsLicense->Create();
	}	

	return $dbItem
}

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

	my $dbItem = $self->_initSave();

	$self->_setDBValues($dbItem);

	$dbItem->save();


    # We don't want to allow these to be changed if the license has been included in a committed run.
    my $numCommittedRuns = 0;
    
    if ($self->McpsLicenseID())
    {
        $numCommittedRuns = RPS::DB::Item::McpsLicenseRun->NumCommittedRuns($self->McpsLicenseID());
    }
    
    if ($numCommittedRuns == 0)
    {   
        # Now save the historical retention list.
        #
        foreach my $retention (@{$self->{McpsLicenseRetentionList}->getList()})
        {
            
            if(defined $retention && $retention->UnitsHeld())
            {
                # assign from the newly saved dbObj
                $retention->McpsLicenseID($dbItem->mcps_license_id);
                $retention->RetentionCategory($dbItem->retention_category);
            }
        }
        $self->{McpsLicenseRetentionList}->save();
    }
    
    $self->_setPropertiesFromDBItem($dbItem);
    
    #$self->_init(_dbItem => $dbObj);

}




###
1;#
###
