#!/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 ; 

&today;

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

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

our ($q)                 = CGI -> new() ;
our $iaction             = $q -> param('iaction')                || $action ;
our $isaved              = $q -> param('isaved')                 || '' ;

# our $debug   = 1 ;
# our $testing = 1 ;

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

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

&page_opts ;

#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
our $srchscr   = 1 ;

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 $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 validate { }

sub insert   { } 

sub update {
	
	my %seen_event_id = () ; 
	
	foreach (keys %i) {
		next if $_ !~ /^update_/ || ($_ =~ /^update_/ && !$i{$_}) ; 
		local @abc = split("_",$_) ;	
		$seen_event_id{$abc[-3]} = 1 ;
	}

	local $e_q_sql = join (" OR ",map { "id='$_'" } keys %seen_event_id) ;
	
	&db_min_ro($table,'id,event_system_id_multiple,date_from,date_to,set_up_groups_head_operator_ids,set_up_groups_status_ids',"$e_q_sql",'','') ; 
	
	my %ii = () ; my %iii = %i  ;

	foreach my $event_id (keys %seen_event_id) {

		my @event_system_ids			= split(/\;/,$db{$table}{$event_id}{event_system_id_multiple}) ; 
		
		my @set_up_head_operator_ids 	= split(/\;/,$db{$table}{$event_id}{set_up_groups_head_operator_ids}) ; 
		my @set_up_status 				= split(/\;/,$db{$table}{$event_id}{set_up_groups_status_ids}) ;
		
		my $system_cnt = 0 ; 
 
		foreach (@event_system_ids) {
			$system_cnt++ ;
			$head_operator_hash{cnt}{$event_id}{$_}	= $system_cnt ;
			$status_hash{cnt}{$event_id}{$_}		= $system_cnt ;
		}
		
		$system_cnt = 0 ; 
		
		foreach my $system_data (@set_up_head_operator_ids) {
			$system_cnt++ ;
			next unless $system_data ; 
			if ($system_data =~ /~/) {
				($system_id,$system_data) = split(/\~/,$system_data) ; 
			} else {
				$system_id = $event_system_ids[$system_cnt - 1] ;
			}
			next unless $head_operator_hash{cnt}{$event_id}{$system_id} ;
			my $day_cnt = 0 ; 
			foreach my $daily_data (split(/\-/,$system_data)) {
				$day_cnt++ ;
				$ii{$event_id}{$system_id}{$day_cnt}{head_operator} = $daily_data ;
			}
		}
		
		$system_cnt = 0 ; 
		 
		foreach my $system_data (@set_up_status) {
			$system_cnt++ ;
			next unless $system_data ;
			if ($system_data =~ /~/) {
				($system_id,$system_data) = split(/\~/,$system_data) ; 
			} else {
				$system_id = $event_system_ids[$system_cnt - 1] ;
			} 
			next unless $status_hash{cnt}{$event_id}{$system_id};
			my $day_cnt = 0 ; 
			foreach my $daily_data (split(/\-/,$system_data)) {
				$day_cnt++ ;
				$ii{$event_id}{$system_id}{$day_cnt}{calibration_status} = $daily_data ;
			}
		}		
	}

	foreach my $field (keys %i) {

		local @a = split("_",$field) ;
		
		my $upd_field = "update_$a[-3]\_$a[-2]\_$a[-1]"  ; 

		next unless $i{$upd_field} ;

		if (substr($field,0,19) eq 'calibration_status_') {
			$ii{$a[-3]}{$a[-2]}{$a[-1]}{calibration_status} = $i{$field} ; 

		} elsif (substr($field,0,14) eq 'head_operator_') {
			$ii{$a[-3]}{$a[-2]}{$a[-1]}{head_operator} = $i{$field} ;
		}
		
	}
	
	# my $calid_field   = 'event_system_calibration_ids' ;
	my $calstatus_field = 'set_up_groups_status_ids' ;
	my $headops_field   = 'set_up_groups_head_operator_ids' ;

	foreach my $event_id (keys %ii) {
		
		%i  = () ;

		# &common_debug("1. +++ event_id[$event_id] date_from[$db{$table}{$event_id}{date_from}] date_to[$db{$table}{$event_id}{date_to}]  event_system_calibration_ids[$db{$table}{$event_id}{event_system_calibration_ids}] set_up_groups_status_ids[$db{$table}{$event_id}{set_up_groups_status_ids}] event_system_id_multiple[$db{$table}{$event_id}{event_system_id_multiple}]") ; 
		
		my @event_system_calibration_status = split(/\;/,$db{$table}{$event_id}{set_up_groups_status_ids}) ; 
		my @event_system_ids                = split(/\;/,$db{$table}{$event_id}{event_system_id_multiple}) ; 
		# my @calibration_ids                 = split(";",$db{$table}{$event_id}{event_system_calibration_ids}) ; 
		my @op_head_id						= split(/\;/,$db{$table}{$event_id}{set_up_groups_head_operator_ids}) ;
		
		my @ed_from = &common_split_sql_time($db{$table}{$event_id}{date_from}) ;
		my @ed_to   = &common_split_sql_time($db{$table}{$event_id}{date_to}) ; 
		
		&common_date_array("$ed_from[0]$ed_from[1]$ed_from[2]","$ed_to[0]$ed_to[1]$ed_to[2]") ;
		 
		$sys_cnt = 0;
		
		foreach my $system_id (sort { $status_hash{cnt}{$event_id}{$a} <=> $status_hash{cnt}{$event_id}{$b} } keys %{$status_hash{cnt}{$event_id}}) {
			$sys_cnt++;
			my (@calibrator_statuses_per_event_day) = split(/\-/,$event_system_calibration_status[$sys_cnt-1]);
			my $sys_day=0;
			
			my $line = qq~~ ; 
			
			foreach my $_date (@common_date_array) {
				$sys_day++;
				$line .= "$ii{$event_id}{$system_id}{$sys_day}{calibration_status}-" ;
			}
			$line =~ s/-+$// ;
			$i{$calstatus_field} .= qq~$system_id\~$line~ if $line ;
			$i{$calstatus_field} .= qq~;~ ;
		}
		$i{$calstatus_field} =~ s/;+$// ;

		$sys_cnt = 0;
		foreach my $system_id (sort { $head_operator_hash{cnt}{$event_id}{$a} <=> $head_operator_hash{cnt}{$event_id}{$b} } keys %{$head_operator_hash{cnt}{$event_id}}) {
			$sys_cnt++ ;
			my (@head_op_id_per_event_day) = split(/\-/,$op_head_id[$sys_cnt-1]) ;
			my $sys_day = 0 ;
			
			my $line = qq~~ ; 
			
			foreach my $_date (@common_date_array) {
				$sys_day++;
				$line .= "$ii{$event_id}{$system_id}{$sys_day}{head_operator}-" ;
			}
			
			$line =~ s/-+$// ;
			$i{$headops_field} .= qq~$system_id\~$line~ if $line ;
			$i{$headops_field} .= ";" ;
		}
		$i{$headops_field} =~ s/;+$// ;
		
		$ignore{$calstatus_field} 	= (($i{$calstatus_field} eq $db{$table}{$event_id}{$calstatus_field}) 	|| (!$i{$calstatus_field} 	&& !$db{$table}{$event_id}{$calstatus_field})) 	? 1 : 0  ;
		$ignore{$headops_field} 	= (($i{$headops_field} 	eq $db{$table}{$event_id}{$headops_field}) 		|| (!$i{$headops_field} 	&& !$db{$table}{$event_id}{$headops_field})) 	? 1 : 0  ;
		
		# print "\n $event_id : $calstatus_field 	($ignore{$calstatus_field}) : $i{$calstatus_field} 	, $db{$table}{$event_id}{$calstatus_field}" ;
		# print "\n $event_id : $headops_field 	($ignore{$headops_field}) 	: $i{$headops_field} 	, $db{$table}{$event_id}{$headops_field}" ;
		
		&db_min_upd($table,"`id`='$event_id'") if !$ignore{$calstatus_field} || !$ignore{$headops_field} ;
		
	}
			
	%i = %iii ;
	
	# exit ;
	
} #------------------------------------------------------------------------------------------

sub report_ifields {
	
	&load_search_vars ;
		
	if ($i{date_from} and $i{date_to}) { # check dates
		my $date_from_check = $i{date_from} ;
		   $date_from_check =~ s/\-//g ;
		my $date_to_check = $i{date_to} ;
		   $date_to_check =~ s/\-//g ;
		if ($date_from_check > $date_to_check) { 
			$error = qq~'$i{date_from}' > '$i{date_to}'~ ; &report_screen ; 
		}
		# push @report_sql, "((`date_from` >= '$i{date_from} 00:00:00' AND `date_from` <= '$i{date_to} 23:59:59') OR (`date_to` <= '$i{date_to} 23:59:59' AND `date_to` >= '$i{date_from} 00:00:00'))" ;
		# push @report_sql, "(('$i{date_from} 23:59:59' >= `date_from` AND '$i{date_from} 00:00:00' <= `date_to`) OR ('$i{date_to} 23:59:59' >= `date_from` AND '$i{date_to} 00:00:00' <= `date_to`))" ;
		# push @report_sql, "((`date_from` >= '$i{date_from} 00:00:00' AND `date_from` <= '$i{date_to} 23:59:59') OR (`date_to` >= '$i{date_from} 00:00:00' AND `date_to` <= '$i{date_to} 23:59:59'))" ;
		my @srch_ed_from = &common_split_sql_time($i{date_from}) ;
		my @srch_ed_to   = &common_split_sql_time($i{date_to}) ; 
		
		&common_date_array("$srch_ed_from[0]$srch_ed_from[1]$srch_ed_from[2]","$srch_ed_to[0]$srch_ed_to[1]$srch_ed_to[2]") ;
		
		@report_sql_or = () ;	
		
		foreach my $_srch_date (@common_date_array) {
			my $srch_ccyy_mm_dd = substr($_srch_date,0,4) . '-' . substr($_srch_date,4,2) . '-' . substr($_srch_date,6,2) ;
			push @report_sql_or, "('$srch_ccyy_mm_dd 23:59:59' >= date_from AND '$srch_ccyy_mm_dd 00:00:00' <= date_to)"
		}
		
		my $sql_or = join(' OR ',@report_sql_or) ;
		push @report_sql, "($sql_or)" if $sql_or ; 
		
		# push @report_sql, "((`date_from` >= '$i{date_from} 00:00:00' AND `date_from` <= '$i{date_to} 23:59:59') OR (`date_to` >= '$i{date_from} 00:00:00' AND `date_to` <= '$i{date_to} 23:59:59'))" ;
		push @report_results, "(BETWEEN '$i{date_from}' AND '$i{date_to}')" ;
	} elsif (not $i{date_from}) {
		$error = qq~PLEASE ENTER A START DATE!~ ; 
		&report_screen; 
	} elsif (not $i{date_to}) {
		$error = qq~PLEASE ENTER AN END DATE!~ ; 
		&report_screen; 
	}
	
	# if ($i{calibrator_id} and $i{calibrator_id} ne 'all') { 
		# push @report_results,"`CALIBRATOR` = '$db{users}{1}{name}'" ;
		# push @report_sql, "
			# (
				# `event_system_calibration_ids` LIKE '%;$i{calibrator_id};%' OR 
				# `event_system_calibration_ids` LIKE '%;$i{calibrator_id}' OR 
				# `event_system_calibration_ids` LIKE '$i{calibrator_id};%' OR 
				# `event_system_calibration_ids` LIKE '%-$i{calibrator_id};%' OR 
				# `event_system_calibration_ids` LIKE '%;$i{calibrator_id}-%' OR
				# `event_system_calibration_ids` LIKE '%-$i{calibrator_id}-%' OR	
				# `event_system_calibration_ids` LIKE '$i{calibrator_id}-%' OR		
				# `event_system_calibration_ids` LIKE '%-$i{calibrator_id}'				
			# )
		# " ; 
	# } elsif ($i{calibrator_id} and $i{calibrator_id} eq 'all') {
		# push @report_results,"`CALIBRATORS` = 'ALL'" ;
	# } else {
		# # push @report_sql, "(`user_id` = '')" ; 
	# }
	
	if ($i{calibration_status} and $i{calibration_status} ne 'all') {
		push @report_sql, "
			(
				`set_up_groups_status_ids` LIKE '%;$i{calibration_status};%'	OR 
				`set_up_groups_status_ids` LIKE '%;$i{calibration_status}' 		OR 
				`set_up_groups_status_ids` LIKE '$i{calibration_status};%' 		OR 
				`set_up_groups_status_ids` LIKE '%-$i{calibration_status};%' 	OR 
				`set_up_groups_status_ids` LIKE '%;$i{calibration_status}-%' 	OR
				`set_up_groups_status_ids` LIKE '%-$i{calibration_status}-%' 	OR	
				`set_up_groups_status_ids` LIKE '$i{calibration_status}-%' 		OR	 
				`set_up_groups_status_ids` LIKE '%-$i{calibration_status}'		OR	
				`set_up_groups_status_ids` LIKE '%~$i{calibration_status};%' 	OR 
				`set_up_groups_status_ids` LIKE '%~$i{calibration_status}' 		OR 
				`set_up_groups_status_ids` LIKE '%~$i{calibration_status}-%' 	 				
			)
		" ; 
		push @report_results,"`STATUS` = '$db{calibration_status}{$i{calibration_status}}{status}'" ;
	}
	
	if ($i{client_id}) {
		push @report_sql,"quote_to='$i{client_id}'" ;
		&db_min_ro('customers','id,name',"id='$i{client_id}'",'','') ;
		push @report_results,"CLIENT = '$db{customers}{$i{client_id}}{name}'" ;
	}
		
	$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 {
	
	# if ($i{calibrator_id} and $i{calibrator_id} ne 'all') { 
		# &db_min_ro('users','1,name',"`id` = '$i{calibrator_id}'",'','') ;
	# }
	
	&db_min_ro('calibration_status','id,status','','','') ;
	
	our $calibration_status = 1 ; 
	
} #------------------------------------------------------------------------------------------

sub list_screen {
	
	$print_box_content_rows .= &common_min_forms_start("$lcpage") ;
	
	our $set_up_groups = 1 ; 
	
	&calibration_load_list_vars("$srch_where_sql") ;
 
	# $box_icon .= qq~<div class="box-icon" title='Toggle all Changed' data-toggle="tooltip" data-placement="left"><a class="btn btn-default btn-round" href="javascript:selectAll();" style="padding-top:1px;"><i class="glyphicon glyphicon-usd icon-white"></i></a></div>~ ;
	
	# $trigger_jquery_raw .= qq~
	
	# function selectAll() {
				
		# let jsObject = {
			# $quote_and_ops_ids
		# };

		# for (let key in jsObject) {
			# let op_id = jsObject[key];
			
			# let parts = key.split("_") ;
			# let event_id = parts[1] ;
			
			# \$(\$("#itv-table").dataTable().fnGetNodes()).find(\$("input[name=update_"+event_id+"_"+op_id+"]")).each(function () {      
				
				# console.log("input[name=paid_"+event_id+"_"+op_id+"]") ; 
				# if(\$(this).is(':checked')) { 
					# \$(this).prop('checked',false);
				# } else {
					# \$(this).prop('checked',true);
				# }
			# }) ;
		# }
	# }
	# ~; 
	# $fcol = 2 ; 
	
	# $print_box_content_rows .= qq~<div style = "display:none;">~ ; 
	# $print_box_content_rows .= &common_min_form_input_col("date_from",$i{date_from}) ; #date_from_i_field
	# $print_box_content_rows .= &common_min_form_input_col("date_to",$i{date_to}) ; #date_to_i_field
	# $print_box_content_rows .= &common_min_form_input_col("calibrator_id",$i{calibrator_id}) ;  #operator_id_i_field
	# # $print_box_content_rows .= &common_min_form_input_col("event_quote_id",$i{event_quote_id}) ;
	# $print_box_content_rows .= &common_min_form_input_col("calibration_status",$i{calibration_status}) ;
	# $print_box_content_rows .= qq~</div>~ ;
	
	$fcol = 12 ; 
	
	my $report = 1 ; 
	
	our $xlsxdir = "set_up_groups_report" ; 
	
	our $xlsxreportname = "Set_Up_Groups_Report" ; 
 
	my $date = &common_write_date_interval($i{date_from},$i{date_to}) ;
	
	our $xlsx_title_heading = "Set Up Groups Report $date" ;
	
	my $found_after_date = 0 ; 
	my $today_val = int("$now_year$now_mm$now_dd") ; 
	
	my @date_from_inp = split(/ /,$i{date_from}) ; 
	my ($year_inp1,$month_inp1,$day_inp1) = split(/\-/,$date_from_inp[0]) ; 
	my $date_inp_val1 = int("$year_inp1$month_inp1$day_inp1") ; 
	
	my @date_to_inp = split(/ /,$i{date_to}) ; 
	my ($year_inp2,$month_inp2,$day_inp2) = split(/\-/,$date_to_inp[0]) ; 
	my $date_inp_val2 = int("$year_inp2$month_inp2$day_inp2") ; 
			
	foreach (keys %{$db{$table}}) {
		my @date_from = split(/ /,$db{$table}{$_}{date_from}) ; 
		my ($year_1,$month_1,$day_1) = split(/\-/,$date_from[0]) ; 
		my $date_val1 = int("$year_1$month_1$day_1") ; 
		my @date_to = split(/ /,$db{$table}{$_}{date_to}) ; 
		my ($year_2,$month_2,$day_2) = split(/\-/,$date_to[0]) ; 
		my $date_val2 = int("$year_2$month_2$day_2") ; 
		$found_after_date = 1 if ($today_val <= $date_val1 && $today_val <= $date_inp_val1) || ($today_val <= $date_val2 && $today_val <= $date_inp_val2) ; 
	}

	# if ($found_after_date) { 
		# $trigger_jquery_raw .= qq~
			# \$("#itv-table tr th:nth-last-child(3)").css("width", "12.5%") ;
			# \$("#itv-table tr th:nth-last-child(2)").css("width", "7.5%") ;
			# \$("#itv-table tr th:nth-last-child(4)").css("width", "12.5%") ;
		# ~ ;		 
	# } else {
		# $trigger_jquery_raw .= qq~
			# \$("#itv-table tr th:nth-last-child(3)").css("width", "7.5%") ;
			# \$("#itv-table tr th:nth-last-child(2)").css("width", "7.5%") ;
			# \$("#itv-table tr th:nth-last-child(4)").css("width", "7.5%") ;
		# ~ ;		 
	# }
	
	
	
	&calibration_build_table_and_or_excel(1) ; 

	$fnsortorder = 'asc' ;
	
	&common_min_extra_crumb("$lcpage","Search Screen") ;
 
	$trigger_jquery_raw .= qq~
		\$("#savebutt").click(function() {
			\$("#$lcpage-form").submit() ;
		}) ;
	~ ;
	
} #------------------------------------------------------------------------------------------

sub set_tick {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

sub set_cross {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

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

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'set-up-groups-report' ;

	&common_page_name ;
	   
	our $table     = 'event_quotes' ;
	$page_title    = 'Set Up Groups Report' ;

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

sub report_screen {

	our $lcol = 3 ;
	our $fcol = 5 ;

	$print_box_content_rows .= &common_min_forms_start('report') ;
	
	# &common_min_select_opts('client_id','customers','name','','','',"events='1'") ;
	my $def = ($i{client_id}) ? $i{client_id} : 5 ;
	$allow_deselect{client_id} = 1 ; 
	&common_min_select_opts('client_id','customers','name',$def,'','',"") ;
	
	$print_box_content_rows  .= &common_min_form_select('client_id','') ;
	
	my ($sql_next_day) = &common_add_delta_days(1) ;
	my ($next_year,$next_month,$next_day) = split(/\-/,$sql_next_day) ;
	$print_box_content_rows .= &common_min_form_datepicker('date_from',"$next_year-$next_month-$next_day") ; #unless $i{date_from} ;  
	$print_box_content_rows .= &common_min_form_datepicker('date_to',"$next_year-$next_month-$next_day") ;

	$selected{calibration_status}{all} = 'SELECTED' ;
	if ($i{calibration_status}) {
		$selected{calibration_status}{$i{calibration_status}} = 'SELECTED' ;
		$selected{calibration_status}{all} = '' ;
	} 
	
	&db_min_ro('calibration_status','id,status','','','') ;
	
	# $opts{calibration_status} .= qq~<option value="-1" $selected{calibration_status}{-1}>None</option>~ ; 
	foreach (keys %{$db{calibration_status}}) {
		$opts{calibration_status} .= qq~<option value="$_" $selected{calibration_status}{$_} >$db{calibration_status}{$_}{status}</option>~ ; 
	}
	$opts{calibration_status} .= qq~<option value="all" $selected{calibration_status}{all} >All</option>~ ; 
	
	$preferred_title{calibration_status} = "Status" ;
	
	# $preferred_title{calibrator_id} = "Calibrator" ;
	
	# # &db_min_ro('users',"id,name","`user_type` = 'support' OR `user_type` = 'casual_calibrator'",'','') ;
	# &db_min_ro('users',"id,name,username","(user_type = 'support' OR user_type = 'manager' OR user_type = 'casual_calibrator') AND name NOT LIKE 'Rory%' AND name NOT LIKE 'Handre%'",'','') ;

	# $selected{calibrator_id}{all} = 'SELECTED' ;
	
	# if ($i{calibrator_id}) {
		# $selected{calibrator_id}{$i{calibrator_id}} = 'SELECTED' ;
		# $selected{calibrator_id}{all} = '' ;
	# } 
	
	# # $opts{calibrator_id} .= qq~<option value="-1" $selected{calibrator_id}{-1}>None</option>~ ; 
	# foreach (sort {$db{users}{$a}{name} cmp $db{users}{$b}{name}} keys %{$db{users}}) {
		# next if $useropts{it}{$db{users}{$_}{username}} ;
		# $opts{calibrator_id} .= qq~<option value="$_" $selected{calibrator_id}{$_}>$db{users}{$_}{name}</option>~ ; 
	# }
	# $opts{calibrator_id} .= qq~<option value="all" $selected{calibrator_id}{all}>All</option>~ ; 

	# if ($i{event_quote_id}) {
		# $selected{event_quote_id}{$i{event_quote_id}} = 'SELECTED' ;
	# } else {
		# $selected{event_quote_id}{all} = 'SELECTED' ;
	# }	
	
	# $opts{event_quote_id} .= qq~<option value="all" $selected{event_quote_id}{all}>All</option>~ ; 	
	
	# $print_box_content_rows  .= &common_min_form_select('operator_id','') ; 
	# $print_box_content_rows  .= &common_min_form_select('event_quote_id','') ; 
	$print_box_content_rows  .= &common_min_form_select('calibration_status','') ; 
	# $print_box_content_rows  .= &common_min_form_select('calibrator_id','') ; 
	$print_box_content_rows  .= &common_min_forms_end('','','report') ;

	&common_min_search_screen ;

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

sub screen3 {

	&thead;

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

	# my $page = "$lcpage\s" ;

	my $page = "event-quotes" ;

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

	&common_min_table('id',$page,'list') ;

	our $savjqy = 1 ;

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

	$extra_form_fields .= qq~
	<input type="hidden" name="date_from" 			value="$i{date_from}">
	<input type="hidden" name="date_to"				value="$i{date_to}">
	<input type="hidden" name="calibration_status" 	value="$i{calibration_status}"> 
	~;

	$print_box_content_rows .= &common_min_forms_end('','','save') ;

	require _blank ;

	exit ;

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

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="date_from"        	value="$i{date_from}">
	<input type="hidden" name="date_to"          	value="$i{date_to}"> 
	<input type="hidden" name="calibration_status"	value="$i{calibration_status}">
	<input type="hidden" name="client_id"			value="$i{client_id}">
	<input type="hidden" name="isaved"				value="$success">
	</form>

	</html>
ENDOFTEXT
#

	exit ;

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

use common ;
use report ;
use xlsxcreator ;
use today ;
use calibration ;

1;