aisa/scripts/stock_report.pl
2025-11-26 11:31:54 +02:00

307 lines
12 KiB
Perl

#!/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
if ($iaction eq ''){ &report_screen ; }
&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_id}) {
# push @report_sql, "(`id`='$i{camera_id}')" ;
# push @report_results, "camera = $camera_name{$i{camera_id}}" ;
# $xlsreportname .= '-' . $i{camera_id} ;
# # our $acc_id_sql_where = "`camera_id`='$i{camera_id}'"
# }
# if ($i{camera_system_id}) {
# push @report_sql, "(`camera_system_id`='$i{camera_system_id}')" ;
# push @report_results, "camera_system = $camera_system{$i{camera_id}}" ;
# $xlsreportname .= '-' . $i{camera_system_id} ;
# }
# if ($i{camera_system_id}) {
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) ;
unless ($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","date_delivered","in_stock") ;
&report_xls_export_header("Camera_Stock_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 ; }
}
foreach my $id (sort {$b <=> $a} 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;
foreach (@sql_col_display) {
unless ($_) { next ; } # blank for the buttons column
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 ; }
if ($_ eq 'date_received') { $align = $center ; }
if ($_ eq 'amount_zar') { $val = $db{$table}{$id}{cur_amount} ; }
if ($_ eq 'amount_usd') { $val = $db{$table}{$id}{usd_amount} ; }
if ($_ eq 'camera_serial_nr') { $val = $db{$table}{$id}{serial_nr} ; }
if ($_ eq 'camera_system_id') { $val = $camera_system{$val} ; }
if ($_ eq 'type') { $val = $quote_type{$id} ; }
# if ($_ eq 'in_stock') { if ($db{$table}{$id}{quote_nr}) { $val = 0 ; } else { $val = 1 ; } }
if ($_ eq '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 ; } }
if ($_ eq 'vpu_serial_nr') { $val = $linked_sn{$db{$table}{$id}{serial_nr}} ; } # $val .= ' [' . $linked_cn{$db{$table}{$id}{serial_nr}} . ']' if $linked_cn{$db{$table}{$id}{serial_nr}} ;
&report_xls_export_process_cell('','',$val) ; # ------------------------------------------------------------------------- HTML AFTER
$xlscol++;
$val = &common_camera_links($table,$id,$val) if $_ eq 'camera_nr' or $_ eq 'ref_nr' ;
if ($_ eq 'in_stock') { if ($val==1) { $val = &set_tick('green', '') ; } else { $val = &set_tick('red', '') ; } $align = $center ; }
$print_tbody .= qq~<td $align>$val</td>~ ;
}
$print_tbody .= qq~</tr>~ ;
$xlsrow++ ;
}
&report_xls_export_footer('L',15,'stockreport') ;
$fnsortcol = 3 ;
$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_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 load_list_vars {
my ($where) = @_ ;
&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;