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

# perl /usr/lib/cgi-bin/aisa/scripts/cron/auto_get_next_day_events.pl 1

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

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_vars ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;
&create_excel_file ;
&send_email ;

exit;

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

sub load_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_excel_file {
	
	# return if $file_exists ; 
	
	our $day_of_week = Day_of_Week($next_year,$next_month,$next_day) ; 

	my @day_of_week_name = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") ; 
	
	$day_of_week = $day_of_week_name[$day_of_week] ;
	
	my $date = &common_write_date_interval("$next_year-$next_month-$next_day","$next_year-$next_month-$next_day") ;
	
	our $xlsx_title_heading = "Events $date" ;
	
	our $xlsxdir = "next_day_events" ;
	
	our $xlsxreportname = "Next_Day_Events" ; 
	
	our $calibration_report = 1 ; #our $calibration_report_cron = 1 ;
	
	&calibration_build_table_and_or_excel(0) ; 
	
} #-------------------------------------------------------------------------------

sub send_email {
	
	&common_debug("send_email START") ;
	
	# my @email_adds = ('cristy@aisport.africa','marizen@aisport.africa','dom@aisport.africa','erin@aisport.africa','techsupport@aisport.africa','dan@aisport.africa','kyle@aisport.africa','admin@kre8it.co.za') ;
	
	# # my $to  = 'rory@kre8it.co.za' ; # only 1 email address in $to
	# my $to  = 'cristy@aisport.africa' ; # only 1 email address in $to
	# # my $cc  = 'marizen@aisport.africa;dom@aisport.africa;erin@aisport.africa;techsupport@aisport.africa;dan@aisport.africa;kyle@aisport.africa;admin@kre8it.co.za' ;
	# my $cc  = 'marizen@aisport.africa' ;
	# # my $bcc = 'viv@aisport.africa' ;
	# # my $bcc = 'rory@kre8it.co.za' ;
	# # my $bcc = 'k@re8it.com' ;
	# my $bcc = 'dom@aisport.africa' ;
	
	# &common_debug("send_email $to") ;
	
	# if ($email{$userid}) { $bcc .= ';' . $email{$userid} ; }

	# if ($username eq 'rory')   { $to = 'rory@kre8it.co.za' ; $cc = 'k@re8it.com;rory@mathew.za.net' ; $bcc = 'admin@kre8it.co.za' ; }
	# # if ($debug)   { $to = 'rory@kre8it.co.za' ; $cc = 'k@re8it.com;rory@mathew.za.net' ; $bcc = 'admin@kre8it.co.za' ; }
	# # if ($username eq 'handre') { $to = 'handre@kre8it.co.za' ; }

	if (not -f $xlsxfile) { 
		$attachpath = '' ; $attachfile = '' ; 
	} else {
		$attachpath = $xlsxdir ; $attachfile = $xlsxname ; 
	}
	
	&common_debug("send_email $xlsxpath/$xlsxname") ;

	# my $attach_flyer = 0 ; my $message = qq~Hello World~ ; my $isubject = qq~Hello World~ ;
		
	my $date_to_text = Date_to_Text($next_year,$next_month,$next_day) ;
	
	&common_debug("send_email : $date_to_text") ;
	
	$cntsend=0;
	
	my $to  = ($debug) ? $email_it_1 : $email_events{1} ;
	
	delete $email_events{1}; # remove as to not Cc as well as To
	
	my @keys     = sort {$a <=> $b} keys %email_events;
	my $last_key = $keys[-1];
	
	my $bcc = ($debug) ? '' : $email_events{$last_key} ;
	
	delete $email_events{$last_key};

	my $cc  = ($debug) ? '' : join(", ", values %email_events);
	
	# print "'','$to','$cc','$bcc','Calibration : $date_to_text','Please see attached calibration...','','html','',$xlsxpath,$xlsxname,'vnd.openxmlformats-officedocument.spreadsheetml.sheet',0,0"
	
	&common_send_smtp_mail('',"$to","$cc","$bcc","Calibration : $date_to_text","Please see attached calibration...",'','html','',$xlsxpath,$xlsxname,'vnd.openxmlformats-officedocument.spreadsheetml.sheet',0,0) ;
	
	# # foreach my $_eadd (@email_adds) {
	# foreach my $_eaddcnt (keys %email_events) {
		# my $_eadd = $email_events{$_eaddcnt} ;
		# $cntsend++ ;
		# $to  = ($debug) ? $email_it_1 : $_eadd ;
		# $cc  = '' ;
		# $bcc = ($email{$userid} && $cntsend == 1) ? "$email{$userid}" : '' ; $bcc  = 'rory@mathew.za.net' if $debug ;
		# &common_send_smtp_mail('',"$to","$cc","$bcc","Calibration : $date_to_text","Please see attached calibration...",'','html','',$xlsxpath,$xlsxname,'vnd.openxmlformats-officedocument.spreadsheetml.sheet',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 ;

1;