#!/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/link_event_cost_items_to_event_systems.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 {
	
	&db_min_ro('event_cost_items','id,name,excl_from_expenses',"name LIKE 'Fixed%' OR name LIKE 'Mobile%' OR name LIKE 'Cricket%'",'','') ; 
	
 # UPDATE event_systems SET event_cost_item_id = '0' WHERE event_cost_item_id != '0' AND id != '396' AND id != '109' AND id != '398' AND id != '400' AND id != '104' AND id != '399' AND id != '397' ;
	
	&db_min_ro('event_systems','id,name,description,system_type,event_cost_item_id',"id != '396' AND id != '109' AND id != '398' AND id != '400' AND id != '104' AND id != '399' AND id != '397'",'','') ; 
	
	foreach my $sys_id (sort {$db{event_systems}{$a}{system_type} cmp $db{event_systems}{$b}{system_type}} keys %{$db{event_systems}}) {
		
		my $system_type = $db{event_systems}{$sys_id}{system_type} ;
		my $system_name = $db{event_systems}{$sys_id}{name} ;
		my $system_desc = $db{event_systems}{$sys_id}{description} ;
		my $cost_ids = "" ; 
		 
		print "\n excl_from_expenses : $db{event_systems}{$sys_id}{event_cost_item_id} , $db{event_cost_items}{$db{event_systems}{$sys_id}{event_cost_item_id}}{name} , SYSTEM : $system_name [$system_desc] ($system_type)" if $db{event_cost_items}{$db{event_systems}{$sys_id}{event_cost_item_id}}{excl_from_expenses} ;
		 
		foreach my $cost_id (keys %{$db{event_cost_items}}) {
			
			next if $db{event_cost_items}{$cost_id}{excl_from_expenses} ;
			
			my $cost_name = $db{event_cost_items}{$cost_id}{name} ;
			
			unless (($cost_name =~ /- AISA/ && $system_desc !~ /\bAISA\b/) || ($cost_name =~ /- SSS/ && $system_desc !~ /\bSSS\b/) || ($cost_name !~ /- SSS/ && $system_desc =~ /\bSSS\b/) || ($cost_name !~ /- AISA/ && $system_desc =~ /\bAISA\b/ && $system_desc !~ /- SSS/)) {

				if ($system_type eq 'mobile' && $cost_name =~ /\bMobile\b/ && $cost_name !~ /\bCricket\b/) {
					if ($system_name =~ /\bPXLS1\b/ && $cost_name =~ /\bS1\b/) {
						$cost_ids .= "$cost_id," ;
					} elsif (($system_name =~ /\bPXLS3\b/ || $system_name =~ /PXLS3_/) && $cost_name =~ /\bS3\b/) {
						$cost_ids .= "$cost_id," ;
					} elsif ($system_name !~ /PXLS3_/ && $system_name !~ /\bPXLS3\b/ && $cost_name !~ /\bS3\b/ && $system_name !~ /\bPXLS1\b/ && $cost_name !~ /\bS1\b/) {
						$cost_ids .= "$cost_id," ;
					} 
				} elsif ($system_type eq 'fixed' && $cost_name =~ /\bFixed\b/ && $cost_name !~ /\bCricket\b/) {
					$cost_ids .= "$cost_id," ;
				} elsif ($system_type eq 'solo' && ($cost_name =~ /\bSolo\b/ || $cost_name =~ /\bCricket\b/)) { 
					if ($cost_name =~ /\bCricket\b/ && $system_desc =~ /\bCricket\b/) {
						$cost_ids .= "$cost_id," ; 
					} elsif ($cost_name =~ /\bSolo\b/ && ($system_desc =~ /\bSOLO\b/ || $system_desc =~ /\bSolo\b/)) {
						$cost_ids .= "$cost_id," ; 
					} 
				}
					
			} elsif ($system_desc =~ /\bSports AI\b/ && $cost_name =~ /- AISA/) {
				
				# print "\n 2 \n " ;
				
				if ($system_type eq 'mobile' && $cost_name =~ /\bMobile\b/ && $cost_name !~ /\bCricket\b/) {
					if (($system_name =~ /\bPXLS3\b/ || $system_name =~ /PXLS3_/) && $cost_name =~ /\bS3\b/) {
						$cost_ids .= "$cost_id," ; 
					}
				}
			} elsif ($system_desc =~ /\bAISA\b/ && $system_desc =~ /- SSS/ && ($cost_name =~ /- SSS/ || $cost_name =~ /\bAISA\b/)) {

				if ($system_type eq 'mobile' && $cost_name =~ /\bMobile\b/ && $cost_name !~ /\bCricket\b/) {
;
					
					if (($system_name =~ /\bPXLS3\b/ || $system_name =~ /PXLS3_/) && $cost_name =~ /\bS3\b/) {
						 					 
						$cost_ids .= "$cost_id," ;
						# $cost_ids .= "$cost_id," ;

					}
				}
			}
		}
		
		chop $cost_ids if $cost_ids;
		
		$cost_ids = "10" if $cost_ids eq '19,10' || $cost_ids eq '10,19' ;
		$cost_ids = "22" if $cost_ids eq '28,22' || $cost_ids eq '22,28' ;
		$cost_ids = "24" if $cost_ids eq '29,24' || $cost_ids eq '24,29' ;
		
		# print "\n UPDATE event_systems SET event_cost_item_id='$cost_ids' WHERE id = '$sys_id' ; " if $cost_ids && $cost_ids !~ /,/  ; ;
		# print "\n UPDATE event_systems SET event_cost_item_id='$cost_ids' WHERE id = '$sys_id' ; " if !$cost_ids || $cost_ids =~ /,/  ;
		
		# print "\n cost_ids : $cost_ids" ;
		# print "\n $sys_id $cost_ids CHANGED TO $db{event_systems}{$sys_id}{event_cost_item_id}" if $cost_ids && $db{event_systems}{$sys_id}{event_cost_item_id} && $cost_ids ne $db{event_systems}{$sys_id}{event_cost_item_id} ;
		
		
		# print "\n CHANGE TO $db{event_cost_items}{$cost_ids}{name} ($cost_ids) FROM $db{event_cost_items}{$db{event_systems}{$sys_id}{event_cost_item_id}}{name} ($db{event_cost_items}{$db{event_systems}{$sys_id}{event_cost_item_id}}{excl_from_expenses})" if $db{event_systems}{$sys_id}{event_cost_item_id} ne $cost_ids ;
		
		$cost_ids = join(",", map { "$db{event_cost_items}{$_}{name}" } split (/\,/,$cost_ids)) ;
		
		# print "\n $sys_id : $system_name [$system_desc] ($system_type) ---> $cost_ids\n" if !$cost_ids || $cost_ids =~ /,/  ;
		# print "\n $sys_id : $system_name [$system_desc] ($system_type) ---> $cost_ids\n" if $cost_ids && $cost_ids !~ /,/  ;
		
	}
	
} #-------------------------------------------------------------------------------


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