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

use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util qw(todayPretty);


use lib '/app/tools/rps/lib';
use Common::FormObject;
use RPS::Product::Product;
use RPS::Product::ProductWithDigital;
use RPS::Product::StatusList;
use RPS::Product::TypeList;
use RPS::Catalog::Album;
use RPS::Catalog::AlbumSimple;
use RPS::LabelList;
use RPS::ServiceList;
use RPS::Price::PriceList;
use RPS::Price::PriceLevelList;
use RPS::DB::Item::PriceLevel;

use RPS::RoyaltyRun::RoyaltyRunStatusList;



use base 'Common::FormObject';

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

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

    # JPK -- Album id is required.
    # I _could_ derive it from productID, but productID is optional...
    # This form is used both for creating new products and editing existing ones.
    #
	# -jff- I moved the assert(albumID) inside the else because that's
	# when you really need it. I like to be able to call this page
	# with just a productID.
	#
    my $albumID = $args{albumID};

	if ($args{productID})
	{
		$self->{Product} = RPS::Product::ProductWithDigital->new(productID => $args{productID});
		$self->{Album} = RPS::Catalog::AlbumSimple->new(albumID => $self->{Product}->AssetID(), loadSubs => 0);
		$albumID = $self->{Product}->AssetID();
	}
	else
	{
    	assert($albumID);
		$self->{Album} = RPS::Catalog::AlbumSimple->new(albumID => $albumID, loadSubs => 0);
		$self->{Product} = RPS::Product::ProductWithDigital->new(albumID => $albumID, catalogNumber => $self->{Album}->CatalogNumber());
	}

    # JPK = This is going to be a problem.
    # There are a shit-ton of prices.  Takes a LONG LONG time for this to execute.
    #
    # This will kill any demo.  And it's not like we need the edit page to work.
    # Or, just make this not totally suck.
    #
    # !!! It might just suck right now because we are pounding the hell out of the database as we import their catalog...
    # !!! Actually, it was this silly 'InUse' test, that was expensive and did not scale well at all.
    #
	$self->{PriceList} = RPS::Price::PriceList->new();

	$self->{PriceLevelList} = RPS::Price::PriceLevelList->new();
	$self->{StatusList} = RPS::Product::StatusList->new();
	$self->{TypeList} = RPS::Product::TypeList->new(albumID => $albumID, productTypeID => $self->{Product}->ProductTypeID());
	$self->{LabelList} = RPS::LabelList->new();
	$self->{ServiceList} = RPS::ServiceList->new();

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

    # Output today's date, for the convenience of the template.
    #
    $self->{TodaysDate} = todayPretty();

	return $self;
}

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

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

sub validate
{
	my ($self) = shift;

	# JPK - This is one of those situations where I _don't_ want to
	# validate everything this object contains.  It's a waste of time
	# to validate the <Album>, for example, since we don't support making
	# changes to it.
	#
	my $valid = $self->{Product}->validate( @_ );
	return $valid;
}

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

	return $self->{Product}->ProductID();
}


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

	my $userID = Common::RSApp::GetActiveUserID();


	# I need a way to simply save the product(s) that have been edited.
	#
	# !!! I may be able to rely on the DB::Item objects keeping track of
	#     their own dirtiness, and simply iterate over everything.
	#
	$self->{Product}->save();
}



###
1;#
###
