#!/usr/bin/perl
BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
require cfg ;
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use CGI;
use POSIX;
# unless ($username eq 'rory') { print "<<<<<<<<<<<<<<<< MAINTENANCE IN PROGRESS >>>>>>>>>>>>>>>" ; exit ; }
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
our ($q) = CGI -> new() ;
$iaction = $q -> param('iaction') || '' ;
$iall = $q -> param('iall') || '' ;
$iboth = $q -> param('iboth') || '' ;
$isaved = $q -> param('isaved') || '' ;
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
print "Content-type: text/html\n\n";
&today;
if ($iaction eq ''){
&base_screen ;
}
if ($iaction eq 'edit'){
&common_load_params ;
&get_storage_list ;
&edit_screen ;
&screen1;
}
if ($iaction eq 'save'){
&common_load_params ;
&process_storage_for_save ;
$savetype = "SAVED" ;
$viewsaved = 0 ;
&screen2;
}
if ($iaction eq 'update'){
&common_load_params ;
&check_update_storage ;
&update_storage(1) ;
$savetype = "UPDATED" ;
&screen2;
}
if ($iaction eq 'delete'){
&common_load_params ;
&delete_storage ;
$savetype = "DELETED" ;
$viewsaved = 1 ;
&screen2;
}
exit;
#------------------------------------------------------------------------------------------
sub process_storage_for_save {
@input_array = $q -> param('input_array');
our $batchno = &db_get_max_storage_batch_no ;
foreach (@input_array) {
$at_least_one_input = 1 ;
if ($i{storageid}{$_}){
&update_storage($_) ;
}
else
{
&insert_storage ;
}
}
unless ($at_least_one_input) { $error = qq(NOTHING SELECTED); &base_screen ; }
} #------------------------------------------------------------------------------------------
sub delete_storage {
unless ($i{storageid}) { return ; }
&db_open_upd ;
$sql = qq(DELETE FROM storage WHERE ID = "$i{storageid}" AND HAWB = "$i{hawb}") ;
&common_debug($sql) ;
$sth = $dbh -> do ($sql) or die "could not execute :
$sql
$!" ;
&db_close_conn ;
} #------------------------------------------------------------------------------------------
sub check_update_storage {
$input = $q -> param('input_array');
unless ($input) { $error = qq(NOTHING SELECTED); &base_screen ; return ; }
$viewsaved = 0 ;
if (($i{dateout}{1}) and ($i{storageid}{1}) and ($i{waybillno}{1})) { $viewsaved = 1 ; }
} #------------------------------------------------------------------------------------------
sub update_storage {
my ($field) = @_ ;
unless ($i{storageid}{$field}) { return ; }
&db_open_upd ;
$sql = qq(UPDATE storage SET
DateOut = "$i{dateout}{$field}",
Remarks = "$i{remarks}{$field}"
WHERE ID = "$i{storageid}{$field}" AND HAWB = "$i{waybillno}{$field}") ;
&common_debug($sql) ;
$sth = $dbh -> do ($sql) or die "could not execute :
$sql
$!" ;
&db_close_conn ;
} #------------------------------------------------------------------------------------------
sub insert_storage {
&db_open_upd ;
$sql = qq(INSERT INTO storage (ID,BatchNo,DateCreated,HAWB,DateOut,Remarks)
VALUES ("","$batchno","$now_ccyy_mm_dd","$i{waybillno}{$_}","$i{dateout}{$_}","$i{remarks}{$_}");) ;
&common_debug($sql) ;
$sth = $dbh -> do ($sql) or die "could not execute :
$sql
$!" ;
&db_close_conn ;
} #------------------------------------------------------------------------------------------
sub db_get_max_storage_batch_no {
&db_open_ro ;
my $sql = qq(SELECT BatchNo FROM storage ORDER BY BatchNo DESC LIMIT 1) ;
my $sth = $dbh->prepare($sql) ;
$sth -> execute() or die "Could not execute SQL statement $sql ... maybe invalid? $!";
my $max_batch_no_array_ref = $sth->fetchall_arrayref();
$sth->finish();
&db_close_conn ;
my $new_batchno = 1 ;
foreach $row (@$max_batch_no_array_ref) {
$batchno = @$row[0] ;
$new_batchno = $batchno ;
$new_batchno++ ;
}
return ($new_batchno) ;
} #------------------------------------------------------------------------------------------
sub get_storage_list {
if ($iall) {
&db_load_all_parcels ;
&get_db_storage_list(1) ;
}
elsif ($iboth) {
&db_load_all_parcels ;
&get_db_storage_list(3) ;
&get_db_waybill_storage_list ;
}
else
{
&db_load_all_parcels ;
&get_db_storage_list(2) ;
&get_db_waybill_storage_list ;
}
} #------------------------------------------------------------------------------------------
sub get_db_waybill_storage_list {
&db_open_ro ;
my $sql = qq(SELECT * FROM waybills WHERE OthSvc1 = 'Storage') ;
# my $sql = qq(SELECT * FROM waybills WHERE OthSvc1 = 'Packaging') ;
my $sth = $dbh->prepare($sql) ;
$sth -> execute() or die "Could not execute SQL statement $sql ... maybe invalid? $!";
my $id_array_ref = $sth->fetchall_arrayref();
$sth->finish();
&db_close_conn ;
foreach $row (@$id_array_ref) {
&db_waybills_fields ;
if ($storage{$wb{waybillno}}{$wb{storageno}}) { next ; }
my $checked = '' ;
if ($i{hawb}) { unless ($i{hawb} eq $wb{waybillno}) { next ; } $checked = 'CHECKED' ; }
$r++ ;
if ($i{id}{$wb{waybillno}}) { $hidden_fields_edit = qq() ; } else { $hidden_fields_edit = '' ; }
my $invno = sprintf("%.0f",($wb{'invno'})) ;
$print_contents = '' ;
if ($contents{$wb{waybillno}}){ &db_package_parcels($wb{waybillno}) ; }
if ($wb{volmass} > $wb{actmass}) { $chgmass = ceil($wb{volmass}); } else { $chgmass = ceil($wb{actmass}); }
my $view_wb_link = qq(dlgMdl('$useropts{scripts}/dialog/view_waybill.pl?$wb{waybillno}','HAWB $wb{waybillno} : $wb{client} : $wb{coldate}','','max-dialog');) ;
$tbody .= qq(