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

use strict;
use warnings;

use Data::Dumper;

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


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

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

    $self->{Account} = [];

	if($args{payorID} && $args{publisherID})
	{
		$self->{_payorID} = $args{payorID};
		$self->{_publisherID} = $args{publisherID};

		my $collection = $self->_publisherAccountDB()->GetByPayorPublisherID($args{payorID}, $args{publisherID});
		
		my $currentAccountID = 0;
		
		if ($args{currentAccountID})
		{
		    $currentAccountID = $args{currentAccountID};
		}		

		while (my $obj = $collection->next())
		{    
		    
		    # We have to add in the current account elsewhere as it may not exist yet.
		    #
		    if ($obj->finance_account_id != $currentAccountID)
		    {
                push @{$self->{Account}}, $self->_publisherAccount()->new(_dbItem => $obj);

                # We need to know the publisher names...
                #
                my $pubIDName = $self->_publisherID();
                my $accountPublisher = $self->_publisherObj()->new(
                    publisherID => $obj->$pubIDName,
                    loadSubs => 0,
                    readOnly => 1,
                );
                push @{$self->{PublisherList}{Publisher}}, $accountPublisher;                  
                
            }          
		}
	}

	return $self;
}


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

	my $valid = 1;
	foreach my $account (@{$self->{Account}})
	{
		next if(!defined $account);

        if (! $account->validate())
        {
            $valid = 0;
        }
	}

	return $valid;
}


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

	foreach my $account (@{$self->{Account}})
	{
		#if(defined $account && $account->{_hasData})
		if(defined $account)
		{
			$account->save();
		}
	}
}

sub _listProperties
{
    return
    {
        Account => 1
    };
}

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

	if ($propertyName eq 'Account')
	{
		return 1;
	}
	return 0;
}

# This is the special sort of list accessor.
#
sub Account
{
    my ($self, $index, $methodAddressList, $newValue) = @_;

    return $self->{Account}[$index]->_accessProperty($methodAddressList, $newValue);
}


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

	return $self->{_payorID};
}

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

	return $self->{_publisherID};
}

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

	return $self->{Account};
}


###
1;#
###

