#!/usr/bin/perl

# $ENV{DBIC_TRACE}=1;
use JSON;
use strict;
use Data::Show;
use lib '/home/sites/clm.dmadelivers.com/www/clmapp/lib';
use CLMApp::Schema;
use DateTime;
use Text::CSV;
use POSIX;


my $dbix = CLMApp::Schema->connect("dbi:mysql:host=rds1.dmadelivers.com;db=dmaclm", 'dmaclm', '3lUz1OV2!',{unsafe=>1,RaiseError=>0,PrintError=>1});

&default;


sub default{


		# 'me.customerid' => 10003
		my @customersystems = $dbix->resultset('CustomerSystem')->search({'me.deleted' => 0,'systems_customer.statusid' => 1},{
			prefetch => [qw/ordersystems invoicesystems systems_customer/],
		# '+select' => [
		# 		{ 'group_concat' => 'system.systemname', -as => 'systemname'  },
		# 		{ 'group_concat' => 'system.systemid', -as => 'systemid'  }
		# ], 
		# '+as' => [qw/systemname systemid/],
		# columns => $columns,
		# join => {'back_office_system_customers' => 'system'},
			result_class=>'DBIx::Class::ResultClass::HashRefInflator',
			# group_by => [qw/me.customerid/],
			order_by => [qw/me.customerid/],
	   });


# 	   foreach my $cs (@customersystems){	   		
# last;
# 	   		if(!defined($cs->{invoicesystems}) && !defined($cs->{ordersystems})){
# 	   			show $cs;
# 	   			print join("\t",$cs->{systems_customer}->{customernumber},$cs->{customerid},$cs->{customersystemid},$cs->{systemid}) . "\n";
	   			
# 	   			# $dbix->resultset('CustomerSystem')->search({ customersystemid => $cs->{customersystemid} },{})->update({active => 0, deleted => 1, updatedby => 1, updated => \'NOW()'});
# 	   		}


# 	   		# if(!$cs->{invoicesystems} && !$cs->{ordersystems}){
# 	   		# 	print $cs->{customerid} . "\n";
# 	   		# }


# 	   }

		my $custsysseen = {};
		foreach my $cs (@customersystems){
			push(@{ $custsysseen->{$cs->{customerid}}->{$cs->{systemid}}->{ $cs->{systemtype} } }, $cs->{customersystemid});
		}

# $dbix->resultset('CustomerSystem')->search({ customersystemid => 55016 },{})->update({active => 0, deleted => 1, updatedby => 1, updated => \'NOW()'});

		my @tobedeleted = ();		
	   foreach my $cs (@customersystems){
	   		# next unless $cs->{customerid} == 11175;
	   		my @custsys = sort( @{$custsysseen->{ $cs->{customerid} }->{ $cs->{systemid} }->{ $cs->{systemtype} }} );
	   		# show $custsysseen->{ $cs->{customerid} }->{ $cs->{systemid} }->{ $cs->{systemtype} };
	   		my $custsyscnt = scalar(@{$custsysseen->{ $cs->{customerid} }->{ $cs->{systemid} }->{ $cs->{systemtype} }});
#	   		show $cs if $cs->{customerid} == 11175;
	   		if(defined($cs->{invoicesystems})){
	   			if($cs->{systemfiles} ne $cs->{invoicesystems}->{systemfiles} && $custsyscnt >= 2){
	   				print join("\t",$custsyscnt,$cs->{customersystemid},$cs->{systemid},$cs->{customerid},$cs->{systemfiles},$cs->{invoicesystems}->{systemfiles}) . "\n";
	   				push(@tobedeleted, $cs->{customersystemid});
	   			}
	   		}
	   		if(defined($cs->{ordersystems})){
	   			if($cs->{systemfiles} ne $cs->{ordersystems}->{systemfiles} && $custsyscnt >= 2){
	   				if($cs->{customersystemid} != $custsys[-1]){
	   					# push(@tobedeleted, $cs->{customersystemid});
	   					print join("\t",$custsyscnt,$cs->{customersystemid},$cs->{systemid},$cs->{customerid},$cs->{systemfiles},$cs->{ordersystems}->{systemfiles}) . "\n";
	   					push(@tobedeleted, $cs->{customersystemid});

	   				}

	   			}
	   		}	   		
	   }

	   show @tobedeleted;
	   foreach my $tbd (@tobedeleted){

			$dbix->resultset('CustomerSystem')->search({ customersystemid => $tbd },{})->update({active => 0, deleted => 1, updatedby => 1, updated => \'NOW()'});

	   }

	   # show \@customersystems;
	   show 'ALL:', scalar(@customersystems);

}

1;