#!/usr/bin/perl

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

print "Content-type: text/html\n\n";

# unless ($useropts{it}{$username}) { print "<<<<<<<<<<<< MAINTENANCE IN PROGRESS >>>>>>>>>" ; exit ; }

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Mail::Sendmail;
use Fcntl qw(:flock);
use File::Copy;
use File::Basename;
use Date::Calc qw(:all);
use Excel::Writer::XLSX;
use Time::Piece ; 
use LWP::UserAgent;
use XML::LibXML;
use LWP::Simple;
use XML::Simple ;
use JSON ;

&today;

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

@ARGV   = split(/\\*\&/, $ENV{'QUERY_STRING'});
$action = $ARGV[0] ;

our ($q)                 = CGI -> new() ;
our $iaction             = $q -> param('iaction')	|| $action ;
our $isaved              = $q -> param('isaved')	|| '' ;
                                                
# When the “Create Waybill Out”  box in the column is selected and “save" 
# is clicked at the bottom of the page, each row selected talks to the 
# DCB platform and waybills get created using the information linked to each row. 
# The check box in that row and column should then disappear to indicate that a waybill for that step has been created.

# When the “Create Waybill In” box in the column is selected and “save” is clicked at the bottom of the page, each row selected talks to the DCB platform and waybills get created using the information linked to each row. This time, however, the “From” Venue becomes the delivery venue and the “To” venue becomes the pick up venue. These venues will still need to be able to be edited because sometimes systems aren’t returned to where they came from originally. The check box in that row and column should then disappear to indicate that a waybill for that step has been created.

# When the “Process Complete” box in the column is selected and “save” is clicked at the bottom of the page, each row selected now gets removed from the logistics page and archived.

# our $debuqg     = 1 ;
# our $testing   = 1 ;
# our $testdcd   = 1 ;

# if ($username ne 'handre') {
	# print "<< MAINTENANCE IN PROGRESS >>" ; exit ; 	
# }

# -------- page opts --------------------------------------------------------------------------------------------------------------------------------------------------

&page_opts ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
our $srchscr = 1 ;
	
	# $is_dcb_partner = 0 ;
	
our $open_new_tab = qq~target="_blank"~ ;	
	
if ($iaction eq 'save') {  	
	&db_open_upd ;
		our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
			&common_min_load_params ;
			&report_ifields;
			&update ;
			&list_screen ;    
		$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
	&db_close_conn ;
	exit if ($username eq 'handre' || $username eq 'rory') && $testing ;
	&screen3;
	&redirect_screen;
}

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	
		&report_screen if $iaction eq '' ; 
		
		if ($iaction eq 'report') {  	
			&common_min_load_params ;
			&report_ifields;
			&list_screen ;    
			&screen3;
		}
		
		&common_min_action;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

exit;

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

sub update {
	
	&logistics_load_db_vars ;
	
	my %AISA_supplier = () ; 
	foreach (keys %{$db{logistics_suppliers}}) {
		$AISA_supplier{$_} = 1 if $db{logistics_suppliers}{$_}{supplier} =~ /AISA/ ;
	}
	
	our %ii = () ; my %iii = %i  ; my $saved_from_to = "" ; # 
	my %saved_f_t = () ; $saved_f_t{"from"} = "to" ; $saved_f_t{"to"} = "from" ; # my %process_complete_envet_ids = () ; 
	
	# my $ft = '' ;
	
	$ignore{'oaction'} = 1 ; my %calulated_default_values = () ;
	
	my %iii = () ; 
	
	foreach (sort keys %i) {

		local @field_parts = split(/\_/,$_) ;
		
		my $eid  = $field_parts[-1] ;	
		my $ft   = $field_parts[-2] ;	
		
		next if $eid !~ /\d+/ || ($ft ne 'from' && $ft ne 'to') ; 
		
		my $upd_field_1 = "update_from_$eid" ;
		my $upd_field_2 = "update_to_$eid" ;
		
		next if !$i{$upd_field_1} && !$i{$upd_field_2} ;

		# &common_debug("$_ [eid=$eid] [ft=$ft]") ;
		
		$seen_create_waybill{from}{$eid} = 1 if $_ =~ /create_waybill_from/ && $i{$_} ; 
		$seen_create_waybill{to}{$eid} = 1 if $_ =~ /create_waybill_to/ && $i{$_} ; 
		
		if ((($_ =~ /create_waybill_from/ && $i{$upd_field_1}) || ($_ =~ /create_waybill_to/ && !$seen_create_waybill{from}{$eid} && $i{$upd_field_2})) && $i{$_}) {
			
			my $venue_field = "venue_location_$ft\_$eid" ;
			my $venue       = $i{$venue_field} ;
			our @items      = split(/\|/,$i{"item_$ft\_$eid"}) ;
			
			our $items_cnt  = scalar @items || 1 ;
			
			our ($suburb,$city,$postal_code,$address,$address1,$address2,$address3,$address4,$routeCode,$dcbdate) ; my @add_sql_upd = () ; my $add_sql_upd = '' ;
			
			$city        = ($db{organisations}{$venue}{name}) ? $db{organisations}{$venue}{name} : '' ;  ### $db{organisations}{$venue}{name} ??
			$suburb      = ($db{organisations}{$venue}{suburb}) ? $db{organisations}{$venue}{suburb} : '' ; 
			$postal_code = ($db{organisations}{$venue}{postal_code}) ? $db{organisations}{$venue}{postal_code} : '' ; 
			$address1    = ($db{organisations}{$venue}{address_line_1}) ? $db{organisations}{$venue}{address_line_1} : '' ; 
			$address2    = ($db{organisations}{$venue}{address_line_2}) ? $db{organisations}{$venue}{address_line_2} : '' ; 
			$address3    = ($db{organisations}{$venue}{address_line_3}) ? $db{organisations}{$venue}{address_line_3} : '' ; 
			$address4    = ($db{organisations}{$venue}{address_line_4}) ? $db{organisations}{$venue}{address_line_4} : '' ; 
			$routeCode   = ($db{organisations}{$venue}{dcb_route_code}) ? $db{organisations}{$venue}{dcb_route_code} : '' ; 
			# $dcbdate     = ($ft eq 'to') ? substr($db{$table}{$eid}{date_from},0,10) : substr($db{$table}{$eid}{date_to},0,10) ; 
			$dcbdate     = ($ft eq 'to') ? $db{$table}{$eid}{date_from} : $db{$table}{$eid}{date_to} ; 
			
			# &common_debug("1. CHECK [$_] [ft=$ft] [eid=$eid] [venue_field=$venue_field] [venue=$venue] [!$postal_code || !$suburb || !$city || !$address1]") ;
			
			if (!$postal_code || !$suburb || !$city || !$address1) {
				($suburb,$city,$postal_code,$address) = &common_location_osm_address($db{organisations}{$venue}{name}) if $db{organisations}{$venue}{name} ;
				
				# &common_debug("($suburb,$city,$postal_code,$address) = &common_location_osm_address($db{organisations}{$venue}{name}) if $db{organisations}{$venue}{name} ;") ;
				
				push @add_sql_upd, qq~city = "$city"~ if $city && !$db{organisations}{$venue}{city} ;
				push @add_sql_upd, qq~suburb = "$suburb"~ if $suburb && !$db{organisations}{$venue}{suburb} ;
				push @add_sql_upd, qq~postal_code = "$postal_code"~ if $postal_code && !$db{organisations}{$venue}{postal_code} ;
					
				if ($address) {
					($address1,$address2,$address3,$address4) = split(/\, /,$address);
					push @add_sql_upd, qq~address_line_1 = "$address1"~ if $address1 && !$db{organisations}{$venue}{address_line_1} ; #, address_line_2 = '$address2', address_line_3 = '$address3', address_line_4 = '$address4'
					push @add_sql_upd, qq~address_line_2 = "$address2"~ if $address2 && !$db{organisations}{$venue}{address_line_2} ;
					push @add_sql_upd, qq~address_line_3 = "$address3"~ if $address3 && !$db{organisations}{$venue}{address_line_3} ;
					push @add_sql_upd, qq~address_line_4 = "$address4"~ if $address4 && !$db{organisations}{$venue}{address_line_4} ;
				}
				
				# $add_sql_upd = join(", ",@add_sql_upd) ;
				# &db_min_raw("UPDATE organisations SET $add_sql_upd  WHERE id='$venue'") if $venue && $add_sql_upd ; 	
			}
			
			# &common_debug("2. CHECK [$suburb && $postal_code && !$routeCode]") ;
			
			if ($suburb && $postal_code && !$routeCode) {
				&dcb_postalcode ;	
				if (!$routeCode) {
					my $suburb_tmp = $suburb ; $suburb = '' ;
					&dcb_postalcode ;
					$suburb = $suburb_tmp ;
				}
				push @add_sql_upd, qq~dcb_route_code = "$routeCode"~ if $routeCode && !$db{organisations}{$venue}{dcb_route_code} ;
			} 
			
			$add_sql_upd = join(", ",@add_sql_upd) ;				
			&db_min_raw("UPDATE organisations SET $add_sql_upd  WHERE id='$venue'") if $venue && $add_sql_upd ; 

			# &common_debug("3. CHECK [routeCode=$routeCode]") ;			
			
			# &dcb_savewabill($venue,$ft,$eid) if $routeCode ;
			&dcb_upload_oms_waybill($venue,$ft,$eid) if $routeCode ;
			
			my @dcb_waybill = split(/\-/,$db{$table}{$eid}{logistics_dcb_waybill}) ; 
			
			# my $dcb_waybill = ($ft eq 'to') ? "$dcb_waybill[0]-1" : "1-$dcb_waybill[1]"  ;
			
			# if ($savewabillResult =~ /Successful/iog) {
			if ($uploadwaybillresult =~ /SUCCESS/iog) {
				# $ii{$eid}{logistics_dcb_waybill}    = ($ft eq 'to') ? "$dcb_waybill[0]-1" : "1-$dcb_waybill[1]"  ;
				if ($ft eq 'from') {
					$ii{$eid}{from}{logistics_dcb_waybill} 	= 1 ;
					$iii{$eid}{from}{status}				= 1 ;
					if ($i{"create_waybill_to_$eid"} && $i{$upd_field_2}) {
						$ii{$eid}{to}{logistics_dcb_waybill}	= 1 ;
						$iii{$eid}{to}{status}					= 1 ;
					}
				} else {
					$ii{$eid}{to}{logistics_dcb_waybill} 	= 1 ;
					$iii{$eid}{to}{status}					= 1 ;				
				}
			}
			$ii{$eid}{from}{logistics_dcb_waybill} 	= 1 if $dcb_waybill[0] && !$ii{$eid}{from}{logistics_dcb_waybill} ;
			$ii{$eid}{to}{logistics_dcb_waybill} 	= 1 if $dcb_waybill[1] && !$ii{$eid}{to}{logistics_dcb_waybill} ;
			
		} 
		
		# if ($_ =~ /process_complete_/) {
			# if (!$i{"update_$ft\_$eid"}) { ## AND ???
				# $process_complete_envet_ids{$eid} = $i{"process_complete_$ft\_$eid"} ;		
			# }
		# }

		if ($_ =~ /item_multi/) {
			# my $e_id = substr($_,4) ;
			next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			$ii{$eid}{item} = $i{$_} ; 
			next ;
		} elsif ($_ =~ /shipment_operator_/ && $_ !~ /shipment_operator_value_was_saved/) {
			# my $e_id = substr($_,4) ;
			next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			# if ($AISA_supplier{$i{"logistics_supplier_id_to_$eid"}} && $i{"update_to_$eid"}) {
			unless ($calulated_default_values{$eid}{shipment_operator}) {
				
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_shipment_operator_id}) ; 
				
				$ii{$eid}{from}{shipment_operator} = $saved_field_from_to[0] ; 
				$ii{$eid}{to}{shipment_operator} = $saved_field_from_to[1] ; 
				
				my $update_field = "update_to_$eid" ; my $sup_field = "logistics_supplier_id_to_$eid" ;
				
				if ($i{$update_field} && $AISA_supplier{$i{$sup_field}}) {
					$ii{$eid}{to}{shipment_operator} = $i{"shipment_operator_to_$eid"} ; 
				}
				
				
				if ($i{"update_from_$eid"} && $AISA_supplier{$i{"logistics_supplier_id_from_$eid"}}) {
					$ii{$eid}{from}{shipment_operator} = $i{"shipment_operator_from_$eid"} ; 
				}
				$calulated_default_values{$eid}{shipment_operator} = 1 ;
			}
			next ;
		} elsif ($_ =~ /poc/) {
			next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			$ii{$eid}{poc} = $i{$_} ;
			next ;
		} 
		elsif (($_ =~ /other_from/ && $_ !~ /other_from_value_was_saved/) || ($_ =~ /other_to/ && $_ !~ /other_to_value_was_saved/)) {
			my $system_location_field = "system_location_to_$eid" ;
			my $venue_location_field = "venue_location_from_$eid" ;
			# my $other_field = "other_to_from\_$eid" ;
			unless ($calulated_default_values{$eid}{other_from_to}) {
				my @saved_field_from_to = split(/\:\|\:/,$db{$table}{$eid}{logistics_location_other}) ; 
				my @saved_field_from_to_2_1 = split(/\|/,$saved_field_from_to[0]) ; 
				my @saved_field_from_to_2_2 = split(/\|/,$saved_field_from_to[1]) ; 
				
				$ii{$eid}{other_from} .= ($i{"update_from_$eid"} && $i{$venue_location_field} eq 'other') ? $i{"other_from_from\_$eid"} . '|' : $saved_field_from_to_2_1[0] . '|' ;
				$ii{$eid}{other_from} .= ($i{"update_to_$eid"} && $i{$system_location_field} eq '5') ? $i{"other_from_to\_$eid"} : $saved_field_from_to_2_1[1] ;				
				$system_location_field = "system_location_from_$eid" ;
				$venue_location_field = "venue_location_to_$eid" ;
				$ii{$eid}{other_to} .= ($i{"update_from_$eid"} && $i{$system_location_field} eq '5') ? $i{"other_to_from\_$eid"} . '|' : $saved_field_from_to_2_2[0] . '|' ;
				$ii{$eid}{other_to} .= ($i{"update_to_$eid"} && $i{$venue_location_field} eq 'other') ? $i{"other_to_to\_$eid"} : $saved_field_from_to_2_2[1] ;
				# $ii{$eid}{other_to} .= $i{"other_to_from\_$eid"} . '|' . $i{"other_to_to\_$eid"} ;
				# $ii{$eid}{other_from} = $saved_field_from_to[0] ;
				# $ii{$eid}{other_to} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{other_from_to} = 1 ;
			} 
			# if ($i{$upd_field}) {
				# $ii{$eid}{other_from} = $i{"other_from_from\_$eid"} . '|' . $i{"other_from_to\_$eid"} ;
				# $ii{$eid}{other_to} = $i{"other_to_from\_$eid"} . '|' . $i{"other_to_to\_$eid"} ;
			# }
		} 
		# elsif ($_ =~ /system_location/ && $_ !~ /changed_system_location/) {
			# next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			# $ii{$eid}{system_location} = $i{$_} ;
			# next ; 
		# } 
		# elsif ($_ =~ /venue_location/ && $_ =~ /changed_venue_location/) {
			# next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			# $ii{$eid}{venue_location} = $i{$_} ;
			# next ;
		# }
		elsif ($_ =~ /received_by/) {

			next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			$ii{$eid}{received_by} = $i{$_} ;
			next ;
		} 

		my $upd_field = "update_$ft\_$eid" ;		 
		# next if !$i{$upd_field} ;
		
		if ($_ =~ /system_location/ && $_ !~ /changed_system_location/) {
			# next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			# $ii{$eid}{system_location} = $i{$_} ;
			# next ; 
			unless ($calulated_default_values{$eid}{system_location}) {
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_system_location_id}) ; 
				$ii{$eid}{from}{system_location} =  $saved_field_from_to[0] ;
				$ii{$eid}{to}{system_location} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{system_location} = 1 ;
			}
			$ii{$eid}{$saved_f_t{$ft}}{system_location} = $i{$_} if $i{$upd_field} ;
		} elsif ($_ =~ /venue_location/ && $_ !~ /changed_venue_location/) {
			# next if !$i{"update_from_$eid"} && !$i{"update_to_$eid"} ; 
			# $ii{$eid}{venue_location} = $i{$_} ;
			# next ;
			unless ($calulated_default_values{$eid}{venue_location}) {
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_venue_id}) ; 
				$ii{$eid}{from}{venue_location} =  $saved_field_from_to[0] ;
				$ii{$eid}{to}{venue_location} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{venue_location} = 1 ;
			}
			$ii{$eid}{$ft}{venue_location} = $i{$_} if $i{$upd_field} ;
			
		} elsif ($_ =~ /waybill_nr/) {
			unless ($calulated_default_values{$eid}{waybill_nr}) {
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_waybill_nrs}) ; 
				$ii{$eid}{from}{waybill_nr} =  $saved_field_from_to[0] ;
				$ii{$eid}{to}{waybill_nr} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{waybill_nr} = 1 ;
			}
			$ii{$eid}{$ft}{waybill_nr} = $i{$_} if $i{$upd_field} ;
		} elsif ($_ =~ /logistics_supplier/) {
			unless ($calulated_default_values{$eid}{logistics_supplier}) {
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_suppliers}) ; 
				$ii{$eid}{from}{logistics_supplier} = $saved_field_from_to[0] ;
				$ii{$eid}{to}{logistics_supplier} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{logistics_supplier} = 1 ;
			}
			$ii{$eid}{$ft}{logistics_supplier} = $i{$_} if $i{$upd_field} ;
		} elsif ($_ =~ /status/) {
			unless ($calulated_default_values{$eid}{status}) {
				my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_status_ids}) ; 
				$ii{$eid}{from}{status} = $saved_field_from_to[0] ;
				$ii{$eid}{to}{status} = $saved_field_from_to[1] ; 
				$calulated_default_values{$eid}{status} = 1 ;
			}
			$ii{$eid}{$ft}{status} = $i{$_} if $i{$upd_field} ;
			$ii{$eid}{$ft}{status} = $iii{$eid}{$ft}{status} if $iii{$eid}{$ft}{status} ;
		} elsif ($_ =~ /special_instructions/ && $_ !~ /other_special_instructions/) {
			unless ($calulated_default_values{$eid}{special_instructions}) {
				my @saved_field_from_to = split(/\-\-/,$db{$table}{$eid}{logistics_special_instructions}) ; 
				# my @saved_field_from_to_2_1 = split(/\:\|\:/,$saved_field_from_to[0]) ; 
				# my @saved_field_from_to_2_2 = split(/\:\|\:/,$saved_field_from_to[1]) ; 
				
				my $other_field_name = "other_special_instructions_from_$eid" ; 
				my $special_field_name = "special_instructions_from_$eid" ; 
				my $update_field_name = "update_from_$eid" ; 
				
				$ii{$eid}{from}{special_instructions} = ($i{$update_field_name}) ? $i{$special_field_name} . ":|:" . $i{$other_field_name } : $saved_field_from_to[0] ;
				
				$other_field_name = "other_special_instructions_to_$eid" ; 
				$special_field_name = "special_instructions_to_$eid" ; 
				$update_field_name = "update_to_$eid" ; 
				
				$ii{$eid}{to}{special_instructions} = ($i{$update_field_name}) ? $i{$special_field_name} . ":|:" . $i{$other_field_name} : $saved_field_from_to[1] ;

				$calulated_default_values{$eid}{special_instructions} = 1 ;
			}
			# if ($i{$upd_field} ) {
				# my $other_field_name = "other_special_instructions_$ft\_$eid" ; 
				# $ii{$eid}{$ft}{special_instructions} = ($ft eq 'to') ? $i{$_} : qq~$i{$_}:|:$i{$other_field_name}~ ;
				# $i{$_} =~ s/\-/ /g; $i{$other_field_name} =~ s/\-/ /g; # remove dash so as to not mess up the split
				# $ii{$eid}{$ft}{special_instructions} = qq~$i{$_}:|:$i{$other_field_name}~ if $i{$upd_field} ;				
			# }
		} elsif ($_ =~ /dcb_comments/ && $_ !~ /dcb_comments_value_was_saved_/) {
			unless ($calulated_default_values{$eid}{dcb_comments}) {
				my @saved_field_from_to = split(/\-\-/,$db{$table}{$eid}{logistics_dcb_comments}) ; 
				$ii{$eid}{from}{dcb_comments} = $saved_field_from_to[0] ;
				$ii{$eid}{to}{dcb_comments} = $saved_field_from_to[1] ;
				$calulated_default_values{$eid}{dcb_comments} = 1 ;
			}
			my $supplier_field = "logistics_supplier_id_$ft\_$eid" ;
			$ii{$eid}{$ft}{dcb_comments} = $i{$_} if $i{$upd_field} && $i{$supplier_field} eq '2' ;
		}  
		# elsif ($_ =~ /other_special_instructions/) { 
			# my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_special_instructions}) ; 
			# $ii{$eid}{$saved_f_t{$ft}}{special_instructions} = ($ft eq 'to') ? $saved_field_from_to[0] : $saved_field_from_to[1] ;
			# $ii{$eid}{$ft}{special_instructions} = $i{$_} ;
		# } 
		# elsif ($_ =~ /process_complete_/) {
			# my @saved_field_from_to = split(/\-/,$db{$table}{$eid}{logistics_complete}) ; 
			# $ii{$eid}{$saved_f_t{$ft}}{logistics_complete} = ($ft eq 'to') ? $saved_field_from_to[0] : $saved_field_from_to[1] ;
			# $ii{$eid}{$ft}{logistics_complete} = $i{$_} ;
		# } 
		elsif ($_ =~ /delivery_time/) {
			unless ($calulated_default_values{$eid}{delivery_time}) {
				my @saved_field_from_to = split(/\|/,$db{$table}{$eid}{logistics_delivery_datetime}) ; 
				$ii{$eid}{from}{delivery_time} = substr($saved_field_from_to[0],11) ;
				$ii{$eid}{to}{delivery_time} = substr($saved_field_from_to[1],11) ;
				$calulated_default_values{$eid}{delivery_time} = 1 ; 
			}
			$ii{$eid}{$ft}{delivery_time} = $i{$_} if $i{$upd_field} ;
		} elsif ($_ =~ /delivery_date/) {
			unless ($calulated_default_values{$eid}{delivery_date}) {
				my @saved_field_from_to = split(/\|/,$db{$table}{$eid}{logistics_delivery_datetime}) ; 
				$ii{$eid}{from}{delivery_date} = substr($saved_field_from_to[0],0,10) ;
				$ii{$eid}{to}{delivery_date} = substr($saved_field_from_to[1],0,10) ;
				$calulated_default_values{$eid}{delivery_date} = 1 ;
			}
			$ii{$eid}{$ft}{delivery_date} = $i{$_} if $i{$upd_field} ;
		} 
	}
 
	my %iii = %i  ;
	
	my $supp_field					= 'logistics_suppliers' ;
	my $waybill_field 				= 'logistics_waybill_nrs' ;
	my $status_field 				= 'logistics_status_ids' ;
	my $item_field 					= 'logistics_item_ids' ;
	my $special_instructions_field 	= 'logistics_special_instructions' ; 
	my $delivery_datetime_field 	= 'logistics_delivery_datetime' ;
	my $received_by_field 			= 'logistics_received_by' ;
	my $other_location				= 'logistics_location_other' ;
	# my $system_location				= 'logistics_system_location_id' ;
	my $dcb_waybill					= 'logistics_dcb_waybill' ;
	my $dcb_comments_field			= 'logistics_dcb_comments' ;
	my $shipment_operator_field		= 'logistics_shipment_operator_id' ;
	my $system_location_field		= 'logistics_system_location_id' ;
	my $venue_field					= 'logistics_venue_id' ; 
	
	my @from_to = ("from","to") ; my %seen_sys_id = () ;
	
	foreach my $event_id (keys %ii) {

		my $upd_field_1 = "update_from_$event_id" ;
		my $upd_field_2 = "update_to_$event_id" ;
		
		next if !$iii{$upd_field_1} && !$iii{$upd_field_2} ;

		%i = () ;

		# if ($db{$table}{$event_id}{logistics_venue_id} ne $ii{$event_id}{venue_location}) {
			# $i{logistics_venue_id} = $ii{$event_id}{venue_location} ;
			# $i{logistics_venue_id} = 0 unless $i{logistics_venue_id} ;
		# }

		foreach my $f_t (@from_to) { 
			my $add_dash                      	 = ($f_t eq 'from') ? '-' : '' ;
			$i{$supp_field}                   	.= "$ii{$event_id}{$f_t}{logistics_supplier}$add_dash" ;
			$i{$waybill_field}                	.= "$ii{$event_id}{$f_t}{waybill_nr}$add_dash"  ;
			$i{$status_field}                 	.= "$ii{$event_id}{$f_t}{status}$add_dash" ;	
			$i{$dcb_waybill}   					.= "$ii{$event_id}{$f_t}{logistics_dcb_waybill}$add_dash" ;	
			$i{$shipment_operator_field}		.= "$ii{$event_id}{$f_t}{shipment_operator}$add_dash" ;	
			$i{$system_location_field}			.= "$ii{$event_id}{$f_t}{system_location}$add_dash" ;	
			$i{$venue_field}					.= "$ii{$event_id}{$f_t}{venue_location}$add_dash" ;	
			$add_dash                      	 	 = ($f_t eq 'from') ? '--' : '' ;
			$i{$special_instructions_field}   	.= "$ii{$event_id}{$f_t}{special_instructions}$add_dash" ;	 
			$i{$dcb_comments_field}				.= "$ii{$event_id}{$f_t}{dcb_comments}$add_dash" ;	 
			# $i{logistics_complete}            .= "$ii{$event_id}{$f_t}{logistics_complete}$add_dash" ;	 
			$add_dash							 = ($f_t eq 'from') ? '|' : '' ;

			$i{$delivery_datetime_field}		.= ($ii{$event_id}{$f_t}{delivery_date} && $ii{$event_id}{$f_t}{delivery_time}) ? "$ii{$event_id}{$f_t}{delivery_date}T$ii{$event_id}{$f_t}{delivery_time}$add_dash" : "$add_dash" ;	
		}    

		
		$i{$item_field}                        	= "$ii{$event_id}{item}" ;	
		# $i{$delivery_datetime_field}			= "$ii{$event_id}{delivery_date}T$ii{$event_id}{delivery_time}" ;	
		$i{$received_by_field}					= "$ii{$event_id}{received_by}" ;	
		# $i{$shipment_operator_field}			= "$ii{$event_id}{shipment_operator}" ;	

		# my @poc_details                        	= split(/\_/,$ii{$event_id}{poc}) ;
		# $i{recipient_name}						= $poc_details[0] ;	
		# $i{recipient_nr}						= $poc_details[1] ;	
		$i{recipient_id}						= $ii{$event_id}{poc} ;	
								               
		# $i{logistics_dcb_waybill}              	= $ii{$event_id}{logistics_dcb_waybill} ;
		
		$i{$other_location}              		= $ii{$event_id}{other_from} . ":|:" . $ii{$event_id}{other_to};
		# $i{$system_location}              		= $ii{$event_id}{system_location} ;
		
		# my $upd_field                          	= "update_$ft\_$event_id" ;

		$i{$item_field} = join (",", map {"$_"} sort {$a <=> $b} split(/\,/,$i{$item_field})) if $i{$item_field} ;
		$db{$table}{$event_id}{logistics_item_ids} = join (",", map {"$_"} sort {$a <=> $b} split(/\,/,$db{$table}{$event_id}{logistics_item_ids})) if $db{$table}{$event_id}{logistics_item_ids} ;

		$ignore{logistics_dcb_waybill}         	= (($i{logistics_dcb_waybill} eq $db{$table}{$event_id}{logistics_dcb_waybill}) || !$i{logistics_dcb_waybill}) ? 1 : 0 ;				
		$ignore{logistics_venue_id}            	= (($i{logistics_venue_id} eq $db{$table}{$event_id}{logistics_venue_id})) ? 1 : 0 ;
		$ignore{$supp_field} 	               	= ((!$i{$supp_field} && !$db{$table}{$event_id}{logistics_suppliers}) || ($i{$supp_field} eq $db{$table}{$event_id}{logistics_suppliers})) ? 1 : 0 ;
		$ignore{$status_field}                 	= ((!$i{$status_field} && !$db{$table}{$event_id}{logistics_status_ids}) || ($i{$status_field} eq $db{$table}{$event_id}{logistics_status_ids})) ? 1 : 0 ;
		$ignore{$waybill_field}                	= ((!$i{$waybill_field} && !$db{$table}{$event_id}{logistics_waybill_nrs}) || ($i{$waybill_field} eq $db{$table}{$event_id}{logistics_waybill_nrs})) ? 1 : 0 ;
		$ignore{$item_field} 	               	= ((!$i{$item_field} && !$db{$table}{$event_id}{logistics_item_ids}) || ($i{$item_field} eq $db{$table}{$event_id}{logistics_item_ids})) ? 1 : 0 ;
		$ignore{$delivery_datetime_field}		= ((!$i{$delivery_datetime_field} && !$db{$table}{$event_id}{$delivery_datetime_field}) || ($i{$delivery_datetime_field} eq $db{$table}{$event_id}{$delivery_datetime_field})) ? 1 : 0 ;
		$ignore{$received_by_field}				= ((!$i{$received_by_field} && !$db{$table}{$event_id}{$received_by_field}) || ($i{$received_by_field} eq $db{$table}{$event_id}{$received_by_field})) ? 1 : 0 ;
		$ignore{$shipment_operator_field}		= ((!$i{$shipment_operator_field} && !$db{$table}{$event_id}{$shipment_operator_field}) || ($i{$shipment_operator_field} eq $db{$table}{$event_id}{$shipment_operator_field})) ? 1 : 0 ;
		$ignore{$special_instructions_field}    = ((!$i{$special_instructions_field} && !$db{$table}{$event_id}{logistics_special_instructions}) || ($i{$special_instructions_field} eq $db{$table}{$event_id}{logistics_special_instructions})) ? 1 : 0 ;
		$ignore{$other_location}         		= (($i{$other_location} eq $db{$table}{$event_id}{$other_location}) || !$i{$other_location}) ? 1 : 0 ;
		# $ignore{$system_location}         		= ($i{$system_location} && $i{$system_location} eq $db{$table}{$event_id}{$system_location}) ? 1 : 0 ;
		$ignore{$dcb_waybill}					= ($i{$dcb_waybill} && $i{$dcb_waybill} eq $db{$table}{$event_id}{$dcb_waybill}) ? 1 : 0 ;
		$ignore{$dcb_comments_field}			= ($i{$dcb_comments_field} && $i{$dcb_comments_field} eq $db{$table}{$event_id}{$dcb_comments_field}) ? 1 : 0 ;
		$ignore{$system_location_field}			= ($i{$system_location_field} && $i{$system_location_field} eq $db{$table}{$event_id}{$system_location_field}) ? 1 : 0 ;
		$ignore{$venue_field}					= ($i{$venue_field} && $i{$venue_field} eq $db{$table}{$event_id}{$venue_field}) ? 1 : 0 ;

		# $ignore{$status_field} 	           	= 1  if (!$i{$status_field} && !$db{$table}{$event_id}{logistics_status_ids}) || ($i{$status_field}	eq $db{$table}{$event_id}{logistics_status_ids}) || !$iii{$upd_field} ; 
		# recipient_name,recipient_nr
		# $ignore{$poc_field} 	               	= 1  if (!$i{$poc_field} 	&& !$db{$table}{$event_id}{logistics_poc_ids}) || $i{$poc_field}	eq $db{$table}{$event_id}{logistics_poc_ids} ;
		# $ignore{poc_name}                     = 1  if ($i{poc_name} eq $db{$table}{$event_id}{poc_name}) || ($i{poc_name} eq $db{$table}{$event_id}{recipient_name}) || !$iii{$upd_field} ; 
		# $ignore{poc_contact_nr}               = 1  if ($i{poc_contact_nr} eq $db{$table}{$event_id}{poc_contact_nr}) || ($i{poc_name} eq $db{$table}{$event_id}{recipient_nr}) || !$iii{$upd_field} ; 
		# $ignore{logistics_complete}           = 1  if ($i{logistics_complete} eq '-') ; 	
		# $ignore{logistics_complete}           = 1  if (!$i{logistics_complete} && !$db{$table}{$event_id}{logistics_complete}) || ($i{logistics_complete} eq $db{$table}{$event_id}{logistics_complete}) || !$iii{$upd_field} ; 	

		$ignore{poc_name} = 1 ;  
		$ignore{poc_contact_nr} = 1 ; 
		$ignore{poc_id} = 0 ; 
		$ignore{recipient_name} = 1 ; 
		$ignore{recipient_nr} = 1 ; 
		$ignore{recipient_id} = 0 ; 
		
		if ($db{$table}{$event_id}{poc_id} && !$db{$table}{$event_id}{recipient_id}) {
			$i{poc_id} = $i{recipient_id} ;
			$ignore{poc_id} = 1  if $i{poc_id} eq $db{$table}{$event_id}{poc_id}  ; 
			$ignore{recipient_id} = 1 ;
		} else {
			$ignore{poc_id} = 1 ;
			$ignore{recipient_id} = 1  if $i{recipient_id} eq $db{$table}{$event_id}{recipient_id} ; 
		}	
		
		$i{recipient_id} = 0 unless $i{recipient_id} ;
		$i{poc_id} = 0 unless $i{poc_id} ;
		
		# print "\n ignore poc_name      		: $ignore{poc_name}" ;
		# print "\n ignore poc_contact_nr  	: $ignore{poc_contact_nr}" ;
		# print "\n ignore poc_id    			: $ignore{poc_id}" ;
		# print "\n ignore recipient_name  	: $ignore{recipient_name}" ;
		# print "\n ignore recipient_nr   	: $ignore{recipient_nr}" ;
		# print "\n ignore recipient_id   	: $ignore{recipient_id}" ;
		# print "\n poc_id 	 				: $i{poc_id} , $db{$table}{$event_id}{poc_id}, $upd_field : $iii{$upd_field}"  ;
		# print "\n recipient_id 				: $i{recipient_id} , $db{$table}{$event_id}{recipient_id}" ;
		
		# exit ; 
		
		# if ($db{$table}{$event_id}{poc_name} && !$db{$table}{$event_id}{recipient_name}) {
			# $i{poc_name} = $i{recipient_name} ;
			# $ignore{poc_name} = 1  if ($i{poc_name} eq $db{$table}{$event_id}{poc_name}) || !$iii{$upd_field} ; 
			# $ignore{recipient_name} = 1 ;
		# } else {
			# $ignore{poc_name} = 1 ;
			# $ignore{recipient_name} = 1  if ($i{recipient_name} eq $db{$table}{$event_id}{recipient_name}) || !$iii{$upd_field} ; 
		# }
		
		# if ($db{$table}{$event_id}{poc_contact_nr} && !$db{$table}{$event_id}{recipient_nr}) {
			# $i{poc_contact_nr} = $i{recipient_nr} ;
			# $ignore{poc_contact_nr} = 1  if ($i{poc_name} eq $db{$table}{$event_id}{poc_name}) || !$iii{$upd_field} ; 
			# $ignore{recipient_nr} = 1 ;
		# } else {
			# $ignore{poc_contact_nr} = 1 ;
			# $ignore{recipient_nr} = 1  if ($i{recipient_nr} eq $db{$table}{$event_id}{recipient_nr}) || !$iii{$upd_field} ; 
		# }

		# $process_complete_envet_ids{$event_id} = 1 if $ignore{logistics_dcb_waybill} && $ignore{logistics_complete} && $ignore{poc_name} && $ignore{poc_contact_nr} && $ignore{logistics_venue_id} && $ignore{$supp_field} && $ignore{$waybill_field} && $ignore{$status_field} && $ignore{$item_field} && $ignore{$poc_field} && $ignore{$special_instructions_field} && $sys_ids ;
		
		# &common_debug("logistics_dcb_waybill [event_id=$event_id] [i=$i{logistics_dcb_waybill}] [db=$db{$table}{$event_id}{logistics_dcb_waybill}]") ;
		# &common_debug("$status_field [ignore=$ignore{$status_field}] [event_id=$event_id] [i=$i{$status_field}] [db=$db{$table}{$event_id}{logistics_status_ids}]") ;
		
		# user=;t_from=2025-01-24T18:14:29|user=25;d_from=2025-01-24T18:20:05|user=25;s_to=2025-01-24T18:42:29|user=25;d_from.t_from=2025-01-27T08:46:12|user=25;=2025-02-03T06:43:07
		
		#------------------------------------------------------------------------------------------ CALCULATE THE logistics_last_changed_by COLUMN : 

		my ($saved_start,$saved_end) = split(/\|/,$db{$table}{$event_id}{logistics_delivery_datetime}) ; 
		my ($saved_start_date,$saved_start_time) = split(/\T/,$saved_start) ; 
		my ($saved_end_date,$saved_end_time) = split(/\T/,$saved_end) ; 
		my ($saved_status_from,$saved_status_to) = split(/\-/,$db{$table}{$event_id}{logistics_status_ids}) ;  

		my $live_time = qq~$now_year-$now_mm-$now_dd\T$now_hour:$now_min:$now_sec~ ;
	
		$saved_date_col_user{$userid}{$live_time}{d_from} 	= 1 if $ii{$event_id}{from}{delivery_date} 	ne $saved_start_date 	&& $iii{$upd_field_1} ;
		$saved_date_col_user{$userid}{$live_time}{t_from} 	= 1 if $ii{$event_id}{from}{delivery_time} 	ne $saved_start_time 	&& $iii{$upd_field_1} ;
		$saved_date_col_user{$userid}{$live_time}{d_to}		= 1 if $ii{$event_id}{to}{delivery_date} 	ne $saved_end_date 		&& $iii{$upd_field_2};
		$saved_date_col_user{$userid}{$live_time}{t_to}		= 1 if $ii{$event_id}{to}{delivery_time} 	ne $saved_end_time		&& $iii{$upd_field_2} ;
		$saved_date_col_user{$userid}{$live_time}{s_to}		= 1 if $ii{$event_id}{to}{status} 			ne $saved_status_to		&& $iii{$upd_field_2} ;
		
		my $saved_user = 0 ; my %seen_col_date = () ; 
		foreach my $a (split(/\|/,$db{$table}{$event_id}{logistics_last_changed_by})) {
			if ($a =~ /user=(\d+)/) {
				$saved_user = $1;
			}	

			foreach my $b (split(/\;/,$a)) {
				my @c = split(/\=/,$b) ;
				if ($c[0] ne 'user') {
					foreach my $col (split(/\./,$c[0])) {
						next if $saved_date_col_user{$userid}{$live_time}{$col} ;
						if ($seen_col_date{$col}) {
							my $cur_date_val = $c[1] ; $cur_date_val =~ s/[:\-T]//g ;
							my $seen_date_val = $seen_col_date{$col} ; $seen_date_val =~ s/[:\-T]//g ;
							if ($seen_date_val < $cur_date_val) {
								$saved_date_col_user{$saved_user}{$seen_col_date{$col}}{$col} = 0 ;
								$saved_date_col_user{$saved_user}{$c[1]}{$col} = 1 ;
							}
						} else {
							$saved_date_col_user{$saved_user}{$c[1]}{$col} = 1 ;
						}
						$seen_col_date{$col} = $c[1] ;
					}	
				}					
			}
		}

		my %seen_user = () ; # my %seen_col = () ; 
		foreach my $user (sort keys %saved_date_col_user) {
			# my %seen_col = () ; 
			foreach my $datetime (sort {$b cmp $a} keys %{$saved_date_col_user{$user}}) {
				my $add_string = ($i{logistics_last_changed_by}) ? qq~|~ : qq~~ ;
				$add_string .= qq~user=$user\;~ unless $seen_user{$user} ;
				
				my $d = qq~~ ; my $cols_exists = 0 ; 
				foreach (sort keys %{$saved_date_col_user{$user}{$datetime}}) {
					next unless $saved_date_col_user{$user}{$datetime}{$_} ; 
					# next if $seen_col{$_} ; $seen_col{$_} = 1 ;
					$d .= qq~$_\.~ ; $cols_exists = 1 if $_ ;
				}
				chop $d ;
				$seen_user{$user} = 1 if $d ;
				$add_string .= qq~$d\=$datetime~ if $d ;		
				$i{logistics_last_changed_by} .= qq~$add_string~ if $cols_exists && $user ;
			}
		}
		
		# $i{logistics_last_changed_by} .= qq~user=$userid~ if $i{logistics_last_changed_by}  ; 
		
		$ignore{logistics_last_changed_by} = ($i{logistics_last_changed_by} eq $db{$table}{$event_id}{logistics_last_changed_by}) ? 1 : 0 ;

		#------------------------------------------------------------------------------------------
		
		if ($event_id) {
			foreach (keys %i) {
				next if $ignore{$_} ; 
				$line .= qq~$_='$i{$_}',~ ; 	
			}
			$line = qq~$now_year$now_year$now_mm$now_dd$now_hour$now_min$now_sec|$now_year-$now_mm-$now_dd $now_hour:$now_min:$now_sec|event_quote_id='$event_id'|user_id=$userid|changed_from|~ . $line if $line ;
			chop $line ;
			# &common_log_changes("events/changes.dat",$line) ; 
			&db_min_upd($table,"id='$event_id'") ;
		}
	
		# # # if ($set) { 
			# # # %i = () ;
			# # # my $upd_set = $set ; $upd_set =~ s/\`//g ;
			# # # $i{last_changed_by} = "$db{$table}{$event_id}{last_changed_by}, $username SET [$upd_set]" ; 
			# # # &db_min_upd($table,"id='$event_id'") if $event_id ;
		# # # }
	}		
	
	%i = %iii ; 

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

# sub calc_changes {
	
# } #------------------------------------------------------------------------------------------

sub report_screen {

	our $lcol = 3 ;
	our $fcol = 5 ;
	
	$allow_deselect{client_id} = 1 ;
	&common_min_select_opts('client_id','customers','name',$i{client_id},'','',"events='1'") ;
	&common_min_select_opts('organisation_ids','organisations','name','','','','','','','','name') ;
	
	my $dcb_sql = ($is_dcb_partner) ? " AND id='2'" : '' ;
	my $dcb_sel = ($is_dcb_partner) ? 2 : '' ;
	&common_min_select_opts('supplier_id','logistics_suppliers','supplier',$dcb_sel,'','',"active = '1'$dcb_sql",'','','','supplier') ;
	&common_min_select_opts('region_id','regions','name','','','code','','','','','name') ;
	
	%status_hash  = ($is_dcb_partner) ? (1 =>"Booked") : (1 =>"Booked",2 => "Pending",3=>"In Transit",4 =>"Delivered") ;
	$i{status_id} = ($is_dcb_partner && !$i{status_id}) ? 1 : $i{status_id} ; 
	
	$selected{$i{status_id}} = "SELECTED" if $i{status_id} ;
	for (1 .. 4) { $opts{status_id} .= qq~<option value="$_" $selected{$_}>$status_hash{$_}</option>~ ; }
 
	$preferred_title{organisation_ids} = "Venue" ;
	
	$print_box_content_rows .= &common_min_forms_start('report') ;
	
	$trigger_jquery .= qq~
		\$("#selectClient_id").val('5').trigger("chosen:updated") ;
	~ ;
	
	$allow_deselect{organisation_ids} = 1 ; 
	$allow_deselect{supplier_id} = 1 ; 
	$allow_deselect{status_id} = 1 ; 
	$allow_deselect{region_id} = 1 ; 
	
	$print_box_content_rows .= &common_min_form_select('client_id',$i{client_id}) ; 
	
	# my ($monday,$sunday) = &common_get_this_monday_sunday ;
	
	my ($now_year2,$now_mm2,$now_dd2) = Add_Delta_Days($now_year,$now_mm,$now_dd,10) ;
	
	$now_mm2 = sprintf("%02d",$now_mm2) ;
	$now_dd2 = sprintf("%02d",$now_dd2) ;
	
	$print_box_content_rows .= &common_min_form_datepicker('date_from',"$now_year-$now_mm-$now_dd") ; #unless $i{date_from} ;
	$print_box_content_rows .= &common_min_form_datepicker('date_to',"$now_year2-$now_mm2-$now_dd2") ; #unless $i{date_from} ;
	$print_box_content_rows .= &common_min_form_select('organisation_ids','') ;
	$print_box_content_rows .= &common_min_form_select('supplier_id','') ;
	$print_box_content_rows .= &common_min_form_select('status_id','') ;	
	$print_box_content_rows .= &common_min_form_select('region_id','') ;	
	
	$print_box_content_rows .= &common_min_forms_end('','','report') ;

	&common_min_search_screen ;

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

sub report_ifields {
	
	&load_search_vars ;

	# if ($i{date_from} && !$i{date_to}) { # check dates
		# push @report_sql, "`date_from` >= '$i{date_from} 00:00:00'" ; ############################################### > OR >=
		# push @report_results, "(EVENT AFTER '$i{date_from}')" ;
	# } elsif (!$i{date_from}) {
		# $error = qq~PLEASE ENTER A START DATE!~ ; 
		# &report_screen; 
	# } elsif ($i{date_from} && $i{date_to}) { # check dates
		# push @report_sql, "(
			# ('$i{date_from} 00:00:00' <= `date_from` AND `date_from` <= '$i{date_to} 23:59:59') OR 
			# ('$i{date_from} 00:00:00' <= `date_to` AND `date_to` <= '$i{date_to} 23:59:59') OR 
			# ('$i{date_from} 00:00:00' <= `date_from` AND `date_to` >= '$i{date_to} 23:59:59') OR 
			# ('$i{date_from} 00:00:00' >= `date_from` AND `date_to` <= '$i{date_to} 23:59:59')
		# )" ; ############################################### > OR >=
		# push @report_results, "(EVENT BETWEEN '$i{date_from}' AND '$i{date_to}')" ;
	# } 
	
	if ($i{date_from} && $i{date_to}) {
		push @report_sql, "(
			('$i{date_from} 00:00:00' <= `date_from` AND `date_from` <= '$i{date_to} 23:59:59') OR 
			('$i{date_from} 00:00:00' <= `date_to` AND `date_to` <= '$i{date_to} 23:59:59') OR 
			('$i{date_from} 00:00:00' >= `date_from` AND `date_to` >= '$i{date_to} 23:59:59') OR 
			('$i{date_from} 00:00:00' <= `date_from` AND `date_to` <= '$i{date_to} 23:59:59')
		)" ; ############################################### > OR >=
		push @report_results, "(EVENT EXISTS BETWEEN '$i{date_from}' AND '$i{date_to}')" ;
	} elsif (!$i{date_from} && $i{date_to}) {
		push @report_sql, "`date_to` >= '$i{date_to} 23:59:59'" ; ############################################### > OR >=
		push @report_results, "(EVENT ENDING BEFORE '$i{date_to}')" ;		
	} elsif ($i{date_from} && !$i{date_to}) {
		push @report_sql, "'$i{date_from} 00:00:00' <= `date_from`" ; ############################################### > OR >=
		push @report_results, "(EVENT STARTING AFTER '$i{date_from}')" ;		
	} elsif (!$i{date_from} && !$i{date_to}) {
		$error = qq~PLEASE ENTER AT LEAST A START OR END DATE!!!~ ;
	} 
	
	if ($i{client_id}) {    
		push @report_sql, "(`quote_to` = '$i{client_id}')" ;
		push @report_results, "Client = $db{customers}{$i{client_id}}{name}" ;
    }
	
	if ($i{organisation_ids}) {
		push @report_sql, "(FIND_IN_SET($i{organisation_ids},organisation_ids))" ;
		push @report_results, "Venue = `$db{organisations}{$i{organisation_ids}}{name}`" ;
	}	
	
	if ($i{supplier_id}) {
		push @report_sql, "((logistics_suppliers REGEXP '(^|;)[^-]*-$i{supplier_id}(\$\|;)') OR (logistics_suppliers REGEXP '(^|;)($i{supplier_id})-[^;]*(\$\|;)'))" ;
		push @report_results, "Supplier = `$db{logistics_suppliers}{$i{supplier_id}}{supplier}`" ;
	}
	
	if ($is_dcb_partner && $i{status_id} != 1) { $i{status_id} = 1 ; } 
	
	if ($i{status_id}) {
		local %status_hash = (1 =>"Booked",2 => "Pending",3=>"In Transit",4 =>"Delivered") ;
		push @report_results, "Status = `$status_hash{$i{status_id}}`" ;
		push @report_sql, "((logistics_status_ids REGEXP '(^|;)[^-]*-$i{status_id}(\$\|;)') OR (logistics_status_ids REGEXP '(^|;)($i{status_id})-[^;]*(\$\|;)'))" ;
	}	
 	
	if ($i{region_id}) {
		push @report_sql, "`region_id` = '$i{region_id}'" ;
		push @report_results, "Region = `$db{regions}{$i{region_id}}{name} [$db{regions}{$i{region_id}}{code}]`" ;		
	}
	
	$report_results_msg = uc join(', ', @report_results) ;

	if (not $report_results_msg) { 
		$error = qq~ENTER AT LEAST ONE SEARCH PARAMETER~ ; 
		&report_screen; 
	} else {
		$isaved = qq~SELECT WHERE $report_results_msg~ ; 
	}
	
	our $srch_where_sql = join(' AND ',@report_sql) ; 

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

sub load_search_vars {
	
	&db_min_ro('customers','id,name',"`id`='$i{client_id}'",'','') if $i{client_id} ;
	&db_min_ro('organisations',"id,name","`id`='$i{organisation_ids}'",'','') if $i{organisation_ids} ;
	&db_min_ro('logistics_suppliers','*',"`id`='$i{supplier_id}'",'','') if $i{supplier_id} ;
	&db_min_ro('regions','id,name,code',"`id`='$i{region_id}'",'','') if $i{region_id} ;
	
} #------------------------------------------------------------------------------------------

sub get_default_date {
	
	my ($date,$diff) = @_ ;
	
	my @sd   = &common_split_sql_time($date) ;
	my @dtyp = &common_add_delta_dhms($sd[0],$sd[1],$sd[2],$sd[3],$sd[4],$sd[5],$diff,0,0,0) ;
	
	return ($dtyp[0]) ;
	
} #------------------------------------------------------------------------------------------

sub load_list_vars {
	
	my ($where) = @_ ; 
	
	&db_min_ro($table,"id,quote_to,ref,organisation_ids,quote_accepted,event_system_id_multiple,region_id,operator_ids,date_from,date_to,poc_name,poc_id,recipient_id,logistics_suppliers,logistics_waybill_nrs,logistics_address_ids,logistics_venue_id,logistics_status_ids,logistics_item_ids,city_id,sport_type_ids,logistics_special_instructions,logistics_complete,logistics_dcb_waybill,logistics_delivery_datetime,logistics_received_by,logistics_location_other,logistics_system_location_id,logistics_last_changed_by,logistics_dcb_comments,logistics_shipment_operator_id","$where AND `quote_accepted` = '1'",'','') ; 
	
	&db_min_ro('event_quotes_poc','*','','','') ; 
	
	&db_min_ro('event_quotes_recipient','*','','','') ; 
	
	# my %seen_poc = () ; load_list_vars
	foreach (sort {$db{$table}{$a}{poc_id} cmp $db{$table}{$b}{poc_id}} keys %{$db{$table}}) { 

		# next if $seen_poc{$db{$table}{$_}{poc_name}}{$db{$table}{$_}{poc_contact_nr}} || !$db{$table}{$_}{poc_name} ; 
		# $seen_poc{$db{$table}{$_}{poc_name}}{$db{$table}{$_}{poc_contact_nr}} = 1 ;
		my $poc_id = ($db{$table}{$_}{recipient_id}) ? $db{$table}{$_}{recipient_id} : $db{$table}{$_}{poc_id} ;
		my $poc_name = ($db{$table}{$_}{recipient_id}) ? $db{event_quotes_recipient}{$db{$table}{$_}{recipient_id}}{name} : $db{event_quotes_poc}{$db{$table}{$_}{poc_id}}{name} ;
		my $poc_nr = ($db{$table}{$_}{recipient_id}) ? $db{event_quotes_recipient}{$db{$table}{$_}{recipient_id}}{contact_nr} : $db{event_quotes_poc}{$db{$table}{$_}{poc_id}}{contact_nr} ;
		next unless $poc_name ;
		$poc_nr =~ s/\'//g ; $poc_name =~ s/ $// ;  
		$opts{poc}{$_} .= ($poc_name && $poc_nr) ? qq~<option value='$poc_id' SELECTED>$poc_name [$poc_nr]</option>~ : ($poc_name && !$poc_nr) ? qq~<option value='$poc_name\_$poc_nr' SELECTED>$poc_name</option>~ : qq~~ ; 
	}
	
	&db_min_ro('event_systems','*',"(`system_type` = 'solo' OR `system_type` = 'mobile' OR `description` lIKE 'cricket%')",'','') ; 
	
	our %logistics_waybill_ids_hash  = () ;
	our %fval = () ;
	
	our %col_database_col = () ;
	
	&db_min_ro('customers','id',"events='1'",'','') ;
	
	foreach my $id (keys %{$db{$table}}) {
		
		$fval{item}{$id}{from}                        = $db{$table}{$id}{logistics_item_ids} ;	
		$fval{item}{$id}{to}                          = $db{$table}{$id}{logistics_item_ids} ;	
		
		my @split_values                              = split(/\-/, $db{$table}{$id}{logistics_suppliers}) ; 
		$fval{supplier}{$id}{from}                    = $split_values[0] ; 
		$fval{supplier}{$id}{to}                      = $split_values[1] ;

		@split_values                                 = split(/\-/,$db{$table}{$id}{logistics_waybill_nrs}) ; 
		$logistics_waybill_ids_hash{$id}{from}        = $split_values[0] ;
		$logistics_waybill_ids_hash{$id}{to}          = $split_values[1] ;

		$fval{waybill_nr}{$id}{from}                  = $split_values[0] ; 
		$fval{waybill_nr}{$id}{to}                    = $split_values[1] ;
		
		@split_values                                 = split(/\-/,$db{$table}{$id}{logistics_status_ids}) ; 
		$fval{status}{$id}{from}                      = $split_values[0] ; 
		$fval{status}{$id}{to}                        = $split_values[1] ; 	
		
		@split_values                                 = split(/\-\-/,$db{$table}{$id}{logistics_dcb_comments}) ; 		
		$fval{dcb_comments}{$id}{from} 				  =  $split_values[0] ; 
		$fval{dcb_comments}{$id}{to} 				  =  $split_values[1] ; 
	
		@split_values                                 = split(/\-\-/,$db{$table}{$id}{logistics_special_instructions}) ; 		

		my @split_from                                = split(/\:\|\:/,$split_values[0]) ; 
		my @split_to                                  = split(/\:\|\:/,$split_values[1]) ; 
		
		$fval{special_instructions}{$id}{from} 	      = $split_from[0] ; 
		$fval{other_special_instructions}{$id}{from}  = $split_from[1] ; 
		$fval{special_instructions}{$id}{to} 		  = $split_to[0] ; 	
		$fval{other_special_instructions}{$id}{to}    = $split_to[1] ; 															  
 
		# my $def_poc_name                              = ($db{$table}{$id}{recipient_name}) ? $db{$table}{$id}{recipient_name} : $db{$table}{$id}{poc_name} ;
		# my $def_poc_nr                                = ($db{$table}{$id}{recipient_nr}) ? $db{$table}{$id}{recipient_nr} : $db{$table}{$id}{poc_contact_nr} ;
		my $def_poc_id                               	= ($db{$table}{$id}{recipient_id}) ? $db{$table}{$id}{recipient_id} : $db{$table}{$id}{poc_id} ;
		
		# $def_poc_name =~ s/ $// ;  
		# $fval{poc}{$id}{from}                         = qq~$def_poc_name\_$def_poc_nr~ if $def_poc_name || $def_poc_nr; 
		$fval{poc}{$id}{from}                         = qq~$def_poc_id~ if $def_poc_id; 
		$fval{poc}{$id}{to}                           = $fval{poc}{$id}{from} ;
				
		my @delivery_split                            = split(/\|/,$db{$table}{$id}{logistics_delivery_datetime}) ; 
		my @delivery_from                             = split("T",$delivery_split[0]) ; 
		my @delivery_to                               = split("T",$delivery_split[1]) ; 
		
		$fval{delivery_date}{$id}{to}                 = $delivery_to[0] || &get_default_date($db{$table}{$id}{date_to},-1) ;
		$fval{delivery_time}{$id}{to}                 = $delivery_to[1] || '15:00:00' ;
		$fval{delivery_date}{$id}{from}               = $delivery_from[0] || &get_default_date($db{$table}{$id}{date_from},1) ;
		$fval{delivery_time}{$id}{from}               = $delivery_from[1] || '15:00:00' ;
		
		my @location_other_split                      = split(/\:\|\:/,$db{$table}{$id}{logistics_location_other}) ;
		my @location_other_to_split                   = split(/\|/,$location_other_split[1]) ; 
		$fval{other_to}{$id}{from}                    = $location_other_to_split[0] ; 
		$fval{other_to}{$id}{to}                      = $location_other_to_split[1] ; 
		
		my @location_other_from_split                 = split(/\|/,$location_other_split[0]) ; 
		$fval{other_from}{$id}{from}                  = $location_other_from_split[0] ; 
		$fval{other_from}{$id}{to}                    = $location_other_from_split[1] ; 
		
		$fval{received_by}{$id}{to}                   = $db{$table}{$id}{logistics_received_by} ;
		$fval{received_by}{$id}{from}                 = $fval{received_by}{$id}{to} ;
		
		@split_values                                 = split(/\-/,$db{$table}{$id}{logistics_shipment_operator_id}) ; 		
		
		$fval{shipment_operator}{$id}{from}			  = $split_values[0] ;
		$fval{shipment_operator}{$id}{to} 			  = $split_values[1] ;		
	
		@split_values                                 = split(/\-/,$db{$table}{$id}{logistics_system_location_id}) ; 		
		
		$fval{system_location}{$id}{from}			  = $split_values[1] ;
		$fval{system_location}{$id}{to}				  = $split_values[0] ;		
		
		@split_values                                 = split(/\-/,$db{$table}{$id}{logistics_venue_id}) ; 		
		
		$fval{venue_location}{$id}{from}			  = $split_values[0] ;
		$fval{venue_location}{$id}{to}				  = $split_values[1] ;

	}

	&db_min_ro('cities','id,name,city_short','','','') ;
	&db_min_ro('sport_types','id,name','','','') ;
	&db_min_ro('regions','code,id','','','') ;
	&db_min_ro('organisations','id,name,region_code','','','') ;
	
	foreach (sort {$db{organisations}{$a}{name} cmp $db{organisations}{$b}{name}} keys %{$db{organisations}}) {
		$opts{venue_location}{$db{regions}{$db{organisations}{$_}{region_code}}{id}} .= qq~<option value="$_">$db{organisations}{$_}{name}</option>~ ;
		# &common_debug("1. venue_location >>>>>> [<option value=$_>$db{organisations}{$_}{name}</option>]");
	}
	
	foreach (keys %{$opts{venue_location}}) {
		$opts{venue_location}{$_} .= qq~<option value="other">OTHER</option>~ ;
	}
	
	# $opts{venue_location}{other} .= qq~<option value="5">Other</option>~ ;
	
	&db_min_ro('users','id,name,user_type,username',"`name` NOT LIKE 'Rory%'",'','') ;	
	
	foreach (sort {$db{users}{$a}{name} cmp $db{users}{$b}{name}} keys %{$db{users}}) {
		$opts{shipment_operator} .= qq~<option value="$_">$db{users}{$_}{name}</option>~ if $db{users}{$_}{user_type} =~ /casual_/;
	}
	
	my $dcb_sql = ($is_dcb_partner) ? " AND id='2'" : '' ;
	&db_min_ro('logistics_suppliers','*',"active = '1'$dcb_sql",'','') ;
	
	our %AISA_supplier = () ; 
	
	foreach (sort {$db{logistics_suppliers}{$a}{supplier} cmp $db{logistics_suppliers}{$b}{supplier}} keys %{$db{logistics_suppliers}}) {
		$logistics_suppliers_selected = ($_ == 2) ? 'SELECTED' : '' ; # DCB Logistics
		$opts{supplier} .= qq~<option value="$_" $logistics_suppliers_selected>$db{logistics_suppliers}{$_}{supplier}</option>~ ; 
		$db{logistics_suppliers}{$_}{name} = $db{logistics_suppliers}{$_}{supplier} ; 
		$AISA_supplier{$_} = 1 if $db{logistics_suppliers}{$_}{supplier} =~ /AISA/ ; 
	}
	
	# $opts{supplier_to} = $opts{supplier_from} ;
	
	&db_min_ro('logistics_locations','*','','','') ;
	
	foreach (sort {$db{logistics_locations}{$a}{location} cmp $db{logistics_locations}{$b}{location}} keys %{$db{logistics_locations}}) {
		$opts{system_location} .= qq~<option value="$_">$db{logistics_locations}{$_}{location}</option>~ ; 
	}
	
	# &common_debug("1. system_location >>>>>> [$opts{system_location}]");

	our %status_hash = (1 =>"Booked",2 => "Pending",3=>"In Transit",4 =>"Delivered") ;
	
	$opts{status} .= qq~<option value="1">$status_hash{1}</option>~ ; 
	$opts{status} .= qq~<option value="2" SELECTED>$status_hash{2}</option>~ ; 
	$opts{status} .= qq~<option value="3">$status_hash{3}</option>~ ; 
	$opts{status} .= qq~<option value="4">$status_hash{4}</option>~ ; 
	
	$opts2{status} = $opts{status} ;
	
	# $opts{status} = $opts{status_to} ; 
	
	&db_min_ro('logistics_items','*','','','') ;
	
	foreach (sort {($db{logistics_items}{$a}{name} =~ /^(\d+)/)[0] <=> ($db{logistics_items}{$b}{name} =~ /^(\d+)/)[0]} keys %{$db{logistics_items}}) {
		next if lc $db{logistics_items}{$_}{name} eq 'other' ;
		$opts{item}.= qq~<option value="$_">$db{logistics_items}{$_}{name}</option>~ ; 
		$logistics_items_qty{$db{logistics_items}{$_}{qty}} .= "$_," if ($db{logistics_items}{$_}{name} =~ /Tripod/ || $db{logistics_items}{$_}{name} =~ /Extension/ || $db{logistics_items}{$_}{name} =~ /Mobile System/) && $db{logistics_items}{$_}{name} =~ / x / ;
	}
	
	foreach (keys %logistics_items_qty) {
		chop $logistics_items_qty{$_} ;
	}
	# foreach (keys %{$opts{item}}) {
		$opts{item} .= qq~<option value="1">Other</option>~ ; 
	# }
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	$print_box_content_rows .= &common_min_forms_start("$lcpage") ;

	&load_list_vars($srch_where_sql) ; 
	
	$fcol = 12 ; 
	
	# my @from_to = ('from','to') ;
	my @from_to = ('to','from') ;
	
	our @sql_col_display = ("nr","FT","event","dates","stime","etime","venue","city","sport","system","from","other_from","to","other_to","supplier","shipment_operator","poc","dcb_comments","item","delivery_date","delivery_time","special_instructions","other_special_instructions","waybill_nr","status","received_by") ;
	
	my $report = 1 ;  
	
	our $xlsx_title_heading = "Logistics Report" ;
	our $xlsxdir            = "logistics_report" ; 
	our $xlsxreportname     = "Logistics_Report" ; 

	&report_xlsx_export_header("$xlsxreportname",$xlsxdir,'',$xlsx_title_heading) ;
	
	@sql_col_display = ("nr","FT","event","dates","stime","etime","venue","city","sport","system","from","other_from","to","other_to","supplier","shipment_operator","poc","dcb_comments","item","delivery_date","delivery_time","special_instructions","other_special_instructions","waybill_nr","status","received_by","create_waybill","update") ;
	
	# $column_same_for_from_and_to{nr} = 1 ; 
	# $column_same_for_from_and_to{event} = 1 ; 
	# $column_same_for_from_and_to{dates} = 1 ; 
	# $column_same_for_from_and_to{stime} = 1 ; 
	# $column_same_for_from_and_to{etime} = 1 ; 
	# $column_same_for_from_and_to{venue} = 1 ; 
	# $column_same_for_from_and_to{city} = 1 ; 
	# $column_same_for_from_and_to{sport} = 1 ; 
	# $column_same_for_from_and_to{system} = 1 ; 
	# $column_same_for_from_and_to{poc} = 1 ; 
	# $column_same_for_from_and_to{item} = 1 ; 
	# $column_same_for_from_and_to{received_by} = 1 ; 
	
	my @rev_sql_col_display = reverse @sql_col_display ;
	
	foreach (@rev_sql_col_display) { $lastchild++ ; $last_child{$_} = $lastchild ; } # &common_debug("last-child : $_ [$last_child{$_}]");
	
	$xlsxrow-- ;
	
	my $column_nr = scalar @sql_col_display ;
	
	# my ($now_year2,$now_mm2,$now_dd2) = Add_Delta_Days($now_year,$now_mm,$now_dd,-7) ; 
	
	my $now_year2 = $now_year ;
	my $now_mm2 = $now_mm - 1 ; 
	$now_year2 -= 1 unless $now_mm2 ;
	$now_mm2 = 12 unless $now_mm2 ;
	$now_mm2 = sprintf("%02d",$now_mm2) ; 
	my $now_dd2 = "01" ;
	
	our $current_date = int("$now_year2$now_mm2$now_dd2") ; our $found_editable = 0 ; 

	our %seen_field_id = () ; our %all_location_ids_per_system = () ; our $all_ids = qq~~ ;
	 
	local @month_names = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ;
	our @months = @month_names ;
	our $quote_and_ops_ids = qq~~ ; 
	
	$fval_table{"supplier"}                = "logistics_suppliers" ; 	
	$fval_table{"status"}                  = "status_hash" ;
	
	$db{status_hash}{1}{name}              = "Booked" ; 
	$db{status_hash}{2}{name}              = "Pending" ; 
	$db{status_hash}{3}{name}              = "In Transit" ; 
	$db{status_hash}{4}{name}              = "Delivered" ; 
	
	$preferred_title{status}               = "Status" ; 
	$preferred_title{supplier}             = "Supplier" ; 
	$preferred_title{item}                 = "Item" ; 
	$preferred_title{poc}                  = "poc" ;
	
	our %linked_from_and_to_fields = () ; our @from_to_field_ids = () ; our @from_to_multi_field_ids = () ; our @from_to_checkbox_field_ids = () ;
	
	our %from_to_opposite = () ;
	
	$from_to_opposite{from} = "to" ; 
	$from_to_opposite{to} = "from" ; 
	
	our $other_from_dropdown_can_exist = 0 ;
	our $other_to_dropdown_can_exist   = 0 ;
	our $other_from_text_can_exist     = 0 ;
	our $other_to_text_can_exist       = 0 ;
	our $dcb_supplier_is_selected	   = 0 ;
	
	my $max_z_index = 0 ;
	
	our $AISA_supplier_selected_somewhere = 0 ; my %skip = () ; 
	our $other_from_system_location_selected_somewhere = 0 ; our $other_to_system_location_selected_somewhere = 0 ; 
	our $other_from_venue_selected_somewhere = 0 ; our $other_to_venue_selected_somewhere = 0 ; 
	our $dcb_supplier_is_selected_somewhere = 0 ;
	
	foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
		
		next unless $db{customers}{$db{$table}{$id}{quote_to}}{id} ;
 
		if ($i{organisation_ids}) {
			my @or_ids_arr =  split(/\,/,$db{$table}{$id}{organisation_ids}) ;	
			my $cnt = 0 ; 
			foreach my $or_id (@or_ids_arr) {
				next unless $or_id ;
				$cnt++ if $or_id eq $i{organisation_ids} ;
			}
			unless ($cnt) {
				$skip{event}{$id} = 1 ;
				next ;
			} 			
		}
		
		my $systems_in_event_count = 0 ; my $system_location_ids = '' ; my $event_venue_ids = '' ;   
		
		local @system_name_ids   	= split(/\;/,$db{$table}{$id}{event_system_id_multiple}) ;	  
		
		my $cnt = 0 ; 
		foreach my $event_sys_id (@system_name_ids) {
			next unless $event_sys_id ;
			next unless $db{event_systems}{$event_sys_id}{system_type} ;
			$cnt++ ;
		}

		unless ($cnt) {
			$skip{event}{$id} = 1 ;
			next ;
		} 	
		
		my @dcb_waybill = split(/\-/,$db{$table}{$id}{logistics_dcb_waybill}) ; $dcb_waybill_nr{from} = $dcb_waybill[0] ; $dcb_waybill_nr{to} = $dcb_waybill[1] ;
		
		my @suppliers2 = split(/\-/,$db{$table}{$id}{logistics_suppliers}) ;
		$supp{'from'} = $suppliers2[0] ; $supp{'to'} = $suppliers2[1] ;
		
		my $start_date        	= substr($db{$table}{$id}{date_from},0,10) ; $start_date =~ s/\-//g ; $start_date = int($start_date) ;
		my $end_date          	= substr($db{$table}{$id}{date_to},0,10)   ; $end_date   =~ s/\-//g ; $end_date   = int($end_date) ;
		
		for my $from_to (sort {$b <=> $a} @from_to) {
			
			my ($status_from,$status_to)		= split (/\-/,$db{$table}{$id}{logistics_status_ids}) ;
			my ($supplier_from,$supplier_to)	= split (/\-/,$db{$table}{$id}{logistics_suppliers}) ;
			
			if (($i{status_id} && (($from_to eq 'from' && $i{status_id} ne $status_from) || ($from_to eq 'to' && $i{status_id} ne $status_to))) || ($i{supplier_id} && (($from_to eq 'from' && $i{supplier_id} ne $supplier_from) || ($from_to eq 'to' && $i{supplier_id} ne $supplier_to)))) {
				$skip{$from_to}{$id} = 1 ;
				next ;				
			}
			
			my $readlonly_row = (($from_to eq 'to' && $current_date > $start_date) || ($from_to eq 'from' && $current_date > $end_date) || ($dcb_waybill_nr{$from_to} && $logistics_waybill_ids_hash{$id}{$from_to} && $db{$table}{$id}{logistics_item_ids} && $supp{$from_to} == 2)) ? 1 : 0 ;
			
			$max_z_index++ if !$readlonly_row ;
			$AISA_supplier_selected_somewhere = 1 if ($from_to eq 'from' && $AISA_supplier{$supplier_from}) || ($from_to eq 'to' && $AISA_supplier{$supplier_to}) ;
			$other_from_system_location_selected_somewhere 	= 1 if $db{$table}{$id}{logistics_system_location_id} =~ /^5-/ && $from_to eq 'from' ;
			$other_to_system_location_selected_somewhere 	= 1 if $db{$table}{$id}{logistics_system_location_id} =~ /-5$/ && $from_to eq 'to' ;			
			$other_from_venue_selected_somewhere 	= 1 if $db{$table}{$id}{logistics_venue_id} =~ /^other-/ && $from_to eq 'from' ;
			$other_to_venue_selected_somewhere 	= 1 if $db{$table}{$id}{logistics_venue_id} =~ /-other$/ && $from_to eq 'to' ;
			$dcb_supplier_is_selected_somewhere = 1 if ($from_to eq 'from' && ($supplier_from eq '2' || !$supplier_from)) || (($supplier_to || $supplier_to eq '2') && $from_to eq 'to') ;
		}
	}

	$max_z_index = $max_z_index * 7 ;
	
	foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
		
		next unless $db{customers}{$db{$table}{$id}{quote_to}}{id} ;
		
		# next unless $id eq '9029' ;
		
		# my @logistics_complete = split(/\-/,$db{$table}{$id}{logistics_complete}) ;
		
		next if $skip{event}{$id} || !$id ;
		
		our @system_name_ids   	= split(/\;/,$db{$table}{$id}{event_system_id_multiple}) ;	  
		our @op_ids        	   	= split(/\,/,$db{$table}{$id}{operator_ids}) ;
		our @venue_ids         	= split(/\,/,$db{$table}{$id}{organisation_ids}) ;
		our @dcb_waybill		= split(/\-/,$db{$table}{$id}{logistics_dcb_waybill}) ; $dcb_waybill_nr{from} = $dcb_waybill[0] ; $dcb_waybill_nr{to} = $dcb_waybill[1] ;
		
		our $items             	= $db{$table}{$id}{logistics_item_ids} ;
		our @suppliers         	= split(/\-/,$db{$table}{$id}{logistics_suppliers}) ; $supp{'from'} = $suppliers[0] ; $supp{'to'} = $suppliers[1] ;
		our $start_date        	= substr($db{$table}{$id}{date_from},0,10) ; $start_date =~ s/\-//g ; $start_date = int($start_date) ;
		our $end_date          	= substr($db{$table}{$id}{date_to},0,10)   ; $end_date   =~ s/\-//g ; $end_date   = int($end_date) ;
		
		# next if ($from_to eq 'from' && $logistics_complete[0]) || ($from_to eq 'to' && $logistics_complete[1]) ;
		
		# next if $i{supplier_id} && $i{supplier_id} ne $fval{'supplier'}{$id}{'from'} && $i{supplier_id} ne $fval{'supplier'}{$id}{'to'} ;
		# next if $i{status_id}   && $i{status_id}   ne $fval{'status'}{$id}{'from'}   && $i{status_id}   ne $fval{'status'}{$id}{'to'} ; 
		
		our $there_is_no_corresponding_TO = 0 ; our $there_is_no_corresponding_FROM = 0 ; 

		our %prev_update_time = () ; 

		my $saved_user = 0 ; 
		foreach my $a (split(/\|/,$db{$table}{$id}{logistics_last_changed_by})) {	
			if ($a =~ /user=(\d+)/) {
				$prev_update_time{user}{na} = $1;
			}
			
			foreach my $b (split(/\;/,$a)) {
				my @c = split(/\=/,$b) ;
				if ($c[0] ne 'user') {
					foreach my $col (split(/\./,$c[0])) { 
						$prev_update_time{date}{$col} = &common_min_date_as_string($c[1]) ;
						$prev_update_time{user}{$col} = $prev_update_time{user}{na} ;
					}
				}
			}
		}
		
		for my $from_to (sort {$b <=> $a} @from_to) {
			
			# $i{status_id}$i{supplier_id}
			
			$there_is_no_corresponding_TO 	= 1 if $from_to eq 'from' 	&& (($i{status_id} && $i{status_id} ne $status_to) 		|| ($i{supplier_id} && $i{supplier_id} ne $supplier_to)) ;
			$there_is_no_corresponding_FROM = 1 if $from_to eq 'to' 	&& (($i{status_id} && $i{status_id} ne $status_from)	|| ($i{supplier_id} && $i{supplier_id} ne $supplier_from)) ;

			next if $skip{$from_to}{$id} ;

			$cali_cnt++;
			$xlsxcol      = 0 ; 
			$print_tbody .= qq~<tr id="$id$dow">~ if $report ;
			$systems_in_event_count++ ; 
			
			&fields_row($from_to,$id);
			
			our $default_javascript = '' ; our $column_nr_cnt = $column_nr+1 ; our $system_count = 0 ; our $time_icon_color = 'black' ; our @waybill = () ; our $items = qq~~ ; our @suppliers = () ;
			
			if ($db{$table}{$id}{logistics_dcb_waybill}) {
				@waybill   = split(/\-/,$db{$table}{$id}{logistics_dcb_waybill}) ; $dcb_waybill_nr{'from'} = $waybill[0] ; $dcb_waybill_nr{'to'} = $waybill[1] ;
				$items     = $db{$table}{$id}{logistics_item_ids} ;
				@suppliers = split(/\-/,$db{$table}{$id}{logistics_suppliers}) ; $supp{'from'} = $suppliers[0] ; $supp{'to'} = $suppliers[1] ;
			}
			
			our $readlonly_row = (($from_to eq 'to' && $current_date > $start_date) || ($from_to eq 'from' && $current_date > $end_date) || ($dcb_waybill_nr{$from_to} && $logistics_waybill_ids_hash{$id}{$from_to} && $items && $supp{$from_to} == 2)) ? 1 : 0 ;
			
			our $supplier_default_val = 0 ;
			our $item_default_val = 0 ;
			our $waybill_nr_default_val = 0 ;
			our $AISA_supplier_selected = 0 ;
			our $other_from_system_location_is_saved = 0 ;
			our $other_to_system_location_is_saved = 0 ;
			our $other_from_venue_location_is_saved = 0 ;
			our $other_to_venue_location_is_saved = 0 ;
			
			foreach (@sql_col_display) { 
				
				$column_nr_cnt-- ;
				
				next unless $_ ; # blank for the buttons column
				
				our $formatting           = $format84 ;
				our $val                  = $db{$table}{$id}{$_} ;
				our $valxlsx              = '' ;
				our $align                = qq~ class="dt-center"~ ; 
				our $nowrap               = '' ;
				our $tooltip_updated 	  = '' ;
				
				$val                      = '' ; $valxlsx = 'MUSTBEBLANK' ; 
				
				our $default_val          = $fval{$_}{$id}{$from_to} ; 
				 
				our $only_display_val     = '' ; 
				our $background_color     = '' ; 
				our $default_val_is_saved = ($default_val && !$readlonly_row) ? 1 : 0 ;	
				our $cell_contains_select = 0 ; 
				our $hide_cell = 0 ; 
				
				&color_row($from_to,$id,$_) ;
				
				&from_to_row($from_to,$id,$_) ;
				
				next if $seen_event{$id} && $column_same_for_from_and_to{$_} ; 

				&val_row($from_to,$id,$_) ;
			
				my $style = ($column_same_for_from_and_to{$_} && !$there_is_no_corresponding_TO && !$there_is_no_corresponding_FROM) ? qq~rowspan="2"~ : qq~~ ;
				
				# $style .= ($default_val_is_saved && !$background_color && !$readlonly_row) ? qq~ style="background-color:#424949;"~ : ($default_val_is_saved && $background_color && !$readlonly_row) ? qq~ style="background-color:$background_color;"~ : qq~~ ; 
				$style .= ($default_val_is_saved && !$background_color && !$readlonly_row) ? qq~background-color:#424949;~ : ($default_val_is_saved && $background_color && !$readlonly_row) ? qq~background-color:$background_color;~ : qq~~ ; 
				$style .= ($cell_contains_select) ? qq~z-index:$max_z_index;~ : qq~~ ;
				$style .= ($hide_cell) ? qq~display:none;~ : qq~~ ;
				$style = qq~ style="$style"~ ;
		
				if ($_ eq 'venue' && $db{$table}{$id}{organisation_ids} !~ /\b$fval{venue_location}{$id}{$from_to}\b/) {
					$style = qq~style='background-color:rgb(255,0,0);'~ ;
					$tooltip_updated = qq~title data-toggle='tooltip' data-placement='top' data-original-title='Does not match with the venue to travel $from_to!'~ ;
					$val    = qq~<span $tooltip_updated>$val</span>~ ;
				}

				$max_z_index-- if $cell_contains_select ;
				
				my $cell_id = ($_ eq 'create_waybill') ? "id='waybill_col_$from_to\_$id'" : ($_ eq 'dcb_comments') ? "id='dcb_comments_col_$from_to\_$id'" : ($_ eq 'shipment_operator') ? "id='s_o_cell_$from_to\_$id'" : ($_ eq 'other_from' && $from_to eq 'to') ? "id='other_from_cell_$id'" : ($_ eq 'other_to' && $from_to eq 'from') ? "id='other_to_cell_$id'" : ($_ eq 'other_from' && $from_to eq 'from') ? "id='other_from_from_cell_$id'" : ($_ eq 'other_to' && $from_to eq 'to') ? "id='other_to_to_cell_$id'" : "" ;
				
				$print_tbody .= qq~<td $align $nowrap $style $cell_id>$val</td>~ if $report ; 

				# next if $_ eq 'create_waybill_out' || $_ eq 'create_waybill_in' || $_ eq 'process_complete' || $_ eq 'update' ;
				# next if $_ eq 'create_waybill' || $_ eq 'process_complete' || $_ eq 'update' ;
				next if $_ eq 'create_waybill' || $_ eq 'update' ;
				
				
				
				&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$valxlsx,$formatting) ;
				$xlsxcol++ ;
			
			}
			$print_tbody .= qq~</tr>~ ;
			$xlsxrow++ ;
			
			$seen_event{$id} = 1 ; 
			
			chop $system_location_ids if $system_location_ids ; chop $event_venue_ids if $event_venue_ids ;
		}
	}

	my $from_to_field_ids_str = join (",",@from_to_field_ids) ; 
	my $from_to_multi_field_ids_str = join (",",@from_to_multi_field_ids) ; 
	my $from_to_checkbox_field_ids_str = join (",",@from_to_checkbox_field_ids) ; 
	
	chop $linked_from_and_to_fields{main} 			if $linked_from_and_to_fields{main} ; 
	chop $linked_from_and_to_fields{multi_selects} 	if $linked_from_and_to_fields{multi_selects} ; 
	chop $linked_from_and_to_fields{checkboxes} 	if $linked_from_and_to_fields{checkboxes} ; 
	
	$trigger_jquery_raw .= qq~
	
		function dcb_checkbox(add_item,ft,event_id) {
			
			let ft_event_id = ft+"_"+event_id ;
			
			let add_waybill = (!\$("input[name='waybill_nr_"+ft_event_id+"']").val()) ? "<br><b>- Add WayBillNr</b>" : "" ;
			
			let set_supplier = (\$("#selectLogistics_supplier_id_"+ft_event_id).val() != '2') ? "<br><b>- Set Supplier to DCB</b>" : "" ;
			
			let required_fields_are_in = (!add_waybill && !add_item && !set_supplier) ? 1 : 0 ;
			
			let checkbox_exists = \$("#waybill_col_"+ft_event_id).find('input[type="checkbox"]').length ;
			 
			if (required_fields_are_in && !checkbox_exists) {
				\$("#waybill_col_"+ft_event_id).html("<input value='1' name='create_waybill_"+ft_event_id+"' type='checkbox' id='create_waybill_"+ft_event_id+"'>").trigger("chosen:updated") ;
			} else if (!required_fields_are_in) {
				\$("#waybill_col_"+ft_event_id).html("<i class='glyphicon glyphicon-remove' title data-toggle='tooltip' data-placement='left' data-original-title='Before sending to DCB:"+add_waybill+""+add_item+""+set_supplier+"'></i>").trigger("chosen:updated") ;
				\$("#waybill_col_" + ft_event_id + " [data-toggle='tooltip']").tooltip({html:true});
			}
		}
		
		function prevent_table_left_scroll () {
				
			let scrollLeft = \$("#$useropts{table_id}").parent().scrollLeft() ; 
			
			setTimeout(() => {
				\$("#$useropts{table_id}").parent().scrollLeft(scrollLeft) ; // Restore scroll position after dropdown change
			},0) ;			
			
		} 
		
	~ ;
	
	$trigger_jquery_raw_on_change_1 .= qq~
		
		// parent.event.preventDefault() ;
 
		prevent_table_left_scroll() ; 
		
		let corresponding_drop_down_field ; let event_id = get_row_nr(this.id,1) ; 
		
		if (this.id.includes("selectItem_")) {
			corresponding_drop_down_field = {$linked_from_and_to_fields{multi_selects}} ; 
			\$("#"+corresponding_drop_down_field[this.id]).val(\$(this).val()).trigger("chosen:updated") ;
			check_whether_to_update(corresponding_drop_down_field[this.id]) ;
		} 
		
		if (this.id.includes("inputWaybill_nr_") || this.id.includes("selectLogistics_supplier_id_") || this.id.includes("selectItem_")) {
			
			let ft_event_id = "from_"+event_id ; 
			
			let add_item = (\$("#selectItem_"+ft_event_id).chosen().val()) ? "" : "<br><b>- Add Item(s)</b>" ;
			
			if (!\$("#waybill_col_"+ft_event_id).find('a[href]').length && !\$('#waybill_col_'+ft_event_id).find('i.glyphicon.glyphicon-ok').length) {
				dcb_checkbox(add_item,'from',event_id) ;
			}
			
			ft_event_id = "to_"+event_id ;
			
			if (!\$("#waybill_col_"+ft_event_id).find('a[href]').length && !\$('#waybill_col_'+ft_event_id).find('i.glyphicon.glyphicon-ok').length) {
				dcb_checkbox(add_item,'to',event_id) ;
			}
		}
		
		// this.id.includes("inputOther_") || 
		
		if (this.id.includes("selectPoc_") || this.id.includes("inputReceived_by_") && (this.id.includes("selectSystem_location_") && !\$("input[name='changed_system_location']").val()) && (this.id.includes("selectVenue_location_") && !\$("input[name='changed_venue_location']").val())) {
			corresponding_drop_down_field = {$linked_from_and_to_fields{main}} ;		
			\$("#"+corresponding_drop_down_field[this.id]).val(\$(this).val()).trigger("chosen:updated") ;
			check_whether_to_update(corresponding_drop_down_field[this.id]) ;
			if (this.id.includes("selectSystem_location_") && !\$("input[name='changed_system_location']").val()) {
				\$("input[name='changed_system_location']").val("1") ;
			}
			if (this.id.includes("selectVenue_location_") && !\$("input[name='changed_venue_location']").val()) {
				\$("input[name='changed_venue_location']").val("1") ;
			}
		}
		
		if (this.id.includes("create_waybill_")) {
			  
			corresponding_drop_down_field = {$linked_from_and_to_fields{checkboxes}} ; 
			if (\$(this).is(":checked") && !\$("#"+corresponding_drop_down_field[this.id]).is(":checked")) {
				\$("#"+corresponding_drop_down_field[this.id]).prop("checked",true) ; 
			} else if (!\$(this).is(":checked") && \$("#"+corresponding_drop_down_field[this.id]).is(":checked")) {
				\$("#"+corresponding_drop_down_field[this.id]).prop("checked",false) ;
			}
			
			let event_id = get_row_nr(this.id,1) ; 
			
			// if (!\$("input[name='update_from_"+event_id+"']").is(":checked")) {
				\$("input[name='update_from_"+event_id+"']").prop("checked",true) ;
			// }				
			
			// if (!\$("input[name='update_to_"+event_id+"']").is(":checked")) {
				\$("input[name='update_to_"+event_id+"']").prop("checked",true) ;
			// }			
			
			// check_whether_to_update(corresponding_drop_down_field[this.id]) ;			
		}
		
	~ ; 
	
	my $from_venue_ids_str = join(",",@from_venue_ids) ; my $from_venue_ids_str_jquery = $from_venue_ids_str ; $from_venue_ids_str_jquery  =~ s/\,/\","/g ; 
	my $to_venue_ids_str = join(",",@to_venue_ids) ; my $to_venue_ids_str_jquery = $to_venue_ids_str ; $to_venue_ids_str_jquery  =~ s/\,/\","/g ; 
	
	my $supplier_ids_str = join(",",@supplier_ids_arr) ; my $supplier_ids_str_jquery = $supplier_ids_str ; $supplier_ids_str_jquery =~ s/\,/\","/g ; 
	
	$opts{shipment_operator} =~ s/\"/\'/g ;
	
	$trigger_jquery_raw .= qq~
		const shipment_operator_opts = "$opts{shipment_operator}" ;
		function add_select (field_name,field_id,placeholder,options,tindex,width) {
			return "<select class='form-control' style='margin:2px 0 2px 0; width: "+width+"%;' name='"+field_name+"' id='"+field_id+"'data-placeholder='"+placeholder+"' data-rel='chosen' tabindex='"+tindex+"'><option value=''></option>"+options+"</select>" ;
		}
	~ ;

	$trigger_jquery_raw_on_change_1 .= qq~
		
		let row_index = get_row_nr(this.id,2) ; 
		
		if (this.id.includes("selectLogistics_supplier_")) {
			
			let AISA_selected_somewhere = 0 ; let AISA_selected_nowhere = 0 ;  var supplierIDs = ["$supplier_ids_str_jquery"] ;
			
			if (\$("#"+this.id).val() == '2') {
				\$("#textareaDcb_comments_"+row_index).css("display","") ;
				if (\$("input[name='dcb_comments_value_was_saved_"+row_index+"']").val() == '1') {
					\$("#dcb_comments_col_"+row_index).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
				}				
			}
			
			if (\$("#"+this.id).val() == '2' && \$("#$useropts{table_id} tr th:nth-last-child($last_child{dcb_comments})").is(":hidden")) {
				\$("#$useropts{table_id} tr th:nth-last-child($last_child{dcb_comments})").css("display","") ;
				for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
					\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{dcb_comments})").css("display","") ; 
				}

			} else if (\$("#"+this.id).val() != '2' && !\$("#$useropts{table_id} tr th:nth-last-child($last_child{dcb_comments})").is(":hidden")) {
				
				// Check if DCB is selected anywhere else ----------------------------------------------
				
				let dcb_selected_somewhere_else = 0 ; 
				
				supplierIDs.forEach(function(selectID) {
					if (\$("#"+selectID).val() == '2') {
						dcb_selected_somewhere_else = 1 ;
						return ;
					}	
				}) ;

				if (!dcb_selected_somewhere_else && !\$("#$useropts{table_id} tr th:nth-last-child($last_child{dcb_comments})").is(":hidden")) {
					\$("#$useropts{table_id} tr th:nth-last-child($last_child{dcb_comments})").css("display","none") ;
					for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
						\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{dcb_comments})").css("display","none") ; 
					}						
				} else if (!\$("#textareaDcb_comments_"+row_index).is(":hidden")) {
					\$("#textareaDcb_comments_"+row_index).css("display","none") ;
					if (\$("input[name='dcb_comments_value_was_saved_"+row_index+"']").val()) {
						\$("#dcb_comments_col_"+row_index).css("background-color","") ; // rgb(66,73,73)
					}
				}
			}
			 			
			// if (\$('option:selected', this).text().includes('AISA') && !\$("#s_o_cell_"+row_index).html()) {
			
			if (\$('option:selected', this).text().includes('AISA') && !\$("#s_o_cell_"+row_index).find('select').length) {

				let placeholder = (row_index.includes("to")) ? "To" : "From" ; placeholder = placeholder + " " + get_row_nr(row_index,1) ; placeholder = "Select Shipment Operator "+placeholder ;
				let select_drop_down = add_select("shipment_operator_"+row_index,"selectShipment_operator_"+row_index,placeholder,shipment_operator_opts,1,0) ;
				
				\$("#s_o_cell_"+row_index).append(select_drop_down).trigger("chosen:updated") ;
				\$("#selectShipment_operator_"+row_index).chosen({ allow_single_deselect:true }) ;
				\$("#selectShipment_operator_"+row_index+"_chosen").css("width","90%") ;
				\$("#selectShipment_operator_"+row_index+"_chosen").css("position","absolute") ;
				\$("#selectShipment_operator_"+row_index+"_chosen").css("left","5%") ;
				\$("#selectShipment_operator_"+row_index+"_chosen").css("top","50%") ;
				\$("#selectShipment_operator_"+row_index+"_chosen").css("transform","translateY(-50%)") ;
				
				if (!\$("#selectShipment_operator_"+row_index).val() && \$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) {
					\$("#selectShipment_operator_"+row_index).val(\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()).trigger("chosen:updated") ;
				}
				
				if (\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) {
					\$("#s_o_cell_"+row_index).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
				}
				
				if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").is(":hidden")) {
					\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").css("display","") ;
					for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
						\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{shipment_operator})").css("display","") ; 
					}
				}
				
				\$("#$useropts{table_id}").on("change","#selectShipment_operator_"+row_index, function() {
					prevent_table_left_scroll() ;  
					check_whether_to_update(this.id) ; 	
				}); 
				
			} else if (\$('option:selected',this).text().includes('AISA') && \$("#s_o_cell_"+row_index).is(":hidden")) {
				if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").is(":hidden")) {
					\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").css("display","") ;
					for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
						\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{shipment_operator})").css("display","") ; 
					}						
				} else {
					\$("#s_o_cell_"+row_index).css("display","") ;
				}
				if (\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) {
					\$("#s_o_cell_"+row_index).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					if (!\$("#selectShipment_operator_"+row_index).val()) {
						\$("#selectShipment_operator_"+row_index).val(\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()).trigger("chosen:updated") ;
					}
				
				}	
			} else if (\$('option:selected',this).text().includes('AISA')) {
				if (\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) {
					\$("#s_o_cell_"+row_index).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					if (!\$("#selectShipment_operator_"+row_index).val()) {
						\$("#selectShipment_operator_"+row_index).val(\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()).trigger("chosen:updated") ;
					}
					\$("#selectShipment_operator_"+row_index+"_chosen").css("display","") ;
				}					
			} else if (!\$('option:selected',this).text().includes('AISA') && !\$("#s_o_cell_"+row_index).is(":hidden")) {
				console.log("1 shipment_operator") ;
				if (!\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").is(":hidden")) {
					let AISA_selected_somewhere = 0 ;
					supplierIDs.forEach(function(selectID) {
						if (\$("#"+selectID+" option:selected").text().includes('AISA')) {
							AISA_selected_somewhere = 1 ;
							return ;
						}
					}) ;
					if (AISA_selected_somewhere) {
						// \$("#s_o_cell_"+row_index).css("display","none") ;
						\$("#selectShipment_operator_"+row_index+"_chosen").css("display","none") ;
						console.log("2 shipment_operator : "+\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) ;
						if (\$("input[name='shipment_operator_value_was_saved_"+row_index+"']").val()) {
							\$("#s_o_cell_"+row_index).css("background-color","") ;
						}
						
					} else {
						\$("#$useropts{table_id} tr th:nth-last-child($last_child{shipment_operator})").css("display","none") ;
						for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
							\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{shipment_operator})").css("display","none") ; 
						}
					}
				}
			}
			
			
		
			// else if (\$('option:selected', this).text().includes('AISA') && \$("#s_o_cell_"+row_index).is(":hidden")) {
			// \$("#s_o_cell_"+row_index).css("display","") ;
			// } 
			// else if (!\$('option:selected', this).text().includes('AISA') && !\$("#s_o_cell_"+row_index).is(":hidden")) {
				
			// }
			
			// let dcb_comments_default_text_can_exist = "$dcb_comments_text_can_exist" ;
			
		}
		
		if (this.id.includes("selectSystem_location_")) {
		
		// \$("$from_venue_ids_str").change( function () {
			
			// const user_type = "$db{users}{$userid}{user_type}" ;
			const user_type = "$glod_user_level" ;
			
			// if (user_type != "casual_c" && user_type != "it" && "$userid" != "24" && "$userid" != "1" && "$userid" != "338") {
			// if (user_type <= 3 && user_type != "it" && "$userid" != "24" && "$userid" != "1" && "$userid" != "338") {
			if (user_type < 3) {
				return ;
			}
			
			let other_from_dropdown_can_exist = "$other_from_dropdown_can_exist" ;
			let other_to_dropdown_can_exist   = "$other_to_dropdown_can_exist" ;
			let other_from_text_can_exist     = "$other_from_text_can_exist" ;
			let other_to_text_can_exist       = "$other_to_text_can_exist" ;

			if (\$("#"+this.id).val() == '5') { 
			
				// \$("#$useropts{table_id} tr th:nth-last-child($last_child{item})").css("width","17%");
				// \$("#$useropts{table_id} tr th:nth-last-child($last_child{status})").css("width","7%");
				// \$("#$useropts{table_id} tr th:nth-last-child($last_child{received_by})").css("width","7%");
				
				// \$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").html("") ; 
				// \$("#$useropts{table_id} tr:eq(2) td:nth-last-child($last_child{other_to})").html("") ; 
				
				if (row_index.includes("to_")) {
					
					console.log("other to") ; 
					
					if (\$("input[name='other_from_to_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_from_to_"+event_id+"']").css("display","") ; 
					}	
					if (\$("input[name='other_from_value_was_saved_to_"+event_id+"']").val()) {
						\$("#other_from_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}	
					if (other_from_dropdown_can_exist || other_from_text_can_exist) {
						if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").is(":hidden")) {
							if (other_from_dropdown_can_exist != "0" || other_from_text_can_exist != "0") {
								\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").css("display","") ;
							}					
						}					
					}
					if (other_from_dropdown_can_exist || other_from_text_can_exist) {
						if (\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").is(":hidden")) {
							for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
								if (other_from_dropdown_can_exist != "0" || other_from_text_can_exist != "0") {
									\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_from})").css("display","") ; 
								}
							}
						}
					}		
					
				} else if (row_index.includes("from_")) {
					
					console.log("other from") ; 
					
					if (\$("input[name='other_to_from_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_to_from_"+event_id+"']").css("display","") ; 
					}		
					if (\$("input[name='other_to_value_was_saved_from_"+event_id+"']").val()) {
						\$("#other_to_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}
					if (other_to_dropdown_can_exist || other_to_text_can_exist) {
						if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").is(":hidden")) {				
							if (other_to_dropdown_can_exist != "0" || other_to_text_can_exist != "0") {
								\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").css("display","") ; 
							}
						}					
					}
					if (other_to_dropdown_can_exist || other_to_text_can_exist) {
						if (\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_to})").is(":hidden")) {
							for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
								if (other_to_dropdown_can_exist != "0" || other_to_text_can_exist != "0") {
									\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_to})").css("display","") ; 
								}
							}
						}
					}					
				}				
				
			} else if (\$("#"+this.id).val() != '5') {
				
				if (!\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").is(":hidden") && row_index.includes("to_")) {

					if (!\$("input[name='other_from_to_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_from_to_"+event_id+"']").css("display","none") ; 
						\$("#other_from_cell_"+event_id).css("background-color","") ;  
						
						console.log("HIDE other_from_cell_") ; 
						
					}	
					
					var selectIDs = ["$to_venue_ids_str_jquery"] ;
					
					let venues_as_other_selected = 0 ; 
					selectIDs.forEach(function(selectID) {
						if (\$(selectID).val() == '5' || \$(selectID).val() == 'other') {
							venues_as_other_selected = 1 ;
							return ;
						}
					});
	
					if (!venues_as_other_selected) {
						\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").css("display","none") ; 
						for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
							\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_from})").css("display","none") ; 
						}
					}
				
				} else if (!\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_to})").is(":hidden") && row_index.includes("from_")) {
					
					console.log("other_from") ; 
					
					if (!\$("input[name='other_to_from_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_to_from_"+event_id+"']").css("display","none") ; 
						\$("#other_to_cell_"+event_id).css("background-color","") ;  
					} 
					
					var selectIDs = ["$from_venue_ids_str_jquery"] ;
					
					let venues_as_other_selected = 0 ; 
					selectIDs.forEach(function(selectID) {
						if (\$(selectID).val() == '5' || \$(selectID).val() == 'other') {
							venues_as_other_selected = 1 ;
							return ;
						}
					}) ;
					
					if (!venues_as_other_selected) {
						\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").css("display","none") ; 
						for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
							\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_to})").css("display","none") ; 
						}
					}
					
				}
				// \$("#$useropts{table_id} tr th:nth-last-child($last_child{item})").css("width","14%");
			}
		}
		
		if (this.id.includes("selectVenue_location_")) {
		
			const user_type = "$glod_user_level" ;
			
			if (user_type < 3) {
				return ;
			}
			
			let other_from_dropdown_can_exist = "$other_from_dropdown_can_exist" ;
			let other_to_dropdown_can_exist   = "$other_to_dropdown_can_exist" ;
			let other_from_text_can_exist     = "$other_from_text_can_exist" ;
			let other_to_text_can_exist       = "$other_to_text_can_exist" ;

			if (\$("#"+this.id).val() == 'other') {

				if (row_index.includes("from_")) {
					
					if (\$("input[name='other_from_value_was_saved_from_"+event_id+"']").val()) {
						\$("#other_from_from_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}	
					
					if (\$("input[name='other_from_from_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_from_from_"+event_id+"']").css("display","") ; 
					}	
					
					if (\$("input[name='other_from_from_value_was_saved_to_"+event_id+"']").val()) {
						\$("#other_from_from_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}	
					
					if (other_from_dropdown_can_exist || other_from_text_can_exist) {
						if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").is(":hidden")) {
							if (other_from_dropdown_can_exist != "0" || other_from_text_can_exist != "0") {
								\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").css("display","") ;
							}					
						}					
					}
					
					if (other_from_dropdown_can_exist || other_from_text_can_exist) {
						if (\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").is(":hidden")) {
							for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
								if (other_from_dropdown_can_exist != "0" || other_from_text_can_exist != "0") {
									\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_from})").css("display","") ; 
								}
							}
						}
					}		
					
				} else if (row_index.includes("to_")) {

					if (\$("input[name='other_to_value_was_saved_to_"+event_id+"']").val()) {
						\$("#other_to_to_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}
 
					if (\$("input[name='other_to_to_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_to_to_"+event_id+"']").css("display","") ; 
					}
					
					if (\$("input[name='other_to_to_value_was_saved_from_"+event_id+"']").val()) {
						\$("#other_to_to_cell_"+event_id).css("background-color","rgb(66,73,73)") ; // rgb(66,73,73)
					}
					
					if (other_to_dropdown_can_exist || other_to_text_can_exist) {
						if (\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").is(":hidden")) {				
							if (other_to_dropdown_can_exist != "0" || other_to_text_can_exist != "0") {
								\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").css("display","") ; 
							}
						}
					}
					
					if (other_to_dropdown_can_exist || other_to_text_can_exist) {
						if (\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_to})").is(":hidden")) {
							for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
								if (other_to_dropdown_can_exist != "0" || other_to_text_can_exist != "0") {
									\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_to})").css("display","") ; 
								}
							}
						}
					}
				}
				
			} else if (\$("#"+this.id).val() != 'other') {
				
				if (!\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").is(":hidden") && row_index.includes("to_")) {
					
					\$("#other_to_to_cell_"+event_id).css("background-color","") ;
					
					if (!\$("input[name='other_to_to_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_to_to_"+event_id+"']").css("display","none") ;
						\$("#other_to_to_cell_"+event_id).css("background-color","") ;
					}
					
					var selectIDs = ["$from_venue_ids_str_jquery"] ;
					
					let venues_as_other_selected = 0 ;
					selectIDs.forEach(function(selectID) {
						if (\$(selectID).val() == '5' || \$(selectID).val() == 'other') {
							venues_as_other_selected = 1 ;
							return ;
						}
					});
	
					if (!venues_as_other_selected) {
						\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_to})").css("display","none") ; 
						for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
							\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_to})").css("display","none") ; 
						}
					}
				
				} else if (!\$("#$useropts{table_id} tr:eq(1) td:nth-last-child($last_child{other_from})").is(":hidden") && row_index.includes("from_")) {
					
					\$("#other_from_from_cell_"+event_id).css("background-color","") ;
					
					if (!\$("input[name='other_from_from_"+event_id+"']").is(":hidden")) {
						\$("input[name='other_from_from_"+event_id+"']").css("display","none") ; 
						\$("#other_from_from_cell_"+event_id).css("background-color","") ;  
					} 
					
					var selectIDs = ["$to_venue_ids_str_jquery"] ;
					
					let venues_as_other_selected = 0 ; 
					selectIDs.forEach(function(selectID) {
						console.log("selectID : "+selectID+" : "+\$(selectID).val()) ; 
						if (\$(selectID).val() == '5' || \$(selectID).val() == 'other') {
							venues_as_other_selected = 1 ;
							return ;
						}
					}) ;
					
					console.log("2 HIDE OTHER FROM COL : "+venues_as_other_selected) ; 
					
					if (!venues_as_other_selected) {
						\$("#$useropts{table_id} tr th:nth-last-child($last_child{other_from})").css("display","none") ; 
						for (let i = \$('#$useropts{table_id}').DataTable()._('tr').length; i >= 1 ; i--) {
							\$("#$useropts{table_id} tr:eq("+i+") td:nth-last-child($last_child{other_from})").css("display","none") ; 
						}
					}
				}
			} 
		}
		// }) ;
	~ ;

	&logistics_jquery($from_to) ;

	&report_xlsx_export_footer('L',15,$xlsxdir) ;

	$fnsortorder = 'desc' ;
	
	&common_min_extra_crumb("$lcpage","Search Screen") ;
	
	$trigger_jquery_raw .= qq~
		\$("#savebutt").click(function() {
			let item_val ;
			$jquery_chosen_sel
			\$("#$lcpage-form").submit() ;
		}) ;
	~ ;
	
	$extra_form_footer = qq~
		<form role="form" id="logisticsform" method="post">
			<input type="hidden" id="logisticsaction" name="iaction" value="">
			<input type="hidden" name="date_from" value="$i{date_from}">
			<input type="hidden" name="date_to" value="$i{date_to}">
			<input type="hidden" name="client_id" 			value="$i{client_id}">
			<input type="hidden" name="organisation_ids"	value="$i{organisation_ids}">
			<input type="hidden" name="supplier_id"			value="$i{supplier_id}">
			<input type="hidden" name="status_id" 			value="$i{status_id}">
			<input type="hidden" name="region_id" 			value="$i{region_id}">
		</form>
	~ ;
	
	$trigger_jquery_raw .= qq~
		function toggleToFrom (ft) {
			\$('#logisticsaction').val(ft);
			\$('#logisticsform').submit();
		}
	~;
	
	&common_min_table_update_checkbox_col(2) ;
	
	&common_min_table_datepicker_jquery ;
	&common_min_table_timepicker_jquery ;
	
	my $all_datepicker_names_str = join(",",@all_datepicker_names) ; 

	my $all_timepicker_names_str = join(",",@all_timepicker_names) ;
	
	$trigger_jquery_raw .= qq~ 
		// \$("$all_datepicker_names_str,$all_timepicker_names_str").css("display","none") ;
		// \$("span").removeClass("input-group-addon");
	~ ; 
	
	$trigger_jquery_raw .= qq~ 
		
	//	setInterval(function() {
			
		\$("$all_datepicker_ids_str,$all_timepicker_ids").on('changeDate', function(e) {

			// const date_live = new Date('$now_year-$now_mm-$now_dd\T$now_hour:$now_min:$now_sec');
			const date_live = new Date();

			let row_nr = get_row_nr(this.id,2) ;

			let delivery_date = \$("#datepickerDelivery_date_"+row_nr).data("date") ;
			let delivery_time = \$("#timepickerDelivery_time_"+row_nr).data("date") ;

			const delivery_datetime_string = delivery_date+"T"+delivery_time ; 
			const delivery_date_time = new Date(delivery_datetime_string);

			if (0 < delivery_date_time - date_live && delivery_date_time - date_live < 86400000) {  	
				\$("#datepickerDelivery_date_"+row_nr+" .glyphicon.glyphicon-calendar").css('color','red');
				\$("#timepickerDelivery_time_"+row_nr+" .glyphicon.glyphicon-time").css('color','red');
			} else {
				\$("#datepickerDelivery_date_"+row_nr+" .glyphicon.glyphicon-calendar").css('color','black');
				\$("#timepickerDelivery_time_"+row_nr+" .glyphicon.glyphicon-time").css('color','black');
			}
			 
			// setInterval(myFunction(this.id), 5000);	
			
		}) ;

	~ ;
	
} #------------------------------------------------------------------------------------------

sub val_row {
	
	my ($from_to,$id,$col_disp) = @_ ;
	
	if ($col_disp eq 'count') {			
		$val = $cali_cnt ; 
	} elsif ($col_disp eq 'nr') {		
		$val = qq~<span style="display:none;">$id\_$from_to</span>~ . &common_min_get_event_quote_button($id,$table) ; 
		$valxlsx = $id ;
	} elsif ($col_disp eq 'event') {	
		$align = qq~ class="dt-left"~ ;
		$nowrap = 'nowrap' ;
		$val = "$db{$table}{$id}{ref}" ;
		if (length($val) > 30) {
			# $val = substr($val,0,27) . qq~<i class="glyphicons glyphicons-more" title data-toggle="tooltip" data-placement="top" data-original-title="[$uc_from_to] $db{$table}{$id}{ref}"></i>~ ;
			$val = substr($val,0,27) . qq~<i class="glyphicons glyphicons-more" title data-toggle="tooltip" data-placement="top" data-original-title="$db{$table}{$id}{ref}"></i>~ ;
		}
		$valxlsx = $db{$table}{$id}{ref} ; 
	} elsif ($col_disp eq 'FT') {
		$val = uc $from_to ;
		$val = qq~<strong>~ . $val . qq~</strong>~ ;
	} elsif ($col_disp eq 'dates') {
		&logistics_dates($from_to,$id) ;				
	} elsif ($col_disp eq 'start') {
		my $mon = substr($db{$table}{$id}{date_from},5,2) ; $nowrap = 'nowrap' ;	
		$val = substr($db{$table}{$id}{date_from},8,2) . ' ' . $month_names[$mon] . substr($db{$table}{$id}{date_from},0,4) . ' @ ' . substr($db{$table}{$id}{date_from},11,5) ;
	} elsif ($col_disp eq 'start_date') {
		$val = substr($db{$table}{$id}{date_from},0,10) ; $nowrap = 'nowrap' ;
	} elsif ($col_disp eq 'stime') {
		$valxlsx = substr($db{$table}{$id}{date_from},11,5) ; $val = $valxlsx ;
	} elsif ($col_disp eq 'end') {
		my $mon = substr($db{$table}{$id}{date_to},5,2) ; $nowrap = 'nowrap' ;
		$val = substr($db{$table}{$id}{date_to},8,2) . ' ' . $month_names[$mon] . substr($db{$table}{$id}{date_to},0,4) . ' @ ' . substr($db{$table}{$id}{date_to},11,5) ;				
	} elsif ($col_disp eq 'end_date') {				
		$val = substr($db{$table}{$id}{date_to},0,10) ; $nowrap = 'nowrap' ;
	} elsif ($col_disp eq 'etime') {
		$valxlsx = substr($db{$table}{$id}{date_to},11,5) ;		
		$val = $valxlsx ;
	} elsif ($col_disp eq 'venue') {

		&logistics_venue($from_to,$id) ;

		# $valxlsx = $val ;
	} elsif ($col_disp eq 'city') {
		$val =  ($db{cities}{$db{$table}{$id}{city_id}}{city_short}) ? $db{cities}{$db{$table}{$id}{city_id}}{city_short} : $db{cities}{$db{$table}{$id}{city_id}}{name} ; $nowrap = 'nowrap' ;
		$valxlsx = $val ; 
	} elsif ($col_disp eq 'region') {
		$val = $db{regions}{$db{$table}{$id}{region_id}}{code} ;		
	} elsif ($col_disp eq 'sport') {
		# $val = '' ;
		$nowrap = 'nowrap' ;
		# foreach my $s_id (split(/\,/,$db{$table}{$id}{sport_type_ids})) {
			# $val .= qq~$db{sport_types}{$s_id}{name}<br>~ ; 
		# }
		# $val = substr($val,0,-4) if $val ;
		$val = join("<br>", map { "$db{sport_types}{$_}{name}" } split(/\,/,$db{$table}{$id}{sport_type_ids})) ;
		$valxlsx = $val ; 
	} elsif ($col_disp eq 'system') {
		$nowrap = 'nowrap' ;
		&logistics_system($from_to,$id) ; 
	} elsif ($col_disp eq 'shipment_operator') {

		$opts{$field{$col_disp}} = $opts{shipment_operator} ;
		
		if (!$readlonly_row) { 
			# $linked_from_and_to_fields{main} .= qq~"selectShipment_operator_$from_to\_$id":"selectShipment_operator_$from_to_opposite{$from_to}\_$id",~ ;			
			$cell_contains_select = 1 ;
			$opts{$field{$col_disp}} = qq~~ unless $AISA_supplier_selected ;
			
			my $val_temp = ($default_val_is_saved && $default_val) ? qq~<input value='$default_val' name='shipment_operator_value_was_saved_$from_to\_$id' style='display:none'>~ : qq~~ ; 
			
			$default_val_is_saved = 0 if !$AISA_supplier_selected ;
			
			if ($default_val_is_saved && $default_val && $AISA_supplier_selected) {
				my $field_id = $field{$col_disp} ;
				$col_disp = "s_o_$default_val" ;
				$field{$col_disp} = $field_id ;
				$opts{$field{$col_disp}} = qq~<option value='$default_val' SELECTED>$db{users}{$default_val}{name}</option>~ ; 
				$default_val = '' ;
				$preferred_placeholder{$field{$col_disp}} = "Select Shipment Operator $from_to $id" ;
			}
			$val = ($AISA_supplier_selected) ? $val_temp . &common_min_table_select($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,'',$col_disp,'','') : $val_temp ; 	
			
			# $val = &common_min_table_select($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'','',$col_name,'','') ; 	
		} else {
			$val = ($db{users}{$default_val}{name}) ? qq~$db{users}{$default_val}{name}~ : qq~~ ;
			$val .= ($db{users}{$default_val}{username} && $db{users}{$default_val}{name}) ? qq~[$db{users}{$default_val}{username}]~ : ($db{users}{$default_val}{username} && !$db{users}{$default_val}{name}) ? qq~$db{users}{$default_val}{username}~ : qq~~ ;
		}
		$hide_cell = 1 if !$AISA_supplier_selected_somewhere ;
		
	} elsif ($col_disp eq 'delivery_date') {
		
		$default_val = $fval{delivery_date}{$id}{$from_to} ;
		 
		# if (($from_to eq 'to' && $current_date <= $start_date) || ($from_to eq 'from' && $current_date <= $end_date)) { 
		
		my $col = "d_$from_to" ;
		$tooltip_updated .= "Last Updated : $prev_update_time{date}{$col}" if $prev_update_time{date}{$col} ;
		$tooltip_updated .= " , " if $tooltip_updated ;
		$tooltip_updated .= "Changed By : $db{users}{$prev_update_time{user}{$col}}{name}" if $prev_update_time{user}{$col} && $prev_update_time{date}{$col} ;
		
		if (!$readlonly_row) { 
		
			$fval{delivery_time}{$id}{$from_to} =~ s/\-/\:/g ;
			
			my $difference = &common_time_diff_seconds("$now_year-$now_mm-$now_dd $now_hour:$now_min:$now_sec","$fval{delivery_date}{$id}{$from_to} $fval{delivery_time}{$id}{$from_to}") ;
			
			if ($difference > 0 && $difference < 86400) {
				$time_icon_color = 'red' ;
			}
			
			# $val = &common_min_table_datepicker($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved,$col_nm,'',$time_icon_color,$update_tooltip,$tooltip_updated) ; 
			$val = &common_min_table_datepicker($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,'',$col_nm,'',$time_icon_color,$update_tooltip,$tooltip_updated) ; 
		} else {
			$nowrap = 'nowrap' ; 
			$tooltip_updated = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_updated'~ if $tooltip_updated ;
			$val    = qq~<span $tooltip_updated>$default_val</span>~ ;
		}		
	} elsif ($col_disp eq 'delivery_time') {
		$default_val = $fval{delivery_time}{$id}{$from_to} ;
		# if (($from_to eq 'to' && $current_date <= $start_date) || ($from_to eq 'from' && $current_date <= $end_date)) {

		my $col = "t_$from_to" ;
		$tooltip_updated .= "Last Updated : $prev_update_time{date}{$col}" if $prev_update_time{date}{$col} ;
		$tooltip_updated .= " , " if $tooltip_updated ;
		$tooltip_updated .= "Changed By : $db{users}{$prev_update_time{user}{$col}}{name}" if $prev_update_time{user}{$col} && $prev_update_time{date}{$col} ;
		
		if (!$readlonly_row) {
			my $col = "t_$from_to" ;
			# $val    = &common_min_table_timepicker($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved,$col_nm,'',$time_icon_color,$tooltip_updated) ; 	
			$val    = &common_min_table_timepicker($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,'',$col_nm,'',$time_icon_color,$tooltip_updated) ; 	
		} else {
			$nowrap = 'nowrap' ; 
			$tooltip_updated = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_updated'~ if $tooltip_updated ;
			$val    = qq~<span $tooltip_updated>$default_val</span>~ ;
		}
	} elsif ($col_disp eq 'received_by' || (substr($col_disp,0,5) eq 'other' && $col_disp ne 'other_special_instructions')) {
		
		# $hide_cell = 1 if substr($col_disp,0,5) eq 'other' && $db{$table}{$eid}{logistics_system_location_id} ne '5' && !$other_system_location_selected_somewhere ;
		
		my $val_temp = qq~~ ; 
		
		if (substr($col_disp,0,5) eq 'other') {
			# $hide_cell = ($other_system_location_selected_somewhere && ($glod_user_level > 3 || $db{users}{$userid}{user_type} eq "it" || $userid eq "24" || $userid eq "1" || $userid eq "338")) ? 0 : 1 ;
			
			# $default_val = $fval{system_location}{$id}{$from_to} ;
			
			$val_temp = ($default_val_is_saved && $default_val) ? qq~<input value='$default_val' name='$col_disp\_value_was_saved_$from_to\_$id' style='display:none'>~ : qq~~ ; 
			$default_val_is_saved = 0 if (!$other_from_system_location_is_saved && $from_to eq 'to' && $col_disp eq 'other_from') || (!$other_to_system_location_is_saved && $from_to eq 'from' && $col_disp eq 'other_to') || (!$other_from_venue_location_is_saved && $from_to eq 'from' && $col_disp eq 'other_from') || (!$other_to_venue_location_is_saved && $from_to eq 'to' && $col_disp eq 'other_to') ;
			$hide_cell = (((($other_from_system_location_selected_somewhere || $other_from_venue_selected_somewhere) && $col_disp eq 'other_from') || (($other_to_system_location_selected_somewhere || $other_to_venue_selected_somewhere) && $col_disp eq 'other_to')) && $glod_user_level >= 3) ? 0 : 1 ;		 
		}
		# $db{$table}{$id}{logistics_system_location_id}
		my $acx = (substr($col_disp,0,5) eq 'other') ? "$col_disp\_$from_to\_location\_$id" : "$col_disp\_$id" ;
		$preferred_placeholder{$field{$col_disp}} = join " ", map { ucfirst $_ } split "_" , $acx ;
		# if (($from_to eq 'to' && $current_date <= $start_date) || ($from_to eq 'from' && $current_date <= $end_date)) { 
		
		if (!$readlonly_row) { 
			$only_display_val = 0 ; $found_editable = 1 ; 	
		} else {
			$only_display_val = 1 ;
		}
		
		if ($col_disp eq 'received_by') {
			push @from_to_field_ids,"#inputReceived_by_$from_to\_$id" ;
			$linked_from_and_to_fields{main} .= qq~"inputReceived_by_$from_to\_$id":"inputReceived_by_$from_to_opposite{$from_to}\_$id",~ ;	
			
		} else {
			push @from_to_field_ids,"#inputOther_$from_to_opposite{$from_to}\_$from_to\_$id" ;
			$linked_from_and_to_fields{main} .= qq~"inputOther_$from_to_opposite{$from_to}\_$from_to\_$id":"inputOther_$from_to\_$from_to_opposite{$from_to}\_$id",~ ;				
		}
		
		$preferred_placeholder{$field{$col_disp}} =~ s/To /System /g if $col_disp =~ /other/ && $from_to eq 'to' ;
		$preferred_placeholder{$field{$col_disp}} =~ s/From /System /g if $col_disp =~ /other/ && $from_to eq 'from' ;

		$val = # (($col_disp eq 'other_from' && $from_to eq 'from') || ($col_disp eq 'other_to' && $from_to eq 'to')) ? "" : 
				(
					(
						($db{$table}{$id}{logistics_system_location_id} =~ /-5$/ && $col_disp eq 'other_to' && $from_to eq 'from') || 
						($db{$table}{$id}{logistics_system_location_id} =~ /^5-/ && $col_disp eq 'other_from' && $from_to eq 'to') || 
						($db{$table}{$id}{logistics_venue_id} =~ /-other$/ && $col_disp eq 'other_to' && $from_to eq 'to') || 
						($db{$table}{$id}{logistics_venue_id} =~ /^other-/ && $col_disp eq 'other_from' && $from_to eq 'from')
					) || 
					$col_disp !~ /other/
				) ? $val_temp . &common_min_table_input($field{$col_disp},$default_val,$only_display_val,$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved) :
					$val_temp . &common_min_table_input($field{$col_disp},$default_val,$only_display_val,$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved,'','',1) ;
		
		$other_from_text_can_exist 	= ($col_disp eq 'other_from' && $from_to eq 'to'   && $default_val) ? 1 : 0 ; 
		$other_to_text_can_exist 	= ($col_disp eq 'other_to'   && $from_to eq 'from' && $default_val) ? 1 : 0 ;
		
		# if (substr($col_disp,0,5) eq 'other' && ($glod_user_level > 3 || $db{users}{$userid}{user_type} eq "it" || $userid eq "24" || $userid eq "1" || $userid eq "338")) {
			# # Do Nothing
		# } elsif (substr($col_disp,0,5) eq 'other') {
			# $valxlsx = "" ;
		# }
	} elsif ($col_disp eq 'operator') {
		$val = ($op_ids[$system_count - 1]) ? $db{users}{$op_ids[$system_count - 1]}{name} : '' ;				
		$formatting = $format92 if $cali_cnt == 2 ;	
	# } elsif ($col_disp eq 'waybill_nr_from' || $col_disp eq 'waybill_nr_to') {
	} elsif ($col_disp eq 'waybill_nr') {
		my $fname   = 'waybill_nr' ; $val = '' ; $valxlsx = 'MUSTBEBLANK' ;
		my $wb_code = 'AIS' . uc substr($from_to,0,1) . '_' ;
		if (!$readlonly_row) {
			$found_editable = 1 ;  
			my $wb_nr = ($default_val) ? $default_val : $wb_code . $id ; 
			$waybill_nr_default_val = ($wb_nr) ? 1 : 0 ;
			# $val = &common_min_table_input($field{$col_disp},$wb_nr,'',$cali_cnt,$column_nr_cnt,'',$default_val_is_saved,$col_disp) ; 
			$val = &common_min_table_input($field{$col_disp},$wb_nr,'',$cali_cnt,$column_nr_cnt,'','',$col_disp) ; 
			$valxlsx = $wb_nr ;
		} else {
			$val = ($logistics_waybill_ids_hash{$id}{$from_to}) ? $logistics_waybill_ids_hash{$id}{$from_to} : (!$readlonly_row) ? $id : "" ;
			$valxlsx = $val ;
		}	
		# &logistics_waybill_nr($from_to,$id) ;
	# } elsif ($col_disp eq 'special_instructions_to' || $col_disp eq 'special_instructions_from') {
	} elsif ($col_disp eq 'special_instructions') {
		if (!$readlonly_row) {
			$found_editable = 1 ; 
			$default_val_is_saved = 1 if $default_val ; 
			# $val = &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'',$default_val_is_saved) ;
			$val = &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'','') ;
		} elsif ($default_val) {
			$val = $default_val ; 
			$valxlsx = $val ;
		} 
	} elsif ($col_disp eq 'other_special_instructions') {
		if ($from_to eq 'to') {
			$val = "" ;
			return ;
		}
		# if ($current_date <= $end_date) {
		if (!$readlonly_row) {
			$found_editable = 1 ; 
			$default_val_is_saved = 1 if $default_val ; 
			# $val = &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'',$default_val_is_saved) ;
			$val = &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'','') ;
		} elsif ($defualt) {
			$val = $defualt ; 
			$valxlsx = $val ;
		} 
	# } elsif ($col_disp eq 'create_waybill_out' || $col_disp eq 'create_waybill_in' || $col_disp eq 'process_complete') {
	# } elsif ($col_disp eq 'create_waybill' || $col_disp eq 'process_complete') {
	} elsif ($col_disp eq 'dcb_comments') {
		if (!$readlonly_row) {
			$found_editable = 1 ; 
			$default_val_is_saved = 1 if $default_val ; 
			my $hide_textarea = ($supplier_default_val ne '2') ? 1 : 0 ; 
			my $val_temp = qq~<input value='$default_val_is_saved' name='dcb_comments_value_was_saved_$from_to\_$id' style='display:none'>~ ; 
			$default_val_is_saved = 0 if $hide_textarea ;
			# $val = $val_temp . &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'',$default_val_is_saved,'','',$hide_textarea) ;
			$val = $val_temp . &common_min_table_textarea($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,'','','','',$hide_textarea) ;
		} elsif ($default_val) {
			$val = $default_val ; 
		} 
		$valxlsx = $default_val ;
		$hide_cell = 1 if !$dcb_supplier_is_selected_somewhere ;
	} elsif ($col_disp eq 'create_waybill') {
		&logistics_create_waybill($from_to,$id) ;
		push @from_to_checkbox_field_ids,"#create_waybill_$from_to\_$id" ;
		$linked_from_and_to_fields{checkboxes} .= qq~"create_waybill_$from_to\_$id":"create_waybill_$from_to_opposite{$from_to}\_$id",~ ;	
	} elsif ($col_disp eq 'update') {
		
		chop $default_javascript if $default_javascript ;
		
		unless ($dcb_waybill_nr{$from_to}) {
			$default_javascript .= qq~,~ if $default_javascript ;
			$default_javascript .= qq~"create_waybill_$from_to\_$id":""~ ;
		}
		# if (!$dcb_waybill_nr{$from_to} && $logistics_waybill_ids_hash{$id}{$from_to} && $items && $supp{$from_to} == 2) {
			# $default_javascript .= qq~,~ if $default_javascript ;
			# $default_javascript .= qq~"create_waybill_$from_to\_$id":""~ ;
		# }
		
		if ($default_javascript) {
			push @default_javascript_by_id, qq~"$from_to\_$id":{$default_javascript}~ ;
			push @all_update_field_ids,"#$ucfirstfield{1},#$ucfirstfield{2},#$ucfirstfield{supplier},#$ucfirstfield{status},#$ucfirstfield{waybill_nr},#$ucfirstfield{item},#$ucfirstfield{poc},#$ucfirstfield{special_instructions},#$ucfirstfield{delivery_date},#$ucfirstfield{delivery_time},#$ucfirstfield{received_by}" ;
			push @all_update_field_ids,"#$ucfirstfield{other_from}" if $ucfirstfield{other_from} ;
			push @all_update_field_ids,"#$ucfirstfield{other_to}" if $ucfirstfield{other_to} ; #$ucfirstfield{other_special_instructions}
			push @all_update_field_ids,"#$ucfirstfield{other_special_instructions}" if $ucfirstfield{other_special_instructions} ne 'input' ;
			push @all_update_field_ids,"#create_waybill_$from_to\_$id" ;
			push @all_update_field_ids,"#$ucfirstfield{dcb_comments}" ;
			push @all_update_field_ids,"#$ucfirstfield{shipment_operator}" ;
		} 
		# if (($from_to eq 'to' && $current_date <= $end_date) || ($from_to eq 'from' && $current_date <= $start_date)) {
		if (($from_to eq 'to' && $current_date <= $start_date) || ($from_to eq 'from' && $current_date <= $end_date)) {
			$quote_and_ops_ids .= qq~"$id":"1",~ ; 
			$val = &common_min_table_checkbox($field{update}) ; 
			$logistics_supplier_ids_hash{$id}{$from_to} = "" unless $logistics_supplier_ids_hash{$id}{$from_to} ; 
			$val = qq~<i class="glyphicon glyphicon-ok"></i>~ if $dcb_waybill_nr{$from_to} && $logistics_waybill_ids_hash{$id}{$from_to} && $items && $supp{$from_to} == 2 ;
		} else {
			$val = qq~<i class="glyphicon glyphicon-remove"></i>~ ;
		}
		$valxlsx = '' ;
		$formatting = $format1 ;		 	
	}

	if (!$valxlsx && $col_disp ne 'dcb_comments' && $col_disp ne 'delivery_date' && $col_disp ne 'delivery_time' && $col_disp ne 'received_by' && $col_disp ne 'item' && substr($col_disp,0,5) ne 'other') {
		$valxlsx = $val ;
	}
	
	$valxlsx =~ s/\MUSTBEBLANK//g ;
	
	if ($col_disp eq 'dates') {
		$valxlsx =~ s/\<br>//g ; 
	} elsif ($col_disp eq 'sport') {
		$valxlsx =~ s/\<br>/\;/g ; 
	}

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

# sub last_updated_tooltip {
	
	# my ($col) = @_ ; 
	# return "title data-toggle='tooltip' data-placement='top' data-original-title='Last Updated : $prev_update_time{$col}'" ;
	
# } #------------------------------------------------------------------------------------------

sub fields_row {
	
	my ($from_to,$id) = @_ ;
	
	our %field = () ;

	$field{1}          	          		= "venue_location_$from_to\_$id" ;
	$field{2}          	          		= "system_location_$from_to\_$id" ;
	$field{supplier}					= "logistics_supplier_id_$from_to\_$id" ;
	$field{status}						= "status_$from_to\_$id" ;
	$field{waybill_nr}					= "waybill_nr_$from_to\_$id" ;
	$field{item}     	          		= "item_$from_to\_$id" ;
	$field{poc}     	          		= "poc_$from_to\_$id" ;
	$field{other_special_instructions}	= ($from_to eq 'from') ? "other_special_instructions_$from_to\_$id" : "" ;
	$field{special_instructions}		= "special_instructions_$from_to\_$id" ;
	$field{update}                		= "update_$from_to\_$id" ;
	$field{delivery_date}				= "delivery_date_$from_to\_$id" ;
	$field{delivery_time}				= "delivery_time_$from_to\_$id" ;
	$field{received_by}					= "received_by_$from_to\_$id" ;
	$field{other_from}					= "other_from_$from_to\_$id" ;
	$field{other_to}					= "other_to_$from_to\_$id" ;
	$field{dcb_comments}				= "dcb_comments_$from_to\_$id" ;
	$field{shipment_operator}			= "shipment_operator_$from_to\_$id" ;
	
	our %ucfirstfield = () ;
	
	foreach (keys %field) {
		$ucfirstfield{$_} = ucfirst $field{$_} ;
		if ($_ eq 'update') {
			$ucfirstfield{$_} = qq~checkbox~ . $ucfirstfield{$_}  ;
		} elsif ($_ eq 'waybill_nr'|| $_ eq 'received_by' || (substr($_,0,5) eq 'other' && $_ ne 'other_special_instructions')) {
			$ucfirstfield{$_} = qq~input~ . $ucfirstfield{$_}  ;
		} elsif ($_ eq 'special_instructions' || $_ eq 'other_special_instructions' || $_ eq 'dcb_comments') {
			$ucfirstfield{$_} = ($ucfirstfield{$_}) ? qq~textarea~ . $ucfirstfield{$_} : qq~1~ ;
		} elsif ($_ eq 'delivery_date') {
			$ucfirstfield{$_} = qq~datepicker~ . $ucfirstfield{$_}  ;
		} elsif ($_ eq 'delivery_time') {
			$ucfirstfield{$_} = qq~timepicker~ . $ucfirstfield{$_}  ;
		} else {
			$ucfirstfield{$_} = qq~select~ . $ucfirstfield{$_} ;
		} 
	}
	
	$event_venue_ids                            .= qq~#$ucfirstfield{1},~ ;
	$system_location_ids                        .= qq~#$ucfirstfield{2},~ ;
	$all_location_ids_per_system{$event_sys_id} .= qq~#$ucfirstfield{2},~ ; 
	$seen_field_id{$ucfirstfield{2}}             = 1 ;

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

sub color_row {
	
	my ($from_to,$id,$col_disp) = @_ ;
	
	my $tooltip_updated  = qq~~ ;
	
	if ($col_disp =~ /status/ && $from_to eq 'to') {
		$tooltip_updated .= "Last Updated : $prev_update_time{date}{s_to}" if $prev_update_time{date}{s_to} ;
		$tooltip_updated .= " , " if $tooltip_updated ;
		$tooltip_updated .= "Changed By : $db{users}{$prev_update_time{user}{s_to}}{name}" if $prev_update_time{user}{s_to} && $prev_update_time{date}{s_to} ;
	}
	
	if ($col_disp =~ /supplier/ || $col_disp =~ /status/ || $col_disp eq 'item' || $col_disp eq 'poc') {
		my $col_name = $col_disp ; 
		# if (($from_to eq 'to' && $current_date <= $start_date) || ($from_to eq 'from' && $current_date <= $end_date)) {
		
		$opts{$col_disp} = $opts2{status} if $col_disp =~ /status/ ;
		
		if (!$readlonly_row) {
			$found_editable = 1 ; 
			$preferred_title{$field{$col_disp}} = $preferred_title{$col_disp} ; 				
			if ($default_val) {
				$valxlsx = $db{$fval_table{$col_disp}}{$default_val}{name} ;	
				if ($col_disp =~ /status/) {
					# $default_val_is_saved = 1 if $default_val ;
					if ($default_val eq '1') { #Booked
						$background_color = "rgb(81,203,242)" ;
					} elsif ($default_val eq '2') { #Pending
						$background_color = "rgb(66,73,73)" ;
					} elsif ($default_val eq '3') { #In Transit
						$background_color = "rgb(255,165,0)" ;
					} elsif ($default_val eq '4') { #Delivered
						$background_color = "rgb(46,204,113)" ;
					} else {
						$default_val = 2 ;
					}
					
					# if ($default_val_is_saved) {
						# $col_name = "status_$default_val" ;
						# $opts{$col_name} = qq~<option value='$default_val' SELECTED >$db{status_hash}{$default_val}{name}</option>~ ;
						# $default_val = '' ;
					# }
					
					# &common_debug(">>>> color_row : $background_color") ;
				}  
			}	
			
			if ($col_disp eq 'item') {
				$multiple{$field{item}} = 1 ;
				$opts{$field{item}} = $opts{item} ;			
				$col_name = "item$system_count" ;
 
				if (!$default_val) {
					$default_val = $logistics_items_qty{$system_count} ;
				}
				
				if ($default_val && $default_val_is_saved) {
					$col_name = "item_$default_val" ;
					$col_name =~ s/\,/\_/g ;	
					$opts{$field{item}}	= join ('', map { "<option value='$_' SELECTED >$db{logistics_items}{$_}{name}</option>" } split /\,/, $default_val);	
					$default_val = '' ;
				}
				
				
				$item_default_val = $default_val ;
				
				$valxlsx = join (';', map { $db{logistics_items}{$_}{name} } split /\,/, $default_val);	
 
				$extra_form_fields .= qq~<input type="hidden" name="item_multi_$from_to\_$id" id="item-$from_to-$id" value="">~;				
				$jquery_chosen_sel .= qq~
					item_val = \$("#selectItem_$from_to\_$id").chosen().val() ; 
					\$("#item-$from_to\-$id").val(item_val) ;  
				~ ;
				# $col_name = $col_disp ; 
				
				push @from_to_multi_field_ids,"#selectItem_$from_to\_$id" ;
				$linked_from_and_to_fields{multi_selects} .= qq~"selectItem_$from_to\_$id":"selectItem_$from_to_opposite{$from_to}\_$id",~ ;	
				
			} elsif ($col_disp eq 'poc') {
				# $col_name = $default_val ;
				$col_name = '' ;
				$opts{$field{poc}} = $opts{poc}{$id} ;
				push @from_to_field_ids,"#selectPoc_$from_to\_$id" ;
				$linked_from_and_to_fields{main} .= qq~"selectPoc_$from_to\_$id":"selectPoc_$from_to_opposite{$from_to}\_$id",~ ;	
				$valxlsx = $default_val ;
				# $opts{$field{poc}} =~ s/value="$default_val"/value="$default_val" SELECTED/g ;
				$default_val = '' ;
				# $default_val =~ s/ /\_/g ;
			}
			# elsif ($col_disp eq 'status') {
				# $col_name                = '' ;
				# $opts{$field{$col_disp}} = $opts{$col_disp} ;
				# # &common_debug(">>>> color_row : $from_to,$id,$col_disp [$field{$col_disp}] [$col_name] [$opts{$field{$col_disp}}]") ;
			# }
			
			if ($col_disp =~ /supplier/) {
				push @supplier_ids_arr,$ucfirstfield{$col_disp} ;
				$supplier_default_val = ($default_val_is_saved) ? $default_val : 2 ;
				$AISA_supplier_selected = 1 if $AISA_supplier{$supplier_default_val} ;
				$dcb_supplier_is_selected = 1 if $supplier_default_val eq '2' ;
				
				# if ($default_val && $default_val_is_saved) {
					# $col_name = "supplier_$default_val" ; 
					# $opts{$field{$col_disp}} = "<option value='$default_val' SELECTED >$db{logistics_suppliers}{$default_val}{supplier}</option>" ;
					# $default_val = '' ;
				# }
				
			}
			$cell_contains_select = 1 ;
			# $val = &common_min_table_select($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved,$col_name,'','') ; 	
 
			$val = &common_min_table_select($field{$col_disp},$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,'',$col_name,'','') ; 	
			if ($tooltip_updated) {
				$tooltip_updated = qq~title data-toggle='tooltip' data-placement='left' data-original-title='$tooltip_updated'~ ;
				$val = qq~<div style="height:20px;" $tooltip_updated>$val</div>~ ;
			}
		} elsif ($default_val) {
			$val     = $db{$fval_table{$col_disp}}{$default_val}{name} ;
			$valxlsx = $val ;
			if ($col_disp =~ /status/) {
				if ($default_val eq '1') { #Booked
					$background_color = "rgb(81,203,242)" ;
				} elsif ($default_val eq '2') { #Pending
					$background_color = "rgb(66,73,73)" ;
				} elsif ($default_val eq '3') { #In Transit
					$background_color = "rgb(255,165,0)" ;
				} elsif ($default_val eq '4') { #Delivered
					$background_color = "rgb(46,204,113)" ;
				}
				if ($tooltip_updated) {
					$tooltip_updated = qq~title data-toggle='tooltip' data-placement='top' data-original-title='$tooltip_updated'~ ;
					$val = qq~<span $tooltip_updated>$val</span>~ ;
				}
			} elsif ($col_disp eq 'item') {
				$valxlsx = join (',', map { $db{logistics_items}{$_}{name} } split /\,/, $default_val)  ;
				$val = $valxlsx if $default_val_is_saved ;
				$valxlsx =~ s/\,/\;/g ;						
			} elsif ($col_disp eq 'poc') {
				$val = $default_val ; 
			}
		} 
	}
	
} #------------------------------------------------------------------------------------------

sub from_to_row {
	
	my ($from_to,$id,$col_disp) = @_ ;
	
	if ($col_disp eq 'from' || $col_disp eq 'to') {	
				
		$default_val = '' ; my $field_id = "" ;
		
		my $col_nm = $col_disp ; 
	
		if (!$readlonly_row) {
				
			my $changed_val = qq~~ ;
				
			if (($col_disp eq 'to' && $from_to eq 'to') || ($col_disp eq 'from' && $from_to eq 'from')) { # && $iaction eq 'report') || ($col_disp eq 'from' && $col_disp eq 'report_from')
			
				$linked_from_and_to_fields{main} .= qq~"$ucfirstfield{1}":"selectVenue_location_$from_to_opposite{$from_to}\_$id",~ ;
				
				push @from_to_field_ids,"#$ucfirstfield{1}" ;
				# $fval{system_location}{$id}{$from_to}		
				# $fval{venue_location}{$id}{$from_to}		
				# $default_val          = $db{$table}{$id}{logistics_venue_id} ;
				$default_val          = $fval{venue_location}{$id}{$from_to} ;
				$default_val_is_saved = ($default_val) ? 1 : 0 ;	
				$field_id             = $field{1} ; 
				$default_val          = $venue_ids[0] unless $default_val ; 
				$valxlsx              = "$db{organisations}{$default_val}{name} [$db{organisations}{$default_val}{region_code}]" if $db{organisations}{$default_val}{region_code} ;

				$opts{$field_id}      = $opts{venue_location}{$db{$table}{$id}{region_id}} ;

				$col_nm               = "$col_disp\_$db{$table}{$id}{region_id}" ;
				
				if ($default_val_is_saved && $default_val && $default_val ne 'other') {
					$opts{$field_id} = qq~<option value='$default_val' SELECTED>$db{organisations}{$default_val}{name}</option>~ ;
					$col_nm = "" ;
					$default_val = '' ;
				} elsif ($default_val_is_saved && $default_val && $default_val eq 'other') {
					$opts{$field_id} = qq~<option value='other' SELECTED>OTHER</option>~ ;
					$col_nm = "" ;
					$default_val = '' ;					
				}
				
				$changed_val = qq~<input name='changed_venue_location_$id' value='$default_val_is_saved'>~ unless $seen_event{venue_location}{$id} ;
				$seen_event{venue_location}{$id} = 1 ;
				
				push @from_venue_ids,"#select"  . ucfirst $field_id if $field_id && $field_id =~ /_to_/ ;
				push @to_venue_ids,"#select"  . ucfirst $field_id if $field_id && $field_id =~ /_from_/ ;

				$other_from_venue_location_is_saved = 1 	if $db{$table}{$id}{logistics_venue_id} =~ /^other-/ ;
				$other_to_venue_location_is_saved = 1 		if $db{$table}{$id}{logistics_venue_id} =~ /-other$/ ;
				
			} else {
				
				$other_from_dropdown_can_exist = 1 if $col_disp eq 'from' && $from_to eq 'to' ;
				$other_to_dropdown_can_exist = 1 if $col_disp eq 'to' && $from_to eq 'from' ;
 
				$linked_from_and_to_fields{main} .= qq~"$ucfirstfield{2}":"selectSystem_location_$from_to_opposite{$from_to}\_$id",~ ;
				
				push @from_to_field_ids,"#$ucfirstfield{2}" ;
				
				$field_id = $field{2} ;
				my $found_loc = 0 ;
				
				# $hide_cell = 1 if $db{$table}{$id}{logistics_system_location_id} ne '5' ;
				
				$other_from_system_location_is_saved = 1 	if $db{$table}{$id}{logistics_system_location_id} =~ /^5-/ ;
				$other_to_system_location_is_saved = 1 		if $db{$table}{$id}{logistics_system_location_id} =~ /-5$/ ;
				
				# if ($db{$table}{$id}{logistics_system_location_id}) {
				if (($db{$table}{$id}{logistics_system_location_id} =~ /^\d+-/ && $from_to eq 'to') || ($db{$table}{$id}{logistics_system_location_id} =~ /-\d+$/ && $from_to eq 'from')) {
					# $default_val = $db{$table}{$id}{logistics_system_location_id} ;
					$default_val = $fval{system_location}{$id}{$from_to} ;
					$default_val_is_saved = 1 ;
					$found_loc = 1 ;
				}
				
				if (!$found_loc) {
					foreach my $event_sys_id (sort @system_name_ids) {
						next unless $event_sys_id ;
						next unless $db{event_systems}{$event_sys_id}{system_type} ;
						if ($db{event_systems}{$event_sys_id}{logistics_location_id}) {
							$valxlsx              = $db{logistics_locations}{$db{event_systems}{$event_sys_id}{logistics_location_id}}{location} ;
							$default_val          = $db{event_systems}{$event_sys_id}{logistics_location_id} ;
							$default_val_is_saved = 0 ;
							$found_loc            = 1 ;
						}
						last ;
					}
				}

				$default_val_is_saved = 0 if !$found_loc ;
				$default_val = &check_city_match($found_loc,$db{cities}{$db{$table}{$id}{city_id}}{name}) unless $default_val ;
				$opts{$field_id} = $opts{system_location} ;
				push @from_venue_ids,"#select"  . ucfirst $field_id if $field_id && $field_id =~ /_from_/ ;
				push @to_venue_ids,"#select"  . ucfirst $field_id if $field_id && $field_id =~ /_to_/ ;
				# $seen_other = 1 if $default_val == 5 ; 
				# if ($default_val_is_saved && $default_val) {
					# $opts{$field_id} = qq~<option value='$default_val' SELECTED>$db{logistics_locations}{$default_val}{location}</option>~ ;
					# $col_nm = "loc_$default_val" ; 
					# $default_val = '' ;
					
				# }
				# $default_val_is_saved
				$changed_val = qq~<input name='changed_system_location_$id' value='$default_val_is_saved'>~ unless $seen_event{system_location}{$id} ;
				$seen_event{system_location}{$id} = 1 ;
				
			}
			$cell_contains_select = 1 ;
			# $val = &common_min_table_select($field_id,$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,$default_val_is_saved,$col_nm) ; 
			
			$val = $changed_val . &common_min_table_select($field_id,$default_val,'',$cali_cnt,$column_nr_cnt,$background_color,'',$col_nm) ; 					
			# &common_debug("2. system_location >>>>>> from_to_row : $field_id : [$db{$table}{$id}{logistics_system_location_id}] [$opts{$field_id}]");
		} else {
			if (($col_disp eq 'to' && $from_to eq 'to') || ($col_disp eq 'from' &&  $from_to eq 'from')) { 
				# $default_val          = $db{$table}{$id}{logistics_venue_id} ;
				$default_val          = $fval{venue_location}{$id}{$from_to} ;
				$default_val_is_saved = ($default_val) ? 1 : 0 ;							
				$default_val          = $venue_ids[0] unless $default_val ; 
				if ($default_val) {
					$val  = qq~$db{organisations}{$default_val}{name}~ ; 
					$val .= qq~ [$db{organisations}{$default_val}{region_code}]~ if $db{organisations}{$default_val}{region_code} ; 
				}
			} else {
				my $found_loc = 0 ;
				
				# $hide_cell = 1 if $db{$table}{$id}{logistics_system_location_id} ne '5' ;
				
				if ($db{$table}{$id}{logistics_system_location_id}) { 
					# $val = $db{logistics_locations}{$db{$table}{$id}{logistics_system_location_id}}{location} ;
					$val = ($from_to eq 'from') ? $db{logistics_locations}{$fval{system_location}{$id}{to}}{location} : $db{logistics_locations}{$fval{system_location}{$id}{from}}{location} ;
					$found_loc = 1 ; 
				}
				
				$default_val_is_saved = ($db{$table}{$id}{logistics_system_location_id} && (($from_to eq 'from' && $fval{system_location}{$id}{to}) || ($from_to eq 'to' && $fval{system_location}{$id}{from}))) ? 1 : 0 ; 
				
				if (!$found_loc) {
					foreach my $event_sys_id (sort @system_name_ids) { 
						next unless $event_sys_id ;
						next unless $db{event_systems}{$event_sys_id}{system_type} ;
						if ($db{event_systems}{$event_sys_id}{logistics_location_id}) { 
							$val = $db{logistics_locations}{$db{event_systems}{$event_sys_id}{logistics_location_id}}{location} ; 
							$found_loc = 1 ; 
							last ;
						} 
					}
				}

				$match = (!$db{$table}{$id}{logistics_system_location_id}) ? &check_city_match($found_loc,$db{cities}{$db{$table}{$id}{city_id}}{name}) : qq~~ ;
				# $match = &check_city_match($found_loc,$db{cities}{$db{$table}{$id}{city_id}}{name}) ;
				$val   = $db{logistics_locations}{$match}{location} if $match ; 
				
			}
			$valxlsx = $val ;
		}
	}

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

sub check_city_match {
	
	my ($found_loc,$city) = @_ ;
	
	my $match = 0 ;
	
	if (!$found_loc && $city =~ /Johannesburg/) {
		$match = 1 ;
	} elsif (!$found_loc && $city =~ /Cape Town/) {
		$match = 2 ;	
	} elsif (!$found_loc && $city =~ /Durban/) {
		$match = 3 ;
	} elsif (!$found_loc && $city =~ /Bloemfontein/) {
		$match = 4 ;
	}
	
	&common_debug("check_city_match found_loc=$found_loc, city=$city >>>>>> match = $match [$db{logistics_locations}{$match}{location}]");
	
	return ($match) ;
	
} #------------------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'logistics-report' ;

	&common_page_name ;
	   
	# our $table     = 'event_quotes' ;
	our $table     = 'event_quotes' ;
	$page_title    = 'Logistics Report'  ;

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

sub thead {
	
	&common_min_thead ;
	
} #-------------------------------------------------------------------------------  

sub screen3 {
	
	our %custom_column_styles = () ; 

	$custom_column_styles{system} 						= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{from} 						= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{to} 							= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{supplier} 					= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{poc} 							= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{item} 						= qq~style="min-width:325px;max-width:325px;"~ ;
	$custom_column_styles{delivery_time} 				= qq~style="min-width:105px;max-width:105px;"~ ;
	$custom_column_styles{delivery_date} 				= qq~style="width:4.5%;min-width:120px;"~ ;
	$custom_column_styles{special_instructions} 		= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{other_special_instructions}	= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{waybill_nr} 					= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{status} 						= qq~style="width:7%;min-width:120px;"~ ;
	$custom_column_styles{received_by} 					= qq~style="width:7%;min-width:120px;"~ ;

	# if ($seen_other && ($glod_user_level > 3 || $db{users}{$userid}{user_type} eq "it" || $userid eq "24" || $userid eq "1" || $userid eq "338")) {	
	if (($other_from_system_location_selected_somewhere || $other_from_venue_selected_somewhere) && $glod_user_level >= 3) {	
		$custom_column_styles{other_from}	= qq~style="width:7%;min-width:120px;"~ ;
	} else {
		$custom_column_styles{other_from}	= qq~style="width:7%;min-width:120px;display:none;"~ ;
	}	
	if (($other_to_system_location_selected_somewhere || $other_to_venue_selected_somewhere) && $glod_user_level >= 3) {	
		$custom_column_styles{other_to}		= qq~style="width:7%;min-width:120px;"~ ;
	} else {
		$custom_column_styles{other_to}		= qq~style="width:7%;min-width:120px;display:none;"~ ;
	}
	
	if (!$dcb_supplier_is_selected_somewhere) {
		$custom_column_styles{dcb_comments}	= qq~style="width:7%;min-width:120px;display:none;"~ ;
	} else {
		$custom_column_styles{dcb_comments}	= qq~style="width:7%;min-width:120px;"~ ;
	}

	if ($AISA_supplier_selected_somewhere) {
		$custom_column_styles{shipment_operator} = qq~style="width:7%;min-width:120px;"~ ;
	} else {
		$custom_column_styles{shipment_operator} = qq~style="width:7%;min-width:120px;display:none;"~ ;
	}

	&thead ;

	if ($isaved) { $alert = &common_min_alert('success',"$ucpage $isaved!",'ok') ; } else { &common_min_alert_type ; }

	my $page = "event-quotes" ;

	if ($s{no}) { $page = $lcpage ; }

	our $list = 0 ;

	&common_min_table('id',$page,'list') ;
	
	# &common_min_table_select_jquery("#$useropts{table_id} td:nth-last-child($last_child{status}),#$useropts{table_id} td:nth-last-child($last_child{poc}),#$useropts{table_id} td:nth-last-child($last_child{supplier}),#$useropts{table_id} td:nth-last-child($last_child{from}),#$useropts{table_id} td:nth-last-child($last_child{to}),#$useropts{table_id} td:nth-last-child($last_child{system})") ;
	
	my $datatable_field_id = "#$useropts{table_id} td:nth-last-child($last_child{status}),#$useropts{table_id} td:nth-last-child($last_child{poc}),#$useropts{table_id} td:nth-last-child($last_child{shipment_operator}),#$useropts{table_id} td:nth-last-child($last_child{supplier}),#$useropts{table_id} td:nth-last-child($last_child{from}),#$useropts{table_id} td:nth-last-child($last_child{to}),#$useropts{table_id} td:nth-last-child($last_child{system})" ;
	
	# &common_min_table_select_jquery($datatable_field_id) ;
	
	&common_min_table_select_fixed_width_jquery($datatable_field_id) ; 
	&common_min_table_select_prevent_dropdown_overlap_jquery_1 ;
	&common_min_table_select_default_values_jquery ;
	&common_min_table_multi_select_jquery ;
	
	# &table_select_fixed_width_jquery($datatable_field_id) ;
	# &common_min_table_select_default_values_jquery ;
	# &common_min_table_multi_select_jquery ;
	
	my $all_multi_select_ids_str = join(",",@all_multi_select_ids) ; 
	
	$all_multi_select_ids_str  =~ s/\,/_chosen span,/g ;
	$all_multi_select_ids_str .= qq~_chosen span~ ; 
	
	$trigger_jquery_raw .= qq~ 
		\$("$all_multi_select_ids_str").css('font-size','10px') ; 
	~ ; 
	
	our $savjqy = 1 ;

	# &common_min_extra_crumb("manage-$lcpage\s","Manage $ucfirstpage\s") ;

	$extra_form_fields .= qq~
	<input type="hidden" name="oaction" 			value="$iaction">
	<input type="hidden" name="date_from" 			value="$i{date_from}">
	<input type="hidden" name="date_to" 			value="$i{date_to}">
	<input type="hidden" name="client_id" 			value="$i{client_id}">
	<input type="hidden" name="organisation_ids"	value="$i{organisation_ids}">
	<input type="hidden" name="supplier_id"			value="$i{supplier_id}">
	<input type="hidden" name="status_id" 			value="$i{status_id}">
	<input type="hidden" name="region_id" 			value="$i{region_id}">
	~ ;

	$print_box_content_rows .= &common_min_forms_end('','','save') if $is_dcb_partner || ($usertype ne 'schools_manager' && $glod_user_level >= 3) ;
	
	$useropts{'common'}{'css'} .= qq~
		tbody tr td { 
			font-size: 10px; 
		}
		
		.tooltip-inner { 
			max-width: 500px; 
			white-space: normal; 
		}
		
		.tooltip {
			position: fixed; /* Ensures the tooltip is positioned relative to the viewport */
		}
		
		#itv-table thead th {
			position: sticky;
			top: 0;
			background: white; /* Ensures the header is visible */
			z-index: 1020; /* Keeps it above other elements */
		}
		
		.dropdown-menu { 
			z-index: 1100 !important; /* Higher than the table header */
		}

	~ ;
		
# z-index: 10000000; /* Ensures the tooltip appears on top of other elements */ 
		
	$trigger_jquery_raw .= qq~
	
		\$('[data-toggle="tooltip"]').tooltip({
			container: 'body',
			placement: 'top', // Or your preferred placement
		});
		
	~ ;
	
	require _blank ;

	exit ;

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

# sub table_select_fixed_width_jquery {
	
	# my ($columns_with_selects) = @_ ; 
	
	# return unless $columns_with_selects ;	
		
	# # foreach (keys %table_select_rows) {
		# # chop $table_select_rows{$_} ;
		# # $trigger_jquery_raw .= qq~\$("$table_select_rows{$_}").css("z-index","$_") ; ~ ;
	# # }
	
	# $trigger_jquery_raw .= qq~
		# \$("$columns_with_selects").css("position","relative") ;
		# \$("$columns_with_selects").css("text-align","center") ;
	# ~ ;
	
	# my $dropdown_chosen_ids_str = join(',',@dropdown_chosen_ids) ; 		
	# my $dropdown_multi_chosen_ids_str = join(',',@dropdown_multi_chosen_ids) ; 		

	# $trigger_jquery .= qq~ 
		# \$("$dropdown_multi_chosen_ids_str").css("width","100%") ;~ if $dropdown_multi_chosen_ids_str ;		
	
	# return unless $dropdown_chosen_ids_str ;
	
	# # $trigger_jquery .= qq~ 
		# # \$("$dropdown_chosen_ids_str").css("position","absolute") ;~ ;	
	# $trigger_jquery .= qq~ 
		# \$("$dropdown_chosen_ids_str").css("width","90%") ;~ ;	
	# $trigger_jquery .= qq~ 
		# \$("$dropdown_chosen_ids_str").css("left","5%") ;~ ;	
	# $trigger_jquery .= qq~ 
		# \$("$dropdown_chosen_ids_str").css("top","50%") ;~ ;	
	# $trigger_jquery .= qq~ 
		# \$("$dropdown_chosen_ids_str").css("transform","translateY(-50%)") ;~ ;		
	
# } #------------------------------------------------------------------------------------------

sub redirect_screen {
	
&common_min_alert_type ;

print <<ENDOFTEXT;
<!DOCTYPE html>
<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>$useropts{title}</title>
    <script src="$useropts{'bower_components'}/jquery/jquery.min.js"></script>   
</head>
<body>
<script>

\$(document).ready(function(){ \$('#$lcpage-form').submit(); })

</script>
</body>

<form role="form" id="$lcpage-form" method="post" action="$lcpage">
<input type="hidden" name="iaction"				value="report">
<input type="hidden" name="oaction"				value="$iaction">
<input type="hidden" name="date_from"        	value="$i{date_from}">
<input type="hidden" name="date_to"      		value="$i{date_to}">
<input type="hidden" name="client_id"      		value="$i{client_id}">
<input type="hidden" name="organisation_ids"	value="$i{organisation_ids}">
<input type="hidden" name="supplier_id"			value="$i{supplier_id}">
<input type="hidden" name="status_id" 			value="$i{status_id}">
<input type="hidden" name="region_id" 			value="$i{region_id}">
<input type="hidden" name="isaved"				value="$success">
</form>

</html>
ENDOFTEXT
#

exit ;

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

use dcb ;
use today ;
use common ;
use report ;
use logistics ;
use xlsxcreator ;

1;