sub calibration_load_list_vars {
	
	my ($where,$for_payments) = @_ ;

	our %quote_status = () ; 

	our %calibrator_users = () ; 

	# &db_min_ro('users','id,name,user_type',"",'','') ;
	# &db_min_ro('users','id,name,user_type,username',"(user_type = 'support' OR user_type = 'manager' OR user_type = 'casual_calibrator') AND name NOT LIKE 'Rory%'",'','') ;
	
	&db_min_ro('users','id,name,user_type,username',"`name` NOT LIKE 'Rory%'",'','') ;	
	
	if (!$for_payments) {
		$opts{calibration_users} .= qq~<option value="none">>>>None<<<</option>~ ;	
		$opts{head_operator}     .= qq~<option value="none">>>>None<<<</option>~ ;	
		foreach my $_uid (sort {$db{users}{$a}{name} cmp $db{users}{$b}{name}} keys %{$db{users}}) {
			next if $useropts{it}{$db{users}{$_uid}{username}} ;
			if ($db{users}{$_uid}{user_type} eq 'support' || $db{users}{$_uid}{user_type} eq 'manager' || $db{users}{$_uid}{user_type} eq 'casual_calibrator') {
				$calibrator_users{$_uid} = $db{users}{$_uid}{name} ;	
				$opts{calibration_users} .= qq~<option value="$_uid">$db{users}{$_uid}{name}</option>~ ;					
			}
			if ($db{users}{$_uid}{user_type} eq 'casual_c') {
				$opts{head_operator} .= qq~<option value="$_uid">$db{users}{$_uid}{name}</option>~ ;	
			}
		}		
	}
		
	&db_min_ro('event_systems','*',"system_type = 'mobile'",'','') ;
	
	my @event_quote_ids = () ; 
					
	# &db_min_ro($table,"id,ref,organisation_ids,quote_accepted,event_system_id_multiple,region_id,sport_type_ids,operator_ids,date_from,date_to,if(date_from < '$now_year-$now_mm-$now_dd 00:00:00',0,1) AS 'after_start_date',event_system_calibration_ids,event_system_calibration_status","$where AND `quote_accepted` = '1'",'','') ; 
	&db_min_ro($table,"id,ref,organisation_ids,quote_accepted,event_system_id_multiple,region_id,sport_type_ids,operator_ids,date_from,date_to,event_system_calibration_ids,event_system_calibration_status,event_system_head_operator_ids,times_from","$where AND `quote_accepted` = '1'",'','') ; 

	my $min_id = 0 ; my $max_id = 0 ; 
	
	our %event_system_calibration_ids = () ; our %event_system_calibration_status = () ; our %event_system_head_operator_id = () ;

	foreach my $id (keys %{$db{$table}}) {
		
		# &common_debug(">>>>>>>>> 1. [$id] $db{$table}{$id}{ref}") ;
		
		next unless $id ; my $cnt = 0 ; 
		
		my @ed_from = &common_split_sql_time($db{$table}{$id}{date_from}) ;
		my @ed_to   = &common_split_sql_time($db{$table}{$id}{date_to}) ; 
		
		&common_date_array("$ed_from[0]$ed_from[1]$ed_from[2]","$ed_to[0]$ed_to[1]$ed_to[2]") ;
		
		# $sort_calibration_by_time_ref{"$ed_from[0]$ed_from[1]$ed_from[2]$ed_from[3]$ed_from[4]-$db{$table}{$id}{ref}"} = $id ;
		$sort_calibration_by_time_ref{"$ed_from[0]$ed_from[1]$ed_from[2]$ed_from[3]$ed_from[4]-$id"} = $id ;
		
		# my @event_sys_calibration_status    = split(";",$db{$table}{$id}{event_system_calibration_status}) ; 
		my @event_sys_calibration_status	= split(/\;/,$db{$table}{$id}{event_system_calibration_status}) ; 
		my @event_system_ids                = split(/\;/,$db{$table}{$id}{event_system_id_multiple}) ; 
		my @calibration_ids                 = split(/\;/,$db{$table}{$id}{event_system_calibration_ids}) ; 
		my @head_operator_ids				= split(/\;/,$db{$table}{$id}{event_system_head_operator_ids}) ; 

		my $evntday=0; my $evntsys=0;	
		
		foreach my $_cid (@calibration_ids) {
			$system_id = $event_system_ids[$evntsys] ;
			$evntsys++;
			
			next unless $system_id ;
			next unless $db{event_systems}{$system_id}{system_type} eq 'mobile' ;
			
			my (@calibrator_ids_per_event_day) = split(/\-/,$_cid);
			$evntday=0; # set the event day back to zero for each set of calibrators
			
			foreach my $_cid_pd (@calibrator_ids_per_event_day) {
				$evntday++;
				next unless $_cid_pd ;;
				$event_system_calibration_ids{$id}{$system_id}{$evntday} = $_cid_pd ;
				# &common_debug("1. >>> $event_system_calibration_ids{$id}{$system_id}{$evntday} [$id] [$system_id] [$evntday]") ; 
			}
			# $evntsys++;
		}
		
		$evntsys=0;	
		
		foreach my $_cstatus (@event_sys_calibration_status) {
			$system_id = $event_system_ids[$evntsys] ;
			$evntsys++;
			next unless $system_id ;
			next unless $db{event_systems}{$system_id}{system_type} eq 'mobile' ;
			
			
			my (@calibrator_status_per_event_day) = split(/\-/,$_cstatus);
			$evntday=0; # set the event day back to zero for each set of calibrators
			foreach my $_cstatus_pd (@calibrator_status_per_event_day) {
				$evntday++;
				next unless $_cstatus_pd ;
				$event_system_calibration_status{$id}{$system_id}{$evntday} = $_cstatus_pd ;
				# &common_debug("1. >>> $event_system_calibration_status{$id}{$system_id}{$evntday} [$id] [$system_id] [$evntday]") ; 
			}
			# $evntsys++;
		}
		
		$evntsys=0;	
		
		foreach my $_cstatus (@head_operator_ids) {
			$system_id = $event_system_ids[$evntsys] ;
			$evntsys++;
			next unless $system_id ;
			next unless $db{event_systems}{$system_id}{system_type} eq 'mobile' ;
			
			my (@head_operator_id_event_day) = split(/\-/,$_cstatus);
			$evntday=0; # set the event day back to zero for each set of calibrators
			foreach my $_cstatus_pd (@head_operator_id_event_day) {
				$evntday++;
				next unless $_cstatus_pd ; 
				$event_system_head_operator_id{$id}{$system_id}{$evntday} = $_cstatus_pd ;
				# &common_debug("1. >>> $event_system_calibration_status{$id}{$system_id}{$evntday} [$id] [$system_id] [$evntday]") ; 
			}
			# $evntsys++;
		}
	
			$evntsys=0;	
		
		if ($for_payments) {
		
			my @calibrator_amounts_paid			= split(/\;/,$db{$table}{$id}{calibrator_amounts_paid}) ; 
			my @calibrator_dates_paid			= split(/\;/,$db{$table}{$id}{calibrator_dates_paid}) ; 
			
			foreach my $_cstatus (@calibrator_amounts_paid) {
				$system_id = $event_system_ids[$evntsys] ;
				$evntsys++;
				next unless $system_id ;
				next unless $db{event_systems}{$system_id}{system_type} eq 'mobile' ;
				
				my (@amounts_paid_event_day) = split(/\-/,$_cstatus);
				$evntday=0; # set the event day back to zero for each set of calibrators
				foreach my $_cstatus_pd (@amounts_paid_event_day) {
					$evntday++;
					next unless $_cstatus_pd ; 
					$calibrator_amounts_paid_hash{$id}{$system_id}{$evntday} = $_cstatus_pd ;
					# &common_debug("1. >>> $event_system_calibration_status{$id}{$system_id}{$evntday} [$id] [$system_id] [$evntday]") ; 
				}
				# $evntsys++;
			}

			$evntsys=0;	
			
			foreach my $_cstatus (@calibrator_dates_paid) {
				$system_id = $event_system_ids[$evntsys] ;
				$evntsys++;
				next unless $system_id ;
				next unless $db{event_systems}{$system_id}{system_type} eq 'mobile' ;
				
				my (@dates_paid_event_day) = split(/\:/,$_cstatus);
				$evntday=0; # set the event day back to zero for each set of calibrators
				foreach my $_cstatus_pd (@dates_paid_event_day) {
					$evntday++;
					next unless $_cstatus_pd ; 
					$calibrator_dates_paid_hash{$id}{$system_id}{$evntday} = $_cstatus_pd ;
					# &common_debug("1. >>> $event_system_calibration_status{$id}{$system_id}{$evntday} [$id] [$system_id] [$evntday]") ; 
				}
				# $evntsys++;
			}
		}
	}

	# &db_min_ro('event_systems','*',"system_type = 'mobile'",'','') ;

	&calibration_check_for_overlapping_dates('event_systems',"event_system_id_multiple") ; 
	
	foreach my $id (sort keys %{$db{$table}}) {
		
		$min_id = $id if not $min_id or $min_id > $id ; 
		$max_id = $id if $max_id < $id ;
	}
	
	my $id_sql = qq~(`id` <= '$max_id' AND `id` >= '$min_id')~ ;
	# $id_sql = qq~ AND $id_sql~ if $where_2 or $where_3 ;
	
	# &db_min_ro('event_quotes_min','*',"$where_2$where_3$id_sql",'','') ;
	&db_min_ro('event_quotes_min','*',"$id_sql",'','') ;
	&db_min_ro('event_cost_items','id,name',"excl_from_expenses < 1",'','') ;
	&db_min_ro('calibration_status','id,status','','','') unless $calibration_status ;
	
	foreach (sort {$db{calibration_status}{$a}{status} cmp $db{calibration_status}{$b}{status}} keys %{$db{calibration_status}}) {
		$opts{calibration_status} .= qq~<option value="$_">$db{calibration_status}{$_}{status}</option>~ ; 
	}

	&db_min_ro('sport_types','*','','','') ;
	&db_min_ro('regions','*','','','') ;
	&db_min_ro('organisations','id,name','','','') ;
	
} #------------------------------------------------------------------------------------------

sub calibration_build_table_and_or_excel {
	
	my ($report,$for_payments) = @_ ;
	
	our @sql_col_display = ("count","event","calibration_date","day","start_date","start_time","venue","region","system","sport","operator","head_operator","calibrator","status","update") ;
	
	@sql_col_display = ("count","event","calibration_date","day","start_date","start_time","venue","region","system","sport","operator","head_operator","calibrator","status","date_payable","amount_payable","paid") if $for_payments ;
	
	&report_xlsx_export_header("$xlsxreportname",$xlsxdir,'',$xlsx_title_heading) ;
	
	$xlsxrow-- ;
	
	my $cali_cnt = 0 ; our $found_after_date = 0  ; my $found_editable = 0 ; 
	
	# foreach my $id (sort {$db{$table}{$a}{date_from} cmp $db{$table}{$b}{date_from}} keys %{$db{$table}}) {
	foreach my $date_ref (sort keys %sort_calibration_by_time_ref) {
		
		my $id = $sort_calibration_by_time_ref{$date_ref} ;
	
		next unless $id ; 
		unless (&common_check_if_string_contains_an_integer($db{$table}{$id}{event_system_id_multiple})) {
			$db{$table}{$id}{event_system_id_multiple} = ";none;" ; 
		} 
		
		# my @system_name_ids = split(";",$db{$table}{$id}{event_system_id_multiple}) ;		
		
		my @system_name_ids = split(/\;/,$db{$table}{$id}{event_system_id_multiple}) ;		
		my @club_ids        = split(/\;/,$db{$table}{$id}{club_ids}) ;
		my @op_ids        	= split(/\,/,$db{$table}{$id}{operator_ids}) ;	
		my @start_times 	= split(/\;/,$db{$table}{$id}{times_from}) ;	  

		my $cnt_rows = 0 ;
		for (1 .. 50) {
			$cnt_rows = $_ if $system_name_ids[$_ - 1] or $club_ids[$_ - 1] ;  
		}
		
		# my @table_op_ids = () ;
		
		# for (1 .. $cnt_rows) {			
			# push @table_op_ids,$op_ids[$_ - 1] if $op_ids[$_ - 1] ;
			# push @table_op_ids,0 unless $op_ids[$_ - 1] ;
		# }
		
		my $system_count = 0 ; 
		
		# foreach my $event_sys_id (split(";",$db{$table}{$id}{event_system_id_multiple})) {
		# foreach my $event_sys_id (sort { $a <=> $b } @system_name_ids) {
		foreach my $event_sys_id (@system_name_ids) {
			
			$system_count++ ;	
					
			next unless $event_sys_id ;
			next unless $db{event_systems}{$event_sys_id}{system_type} eq 'mobile' ;
			
			
			# &common_debug(">>>>>>>>> 2. $system_count [$id] $db{$table}{$id}{ref} [$db{event_systems}{$event_sys_id}{system_type}]") ;
			
			# $event_cnt++ ;		
			
			# foreach my $cal_date (sort {$day_of_week{$a} cmp $day_of_week{$b}} keys %day_of_week) {
			foreach my $cal_date (sort keys %day_of_week) {
		
				
				my $srch_date_from_comp   = $i{date_from} ; $srch_date_from_comp =~ s/\-//g ;
				my $srch_date_to_comp     = $i{date_to} ; $srch_date_to_comp =~ s/\-//g ;
				my $date_from_comp        = substr($db{$table}{$id}{date_from},0,10) ; $date_from_comp =~ s/\-//g ;
				my $date_to_comp          = substr($db{$table}{$id}{date_to},0,10) ; $date_to_comp =~ s/\-//g ;
				my $cal_date_comp         = $cal_date ; # $cal_date_comp =~ s/\-//g ;
				   $cal_date              = substr($cal_date,0,4) . '-' . substr($cal_date,4,2) . '-' . substr($cal_date,6,2) ; 
				my $date_from             = substr($db{$table}{$id}{date_from},0,10) ;
				
				# &common_debug("[$cnt{$event_sys_id}{$id}] : $event_sys_id [$id] $cal_date") ; 
				
				# &common_debug("1. DATE >>> $cal_date_comp < $date_from_comp") if !$report ;				
				next if $cal_date_comp < $date_from_comp ;
				# &common_debug("2. DATE >>> $cal_date_comp > $date_to_comp") if !$report ;
				next if $cal_date_comp > $date_to_comp ;
				
				$cnt{$event_sys_id}{$id}++ ;  ## WHY HERE???????????????
				next if $for_payments && ($db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$cnt{$event_sys_id}{$id}}}{user_type} ne 'casual_calibrator' || $db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$cnt{$event_sys_id}{$id}}}{status} ne 'Done' || ($i{paid_status} eq 'paid' && !$calibrator_amounts_paid_hash{$id}{$system_id}{$evntday}) || ($i{paid_status} eq 'outstanding' && $calibrator_amounts_paid_hash{$id}{$system_id}{$evntday})) ;
				
				
				# &common_debug("3. DATE >>> $cal_date_comp > $srch_date_to_comp") if !$report ;
				next if $cal_date_comp > $srch_date_to_comp ;
				# &common_debug("4. DATE >>> $cal_date_comp < $srch_date_from_comp") if !$report ;
				next if $cal_date_comp < $srch_date_from_comp ;
				
				# &common_debug(">>>>>>>>> 3. $system_count [$id] $db{$table}{$id}{ref} [$db{event_systems}{$event_sys_id}{system_type}]") ;
							
				# &common_debug("[$cnt{$event_sys_id}{$id}] : $event_sys_id [$id] $cal_date [$cal_date_comp > $srch_date_to_comp]") ; 
				

				
				$cali_cnt++;
					
				$xlsxcol = 0 ; 
				$print_tbody .= qq~<tr id="$id$dow">~ if $report ;
				
				foreach (@sql_col_display) { 
				
					# &common_debug("1. XLSX >>> $_") if !$report ;

					next unless $_ ; # blank for the buttons column
					
					my $formatting = $format84 ;
					my $val        = $db{$table}{$id}{$_} ;
					my $val_min    = $val  ;
					my $align      = qq~ class="dt-center"~ ; 
					my $nowrap     = '' ;
					
					# &common_debug("2. XLSX >>> val_min=$val_min") if !$report ;
					
					my $sys_day_cnt = $cnt{$event_sys_id}{$id} ;
					
					if ($_ eq 'count') {			
						# $val = $event_cnt ; 
						$val = $cali_cnt ; 
					} elsif ($_ eq 'event') {	
						$val = "$db{$table}{$id}{ref} ($id)" ;
					} elsif ($_ eq 'calibration_date') {			
						# $val = "$cal_date [$day_of_week{$cal_date}]" ;
						$val = "$cal_date" ;
					} elsif ($_ eq 'day') {			
						$val = "$sys_day_cnt" ;
					} elsif ($_ eq 'start_date') {			
						$val = $date_from ;
					} elsif ($_ eq 'venue') {			
						my @oids = split(/\,/,$db{$table}{$id}{organisation_ids}) ;
						foreach $_oid (@oids) {
							$val .= $db{organisations}{$_oid}{name} . '<br>' ;
						}
						$val = substr($val,0,-4) if $val ; ;				
					} elsif ($_ eq 'region') {				
						$val = $db{regions}{$db{$table}{$id}{region_id}}{code} ;		
					} elsif ($_ eq 'system') {			
						$val = '' ; 
						$val = qq~$db{event_systems}{$event_sys_id}{name}~ ; 
						$val .= qq~ ($db{event_systems}{$event_sys_id}{description})~ if $db{event_systems}{$event_sys_id}{description} ; 
					} elsif ($_ eq 'sport') {		
						$val = '' ; 
						foreach my $sport_id (split(",",$db{$table}{$id}{sport_type_ids})) {
							$db{sport_types}{$sport_id}{name} = uc $db{sport_types}{$sport_id}{name} ;
							$val .= qq~$db{sport_types}{$sport_id}{name}<br>~ ; 
						}
						$val = substr($val,0,-4) if $val ;	
					} elsif ($_ eq 'operator') {	
						$val = '' ; 
						# $val = $db{users}{$table_op_ids[$system_count - 1]}{name} if $table_op_ids[$system_count - 1] ;			
						$val = $db{users}{$op_ids[$system_count - 1]}{name} if $op_ids[$system_count - 1] ;			
						# $formatting = $format92 if $event_cnt == 2 ;	
						$formatting = $format92 if $cali_cnt == 2 ;	
					} elsif ($_ eq 'start_time') {	
						if ($sys_day_cnt == 1 or ($sys_day_cnt > 1 and not $start_times[$sys_day_cnt-2])) {
							$val = substr($db{$table}{$id}{date_from},11,5) ;		
						} else {
							$val = substr($start_times[$sys_day_cnt-2],0,5) ;
						} 
					} elsif ($_ eq 'head_operator') {
						if ($report && !$for_payments) {						
							# if ($db{$table}{$id}{after_start_date} and $event_sys_id ne "-1") {	
							# &common_debug("=== $cal_date_comp >= $now_ccyymmdd && $event_sys_id ne -1") ; 
							
							my $field = "head_operator_$id\_$event_sys_id\_$sys_day_cnt" ;
							if ($cal_date_comp >= $now_ccyymmdd && $event_sys_id ne "-1") {		
								$found_after_date = 1 ; 
								
								$preferred_title{$field} = "Head Operator" ; 
								$opts{$field}            = $opts{head_operator} ;
								$allow_deselect{$field}  = 1 ; 
								$val                     = &common_min_form_select_col($field,'') ;	
								
								# &common_debug("2. >>> $event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt} [$id] [$event_sys_id] [$sys_day_cnt] [$field]") ; 
								
 
								if ($event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt}) {
									$trigger_jquery_raw .= qq~
										\$("#selectHead_operator_$id\_$event_sys_id\_$sys_day_cnt").val("$event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt}") ;
										\$("#selectHead_operator_$id\_$event_sys_id\_$sys_day_cnt").trigger("chosen:updated") ;
										\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(4)").css("background-color","#424949") ;
									~ ;
								}
								
								$trigger_jquery_raw .= qq~\$("#selectHead_operator_$id\_$event_sys_id\_$sys_day_cnt\_chosen").css("width","100%") ;~ ;
								# $val_min = $db{users}{$db{event_systems}{$event_sys_id}{calibrator_id}}{name} ;
								$val_min = $db{users}{$event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;
							} else {
								$val = $db{users}{$event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;
								$val = "None" if $event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt} eq 'none' ;
								# # $val = "$db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} [$sys_day_cnt]" ;
								# $readonly{$field} = 'READONLY' ;
								# $val              = qq~<div style="display:none;">~ . &common_min_form_input_col($field,$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}) . qq~</div>~ ;		
								# $val             .= $db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;		
								$val_min          = $val ;	
							}
							# $val = "None" unless $val ;
							# $val_min = "None" unless $val_min ;
						} else {
							
							# $val = $db{users}{$db{event_systems}{$id}{calibrator_id}}{name} ;
							$val = $db{users}{$event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;
							$val = "None" if $event_system_head_operator_id{$id}{$event_sys_id}{$sys_day_cnt} eq 'none' ;
							# $val = "None" unless $val ;
							$val_min          = $val ;	
						}
						$formatting = $format91 ;		
					} elsif ($_ eq 'calibrator') {
						
						if ($report && !$for_payments) {						
							# if ($db{$table}{$id}{after_start_date} and $event_sys_id ne "-1") {	
							# &common_debug("=== $cal_date_comp >= $now_ccyymmdd && $event_sys_id ne -1") ; 
							
							my $field = "calibrator_id_$id\_$event_sys_id\_$sys_day_cnt" ;
							if ($cal_date_comp >= $now_ccyymmdd && $event_sys_id ne "none") {		
								$found_after_date = 1 ; 
								
								$preferred_title{$field} = "Calibrator" ; 
								$opts{$field}            = $opts{calibration_users} ;
								$allow_deselect{$field}  = 1 ; 
								$val                     = &common_min_form_select_col($field,'') ;	
								
								# &common_debug("2. >>> $event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt} [$id] [$event_sys_id] [$sys_day_cnt] [$field]") ; 

								if ($event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}) {
									$trigger_jquery_raw .= qq~
										\$("#selectCalibrator_id_$id\_$event_sys_id\_$sys_day_cnt").val("$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}") ;
										\$("#selectCalibrator_id_$id\_$event_sys_id\_$sys_day_cnt").trigger("chosen:updated") ;
										\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(3)").css("background-color","#424949") ;				
									~ ;																
								}
								
								$trigger_jquery_raw .= qq~\$("#selectCalibrator_id_$id\_$event_sys_id\_$sys_day_cnt\_chosen").css("width","100%") ;~ ;					
								# $val_min = $db{users}{$db{event_systems}{$event_sys_id}{calibrator_id}}{name} ;		
								$val_min = $db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;		
							} else {
								$val = $db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;
								$val = "None" if $event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt} eq 'none' ;
								# # $val = "$db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} [$sys_day_cnt]" ;
								# $readonly{$field} = 'READONLY' ;
								# $val              = qq~<div style="display:none;">~ . &common_min_form_input_col($field,$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}) . qq~</div>~ ;		
								# $val             .= $db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;		
								$val_min          = $val ;	
							}
							# $val = "None" unless $val ;
							# $val_min = "None" unless $val_min ;
						} else {
							# $val = $db{users}{$db{event_systems}{$id}{calibrator_id}}{name} ;
							
							$val = $db{users}{$event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt}}{name} ;
							$val = "None" if $event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt} eq 'none' ;
							# $val = "None" unless $val ;
							$val_min          = $val ;	
						}
						$formatting = $format91 ;		
					} elsif ($_ eq 'status') {	
						if ($report && !$for_payments) {
							# if ($db{$table}{$id}{after_start_date} and $event_sys_id ne "-1") {			
							my $field = "calibration_status_$id\_$event_sys_id\_$sys_day_cnt" ;
							if ($cal_date_comp >= $now_ccyymmdd && $event_sys_id ne "none") {	
								$opts{$field}            = $opts{calibration_status} ;
								$preferred_title{$field} = "Calibration Status" ;	
								$allow_deselect{$field}  = 1; 
								$val                     = &common_min_form_select_col($field,'') ;

								# &common_debug("STATUS >>> $event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} [$id] [$event_sys_id] [$sys_day_cnt] [$field]") ; 								
								
								if ($event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}) {
									$trigger_jquery_raw .= qq~\$("#selectCalibration_status_$id\_$event_sys_id\_$sys_day_cnt").val("$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}") ;~ ;
									if ($event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} eq '1') { #calibrating
										$trigger_jquery_raw .= qq~\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(2)").css("background-color","#FFA500");~ ;										
									}
									elsif ($event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} eq '2') { #cancelled
										$trigger_jquery_raw .= qq~\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(2)").css("background-color","#AD0502");~ ;										
									} 
									elsif ($event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}  eq '3') { #done
										$trigger_jquery_raw .= qq~\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(2)").css("background-color","#2ECC71");~ ;	#D5EDBB								
									}
									elsif ($event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} eq '4') { #done
										$trigger_jquery_raw .= qq~\$("#itv-table tr:eq($cali_cnt) td:nth-last-child(2)").css("background-color","#424949");~ ;	#D5D8DC									
									}
								} else {
									$trigger_jquery_raw .= qq~\$("#selectCalibration_status_$id\_$event_sys_id\_$sys_day_cnt").val("4") ;~ ;					
								}							
								
								$trigger_jquery_raw .= qq~
									\$("#selectCalibration_status_$id\_$event_sys_id\_$sys_day_cnt").trigger("chosen:updated") ;
									\$("#selectCalibration_status_$id\_$event_sys_id\_$sys_day_cnt\_chosen").css("width","100%");
								~ ;		
								$val_min = $db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} ;
							} else {		
								# $val = $db{calibration_status}{$db{event_systems}{$event_sys_id}{calibration_status}}{status} ;
								$val = $db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} ;
								$val = "None" if !$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} ;
								# $readonly{$field} = 'READONLY' ;
								# $val              = qq~<div style="display:none;">~ . &common_min_form_input_col($field,$db{event_systems}{$event_sys_id}{calibration_status}) . qq~</div>~ ;		
								# $val             .= $db{calibration_status}{$db{event_systems}{$event_sys_id}{calibration_status}}{status} ;		
								$val_min          = $val ;	
							}
							# $val = "None" unless $val ;	
							# $val_min = "None" unless $val_min ;
						} else {
							# $val = $db{calibration_status}{$db{event_systems}{$event_sys_id}{calibration_status}}{status} ;
							$val = $db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} ;
							$val = "None" if !$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt} ;
							# $val = "None" unless $val ;
							$val_min = $val ;
						}
						# if ($db{calibration_status}{$db{event_systems}{$event_sys_id}{calibration_status}}{status} eq "Calibrating") {
						if ($db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} eq "Calibrating") {
							$formatting = $format89 ;
						} elsif ($db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} eq "Cancelled") {
							$formatting = $format90 ;
						} elsif ($db{calibration_status}{$event_system_calibration_status{$id}{$event_sys_id}{$sys_day_cnt}}{status} eq "Done") {
							$formatting = $format88 ;
						} else {
							$formatting = $format91 ;
						}	
					} elsif ($_ eq 'update') {
						
						if ($report && $cal_date_comp >= $now_ccyymmdd && $event_sys_id ne "none") {						
							$found_editable = 1 ; 
							my $field = "update_$id\_$event_sys_id\_$sys_day_cnt" ;
							$val = &common_min_form_checkbox_col($field,'') ;
							#  $val .= qq~<span id="update_history_$id\_$event_sys_id\_$sys_day_cnt" style="display:none;"></span>~ ;  #
							$trigger_jquery_raw .= qq~
								\$("#selectCalibration_status_$id\_$event_sys_id\_$sys_day_cnt,#selectCalibrator_id_$id\_$event_sys_id\_$sys_day_cnt,#selectHead_operator_$id\_$event_sys_id\_$sys_day_cnt").change(function () {
									\$("#checkboxUpdate_$id\_$event_sys_id\_$sys_day_cnt").prop("checked",true) ;
									// \$("#update_history_$id\_$event_sys_id\_$sys_day_cnt").text("1") ;
								}) ;
							~ ;
							
						} else {
							$val = '' ;
						}
						$formatting = $format91 ;		
					} elsif ($_ eq 'date_payable') {
						my $field = "date_payable_$id\_$event_sys_id\_$sys_day_cnt" ;
						
						if ($calibrator_dates_paid_hash{$id}{$event_sys_id}{$sys_day_cnt}) {
							$val = $calibrator_amounts_paid_hash{$id}{$event_sys_id}{$sys_day_cnt} ;
							$val_min = $val ;	
							$val = &common_min_form_input_col($field,"$calibrator_dates_paid_hash{$id}{$event_sys_id}{$sys_day_cnt}") ;
						} else {
							$val = &common_min_form_input_col($field,"$cal_date") ;
							$val_min = $cal_date ;	
						}
						
					} elsif ($_ eq 'amount_payable') {
						my $field = "amount_payable_$id\_$event_sys_id\_$sys_day_cnt" ;
						if ($calibrator_amounts_paid_hash{$id}{$event_sys_id}{$sys_day_cnt}) {
							$val = "Paid" ;
							$val_min = "None" ;
						} else {
							$val = &common_min_form_input_col($field,'500.00') ;
							$trigger_jquery_raw .= qq~
								\$("input[name='$field']").change( function () {
									let input = \$(this).val() ; 
									input = parseFloat(input) ;
									if (!input) { input = 0 ; }
									\$(this).val(input.toFixed(2)) ;
								}) ;
							~ ;
							$val_min = "500.00" ;							
						}
					
					} elsif ($_ eq 'paid') {
						if ($calibrator_amounts_paid_hash{$id}{$event_sys_id}{$sys_day_cnt}) {
							$val = '<i class="glyphicons glyphicons-tick"></i>' ;
							$val_min = "Paid" ;
						} else {
							$paid_fields .= qq~$id\_$event_sys_id\_$sys_day_cnt:$cali_cnt,~ ;
							my $field = "paid_$id\_$event_sys_id\_$sys_day_cnt" ;
							$val = &common_min_form_checkbox_col($field,'') ;							
						}
					} 
					if ($_ ne 'status' and $_ ne 'calibrator' and $_ ne 'update' and $_ ne 'date_payable' and $_ ne 'amount_payable') {
						$val_min = $val ;
						$val_min =~ s/\<br>/\;/g ;	
					}
					$print_tbody .= qq~<td $align>$val</td>~ if $report ; 
					
					# &common_debug("XLSX >>> $event_system_calibration_ids{$id}{$event_sys_id}{$sys_day_cnt} [$id] [$event_sys_id] [$sys_day_cnt] [$field]") ; 
					
					&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val_min,$formatting) ;
					$xlsxcol++ ;
				}
				
				$print_tbody .= qq~</tr>~ if $report ;
				$xlsxrow++ ; 	
			}
			
			# next if $i{calibration_status} ne 'all' and $i{calibration_status} and $i{calibration_status} ne $db{event_systems}{$event_sys_id}{calibration_status} ;
			# next if $i{calibrator_id} ne 'all' and $i{calibrator_id} and $i{calibrator_id} ne $db{event_systems}{$event_sys_id}{calibrator_id} ;
			
		}
	}
			# \$("#itv-table tr th:nth-last-child(1)").css("width","0%") ;
			# \$("#itv-table tr th:nth-last-child(1)").css("display","none") ;	
			
	if (!$found_editable && !$for_payments) {
		$trigger_jquery_raw .= qq~
			\$("#itv-table").find("th:last").remove() ;
			\$("#itv-table tr").each(function() {
				\$(this).find("td:last").remove() ;
			}) ;
		~ ;		
	}

	$worksheet{$ws}->set_column(0,0,10);
	$worksheet{$ws}->set_column(1,1,30);
	$worksheet{$ws}->set_column(2,2,15);
	$worksheet{$ws}->set_column(3,3,5);
	$worksheet{$ws}->set_column(4,5,13);
	$worksheet{$ws}->set_column(6,6,30);
	$worksheet{$ws}->set_column(7,7,8);	
	$worksheet{$ws}->set_column(8,10,30);
	$worksheet{$ws}->set_column(11,12,15);

	&report_xlsx_export_footer('L',15,$xlsxdir) ;
	
} #-------------------------------------------------------------------------------

sub calibration_check_for_overlapping_dates {

	my ($check_table,$checked_column,$date_from_input,$date_to_input,$sys_ids_input,$get_periods) = @_ ; 
 
	my %seen_system_id = () ; my %checked_system_id = () ; my %system_start_date = () ; my %system_end_date = () ; 
	
	my $sys_ids = "" ; 
	
	if ($get_periods) {
		$sys_ids = $sys_ids_input ; 
	}
	
	foreach my $id (sort keys %{$db{$table}}) {
		
		unless ($get_periods) {
			$sys_ids = $db{$table}{$id}{$checked_column} ;
		}		
		foreach (split(";",$sys_ids)) {
			
			$system_start_date{$_}{$id}	= $db{$table}{$id}{date_from} ;
			$system_end_date{$_}{$id} = $db{$table}{$id}{date_to} ;
		}
	}
	
	if ($date_from_input and $date_to_input and $sys_ids_input) {		
		
		foreach (split(";",$sys_ids_input)) {
			
			$system_start_date{$_}{none} = $date_from_input ;
			$system_end_date{$_}{none} = $date_to_input ;
		}
	}
	
	foreach my $id (sort keys %{$db{$table}}) {

		unless ($get_periods) {
			$sys_ids = $db{$table}{$id}{$checked_column} ;
		}	
		
		foreach my $sys_id (split(";",$sys_ids)) {
			
			next unless $sys_id ;
						
			if ($seen_system_id{$sys_id} and not $checked_system_id{$sys_id}) {
				
				$checked_system_id{$sys_id} = 1 ;
				foreach my $id2 (keys %{$system_start_date{$sys_id}}) {
					
					next if $id2 eq $id ;
					my $t1 = &common_check_if_time_is_greater($system_start_date{$sys_id}{$id2},$db{$table}{$id}{date_to}) ; 	## 21<12
					my $t2 = &common_check_if_time_is_greater($system_end_date{$sys_id}{$id2},$db{$table}{$id}{date_to}) ; 		## 22<12
					my $t3 = &common_check_if_time_is_greater($system_start_date{$sys_id}{$id2},$db{$table}{$id}{date_from}) ;	## 21<11
					my $t4 = &common_check_if_time_is_greater($system_end_date{$sys_id}{$id2},$db{$table}{$id}{date_from}) ;		## 22<11
					
					if (($t1 and not $t2) or ($t3 and not $t4) or (not $t3 and $t2) or ($t3 and not $t2)) {
						my $system_name = qq~$db{$check_table}{$sys_id}{name}~ ;
						$system_name .= qq~ ($db{$check_table}{$sys_id}{description})~ if $db{$check_table}{$sys_id}{description} ; 
						
						if ($get_periods) {
							$overlap_dates{$sys_id}{$id2} = "$db{$table}{$id2}{date_from} - $db{$table}{$id2}{date_to}" ;
						} else {
							$trigger_jquery_raw .= qq~
								noty({text:'System $system_name and cannot be used in both event $db{$table}{$id}{ref} and $db{$table}{$id2}{ref} as the times ovelap!!',layout:"center",type:"error",timeout:30000}) ;						
							~ ;								
						}
					}				
				}
			}
			$seen_system_id{$sys_id} = 1 ;
		}
	}
	
} #-------------------------------------------------------------------------------

1 ;