#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

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

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use Mail::Sendmail;
use Fcntl qw(:flock);
use File::Copy;
use File::Basename;
use Date::Calc qw(:all);
use Excel::Writer::XLSX;

&today;

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

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

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

our $debug = 1 ;

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

&page_opts ;

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

print "Content-type: text/html\n\n";
	
our $srchscr   = 1 ;

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	&report_screen if $iaction eq '' ; 
	&common_min_action;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

exit;

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

sub report_ifields {
	
	&load_search_vars ;
		
	if ($i{camera_system_id} and $i{camera_system_id} ne 'main' and $i{camera_system_id} ne 'other' and $i{camera_system_id} ne 'all') {
		push @report_sql, "(`camera_system_id`='$i{camera_system_id}')" ;
		push @report_results, "camera_system = $camera_system{$i{camera_system_id}}" ;
		$xlsreportname .= '-' . $i{camera_id} ;
		# our $acc_id_sql_where = "`camera_id`='$i{camera_id}'"
	} elsif ($i{camera_system_id} eq 'main' or $i{camera_system_id} eq 'other' or $i{camera_system_id} eq 'all') {
		push @report_results, "camera_system = $i{camera_system_id}" ;
	}
		
	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(CAMERAS FROM ($i{date_from}) > CAMERAS TO ($i{date_to})); &report_screen; 
		}
	}
		
	if ($i{date_from}) {
		# push @report_sql, "(`date_active` >= '$i{date_from}')" ;
		# push @report_results, "date_active >= $i{date_from}" ;
		push @report_sql, "(`date_time` >= '$i{date_from}')" ;
		push @report_results, "date_time >= $i{date_from}" ;
	}

	if ($i{date_to}) {
		# push @report_sql, "(`date_active` <= '$i{date_to}')" ;
		# push @report_results, "date_active <= $i{date_to}" ;
		push @report_sql, "(`date_time` <= '$i{date_to}')" ;
		push @report_results, "date_time <= $i{date_to}" ;
	}
		
	if ($i{client_id}) {    
		# push @report_sql, "(`client_id` = '$i{client_id}')" ;
		push @report_results, "Client = $client{$i{client_id}}" ;
	}
		
	if ($i{stock}) {    
		push @report_results, "Stock = $i{stock}" ;
	}
		
	$report_results_msg = uc join(', ', @report_results) ;

	if (not $report_results_msg) { 
		$error = qq(ENTER AT LEAST ONE SEARCH PARAMETER); 
		&report_screen; 
	} else { 
		$isaved = qq(SELECT WHERE $report_results_msg) ; 
	}
 
} #------------------------------------------------------------------------------------------

sub load_search_vars {

	# my $cameras_sql_where = "`id`='$i{camera_id}'" ;
	# &db_min_ro('cameras','id,camera_nr',$cameras_sql_where,'','') ;
	# foreach my $id (keys %{$db{cameras}}) { $camera_name{$id} = $db{cameras}{$id}{camera_nr} ; }

	my $camera_systems_sql_where = "`id`='$i{camera_system_id}'" if $i{camera_system_id} ;
	&db_min_ro('camera_systems','id,name',$camera_systems_sql_where,'','') ;
	foreach my $id (keys %{$db{camera_systems}}) { 
		$camera_system{$id} = $db{camera_systems}{$id}{name} ; 
	} 

	# my $price_list_sql_where = " AND `id`='$i{camera_system_id}'" if $i{camera_system_id} ;
	# &db_min_ro('price_list','id,description',"`excl`<>'1' AND `type`='systems'$price_list_sql_where",'','') ;
	# foreach my $id (keys %{$db{price_list}}) { $camera_system{$id} = $db{price_list}{$id}{name} ; } 
	# my $customers_sql_where = "`id`='$i{client_id}'" if $i{client_id} ;
	# &db_min_ro('customers','*',$customers_sql_where,'','') ;
	# foreach my $id (keys %{$db{customers}}) { $client{$id} = $db{customers}{$id}{name} ; }

	if ($i{client_id}) {
		my $customers_sql_where = "`id`='$i{client_id}'" ;
		&db_min_ro('customers','*',$customers_sql_where,'','') ;
		foreach my $id (keys %{$db{customers}}) { 
			$client{$id} = $db{customers}{$id}{name} ; 
		}
	}

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

sub list_screen {

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

	my $add_sql_where = '' ;   
		
	# our @sql_col_display = ("id","camera_nr","camera_serial_nr","date_received","amount_usd","amount_zar","in_stock") ;
	our @sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","vpu_serial_nr","date_received","amount_usd","amount_zar","type","in_stock","date_delivered","client","ref_nr","in_stock", "events", "demo", "purchase", "rental") ;

	&report_xls_export_header("Management_Report$xlsreportname",'stockreport') ;

	&load_list_vars("$srch_where_sql$add_sql_where") ;

	foreach my $id (sort {$b <=> $a} keys %{$db{$table}}) {
		if ($linked_sn{$db{$table}{$id}{serial_nr}}) { 
			$vpu_is_linked{$linked_sn{$db{$table}{$id}{serial_nr}}} = 1 ; 
		}
	}
	my $cnt_in_stock = 0 ; 
	my %totals = () ; 
	my $display = 1 ; 
		
	my $ws2 = 'Rental' ; 
	&xlsxcreator_add_worksheet($ws2) ;

	my $ws3 = 'Purchase' ; 
	&xlsxcreator_add_worksheet($ws3) ;
	
	@sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","date_received","amount_zar","type","date_delivered","client","ref_nr","rental") ;
	
	&report_build_headings("Management_Report$xlsreportname", $ws2) ;
	
	@sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","date_received","amount_zar","type","date_delivered","client","ref_nr","purchase") ;
	
	&report_build_headings("Management_Report$xlsreportname", $ws3) ;
	
	my $xlsxcol2 = 0 ; 
	my $xlsxrow2 = 4 ; 
	my $xlsxcol3 = 0 ; 
	my $xlsxrow3 = 4 ; 
	
	my @month_array = ("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ; 
	my $year = 21 ; 
	my $month_start = 3 ;
	my $col_count = 12 ; 
	
	&xlsxcreator_write_xlsx($ws2, 1, 12, "36", $format_management_report_months ) ; 
	&xlsxcreator_write_xlsx($ws2, 1, 13, "Months Rentals", $format_management_report_months_2 ) ; 
	&xlsxcreator_write_xlsx($ws3, 1, 12, "36", $format_management_report_months ) ; 
	&xlsxcreator_write_xlsx($ws3, 1, 13, "Months Rentals", $format_management_report_months_2 ) ; 
	
	for my $year_count ($year .. 24){
		for ($month_start .. 12){
			&xlsxcreator_write_xlsx($ws2, 2, $col_count, "$month_array[$_]-$year_count", $format_management_report_data ) ; 
			&xlsxcreator_write_xlsx($ws3, 2, $col_count, "$month_array[$_]-$year_count", $format_management_report_data ) ; 
			$col_count++;
		}
		$month_start = 1 ; 
	}
	
	@sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","vpu_serial_nr","date_received","amount_usd","amount_zar","type","in_stock","date_delivered","client","ref_nr","in_stock", "events", "demo", "purchase", "rental") ;
	
	my $month_at_start = 0 ; 
	my $year_at_start = 0 ; 
	
	my $month_at_start_counter = 0 ; 
	my $year_at_start_counter = 0 ; 
	
	my $month_counter = 0 ; 
	my $year_counter = 0 ; 
	
	my $month_start_row = 3 ; 
	my $year_start_row = 21 ; 
	
	my $months_behind_counter = 0 ; 
	
	my $year_to_months = 0 ; 
	my $months_behind = 0 ; 
	my %totals_rentals = () ; 
	my %totals_purchases = () ; 
	
	foreach my $id (sort { $a <=> $b } keys %{$db{$table}}) {
		
		# if ($i{stock} eq 'stock')             { if     ($db{$table}{$id}{quote_nr} or $db{$table}{$id}{demo_recipient})            { next ; } }
		# if ($i{stock} eq 'purchased')         { unless ($db{$table}{$id}{quote_nr})                                                { next ; } }
		# if ($i{stock} eq 'demo')              { unless ($db{$table}{$id}{demo_recipient} )                                         { next ; } }
		# if ($i{stock} eq 'rental')            { unless ($db{$table}{$id}{camera_system_id}<7 and lc $quote_type{$id} eq 'rental')  { next ; } } 
		# if ($i{camera_system_id} eq 'main')   { unless ($db{$table}{$id}{camera_system_id}<7)                                      { next ; } } 
		# if ($i{camera_system_id} eq 'other')  { unless ($db{$table}{$id}{camera_system_id}>7)                                      { next ; } } 
		
		next if &common_camera_system_filter($table,$id) ;
		if ($i{client_id}) { next unless $client{$i{client_id}} eq $quote_customer{$id} ; }
		# if ($i{stock})     { next unless $i{stock} eq lc $quote_type{$id} ; }
		next if $vpu_is_linked{$db{$table}{$id}{serial_nr}} ;
		
		$print_tbody .= qq~<tr>~ ;
		
		$xlscol = 0 ;
		$xlsxcol2 = 0 ;
		$xlsxcol3 = 0 ;
		$cnt_in_stock = -1 ; 
		my $in_stock = "" ; 
		foreach (@sql_col_display) { 
			
			next unless $_ ; # blank for the buttons column
			$display = 1 ; 
			my $val = $db{$table}{$id}{$_} ;
			my $align = '' ;
			my $center = qq~ class="dt-center"~ ;
			
				 if ($_ eq 'date_delivered') 		{ $val = $db{$table}{$id}{quote_delivery_date} ; 					$align = $center ; 
			} elsif ($_ eq 'date_received') 		{ 																	$align = $center ; 
			} elsif ($_ eq 'amount_zar') 			{ $val = $db{$table}{$id}{cur_amount} ; 
			} elsif ($_ eq 'amount_usd') 			{ $val = $db{$table}{$id}{usd_amount} ; 
			} elsif ($_ eq 'camera_serial_nr') 		{ $val = $db{$table}{$id}{serial_nr}  ; 
			} elsif ($_ eq 'camera_system_id') 		{ $val = $camera_system{$val} ; 
			} elsif ($_ eq 'type') 					{ $val = $quote_type{$id} ; 
			} elsif ($_ eq 'client') 				{ $val = $quote_customer{$id} ; 									$align = $center ; 
			} elsif ($_ eq 'in_stock' ) { 
					 $cnt_in_stock++ ; 
					if ($db{$table}{$id}{quote_nr} or $db{$table}{$id}{demo_recipient} or $db{$table}{$id}{replacement_date} or $db{$table}{$id}{event_system_id}) { 
												      $val = 0 ; 
					} else { 					      $val = 1 ; 
					} 
			} elsif ($_ eq 'vpu_serial_nr') 		{ $val = $linked_sn{$db{$table}{$id}{serial_nr}} ; 
			} elsif ($_ eq 'purchase') {
				 if ($quote_type{$id} eq 'Purchase'){ $val = $db{$table}{$id}{cur_amount} ; $totals{purchase} += $val;	$align = $center ; }
			} elsif ($_ eq 'rental') {
				 if ($quote_type{$id} eq 'Rental') 	{ $val = $db{$table}{$id}{cur_amount} ; $totals{rental} += $val; 	$align = $center ; }
			} elsif ($_ eq 'events') {
				if ($db{$table}{$id}{event_system_id} > 0){ 
													  $val = $db{$table}{$id}{cur_amount} ;  $totals{events} += $val; 	$align = $center ; }
			} elsif ($_ eq 'demo') {
				if ($db{$table}{$id}{demo_recipient}){$val  = $db{$table}{$id}{cur_amount} ; $totals{demo} += $val; 	$align = $center ; }	
			# } elsif ($_ eq 'ref_nr') 			 	{ $val = $ref_nr{$quote_camera_system_id{$id}} if $ref_nr{$quote_camera_system_id{$id}} ; }
			} elsif ($_ eq 'ref_nr') 			 	{ $val = $db{$table}{$id}{quote_nr} ; } 
			# $val .= ' [' . $linked_cn{$db{$table}{$id}{serial_nr}} . ']' if $linked_cn{$db{$table}{$id}{serial_nr}} ;
			if ($_ eq 'in_stock') { 
					 if ($val==1 and $cnt_in_stock == 0) { $val = &set_tick('green', '') ; $display = 0 ; $in_stock = "Yes" ; 
				} elsif ($val==1 and $cnt_in_stock == 1) { $val = $db{$table}{$id}{cur_amount}; $totals{in_stock} += $val ; 
				} elsif ($val==0 and $cnt_in_stock == 0) { $val = &set_cross('red', '')  ; $display = 0 ; $in_stock = "No" ; 
				} elsif ($val==0 and $cnt_in_stock == 1) { $val = "" ; 	
				}																										$align = $center ; 
			}
			# &report_xls_export_process_cell('','',$val) if  $cnt_in_stock != 0 ; # ------------------------------------------------------------------------- HTML AFTER
			if ($display == 1 and $_ ne 'ref_nr' ) { 
				&xlsxcreator_write_xlsx($ws, $xlsrow, $xlscol, $val, $format_management_report_data) ; 	
			} elsif ($display == 1 and $_ eq 'ref_nr' ) { 	 
			 	if ($val){ &xlsxcreator_write_xlsx($ws, $xlsrow, $xlscol, $val, $format_management_report_data_ref_nr) ; } 
				else { &xlsxcreator_write_xlsx($ws, $xlsrow, $xlscol, "", $format_management_report_data_ref_nr) ; }	
			} elsif ($display == 0) { 
				&xlsxcreator_write_xlsx($ws, $xlsrow, $xlscol, $in_stock, $format_management_report_data) ; 
			} 
			$xlscol++; 
			
			if ($_ eq 'camera_nr' or $_ eq 'camera_system_id' or $_ eq 'camera_serial_nr' or $_ eq 'date_received' or $_ eq 'amount_zar' or $_ eq 'type' or $_ eq 'date_delivered' or $_ eq 'client' or $_ eq 'ref_nr' or $_ eq 'rental' or $_ eq 'purchase') {
				if ($quote_type{$id} eq 'Rental' and $_ ne 'purchase'){
					&xlsxcreator_write_xlsx($ws2, $xlsxrow2, $xlsxcol2, $val, $format_management_report_data) ;
					$xlsxcol2++ ;
				} elsif ($quote_type{$id} eq 'Purchase' and $_ ne 'rental') {
					&xlsxcreator_write_xlsx($ws3, $xlsxrow3, $xlsxcol3, $val, $format_management_report_data) ;
					$xlsxcol3++ ;
				}
			}
		
			$month_at_start = int(substr($db{$table}{$id}{quote_delivery_date}, 5, 2));
			$year_at_start = int(substr($db{$table}{$id}{quote_delivery_date}, 2, 2)) ;
			
			$month_at_start_counter = $month_at_start ;
			$year_at_start_counter  = $year_at_start ;
			if ($_ eq 'ref_nr' or $_ eq 'camera_nr')	{ $val = &common_camera_links($table,$id,$val)  ;} 
			$print_tbody .= qq~<td $align>$val</td>~ ;  
		}
	
		$month_counter = 3 ; 
		$year_counter = 21 ;
	
		if ($quote_type{$id} eq 'Rental' or $quote_type{$id} eq 'Purchase'){
			for my $col_counter (1 .. 36) {	
				last if $month_at_start == 0 or $year_at_start == 0 ; 	
				if ($year_at_start > 21 or ($year_at_start == 21 and $month_at_start >= 3)){  	
					if ($quote_type{$id} eq 'Rental'){
						&xlsxcreator_write_xlsx($ws2, $xlsxrow2, 11 + $col_counter + $month_at_start - $month_start_row + 12 * ($year_at_start - $year_start_row) , $db{$table}{$id}{cur_amount}/36, $format_management_report_data)  ;
						$totals_rentals{11 + $col_counter + $month_at_start - $month_start_row + 12 * ($year_at_start - $year_start_row)} += $db{$table}{$id}{cur_amount}/36 ; 
					} elsif ($quote_type{$id} eq 'Purchase'){
						&xlsxcreator_write_xlsx($ws3, $xlsxrow3, 11 + $col_counter + $month_at_start - 3 + 12 * ($year_at_start - 21) , $db{$table}{$id}{cur_amount}, $format_management_report_data) ;
						$totals_purchases{11 + $col_counter + $month_at_start - 3 + 12 * ($year_at_start - 21)} += $db{$table}{$id}{cur_amount} ; 
						last ; 
					}		
				} elsif (($month_at_start_counter >= 3 and $year_at_start_counter >= 21 and (($year_at_start < 21) or ($year_at_start == 21 and $month_at_start < 3 ))) or ($year_at_start_counter > 21 and (($year_at_start < 21) or ($year_at_start == 21 and $month_at_start < 3 )))) {
					$months_behind = 3 - $month_at_start if $year_at_start == 21 ; 
					$months_behind = 3 + 12 - $month_at_start + 12 * (21 - $year_at_start - 1) if  $year_at_start < 21 ;
					if ($quote_type{$id} eq 'Rental'){
						&xlsxcreator_write_xlsx($ws2, $xlsxrow2, 11 + $col_counter - $months_behind , $db{$table}{$id}{cur_amount}/36, $format_management_report_data) ;
						$totals_rentals{11 + $col_counter - $months_behind} += $db{$table}{$id}{cur_amount}/36 ; 
					} elsif ($quote_type{$id} eq 'Purchase') {
						if (($year_at_start < 21) or ($year_at_start == 21 and $month_at_start < 3 )){last ;}
						&xlsxcreator_write_xlsx($ws3, $xlsxrow3, 12 + $month_at_start - 3 + 12 * ($year_at_start - 21) , $db{$table}{$id}{cur_amount}, $format_management_report_data) ;	
						$totals_purchases{12 + $month_at_start - 3 + 12 * ($year_at_start - 21)} += $db{$table}{$id}{cur_amount} ;
						last ; 
					}					
				}
				$month_counter++ ;
				if ($month_counter == 13) {
					$year_counter++ ;
					$month_counter = 1 ;
				} 
				$month_at_start_counter++ ;
				if ($month_at_start_counter == 13) {
					$year_at_start_counter++ ;
					$month_at_start_counter = 1 ;
				} 
				last if $year_at_start_counter == 25 ; 
				# last if $year_counter == $now_year and $month_counter == $now_mm ; 				
			}
		}
		
		$print_tbody .= qq~</tr>~ ;
		$xlsrow++ ;
		$xlsxrow2++ if $quote_type{$id} eq 'Rental';
		$xlsxrow3++ if $quote_type{$id} eq 'Purchase';
	}
	$print_tbody .= qq~<tr>~ ;
	$print_tbody .= qq~<th style = "display:none;">ZZZZZZZZZZ</th>~ ;
	for (1 .. 11){$print_tbody .= qq~<th></th>~ ;}
	$print_tbody .= qq~<th class="dt-right">Total</th>~ ;
	&xlsxcreator_write_xlsx($ws, $xlsrow, 11, "Total ", $format_management_report_total);
	# my @totals_row = ('in_stock','events','demo','purchase','rental') ; 
	
	my %totals_row = (
		12 => 'in_stock', 
		13 => 'events' , 
		14 => 'demo', 
		15 => 'purchase', 
		16 => 'rental',
	) ; 
	
	foreach (keys %totals_row){
		$totals{$totals_row{$_}} = sprintf("%.2f", $totals{$totals_row{$_}}) ; 
		&xlsxcreator_write_xlsx($ws, $xlsrow, $_, $totals{$totals_row{$_}}, $format_management_report_totals) ; 
		$val = $totals{$totals_row{$_}} ;
		$print_tbody .= qq~<th class="dt-center">$val</th>~ ;
	}
	
	&xlsxcreator_write_xlsx($ws2, $xlsxrow2, 8, "Total ", $format_management_report_total);
	&xlsxcreator_write_xlsx($ws3, $xlsxrow3, 8, "Total ", $format_management_report_total);
	&xlsxcreator_write_xlsx($ws2, $xlsxrow2, 9, $totals{'rental'}, $format_management_report_totals);
	&xlsxcreator_write_xlsx($ws3, $xlsxrow3, 9, $totals{'purchase'}, $format_management_report_totals);
	
	for (12 .. 57){
		&xlsxcreator_write_xlsx($ws2, $xlsxrow2, $_, $totals_rentals{$_}, $format_management_report_data) ;
		&xlsxcreator_write_xlsx($ws2, $xlsxrow2, $_, "-", $format_management_report_data) if not $totals_rentals{$_};
		&xlsxcreator_write_xlsx($ws3, $xlsxrow3, $_, $totals_purchases{$_}, $format_management_report_data) ;
		&xlsxcreator_write_xlsx($ws3, $xlsxrow3, $_, "-", $format_management_report_data) if not $totals_purchases{$_};		
	}
	
		# elsif ($_ eq 'events') 		{ &xlsxcreator_write_xlsx($ws, $xlsrow, 13, $totals{$_}, $format_management_report_totals); }
		# elsif ($_ eq 'demo') 		{ &xlsxcreator_write_xlsx($ws, $xlsrow, 14, $totals{$_}, $format_management_report_totals); }
		# elsif ($_ eq 'purchase') 	{ &xlsxcreator_write_xlsx($ws, $xlsrow, 15, $totals{$_}, $format_management_report_totals); }
		# elsif ($_ eq 'rental') 		{ &xlsxcreator_write_xlsx($ws, $xlsrow, 16, $totals{$_}, $format_management_report_totals); }
	$worksheet{$ws} -> set_column(12, 16, 15) ; $worksheet{$ws} -> set_row($xlsrow, 25) ;
	$print_tbody .= qq~</tr>~ ;
	$worksheet{$ws} -> set_column(1, 3, 20) ;
	$worksheet{$ws} -> set_column(10, 10, 25) ;
	$worksheet{$ws} -> set_row(0, 30) ;
	$worksheet{$ws} -> set_row(2, 20) ;

	$worksheet{$ws2} -> set_row(0, 30) ;
	$worksheet{$ws2} -> set_row(2, 20) ;
	$worksheet{$ws2} -> set_row($xlsxrow2, 30) ;
	$worksheet{$ws3} -> set_row($xlsxrow3, 30) ;
	
	$worksheet{$ws2} -> set_column(0, 0, 15) ;
	$worksheet{$ws2} -> set_column(1, 2, 20) ;
	$worksheet{$ws2} -> set_column(3, 6, 15) ;
	$worksheet{$ws2} -> set_column(7, 7, 25) ;
	$worksheet{$ws2} -> set_column(8, 9, 15) ;
	$worksheet{$ws2} -> set_column(12, 57, 10) ;
	
	$worksheet{$ws3} -> set_row(0, 30) ;
	$worksheet{$ws3} -> set_row(2, 20) ;
	
	$worksheet{$ws3} -> set_column(0, 0, 15) ;
	$worksheet{$ws3} -> set_column(1, 2, 20) ;
	$worksheet{$ws3} -> set_column(3, 6, 15) ;
	$worksheet{$ws3} -> set_column(7, 7, 25) ;
	$worksheet{$ws3} -> set_column(8, 9, 15) ;
	$worksheet{$ws3} -> set_column(12, 57, 10) ;
	
	# foreach my $id (sort { $a <=> $b } keys %{$db{$table}}) {
		
		# next unless $quote_type{$id} eq 'Rental' or  $quote_type{$id} eq 'Purchase';
		# next if &common_camera_system_filter($table,$id) ;
		# if ($i{client_id}) { next unless $client{$i{client_id}} eq $quote_customer{$id} ; }
		# next if $vpu_is_linked{$db{$table}{$id}{serial_nr}} ;
	
		# for (@sql_col_display) {
			# $val = $db{$table}{$id}{$_} ;
			# if ($_ eq 'camera_system_id') { 
				# $val = $camera_system{$val} ; 
			# } elsif ($_ eq 'camera_serial_nr') {
				# $val = $db{$table}{$id}{serial_nr}  ; 
			# } elsif ($_ eq 'amount_zar') {
				 # $val = $db{$table}{$id}{cur_amount} ; 	
			# } elsif ($_ eq 'type') {
				# $val = $quote_type{$id} ; 
			# } elsif ($_ eq 'date_delivered') {
				# $val = $db{$table}{$id}{quote_delivery_date} ; 	
			# } elsif ($_ eq 'client') {
				 # $val = $quote_customer{$id} ; 
			# } elsif ($_ eq 'ref_nr') {
				 # $val = $db{$table}{$id}{quote_nr} ;
			# } elsif ($_ eq 'rental' or $_ eq 'purchase') {
			 # $val = $db{$table}{$id}{cur_amount} ;
			 # # if $quote_type{$id} eq 'Rental';
			# }	
			# if (($_ eq 'camera_system_id' or $_ eq 'camera_serial_nr' or $_ eq 'amount_zar' or $_ eq 'type' or $_ eq 'date_delivered' or $_ eq 'client' or $_ eq 'ref_nr') and ($_ eq 'rental' or $_ eq 'purchase') {
				# if ($quote_type{$id} eq 'Rental'){
					# &xlsxcreator_write_xlsx($ws2, $xlsrow2, $xlscol2, $val, $format_management_report_data)
					# $xlsxcol2++ ;
				# } elsif ($quote_type{$id} eq 'Purchase') {
					# &xlsxcreator_write_xlsx($ws3, $xlsrow2, $xlscol2, $val, $format_management_report_data)
					# $xlsxcol3++ ;
				# }
			# }
		# }	
		# $xlsxrow2++ $quote_type{$id} eq 'Rental';
		# $xlsxrow3++ $quote_type{$id} eq 'Purchase';
	# }
	
	# @sql_col_display = ("camera_nr","camera_system_id","camera_serial_nr","vpu_serial_nr","date_received","amount_usd","amount_zar","type","in_stock","date_delivered","client","ref_nr","in_stock", "events", "demo", "purchase", "rental") ;

	&report_xls_export_footer('L',15,'stockreport') ;
	
	# $fnsortcol   = 3 ; 
	# $fnsortorder = 'asc' ;
	$fnsortcol   = 0; 
	$fnsortorder = 'asc' ;
	$print_more_boxes .= qq~<br><button type="button" class="btn btn-default" onclick="history.go(-1);">Back</button><br><br>~ ;
	&common_min_extra_crumb("$lcpage-report","Search Screen") ;
	
} #------------------------------------------------------------------------------------------

sub set_neg_value_to_zero {
	
	my ($value) = @_ ; 
	if ($value < 0 ){
		return 0 ; 
	} else {
		return $value ; 
	}
}

sub set_tick {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-ok"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

sub set_cross {
	
	my ($color,$tooltip) = @_ ;
		
	$tick{green}  = qq~<span style="display:none;">2</span><span class="label label-success"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{orange} = qq~<span style="display:none;">1</span><span class="label label-warning"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;
	$tick{red}    = qq~<span style="display:none;">0</span><span class="label label-danger"><i $tooltip class="glyphicon glyphicon-remove"></i></span>~ ;

	return ($tick{$color}) ;
			
} #------------------------------------------------------------------------------------------

sub load_list_vars {
	
	my ($where) = @_ ;
	# our %ref_nr = () ; 
	# &db_min_ro('quotes','id,camera_system_id, quote_nr','','','') ;
	# foreach my $id (keys %{$db{quotes}}) { 
		# $ref_nr{$db{quotes}{$id}{camera_system_id}} = $db{quotes}{$id}{quote_nr} ; 
	# } 	
	&db_min_ro('camera_systems','id,name','','','') ;
	foreach my $id (keys %{$db{camera_systems}}) { 
		$camera_system{$id} = $db{camera_systems}{$id}{name} ; 
	} 	
	&db_min_ro('customers','id,name','','','') ;
	foreach my $id (keys %{$db{customers}}) { 
		$customer{$id} = $db{customers}{$id}{name} ; 
	} 
	# &common_load_quote_vars('>0');
	&common_load_quote_vars("cameras.quote_nr>0 AND cameras.quote_nr=quotes.quote_nr");
		
	&db_min_ro($table,'*',$where,'','') ;

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

sub thead {
	&common_min_thead ;
} #-------------------------------------------------------------------------------

sub page_opts {

	our $glyphicon = 'list' ;
	our $lcpage    = 'stock' ;

	&common_page_name ;
	   
	our $table     = 'cameras' ;
	$page_title    = 'Stock' ;

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

sub report_screen {

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

	$print_box_content_rows .= &common_min_forms_start('report') ;

	&common_camera_opts ;

	&common_min_select_opts('camera_system_id','camera_systems','code',$i{camera_system_id},'') ;
	# &common_min_select_opts('camera_system_id','price_list','description','','') ;
	&common_min_select_opts('client_id','customers','name',$i{client_id},'') ;

	my @datefrom = &common_add_delta_ymd(-2,0,0) ;

	$print_box_content_rows  .= &common_min_form_datepicker('date_from',"$datefrom[0]-01-01") ;  
	$print_box_content_rows  .= &common_min_form_datepicker('date_to',"$now_year-$now_mm-$now_dd") ; 
	$print_box_content_rows  .= &common_min_form_select('camera_system_id','') ; 
	# $opts{stock}              = qq~<option value=""></option>
	# <option value="stock">In Stock</option>
	# <option value="purchased">Purchased</option>
	# <option value="rental">Rental</option>
	# <option value="demo">Demo</option>~ ; 
	$print_box_content_rows  .= &common_min_form_select('stock','','',1,'') ; 
	$print_box_content_rows  .= &common_min_form_select('client_id','') ; 
	$print_box_content_rows  .= &common_min_forms_end('','','report') ;

	&common_min_search_screen ;

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

use common ;
use report ;
use xlsxcreator ;
use today ;

1;