sub report_create_xls {

	our $xlspath = "$htmlpath/xls/customreport" ;

	&xlsxcreator_delete_previous_xlsx($xlspath) ;

	&report_xls_details_header ;

	foreach my $id (sort {$b <=> $a} keys %xls_hash) { 
		&xlsxcreator_write_xls($ws,$xlsrow,0,$id,$format18);
			
		my $col=1;
		foreach my $field (sort keys %{$xls_hash{$id}}) { 	
			&xlsxcreator_write_xls($ws,$xlsrow,$col,$xls_hash{$id}{$field},$format1);
			$col++ ;
			}
		$xlsrow++ ;
		}
		
	my $col=1;
		
	foreach my $id (sort {$b <=> $a} keys %xls_hash) { 
		foreach my $field (sort keys %{$xls_hash{$id}}) { 	
			$fieldrow = $xlsrow ;
			foreach my $curr (keys %curr_hash) {
				if ($ttl{$field}{$curr}) {
					my $ttl = &common_commify( sprintf("%0.2f",$ttl{$field}{$curr} )) ;
					&xlsxcreator_write_xls($ws,$fieldrow,$col,"$curr $ttl",$format57) ;
					$fieldrow++;
					}
				}
			$col++ ;
			}        
		last;
		}
		
	$worksheet{$ws}->set_column('A:Z',20);
	$worksheet{$ws}->activate();
	$worksheet{$ws}->select();

	&xlsxcreator_close_xls;

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

sub report_xls_details_header {

	our $xlsname = 'CUSTOM-REPORT-' . $now_ccyymmdd . '.xls' ;
	my $xlsfile  = $xlspath . '/' . $xlsname ;

	&xlsxcreator_create_xls($xlsfile);

	&xlsxcreator_xls_format;

	$ws = 'custom_report';

	&xlsxcreator_add_worksheet($ws) ;

	&xlsxcreator_write_xls($ws,0,0,"Film Freight : $success",$format2);

	&xlsxcreator_write_xls($ws,2,0,'ID',$format18);
	# $xlsrow = 2 ;
	my $col=1;
	foreach my $id (sort {$b <=> $a} keys %xls_hash) { 
		foreach my $field (sort keys %{$xls_hash{$id}}) {
			my $uc_field = uc $field ;
			&xlsxcreator_write_xls($ws,2,$col,$uc_field,$format18);
			$col++;
			}        
		last;
		}

	&xlsxcreator_freeze_panes($ws,3,0) ; # Freeze the first 3 rows

	$xlsrow=3;

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

sub report_pdf_start {

	my ($report) = @_ ;

	$layout = 'landscape' ;

	our $pdfpath = "$pdfpath/export" ;

	my $pdfnamestart = uc $report ; $pdfnamestart =~ s/_/-/gi ;

	our $pdfname = $pdfnamestart . '-' . $now_ccyymmdd. $now_hour . $now_min . $now_sec . '.pdf' ;
	my $pdffile  = $pdfpath . '/' . $pdfname ;

	my $pdftitle = $report ; $pdftitle =~ s/_/ /gi ;

	&pdf_delete_previous_pdf($pdfpath) ;
	&pdf_initialise($pdfpath,$pdfname,"Film Freight $pdftitle") ; ;
	&pdf_initialise_page ;

	$up     = 180 ;
	$across = 1 ;
	&pdf_page_header_landscape ;

	&pdf_nl_gap_3 ;
	&pdf_nl_gap_3 ;
	&pdf_nl_gap_3 ;
	&pdf_x_large_bold_text (10, $up, $pdftitle) ;
	&pdf_nl_gap_3 ;

	my $date = pdf_date_to_text($now_ccyy_mm_dd);
	&pdf_medium_text (220, $up, "Date : $date") ;

	&pdf_nl;
	$top=$up;
	&pdf_black_line(10,$up,287,$up) ; # horizontal
	&pdf_nl_s;

	foreach (@pdf_columns) { 
		my $txtstart = length($_) * 2 + 2 ;
		&pdf_small_bold_text ($txtstart,$up,$_) ; 
		}

	&pdf_nl_tt;
    
} #------------------------------------------------------------------------------------------

sub report_pdf_line {

	my ($id) = @_ ;

	&pdf_black_line(10,$up,287,$up) ; # horizontal
	&report_grid_pdf_check_value_of_up(20) ;
	&pdf_nl;

	foreach (@pdf_columns) { 
		my $txtstart = length($_) * 2 + 2 ;
		&pdf_small_bold_text ($txtstart,$up,$db{$table}{$id}{$_}) ;
		} 

	&pdf_nl_tt;
               
} #------------------------------------------------------------------------------------------

sub report_pdf_end {

	&pdf_black_line(10,$up,287,$up) ; # horizontal

	foreach (@pdf_columns) { 
		my $colstart = length($_) * 2 ;
		&pdf_black_line ($colstart,$top,$colstart,$up) ; # vertical 
		}	

	&pdf_footer_landscape ;
	&finish_pdf ;
    
} #------------------------------------------------------------------------------------------

sub report_xlsx_export_header_events {

	my ($report,$xlsxfolder) = @_ ;

	our $xlsxpath = "$htmlpath/xlsx/$xlsxfolder" ;

	my $xlsxnamestart = uc $report ; 
	
	# $xlsxnamestart =~ s/_/-/gi ;

	&xlsxcreator_delete_previous_xlsx($xlsxpath) ;

	our $xlsxname = $xlsxnamestart . '-' . $now_ccyymmdd. $now_hour . $now_min . $now_sec . '.xlsx' ;
	
	my $xlsxfile  = $xlsxpath . '/' . $xlsxname ;
	
	&xlsxcreator_create_xlsx($xlsxfile);

	&xlsxcreator_xlsx_format;

	$ws = $report;

	&xlsxcreator_add_worksheet($ws) ;
	$now_yy += 2000 ; 
	my $xlsxtitle = "Interactive Television Africa Events Division $now_yy" ; 
	
	$xlsxtitle =~ s/_/ /gi ;
	
	my @letters = ("", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R","S", "T", "U", "V", "W", "X", "Y", "Z") ; 
	my $cnt =  0; 
	for (@col_display_excel) {$cnt++;}
	my $range = "A1:$letters[$cnt]" ;
	$range .= "1" ; 
	
	$worksheet{$ws} -> merge_range($range, "$xlsxtitle", $format_event_heading) ; 
	
	my @month_fullname = ("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") ; 
	my $current_month = $month_fullname[int($now_mm)] ; 
	
	$range = "A2:$letters[$cnt]" ;
	$range .= "2" ; 
	
	$worksheet{$ws} -> merge_range($range, "$current_month $now_yy", $format_event_heading_2) ; 
	$range = "A4:$letters[$cnt]" ;
	$range .= "4" ; 
	$worksheet{$ws} -> merge_range($range, "", $format_empty_row) ; 
	
	# &xlsxcreator_write_xlsx($ws,0,0,$xlsxtitle,$format2);

	our $xlsxcol=0;

	foreach (@col_display_excel) { 
		my $coltitle = $_ ; 
		$coltitle =~ s/\_id//g ; 
		$coltitle =~ s/\_/ /g ;
		my $ucfirst = join '', map { ucfirst lc } split /(\s+)/, $coltitle;
		&xlsxcreator_write_xlsx($ws,2,$xlsxcol,$ucfirst,$format_event_heading_2); 
		$xlsxcol++; 
	}

	&xlsxcreator_freeze_panes($ws,3,0) ; # Freeze the first 3 rows

	our $xlsxrow=4;

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

sub report_xls_export_header {

	my ($report,$xlsfolder) = @_ ;

	unless ($xlsfolder) { $xlsfolder = 'export' ; }

	# my $xlsfolder = lc $report ; $xlsfolder =~ s/_//gi ;

	our $xlspath = "$htmlpath/xls/$xlsfolder" ;

	my $xlsnamestart = uc $report ; $xlsnamestart =~ s/_/-/gi ;

	&xlsxcreator_delete_previous_xlsx($xlspath) ;

	our $xlsname = $xlsnamestart . '-' . $now_ccyymmdd. $now_hour . $now_min . $now_sec . '.xlsx' ;
	my $xlsfile  = $xlspath . '/' . $xlsname ;

	&xlsxcreator_create_xlsx($xlsfile);

	&xlsxcreator_xlsx_format;

	$ws = $report;

	&xlsxcreator_add_worksheet($ws) ;

	&report_build_headings($report, $ws) ; 
	
	our $xlscol = 0 ;
	our $xlsrow = 4 ;

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

sub report_xlsx_export_header {

	my ($report,$xlsxfolder,$csv_folder,$title_heading) = @_ ;

	unless ($xlsxfolder) { $xlsxfolder = 'export' ; }

	# my $xlsfolder = lc $report ; $xlsfolder =~ s/_//gi ;

	our $xlsxpath = "$htmlpath/xlsx/$xlsxfolder" ;

	my $xlsxnamestart = uc $report ; $xlsxnamestart =~ s/_/-/gi ;

	&xlsxcreator_delete_previous_xlsx($xlsxpath) ;

	our $xlsxname = $xlsxnamestart . '-' . $now_ccyymmdd. $now_hour . $now_min . $now_sec . '.xlsx' ;
	my $xlsxfile  = $xlsxpath . '/' . $xlsxname ;

	&xlsxcreator_create_xlsx($xlsxfile) ;

	&xlsxcreator_xlsx_format;

	$ws = $report ;

	&xlsxcreator_add_worksheet($ws) ;

	&report_build_headings($report,$ws,$title_heading) ; 
	
	our $xlsxcol = 0 ;
	our $xlsxrow = 4 ;

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

sub report_build_headings {
	
	my ($report,$worksheet,$title_heading,$xlsx_start_row,$xlsx_start_col) = @_ ; 
	my $xlsxtitle = ($xlsxheading) ? $xlsxheading : $report ; 
	$xlsxtitle = $title_heading if $title_heading ; 
	$xlsxtitle =~ s/_/ /gi ;
	&xlsxcreator_write_xlsx($worksheet,0,0,$xlsxtitle,$format_management_report_title) if $xlsxtitle ;
	my $col = ($xlsx_start_col) ? $xlsx_start_col : 0 ;
	
	return unless scalar @sql_col_display ;
	
	if ($calibration_report_cron) {
		foreach (@sql_col_display) { 
			 &xlsxcreator_write_xlsx($ws,1,$col,"$day_of_week",$format93) unless $col ;	
			 &xlsxcreator_write_xlsx($ws,1,$col,"",$format93) if $col ;	
			 $col++ ;
		}
		$col = 0;
	}
	
	my $titles_row = ($xlsxtitle) ? 2 : ($xlsx_start_row) ? $xlsx_start_row : 0 ;
	
	foreach (@sql_col_display) { 
		next if not $_ ;
		my $coltitle = $_ ; 
		$coltitle =~ s/\_id//g ; 
		$coltitle =~ s/\_/ /g ;
		my $ucfirst = join '', map { ucfirst lc } split /(\s+)/, $coltitle;
		&xlsxcreator_write_xlsx($worksheet,$titles_row,$col,$ucfirst,$format_management_report_headings) unless $calibration_report;  
		&xlsxcreator_write_xlsx($worksheet,$titles_row,$col,$ucfirst,$format93) if $calibration_report; 
		$col++; 
	}
	
	$titles_row++ ;
	&xlsxcreator_freeze_panes($worksheet,$titles_row,0) ; # Freeze the first 3 rows
	
}

sub report_xlsx_export_process_cell {

	my ($changed,$money,$val) = @_ ;

	if ($changed) {
		&report_xlsx_export_output_cell($format23,$val) ;			
		}
	elsif ($money) {
		&report_xlsx_export_output_cell($format3,$val) ;					
		}
	else
		{
		&report_xlsx_export_output_cell($format1,$val) ;					
		}

} #------------------------------------------------------------------------------------------\


sub report_write_events {

	my ($val, $last) = @_ ;
	
	&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val,$format_event_data) if $last == 0;
	&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val,$format_event_data_2) if $last == 1;

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

sub report_xlsx_export_output_cell {

	my ($format,$val) = @_ ;

	&xlsxcreator_write_xlsx($ws,$xlsxrow,$xlsxcol,$val,$format);

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

sub report_xls_export_process_cell {

my ($changed,$money,$val) = @_ ;

if ($changed) {
	&report_xls_export_output_cell($format23,$val) ;			
	}
elsif ($money) {
	&report_xls_export_output_cell($format3,$val) ;					
	}
else
	{
	&report_xls_export_output_cell($format1,$val) ;					
	}

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

sub report_xls_export_output_cell {

	my ($format,$val) = @_ ;

	&xlsxcreator_write_xlsx($ws,$xlsrow,$xlscol,$val,$format);

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

sub report_xlsx_export_footer {

	my ($resize_cell_to,$col_width,$report) = @_ ;

	my $col = 0; 
	my $output_row = $xlsxrow+1 ;

	foreach (@sql_col_display) {	
		next if not $_ ;
		if ($money_format{$_}) {
			my $cellfrom = &xl_rowcol_to_cell(4,$col) ;
			my $cellto   = &xl_rowcol_to_cell($xlsxrow,$col) ;
			&xlsxcreator_write_formula($ws,$output_row,$col,"=SUM($cellfrom:$cellto)",$format67); 
			}		
		$col++ ;
	}

	# $worksheet{$ws}->set_column("A:$resize_cell_to",$col_width) ;
	$worksheet{$ws}->activate() ;
	$worksheet{$ws}->select() ;

	&xlsxcreator_close_xlsx ;

	if (-f "$xlsxpath/$xlsxname") { 
		$box_icon .= qq(<div class="box-icon" title='Export to Excel' data-toggle="tooltip" data-placement="left"><a class="btn btn-default btn-round" href="/xlsx/$report/$xlsxname" style="padding-top:1px;"><i class="glyphicon glyphicon-download-alt icon-white"></i></a></div>) ;
	}
	
} #------------------------------------------------------------------------------------------

sub report_xls_export_footer {

	my ($resize_cell_to,$col_width,$report) = @_ ;

	unless ($report) { $report = 'export' ; }

	my $col = 0; my $output_row = $xlsrow+1 ;

	foreach (@sql_col_display) {	
		next if not $_ ;
		if ($money_format{$_}) {
			my $cellfrom = &xl_rowcol_to_cell(4,$col) ;
			my $cellto   = &xl_rowcol_to_cell($xlsrow,$col) ;
			&xlsxcreator_write_formula($ws,$output_row,$col,"=SUM($cellfrom:$cellto)",$format67); 
			}		
		$col++ ;
		}

	$worksheet{$ws}->set_column("A:$resize_cell_to",$col_width);
	$worksheet{$ws}->activate();
	$worksheet{$ws}->select();

	&xlsxcreator_close_xlsx;

	if (-f "$xlspath/$xlsname") { 
		$box_icon .= qq(<div class="box-icon" title='Export to Excel' data-toggle="tooltip" data-placement="left"><a class="btn btn-default btn-round" href="/xls/$report/$xlsname" style="padding-top:1px;"><i class="glyphicon glyphicon-download-alt icon-white"></i></a></div>) ;
		}

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

sub report_xls_add_chart {

	&xlsxcreator_add_pie_chart($ws);

	# Configure the series.
	$chart{$ws}->add_series(
		name       => 'Pie Chart',
		# categories => '=risk_report!$A$4:$A$18',
		# values     => '=risk_report!$B$4:$B$18',
		categories => "=$ws!A4:A$catspan",
		values     => "=$ws!B4:B$catspan",
	);

	# Add a title.
	$chart{$ws}->set_title( name => 'Report Pie Chart' );

	$xlsrow+=5 ;

	# Insert the chart into the worksheet (with an offset).
	# $worksheet{$ws}->insert_chart('B23',$chart{$ws},25,10);
	$worksheet{$ws}->insert_chart("B$xlsrow",$chart{$ws},40,16);
		
} #------------------------------------------------------------------------------------------

sub report_grid_pdf_check_value_of_up {

	my ($value) = @_ ;

	if ($up < $value) {
		&report_grid_pdf_print_vertical_lines_table ;
		&pdf_initialise_page ;
		$up     = 195 ;
		$across = 1 ;
		$top    = $up;
		&pdf_black_line(10,$up,287,$up) ; # horizontal
		}

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

sub report_grid_pdf_print_vertical_lines_table {

	# &pdf_black_line (10,$top,10,$up) ; # vertical

	&pdf_black_line ($colstart[0],$top,$colstart[0],$up) ; # vertical
	&pdf_black_line ($colstart[1],$top,$colstart[1],$up) ; # vertical
	&pdf_black_line ($colstart[2],$top,$colstart[2],$up) ; # vertical
	&pdf_black_line ($colstart[3],$top,$colstart[3],$up) ; # vertical
	&pdf_black_line ($colstart[4],$top,$colstart[4],$up) ; # vertical
	&pdf_black_line ($colstart[5],$top,$colstart[5],$up) ; # vertical
	&pdf_black_line ($colstart[6],$top,$colstart[6],$up) ; # vertical
	&pdf_black_line ($colstart[7],$top,$colstart[7],$up) ; # vertical
	&pdf_black_line ($colstart[8],$top,$colstart[8],$up) ; # vertical
	&pdf_black_line ($colstart[9],$top,$colstart[9],$up) ; # vertical
	&pdf_black_line ($colstart[10],$top,$colstart[10],$up) ; # vertical

	# &pdf_black_line(287,$top,287,$up) ; # vertical
	
} #------------------------------------------------------------------------------------------

1;
