package MpmasProducerOrganization;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
	return "ProducerOrganization".$_[0]->file_suffix;
}


sub prepare_special {
	my $self = $_[0];
	
	my $potab = $self->get_global("PRODUCER_ORGANIZATIONS");
	my $poservicestab = $self->get_global("PRODUCER_ORGANIZATION_SERVICES");
	
	my $poInfo = MpmasInputBlock->new(2, 10);
	$poInfo->add_row("1", "Producer organization");
	$poInfo->add_comment_row("", "Description", $potab->query([$self->argument], 2));
	$poInfo->add_row("", "ID of producer organization", $self->argument() );
	
	my $poAssets = MpmasInputBlock->new(2, 20);
	my $cA = 0;
	for my $o ($self->networkobjects) {
		
		if (defined $o->subelement_field("producer_organization", "membership") and $o->subelement_field("producer_organization", "membership") == $self->argument()) {
			$poAssets->add_row("", $o->name, $o->oid);
			$cA++;
		}
	}
	$poInfo->add_row("",  "Number of membership types", $cA);
	$poInfo->add_empty_rows(1);
	$self->add_block($poInfo);
	$poAssets->add_empty_rows(1);
	$self->add_block($poAssets);			
	
	my $poServices = MpmasInputBlock->new(2, 30);
	$poServices->add_empty_rows(1);
	$poServices->add_comment_row("2", "LP accounting information");
	$poServices->add_comment_row("", "Service", "Quantity constraint PO", "Quantity activity members", "Profit activity PO", "Quantity activity PO");
	my $cc = 0;
	
	my $mip = $self->file_of_set("MpmasProducerOrganizationMILPSet", "MpmasProducerOrganizationMILP_". $self->argument);
	for my $s ($poservicestab->query([$self->argument()],0) ) {
			$cc++;
			
			my $consQPO = $poservicestab->query([$self->argument(), $s], 4);
			print STDERR "Error: No  column 4 'PO_qconstraint' for service '$s' of PO ".$self->argument. " given in PRODUCER_ORGANIZATION_SERVICES!\n" and exit(1) unless (defined $consQPO);
			my $obj_consQPO = $mip->constraint($consQPO);
			print STDERR "Error in  in PRODUCER_ORGANIZATION_SERVICES, column 4 'PO_qconstraint' for service '$s' of PO ".$self->argument. ": No constraint '$consQPO' defined in matrix of PO\n" and exit(1) unless (defined $obj_consQPO);
			
			my $actQM = $poservicestab->query([$self->argument(), $s], 3);
			print STDERR "Error: No  column 3 'member_activity' for service '$s' of PO ".$self->argument. " given in PRODUCER_ORGANIZATION_SERVICES!\n" and exit(1) unless (defined $actQM);
			my $obj_actQM = $self->activity($actQM);
			print STDERR "Error in  in PRODUCER_ORGANIZATION_SERVICES, column 3 'member_activity'  for service '$s' of PO ".$self->argument. ": No activity '$actQM' defined in farm agent matrix\n" and exit(1) unless (defined $obj_actQM);
			
			my $actPPO = $poservicestab->query([$self->argument(), $s],6);
			print STDERR "Error: No  column 6 'PO_profit' for service '$s' of PO ".$self->argument. " given in PRODUCER_ORGANIZATION_SERVICES!\n" and exit(1) unless (defined $actPPO);
			my $obj_actPPO = $mip->activity($actPPO);
			print STDERR "Error in  in PRODUCER_ORGANIZATION_SERVICES, column 6 'PO_profit' for service '$s' of PO ".$self->argument. ": No activity '$actPPO' defined in matrix of PO\n" and exit(1) unless (defined $obj_actPPO);
			
			my $actQPO = $poservicestab->query([$self->argument(), $s], 5);
			print STDERR "Error: No  column 5 'PO_qactivity' for service '$s' of PO ".$self->argument. " given in PRODUCER_ORGANIZATION_SERVICES!\n" and exit(1) unless (defined $actQPO);
			my $obj_actQPO = $mip->activity($actQPO);
			print STDERR "Error in  in PRODUCER_ORGANIZATION_SERVICES, column 5 'PO_qactivity' for service '$s' of PO ".$self->argument. ": No activity '$actQPO' defined in matrix of PO\n" and exit(1) unless (defined $obj_actQPO);

			$poServices->add_row("",$s, $obj_consQPO->lp_row, $obj_actQM->lp_column, $obj_actPPO->lp_column, $obj_actQPO->lp_column);		
	}
	print STDERR "Error: No PO services for PO ". $self->argument . " given in PRODUCER_ORGANIZATION_SERVICES!\n" and exit(1)  unless ($cc > 0);
	$poServices->add_row_at_beginning("",	"Number of PO services to members", $cc);
	$self->add_block($poServices);								
	
	return 1;
}
1;
