#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

use CGI::Carp qw(fatalsToBrowser) ;
# use Date::Calc qw(:all);
# use DBI;

# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/compare_sss_aisa_synking.pl?1
# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/compare_sss_aisa_synking.pl?1

#-------------------------------------------------------------------------------

my  @ARGV     = split(/\&/, $ENV{'QUERY_STRING'});
our $debug    = $ARGV[0] ;
# our $username = 'rory' ;

if ($debug) { print "Content-type: text/html\n\n"; }

#------------------------------------------------------------------------------------------

&today ;
	
&db_open_upd ;
# &db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
		&load_vars ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

if ($debug) { print "Done."; }

exit;

#------------------------------------------------------------------------------------------

sub load_vars {
 
	my $table_aisa = 'event_quotes aisa' ;
	my $table_sss = 'event_quotes sss' ;
	
	&db_min_ro($table_aisa,"*","sss_quote_nr <> '0'","","") ; 
	foreach my $id1 (keys %{$db{$table_aisa}}) {
		foreach my $col (keys %{$db{$table_aisa}{$id1}}) {
			$aisa_col{$col} = 1 ; 
		}
		last ;
	}
	
	
	&db_switch_conn('sss') ;
	&db_min_ro($table_sss,"*","aisa_quote_nr <> '0'","","") ; 
	
	# foreach my $id2 (sort {$b <=> $a} keys %{$db{$table_sss}}) {
		# next unless $db{$table_sss}{$id2}{supplier_ids} =~ /\b63\b/ ;
		# foreach my $col (keys %{$db{$table_sss}{$id2}}) {
			# print "\n col : $col" ;
		# }
		# last ;
	# }
	
	foreach my $id (sort {$b <=> $a} keys %{$db{$table_sss}}) {
		# print "\n SSS ID : $id , AISA ID : $db{$table_sss}{$id}{aisa_quote_nr} \n" ;
		next unless $db{$table_sss}{$id}{supplier_ids} =~ /\b63\b/ ;
		foreach my $col (keys %{$db{$table_sss}{$id}}) {
			next if !$aisa_col{$col} || $col eq 'poc_name' || $col eq 'poc_contact_nr' || $col eq 'user_id' || $col eq 'id' || $col eq 'quote_nr' || $col eq 'excl_3_payment_terms' ;
			$db{$table_sss}{$id}{$col} =~ s/;+$// ;
			$db{$table_aisa}{$db{$table_sss}{$id}{aisa_quote_nr}}{$col} =~ s/;+$// ;
			
			
			print "\n SSS ID : $id , AISA ID : $db{$table_sss}{$id}{aisa_quote_nr}  , $col SSS : $db{$table_sss}{$id}{$col} , AISA : $db{$table_aisa}{$db{$table_sss}{$id}{aisa_quote_nr}}{$col} " if $db{$table_sss}{$id}{$col} ne $db{$table_aisa}{$db{$table_sss}{$id}{aisa_quote_nr}}{$col} ;
		}
	}

} #-------------------------------------------------------------------------------
 

use db ;
use today ;
use common ;
use common_min ;
