#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
use Date::Calc qw(:all);
use DBI;
    
# require cfg ;

print header;   # CGI.pm method

# run with https://www.ffwaybill.co.za/cgi-bin/scripts/admin/load_leave_settings.pl

exit;

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

&today;

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

# unless ($username eq 'rory') { print ">>>>>> RORY DOING MAINTENANCE <<<<<<"; exit; }

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

our $debug = 1 ;
our $username = 'rory' ;

my $last_year = $now_year - 1 ;

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

&common_min_debug(">>> START <<< load_leave_settings.pl");

&read_leave_settings;
&read_leave_calendar;
&process;

&common_min_debug(">>> END <<< load_leave_settings.pl");

exit;

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

sub read_leave_settings {

# &db_min_ro(leave_settings,'*',"`leave_year`='$i{leave_year}' AND `category`='$i{category}' AND `user_id`='$i{user_id}'",'','') ;
&db_min_ro('leave_settings','*',"`leave_year`='$now_year' OR `leave_year`='$last_year'",'','') ;

foreach my $id (keys %{$db{leave_settings}}) { 
	# if (($db{leave_settings}{$id}{leave_year} eq $i{leave_year}) and ($db{leave_settings}{$id}{category} eq $i{category}) and ($db{leave_settings}{$id}{user_id} eq $i{user_id})) { 
	$days_entitled{$db{leave_settings}{$id}{category}}{$db{leave_settings}{$id}{leave_year}}{$db{leave_settings}{$id}{user_id}}     = $db{leave_settings}{$id}{days_entitled} ; 
	$days_carried_over{$db{leave_settings}{$id}{category}}{$db{leave_settings}{$id}{leave_year}}{$db{leave_settings}{$id}{user_id}} = $db{leave_settings}{$id}{days_carried_over} ; 	
	}


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

sub read_leave_calendar {

my $last_year = $now_year - 1 ;

&db_min_ro('events','*',"(((`startdate` LIKE '$now_year%') OR (`enddate` LIKE '$now_year%')) OR ((`startdate` LIKE '$last_year%') OR (`enddate` LIKE '$last_year%'))) AND `category`='leave'",'`id` DESC','') ;

foreach my $id (keys %{$db{events}}) { 
	# if (($db{leave_settings}{$id}{leave_year} eq $i{leave_year}) and ($db{leave_settings}{$id}{category} eq $i{category}) and ($db{leave_settings}{$id}{user_id} eq $i{user_id})) { 
	# $days_taken{$db{leave_settings}{$id}{leave_year}}{$db{leave_settings}{$id}{category}}        = $db{leave_settings}{$id}{days_entitled} ; 
	# $days_carried_over{$db{leave_settings}{$id}{leave_year}}{$db{leave_settings}{$id}{category}} = $db{leave_settings}{$id}{days_carried_over} ; 
	
	my ($sdate,$stime) = split(/ /,$db{events}{$id}{startdate}) ;    
    my $s_ccyymmdd = &common_min_convert_sql_date_time($sdate);
    my ($edate,$etime) = split(/ /,$db{events}{$id}{enddate}) ;    
    my $e_ccyymmdd = &common_min_convert_sql_date_time($edate);

    &dates_array($s_ccyymmdd,$e_ccyymmdd) ;
	
	my $val = $db{events}{$id}{days} ;
	
	# $days_taken{$db{events}{$id}{type}}+=$val;
	# $cats{$db{events}{$id}{type}} = 1 ;             
				
	my $scalar = scalar @dates_array ; my $taken_per_day = 1 ; 
	
	if ($scalar>0) { 
		$taken_per_day = $val / $scalar ; 
		} 
	else 
		{ 
		# $taken_per_day = $days_taken{$db{events}{$id}{type}} ;
		$taken_per_day = $db{events}{$id}{days} ;
		my $sccyymmdd = &common_min_convert_sql_date_time($db{events}{$id}{startdate}); 
		push @dates_array, $sccyymmdd ; # create array when start and end date same
		}
	
	foreach my $date (@dates_array) {
		my $ccyy = substr($date,0,4) ;
		my $mm   = substr($date,4,2) ; $mm = sprintf("%0.0f",$mm) ;
		my $dd   = substr($date,6,2) ; $dd = sprintf("%0.0f",$dd) ;
		
		$days_taken_by_year{$db{events}{$id}{type}}{$ccyy}{$db{events}{$id}{user_id}}+=$taken_per_day;
				
		# if (($day_of_week{$date} > 0) and (($day_of_week{$date} < 6))) { # mon - fri
			# $days_txt{$mm}{$db{events}{$id}{type}} .= "$dd/" ;
			# }
		
		# &common_min_debug(">>> $db{events}{$id}{type} [$mm] [$days_taken_by_month{$mm}{$ccyy}{$db{events}{$id}{type}}] [$taken_per_day]");
		}
	}


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

sub process {

&db_min_ro('users','*','','','') ;
foreach my $id (keys %{$db{users}}) { $user{$id} = $db{users}{$id}{name} ; }

foreach my $year (keys %{$days_entitled{annual}}) {
	foreach my $user_id (keys %{$days_entitled{annual}{$year}}) { 
		if ($done{$user_id}{$year}) { next ; }
		# &common_min_debug(">>> 1. $year [$user{$user_id}] [days_entitled=$days_entitled{annual}{$year}{$user_id}] [days_carried_over=$days_carried_over{annual}{$year}{$user_id}] [days_taken in $year = $days_taken_by_year{annual}{$year}{$user_id}]");
		$done{$user_id}{$year} = 1 ;
		$leave_settings_for_current_year{$user_id}{$year} = 1 ;
		&insert_leave_settings($user_id,$year) ;
        }
	}

foreach my $year (keys %{$days_taken_by_year{annual}}) {
	foreach my $user_id (keys %{$days_taken_by_year{annual}{$year}}) { 
		if ($done{$user_id}{$year}) { next ; }
		# &common_min_debug(">>> 2. $year [$user{$user_id}] [days_entitled=$days_entitled{annual}{$year}{$user_id}] [days_carried_over=$days_carried_over{annual}{$year}{$user_id}] [days_taken in $year = $days_taken_by_year{annual}{$year}{$user_id}]");
		$done{$user_id}{$year} = 1 ;
		&insert_leave_settings($user_id,$year) ;
        }
	}
    
foreach my $id (keys %{$db{users}}) { 
    unless ($done{$id}{$now_year}) { 
        &insert_leave_settings($id,$now_year) ; 
        }
    }

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

sub insert_leave_settings {

my ($user_id,$year) = @_ ;

unless ($year eq $now_year)                                    { return ; } # &common_min_debug("insert_leave_settings ERROR : $year ne $now_year");
if     ($leave_settings_for_current_year{$user_id}{$now_year}) { &common_min_debug("insert_leave_settings ERROR : $year settings already EXIST [$user{$user_id}]"); return ; }
if     (lc substr($user{$user_id},0,4) eq 'rory')              { return ; }
if     (lc substr($user{$user_id},0,4) eq 'test')              { return ; }

# $i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;

my $balance = $days_entitled{annual}{$last_year}{$user_id} + $days_carried_over{annual}{$last_year}{$user_id} - $days_taken_by_year{annual}{$last_year}{$user_id} ;

$i{user_id} = $user_id ;
$i{days_entitled} = 15 ;
$i{category} = 'annual' ;
$i{leave_year} = $now_year ;
$i{lastupdate} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
$i{days_carried_over} = $balance ;
$i{leave_start_date} = "$year-01-01" ;

# &common_min_debug("insert_leave_settings INSERT >>> [$user{$user_id},$year] days_carried_over=$i{days_carried_over} [$balance = $days_entitled{annual}{$last_year}{$user_id} + $days_carried_over{annual}{$last_year}{$user_id} - $days_taken_by_year{annual}{$last_year}{$user_id}] ;");

&db_min_insert('leave_settings') ;

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

sub dates_array {

my ($start_date,$end_date) = @_ ;

my $array_date = $start_date ;

@dates_array=() ;

while ($array_date < $end_date) {

    push @dates_array, $array_date ;

    my $stay_date_dd   = substr($array_date,6,2) ;
    my $stay_date_mm   = substr($array_date,4,2) ;
    my $stay_date_ccyy = substr($array_date,0,4) ;

    $day_of_week{$array_date} = Day_of_Week($stay_date_ccyy,$stay_date_mm,$stay_date_dd) ; # 1 is Monday

    my ($ccyy,$month,$day)  = Add_Delta_Days($stay_date_ccyy,$stay_date_mm,$stay_date_dd,1) ;
    $month  = sprintf("%02s", $month) ;
    $day    = sprintf("%02s", $day) ;

    $array_date = "$ccyy$month$day" ;
    }

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

# sub validate {

# &db_min_ro(leave_settings,'*',"`leave_year`='$i{leave_year}' AND `category`='$i{category}' AND `user_id`='$i{user_id}'",'','') ;

# foreach my $id (keys %{$db{leave_settings}}) { 
	# if (($db{leave_settings}{$id}{leave_year} eq $i{leave_year}) and ($db{leave_settings}{$id}{category} eq $i{category}) and ($db{leave_settings}{$id}{user_id} eq $i{user_id})) { 
		# $exists = 1 ; 
		# } 
	# }


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

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

1;