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

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

my ($q)         = CGI -> new() ;
$iaction        = $q -> param('iaction')     || '' ;

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

print "Content-type: text/html\n\n";

&today;

if ($iaction eq ''){
    &display_screen ;
    }
       
if ($iaction eq 'search'){
    &search_ifields ;
    &db_load_airports ;
    &db_get_supplier_opts ;
    $supplier_invoice = 1 ;
    &db_load_journeys ;
    &search_waybills ;
    &search_waybills_no_parcels ; # must come after
    &display_screen ;
    }
    
exit ;

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

sub search_ifields {

for my $p ($q->param) { my $val = $q->param($p); $isrch{$p} = $val ; }
    
@origins      = split(/\,/,$isrch{'origins'})      ;
@destinations = split(/\,/,$isrch{'destinations'}) ;   

foreach (@origins) {
    my ($origin_id,$origin_name) = split(/\:/,$_) ;
    if ($isrch{'iallexceptorig'}){
        $add_destination_sql .= qq( (waybills.originid <> '$origin_id') AND) ;
        push @search_results, "Origin <> $origin_name" ;
        }
    else
        {
        $add_origin_sql .= qq((waybills.originid = '$origin_id') OR ) ;
        push @search_results, "Origin = $origin_name" ;
        }
    }    
if ($add_origin_sql) { $add_origin_sql = substr($add_origin_sql,0,-4); $add_sql .= qq( AND ($add_origin_sql)) ; }
   
foreach (@destinations) {
    our ($dest_id,$dest_name) = split(/\:/,$_) ;
    $dest_names .= $dest_name . ', ' ;
    if ($isrch{'iallexceptdest'}){ 
        $add_destination_sql .= qq( (waybills.destid <> '$dest_id') AND) ;
        push @search_results, "Destination <> $dest_name" ;
        }
    else
        {
        $add_destination_sql .= qq((waybills.destid = '$dest_id') OR ) ;
        push @search_results, "Destination = $dest_name" ;
        }
    }    
$dest_names = substr($dest_names,0,-2);
if ($add_destination_sql) { $add_destination_sql = substr($add_destination_sql,0,-4); $add_sql .= qq( AND ($add_destination_sql)) ; }
    

if ($isrch{'inopod'}){
    push @search_results, "POD Date = ''" ;
    $add_sql .= qq( AND (poddate = '0000-00-00') ) ;
    $print_pdf_butt = qq(<a class="btn btn-default btn-sm" href="javascript:printReport();"><i class="glyphicon glyphicon-print"></i> Print</a>) ;
    $add_search_print_col = 1 ;
    }
    
$search_results_msg = join(', ', @search_results) ;

$search_results_param = $search_results_msg ; $search_results_param =~ s/, POD Date = ''//gi ; $search_results_param =~ s/'//gi ;

unless ($search_results_msg) { $error = qq(ENTER AT LEAST ONE SEARCH PARAMETER) ; &display_screen ; }
   
} #------------------------------------------------------------------------------------------

sub search_waybills {

&db_open_ro ;

my $select = &db_waybill_list_select ;

my $sql = qq($select
FROM waybills, parcels
WHERE parcels.waybillno = waybills.waybillno $add_sql
ORDER BY waybills.capturedate DESC $limit) ;

# &common_debug($sql) ;

my $sth = $dbh->prepare($sql) ;

$sth -> execute() or die "Could not execute SQL statement $sql ... maybe invalid? $!";
$id_array_ref = $sth->fetchall_arrayref();

$sth->finish();

&db_close_conn ;

&db_process_waybill_list ;
   
} #------------------------------------------------------------------------------------------

sub search_waybills_no_parcels {

&db_open_ro ;

# if (substr($add_sql,4) eq ' AND') { $add_sql = substr($add_sql,4) ; }
$add_sql = substr($add_sql,5) ;

my $sql = qq(SELECT * FROM waybills
WHERE $add_sql
ORDER BY waybills.capturedate DESC $limit) ;

# &common_debug($sql) ; exit ;

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 ($contents{$wb{waybillno}}) { next ; }
    &db_process_wb_list_tbody;        
    }
  
} #------------------------------------------------------------------------------------------

sub display_screen {

&get_opts ;

if ($add_search_print_col) { $print_extra_th = qq(<th style="display:none;">&nbsp;</th>) ; }

if ($tbody) {
    $print_results_table .= qq(<div class="row">
    <div class="box col-md-12">
        <div class="alert alert-success" id="search-alert" role="alert"><i class="glyphicon glyphicon-ok"></i> SEARCH RESULTS : $search_results_msg $limit</div>
    </div>
    </div><!--/row-->
    <div class="row">
    <div class="box col-md-12">
    <div class="box-inner">
    <div class="box-header well" data-original-title="">
        <h2><i class="glyphicon glyphicon-film"></i> Waybill List</h2>
    </div>
    <div class="box-content">
    <table id="wblist" class="table table-striped table-bordered bootstrap-datatable datatable responsive">
    <thead>
    <tr>
        <th>HAWB</th>
        <th style="display:none;">sort</th>
        <th>COLDATE</th>
        <th>INV</th>
        <th>POD</th>
        <th class="dt-center">MAWB</th>
        <th>FLAGS</th>
        <th>BILL TO</th>
        <th>SENDER</th>
        <th class="dt-center">ORIG</th>
        <th>CONSIGNEE</th>
        <th class="dt-center">DEST</th>
        <th>CONTENTS</th>
        <th class="dt-center">PCS</th>
        <th class="dt-center">KG</th>
        <th>SERVICE</th>
        <th>$print_pdf_butt&nbsp;</th>
        $print_extra_th
    </tr>
    </thead>
    <tbody>
        $tbody
    </tbody>
    </table>
    </div>
    </div>
    </div>
    <!--/span-->

    </div><!--/row-->) ;
    
    $trigger_jquery .= qq(var oTable = \$('#wblist').dataTable(); oTable.fnSort( [ [1,'desc'] ] ); \$("#minimize-search").trigger('click');) ;
    }
elsif ($error) {
    $print_results_table .= qq(<div class="row">
    <div class="box col-md-12">
        <div class="alert alert-danger" id="search-alert" role="alert"><i class="glyphicon glyphicon-exclamation-sign"></i> ERROR : $error !</div>
    </div>
    </div><!--/row-->) ;
    }
elsif ($search_results_msg) {
    $print_results_table .= qq(<div class="row">
    <div class="box col-md-12">
        <div class="alert alert-warning" id="search-alert" role="alert"><i class="glyphicon glyphicon-info-sign"></i> NO RESULTS : $search_results_msg !</div>
    </div>
    </div><!--/row-->) ;
    }

# if ($username eq 'rory'){   
    # $trigger_jquery .= qq(\$("#selectOrigin").trigger('chosen:activate');) ;
    # }
    
&screen1 ;    

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

sub get_opts {

&db_get_hawb_opts(1) ;
&db_get_clients_opts ;
&db_get_town_opts ;
&db_get_shipper_opts ;
&db_get_supplier_opts ;

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

sub screen1 {

require pod_report ;

exit;

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

use db ;
use today ;
use common ;

1;