#!/usr/bin/perl

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

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
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 ;

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

&page_opts ;

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

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

&common_min_action;

exit;

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

sub validate {

	my $exists   = 0 ;

	# $i{name} = &common_fix_str($i{name}) ;

	&db_min_ro($table,'*','','','') ;
	
	my $cost = '' ; my $seen_start = '' ; my $seen_end = '' ; my %seen_date = () ; 
	
	foreach my $id (keys %{$db{$table}}) {
		
		$seen_date{$id}{valid_from} = $db{$table}{$id}{valid_from} ;
		$seen_date{$id}{valid_to} 	= $db{$table}{$id}{valid_to} ;
		$seen_date{$id}{cost} 		= $db{$table}{$id}{cost} ;
	
	}
	
	foreach my $id2 (keys %seen_date) {

		if (&common_min_greater_date($seen_date{$id2}{valid_from},$i{valid_from}) && &common_min_greater_date($i{valid_from},$seen_date{$id2}{valid_to})) {
			$exists = 1 ;
		} 
		if (!$exists) {
			if (&common_min_greater_date($seen_date{$id2}{valid_from},$i{valid_to}) && &common_min_greater_date($i{valid_to},$seen_date{$id2}{valid_to})) {
				$exists = 1 ;
			} 	
			if (!$exists) {
				if (&common_min_greater_date($i{valid_from},$seen_date{$id2}{valid_from}) && &common_min_greater_date($seen_date{$id2}{valid_to},$i{valid_to})) {
					$exists = 1 ;
				}
				if (!$exists) {
					if (&common_min_greater_date($seen_date{$id2}{valid_from},$i{valid_from}) && &common_min_greater_date($i{valid_to},$seen_date{$id2}{valid_to})) {
						$exists = 1 ;
					}	
				}			
			}
		}

		if ($exists) {
			$cost = $seen_date{$id2}{cost} ; 
			$seen_start = $seen_date{$id2}{valid_from} ; 
			$seen_end = $seen_date{$id2}{valid_to} ; 
			last ;
		}
	}

	if ($exists) {
		$alert = &common_min_alert('warning',"YOUR DATE RANGE OVERLAPS WITH THE COST $cost FROM $seen_start TO $seen_end!!!",'ok') ;
		%col_name = () ;
		&common_min_error_screen ;
		&common_min_screen1 ;
    }

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

sub insert {

	&add_db_fields ;
	
	$i{id} = &db_min_get_max($table,'id') ;

	&db_min_insert($table) ;

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

sub update {

	unless ($i{id}) { $error = qq(NO ID) ; return ; }

	&edit_db_fields ;

	&db_min_upd($table,"id='$i{id}'") ;

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

sub list_screen {
	
	# &db_min_ro('event_systems','*',"(`system_type` = 'solo' OR `system_type` = 'mobile' OR `description` lIKE 'cricket%')",'','') ; 
	
	&db_min_ro($table,'*','','','') ;
	
	foreach my $id (keys %{$db{$table}}) {	
		$cost_dates{$id}{valid_from} = $db{$table}{$id}{valid_from} ;
		$cost_dates{$id}{valid_from} =~ s/\-//g ;
		$cost_dates{$id}{valid_to} = $db{$table}{$id}{valid_to} ;
		$cost_dates{$id}{valid_to} =~ s/\-//g ;	
	}
	
	&db_min_ro('event_quotes',"id,date_from,date_to,logistics_shipment_operator_id","logistics_shipment_operator_id IS NOT NULL AND logistics_shipment_operator_id != '-'",'','') ;
	
	foreach my $id (keys %{$db{event_quotes}}) {
		my @operators = split(/\-/,$db{event_quotes}{$id}{logistics_shipment_operator_id}) ; 
		my $date_from = substr($db{event_quotes}{$id}{date_from},0,10) ; 
		$date_from =~ s/\-//g ;
		my $date_to = substr($db{event_quotes}{$id}{date_to},0,10) ; 
		$date_to =~ s/\-//g ;

		foreach my $id2 (keys %cost_dates) {
			if ($operators[0] && $cost_dates{$id2}{valid_from} <= $date_from && $date_from <= $cost_dates{$id2}{valid_to}) {
				$cost_id_used{$id2} = 1 ;
			}
			if (!$cost_id_used{$id2} && $operators[1] && $cost_dates{$id2}{valid_from} <= $date_to && $date_to <= $cost_dates{$id2}{valid_to}) {
				$cost_id_used{$id2} = 1 ;
			}
		}
	}
		# 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++ ;
		# }

		# next unless $cnt ;

	# }
	
	our @months = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ; 

	foreach my $id (keys %{$db{$table}}) {
		
		my $edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="Request user rights in order to edit." data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
		my $del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="Request user rights in order to delete." data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;  
		
		if ($glod_user_level >= 3) {
			$edit_butt = qq~&nbsp;<a class="btn btn-info btn-xs" href="javascript:editMinItem('$db{$table}{$id}{id}');"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ; 
		}
		
		if ($glod_user_level >= 3) {
			$del_butt  = qq~&nbsp;<a class="btn btn-danger btn-xs" href="javascript:deleteMinItem('$db{$table}{$id}{regions}','$db{$table}{$id}{id}')"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ; 
		}
			
		if ($cost_id_used{$id}) {
			# $edit_butt = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="In system use!" data-toggle="tooltip"><i class="glyphicon glyphicon-edit icon-white"></i></a>~ ;   
			$del_butt  = qq~&nbsp;<a class="btn btn-default btn-xs" href="#" title="In system use!" data-toggle="tooltip"><i class="glyphicon glyphicon-trash icon-white"></i></a>~ ;  
		}
		
		my $valid_from 	= &common_min_date_as_string($db{$table}{$id}{valid_from}) ;
		my $valid_to 	= &common_min_date_as_string($db{$table}{$id}{valid_to}) ;
		
		$print_tbody .= qq~
			<tr id="$id">
				<td>$id</td>
				<td>$db{$table}{$id}{cost}</td>
				<td>$valid_from</td>
				<td>$valid_to</td>
				<td nowrap>
					$edit_butt
					$del_butt
				</td>
			</tr>
		~ ;
	}

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

sub add_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

	$ignore{id} = 1 ;

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

sub edit_db_fields {
	
	&sort_fields;
		
	&hidden_db_fields;

	$hidden{id} = 1 ; # 1 = bypass for db update

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

sub hidden_db_fields {
	
	$ignore{iaction}        = 1 ;
	$required{cost}         = 9 ;
	$required{valid_from}	= 1 ;
	$required{valid_to}		= 1 ;
	$datepicker{valid_from}	= 1 ;
	$datepicker{valid_to}	= 1 ;
	
} #------------------------------------------------------------------------------------------

sub sort_fields {
	
	%sort_field = () ;

	$sort_field{1}  = 'cost' ; # $preferred_title{code} = 'Code' ;
	$sort_field{2}  = 'valid_from' ; # $preferred_title{name} = 'Name' ;
	$sort_field{3}  = 'valid_to' ; # $preferred_title{latitude} = 'Latitude' ;
	
} #-------------------------------------------------------------------------------

sub add_screen {

	# called from common_add_screen

	&select_opts ;

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

sub edit_screen {

	&select_opts ;

    # &common_min_select_opts('code','regions','name',$db{$table}{$i{id}}{name},'','',$sql_or,'name');
} #------------------------------------------------------------------------------------------

sub select_opts {

	# $required{based_in} = 1 ;
	# $select{based_in} = 1 ;
	# $opts{based_in} = qq~<option value="JNB" $selected{based_in}{'JNB'}>Johannesburg</option><option value="CPT" $selected{based_in}{'CPT'}>Cape Town</option>~ ;
	
	# my $sql_or = '' ;
	
	# if ($is_schools_manager || $is_operator) {
		# @report_sql_or = () ;
		# foreach $_reg_id (keys %{$glob_regids{$userid}}) {
			# push @report_sql_or, "id = '$_reg_id'"
		# }
		# $sql_or = join(' OR ',@report_sql_or) ;
	# }

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

sub thead {

	$print_thead = qq~<THEAD><TR><TH>ID</TH><TH>COST</TH><TH>VALID FROM</TH><TH>VALID TO</TH><TH>&nbsp;</TH></TR></THEAD>~ ;
    
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'globe' ;
	our $lcpage    = 'logistics-shipment-costs' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
	our $table     = 'logistics_shipments_cost' ;
	
	$s{no}         = 1 ;

	&common_min_add_extras ;

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

use common ;
use today ;

1;