#!/usr/bin/perl

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

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

&today ;

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

use DBI;
use CGI::Carp qw(fatalsToBrowser);

&load_waybills ;

exit ;

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

sub load_waybills {

&db_open_ro ;

my $sql = qq(SELECT * FROM ffwaypls_filmfreight.waybills) ;

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 ;  
    unless (substr($wb{waybillno},0,1) eq 'F') { next; }    
    my $new_waybillno = substr($wb{waybillno},1) ;
    &db_open_upd ;
        &update_waybills($new_waybillno,$wb{waybillno}) ;
        # &update_ata_carnet($new_waybillno,$wb{waybillno}) ;
        # &update_journeys($new_waybillno,$wb{waybillno}) ;
        &update_manifests($new_waybillno,$wb{waybillno}) ;
        &update_parcels($new_waybillno,$wb{waybillno}) ;
        # &update_supplier_invoice_waybills($new_waybillno,$wb{waybillno}) ;
    &db_close_conn ;
    }
   
} #------------------------------------------------------------------------------------------

sub update_waybills {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE waybills SET waybillno = '$new_waybillno' WHERE waybillno = '$waybillno'
) ;
          
&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

sub update_manifests {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE manifests SET waybillno = '$new_waybillno' WHERE waybillno = '$waybillno'
) ;

&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

sub update_parcels {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE parcels SET waybillno = '$new_waybillno' WHERE waybillno = '$waybillno'
) ;

&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

sub update_ata_carnet {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE ata_carnet SET hawb = '$new_waybillno' WHERE hawb = '$waybillno'
) ;

&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

sub update_journeys {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE journeys SET hawb = '$new_waybillno' WHERE hawb = '$waybillno'
) ;

&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

sub update_supplier_invoice_waybills {

my ($new_waybillno,$waybillno) = @_ ;

$sql = qq(UPDATE supplier_invoice_waybills SET waybillno = '$new_waybillno' WHERE waybillno = '$waybillno'
) ;
          
&common_debug($sql) ;

$sth = $dbh -> do ($sql) or die "could not execute :<BR>$sql<BR>$!" ;

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

use db ;
use today ;
use common ;

1;