#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
use CGI::Carp qw(fatalsToBrowser);
use Mail::POP3Client;
use MIME::Parser;
use Fcntl qw(:flock) ;
use DBI ;
use MIME::Lite;
use Mail::Sendmail;
use Date::Calc qw(:all);
use Excel::Writer::XLSX ; 
use Time::Piece ; 

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

require cfg_paths ;

&config_set_vars ;
&config_set_paths ;

# https://itvadmin.co.za/cgi-bin/scripts/cron/auto_get_next_day_events.pl?1

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

# my  @ARGV     = split(/\&/, $ENV{'QUERY_STRING'});
our $debug = $ARGV[0] ; our $username = 'rory' if $debug == 1 ; print "Content-type: text/html\n\n" if $debug == 1 ;

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

&today;

# &common_send_smtp_mail('','rory@kre8it.co.za','','',"Calibration : $now_dd_mm_ccyy","Start calibration cron...",'',1,'','','','',0,0) ;

&db_open_ro;
	$db_ignore_open_close=1;
		&load_calibration_vars ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;
&create_calibration_excel_file ;
&send_email('calibration') ;
&create_fixedsystem_excel_file ;
&send_email('fixed') ;

exit;

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

sub load_calibration_vars {
	
	our ($sql_next_day) = &common_add_delta_days(1) ;
	
	our ($next_year,$next_month,$next_day) = split(/\-/,$sql_next_day) ;
	
	our $table = 'event_quotes' ;
	
	&db_min_ro($table,"1,count(*) AS 'next_day_cnt'","('$sql_next_day 23:59:59' >= `date_from` AND '$sql_next_day 00:00:00' <= `date_to`) AND `quote_accepted` = '1'",'','') ;
	unless ($db{event_quotes}{1}{next_day_cnt}) {
		$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
		&db_close_conn ;
		exit ;	
	}
	
	$i{date_from} = $sql_next_day ; # hack for calibration_build_table_and_or_excel to simulate search
	$i{date_to}   = $sql_next_day ; # hack for calibration_build_table_and_or_excel to simulate search
	
	&calibration_load_list_vars("('$sql_next_day 23:59:59' >= `date_from` AND '$sql_next_day 00:00:00' <= `date_to`)") ;
	
} #-------------------------------------------------------------------------------

sub create_calibration_excel_file {
	
	# return if $file_exists ; 
	
	our $day_of_week        = Day_of_Week($next_year,$next_month,$next_day) ; 
	our @day_of_week_name   = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") ; 
	$day_of_week            = $day_of_week_name[$day_of_week] ;
	our $date               = &common_write_date_interval("$next_year-$next_month-$next_day","$next_year-$next_month-$next_day") ;
	our $xlsx_title_heading = "Calibration Events $date" ;
	our $xlsxdir            = "next_day_events" ;
	our $xlsxreportname     = "Next_Day_Events_Calibration" ; 
	our $calibration_report = 1 ; #our $calibration_report_cron = 1 ;
	
	&calibration_build_table_and_or_excel(0) ; 
	
} #-------------------------------------------------------------------------------

sub create_fixedsystem_excel_file {
	
	our $xlsx_title_heading = "Fixed Systems Test Events $date" ;
	our $xlsxreportname     = "Next_Day_Events_Fixed_Systems_Test" ;
	
	&db_min_ro('event_systems','*',"`system_type` = 'fixed'",'','') ;
	
	&fixedsystem_build_table_and_or_excel(0) ; 
	
} #-------------------------------------------------------------------------------

sub send_email {
	
	my ($fxc) = @_ ;
	
	&common_debug("send_email START") ;
	
	# my @email_adds = ('cristy@itvafrica.net','marizen@itvafrica.net','dom@itvafrica.net','erin@itvafrica.net','techsupport@itvafrica.net','dan@itvafrica.net','kyle@itvafrica.net','admin@kre8it.co.za') ;
	my @email_adds = ('admin@kre8it.co.za') ;

	if (not -f $xlsxfile) { 
		$attachpath = '' ; $attachfile = '' ; 
	} else {
		$attachpath = $xlsxdir ; $attachfile = $xlsxname ; 
	}
	
	&common_debug("send_email $xlsxpath/$xlsxname") ;
		
	my $date_to_text = Date_to_Text($next_year,$next_month,$next_day) ;
	
	&common_debug("send_email : $date_to_text") ;
	
	$cntsend=0;
	
	foreach my $_eadd (@email_adds) {
		$cntsend++ ;
		$to  = ($debug) ? 'rory@kre8it.co.za' : $_eadd ;
		$cc  = '' ;
		$bcc = ($email{$userid} && $cntsend == 1) ? "$email{$userid}" : '' ; $bcc  = '' if $debug ;
		if ($fxc eq 'calibration') {
			&common_send_smtp_mail('',"$to","$cc","$bcc","Calibration : $date_to_text","Please see attached calibration...",'','html','',$xlsxpath,$xlsxname,'',0,0) ;
		} elsif ($fxc eq 'fixed') {
			&common_send_smtp_mail('',"$to","$cc","$bcc","Fixed System Tests : $date_to_text","Please see attached Fixed System Tests...",'','html','',$xlsxpath,$xlsxname,'',0,0) ;
		}
		last if $debug ;
	}

	# &common_send_smtp_mail('',$to,'',$bcc,$isubject,$message,'',1,'','','','',0,0) ;
	
} #-------------------------------------------------------------------------------

use db ;
use common ;
use today ;
use xlsxcreator ;
use report ; 
use calibration ;
use fixedsystems ;

1;