
#!/usr/bin/perl
use strict;
use Data::Show;

use lib '/home/sites/clm.dmadelivers.com/www/clmapp/lib';
use CLMApp::Schema;
my $dbix = CLMApp::Schema->connect("dbi:mysql:host=rds1.dmadelivers.com;db=dmaclm", 'dmaclm', '3lUz1OV2!',{unsafe=>1,RaiseError=>0,PrintError=>1});
$ENV{DBIC_TRACE} = 1;

# sync database from live - temporary as we cannot do this once old fields are deprecated

# delete existing CustomerSystem entries delete from CustomerSystem;


&unassign;
&reassign;
&default;
# &remove;


# find where there is bad assignment

sub reassign{

	my @oisystems = $dbix->resultset('OrderInvoiceSystem')->search({ 
		# deleted => 0,
	},{result_class=>'DBIx::Class::ResultClass::HashRefInflator',});

	my $oisystems = {};
	foreach my $ois (@oisystems){
		$oisystems->{$ois->{id}} = $ois;
# show $ois;		
		$ois->{systemfiles} = $ois->{systemfiles} =~ /^\[/ ? $ois->{systemfiles} : '[]';
		$dbix->resultset('CustomerSystem')->search({systemid =>  $ois->{id}},{result_class=>'DBIx::Class::ResultClass::HashRefInflator',})->update({systemfiles => $ois->{systemfiles}});
		
	}


	
}


sub remove{

	# remove invoicesystems
	my @cs = $dbix->resultset('Customer')->search({ 
		statusid => 1,
	},{
		prefetch => [{'customersystem' => [qw/invoicesystems ordersystems market/] },'status','distributor','concept','chaingrp','chainunit','user','warehouse','orderflag','market','ordersystems','invoicesystems',{'back_office_system_customers' => 'system'}],			
		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	});


	foreach my $cs (@cs){
		if(!$cs->{invoicesystems}){
			$dbix->resultset('CustomerSystem')->search({ customerid => $cs->{customerid}, systemtype => 1 })->update({deleted => 1});
		}



	}


}

sub unassign{

	my @oisystems = $dbix->resultset('OrderInvoiceSystem')->search({ 
		# deleted => 0,
	},{

		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	});
	my $oisystems = {};
	my @invoicesystems;
	my @ordersystems;
	foreach my $ois (@oisystems){
		$oisystems->{$ois->{id}} = $ois;
		push(@invoicesystems, $ois->{id}) if $ois->{invoicesystem} == 1;
		push(@ordersystems, $ois->{id}) if $ois->{ordersystem} == 1;
	}

print join(",",@invoicesystems) . "\n";
print join(",",@ordersystems) . "\n";
	# customers where invoice system is an order system
	my @ics = $dbix->resultset('Customer')->search({ 
		statusid => 1,
		# invoicesystem => {'IN' => \@ordersystems}
		-and =>[
			{invoicesystem => {'NOT IN' => \@invoicesystems}},
			{invoicesystem => {'>' => 0}}
		],
	},{
		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	})->update({invoicesystem => 0});


	# customers where invoice system is an order system
	my @ocs = $dbix->resultset('Customer')->search({ 
		statusid => 1,
		# invoicesystem => {'IN' => \@ordersystems}
		-and =>[
			{ordersystem => {'NOT IN' => \@ordersystems}},
			{ordersystem => {'>' => 0}}
		],
		
	},{
		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	})->update({ordersystem => 0});




show scalar(@ics), scalar(@ocs);
	# for(my $x = 0; $x < 10; $x++){
	# 	show $ocs[$x];
	# }
}



sub default{

	my @oisystems = $dbix->resultset('OrderInvoiceSystem')->search({ 
		# deleted => 0,
	},{

		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	});
	my $oisystems = {};
	foreach my $ois (@oisystems){
		$oisystems->{$ois->{id}} = $ois;
	}


	my @markets = $dbix->resultset('Market')->search({ 
	},{
		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	});
	my $markets = {};
	foreach my $mk (@markets){
		$markets->{$mk->{marketid}} = $mk;
	}


	my @cs = $dbix->resultset('Customer')->search({ 
		statusid => 1,
		# customerid => 1046585
	},{

		result_class=>'DBIx::Class::ResultClass::HashRefInflator',
	});
# show \@cs;
	print join("\t",/CustomerId CustomerNumber MarketId InvoiceSystem OrderSystem AccountingCode FileCode/) . "\n";
	foreach my $c (@cs){

		my $marketid = $c->{marketid};
		my $invoicesystem = $c->{invoicesystem};
		my $ordersystem = $c->{ordersystem};
		my $accountingcode = $c->{accountingcode};
		my $filecode = $c->{filecode};


		my $orderobj = {
			customerid => $c->{customerid},
			systemtype => 0,
			systemid => $ordersystem,
			systemfiles => $oisystems->{ $ordersystem }->{systemfiles},
			marketid => $marketid,
			accountingcode => $accountingcode,
			filecode => $filecode,
			active => 1,
		};

		my $invoiceobj = {
			customerid => $c->{customerid},
			systemtype => 1,
			systemid => $invoicesystem,
			systemfiles => $oisystems->{ $invoicesystem }->{systemfiles},
			marketid => $marketid,
			accountingcode => $accountingcode,
			filecode => $filecode,
			active => 1,
		};

		
		# print join("\t",$c->{id},$c->{customernumber},($marketid ? 1 : 0),($invoicesystem ? 1 : 0),($ordersystem ? 1 : 0),($accountingcode ? 1 : 0),($filecode ? 1 : 0)) . "\n";
		print join("\t",$c->{customerid},$c->{customernumber},$markets->{$marketid}->{customwarehousenumber},$oisystems->{$invoicesystem}->{systemname},$oisystems->{$ordersystem}->{systemname},$accountingcode,$filecode) . "\n";
		
		if($ordersystem){
			my $count = $dbix->resultset('CustomerSystem')->search($orderobj)->count;
			if(!$count){
				$dbix->resultset('CustomerSystem')->create($orderobj);		
			}
			
		}
		if($invoicesystem){
			my $count = $dbix->resultset('CustomerSystem')->search($invoiceobj)->count;
			if(!$count){
				$dbix->resultset('CustomerSystem')->create($invoiceobj);	
			}
		}
		
		

	}




	
}


1;