#!/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 Spreadsheet::WriteExcel;
use Spreadsheet::WriteExcel::Utility;
    
require cfg ;

print header;   # CGI.pm method

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

&today;

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

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

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

# our $debug = 1 ;

&common_min_restriction_super ;

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

&page_opts ;

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

if ($iaction eq ''){
    &report_screen ;
    # &common_min_screen1;
	}
elsif ($iaction eq 'report'){
	&common_min_load_params ;
	&report_ifields;
	&list_screen ;    
    &report_screen ;
    # &common_min_screen3;
	}

exit;

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

sub report_ifields {

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(DATE FROM ($i{date_from}) > DATE TO ($i{date_to})); &report_screen; }
    }

if ($i{date_from}) {
    push @report_sql, "(`startdate` >= '$i{date_from}')" ;
    push @report_results, "startdate >= $i{date_from}" ;
    }

if ($i{date_to}) {
    push @report_sql, "(`startdate` <= '$i{date_to}')" ;
    push @report_results, "startdate <= $i{date_to}" ;
    }
	
&db_min_ro('users','*',"`id`='$i{user_id}'",'','') ;
foreach my $id (keys %{$db{users}}) { $user{$id} = $db{users}{$id}{name} ; }

push @report_sql, "(`user_id` = '$i{user_id}')" ;
push @report_results, "Consultant = $user{$i{user_id}}" ;

$srch_where_sql = join(' AND ', @report_sql) ;

$report_results_msg = uc join(', ', @report_results) ;

unless ($i{user_id})         { $error = qq(PLEASE SELECT A CONSULTANT); &report_screen;          } else { $success = qq(REPORT WHERE $report_results_msg) ; }
unless ($report_results_msg) { $error = qq(ENTER AT LEAST ONE REPORT PARAMETER); &report_screen; } else { $success = qq(REPORT WHERE $report_results_msg) ; }
 
} #------------------------------------------------------------------------------------------

sub list_screen {

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

our @sql_col_display = ("date_time","user_id","startdate","enddate","notes","duration") ;

# our %money_format = (agent_balance => 1);

my $xlsfolder = 'weekendwork' ;

&report_xls_export_header("$user{$i{user_id}}_Weekend_Work_Report",$xlsfolder) ;

# &db_min_ro($table,'*','','','') ;
&db_min_ro($table,'*',"$srch_where_sql AND `category`='weekend_work'",'','') ;

foreach my $id (keys %{$db{$table}}) {

    # unless ($db{$table}{$id}{category} eq 'weekend_work') { next ; }

    $print_tbody .= qq(<tr>) ;
		
	$xlscol=0;
    
    foreach (@sql_col_display) { 
        my $val = $db{$table}{$id}{$_} ;
		if ($_ eq 'user_id')  { $val = $user{$db{$table}{$id}{$_}}  ; }        		
		
		if ($_ eq 'duration') { # calculate time worked
			my ($s_dy,$s_dm,$s_dd,$s_th,$s_tm,$s_ts) = &common_min_split_sql_date_time($db{$table}{$id}{startdate}) ;
			my ($e_dy,$e_dm,$e_dd,$e_th,$e_tm,$e_ts) = &common_min_split_sql_date_time($db{$table}{$id}{enddate}) ;
			my ($Dd,$Dh,$Dm,$Ds) = Delta_DHMS($s_dy,$s_dm,$s_dd, $s_th,$s_tm,$s_ts, $e_dy,$e_dm,$e_dd, $e_th,$e_tm,$e_ts);
			$val = $Dd . ' days, ' . $Dh . ' hrs, ' . $Dm . ' min'  ; 
			} 
      		
		&report_xls_export_process_cell('',$money_format{$_},$val) ;
        $xlscol++; 
        if ($_ eq 'user_id') { $val = qq(<span class="label-primary label">$user{$db{$table}{$id}{$_}}</span>); }
		$print_tbody .= qq(<td>$val</td>) ;  
        }
		
	$print_tbody .= qq(</tr>) ;
    
    $xlsrow++ ;
    }
	
# $print_ttl_agent_balance = &common_min_commify(sprintf("%0.2f",$ttl_agent_balance)) ;
# our $add_th_row = qq(<tr><th colspan='4'></th><th class="dt-center">$print_ttl_agent_balance</th></tr>) ; # used in common_min_thead
	
&report_xls_export_footer('F',30) ;

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

sub report_screen {

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

# &db_min_ro($table,'*','','',1) ;

@user_excl_sql = () ;

foreach (keys %{$useropts{it}}) {
	push @user_excl_sql, "username <> '$_'" ;
}

my $sql_where = join(" AND ", @user_excl_sql) ;

# my $sql_where = '' ; unless ($useropts{super}{lc $username}) { $sql_where = "`id`='$userid'" ; }
unless ($useropts{super}{lc $username}) { $sql_where = " AND `id`='$userid'" ; }
	
&common_min_select_opts('user_id','users','name',$i{user_id},'','',"$sql_where") ;

$print_box_content_rows .= &common_min_forms_start('report') ;
$print_box_content_rows .= &common_min_form_datepicker('date_from',$i{date_from}) ;  
$print_box_content_rows .= &common_min_form_datepicker('date_to',$i{date_to}) ; 
$print_box_content_rows .= &common_min_form_select('user_id','','',1,'','Consultant') ;
$print_box_content_rows .= &common_min_forms_end('','','report') ; 

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

if ($print_tbody) { &thead; &common_min_table('id',$page,'list') ; }

&screen1 ;

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

sub thead {

&common_min_thead ;
    
} #-------------------------------------------------------------------------------

sub screen1 {

&common_min_alert_type ;

require _blank ;

exit ;

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

sub page_opts {


our $glyphicon = 'global' ;
our $lcpage    = 'weekend-report' ; $ucpage = uc $lcpage ; $ucfirstpage = ucfirst $lcpage ;
our $table     = 'events' ;
our $srchscr   = 1 ;

# our $open_new_tab = qq(target="_blank") ; # opens booking page in a new tab
# our $fnsortorder  = 'asc' ;

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

use common ;
use today ;
use report ;
use xlscreator ;

1;