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

&db_open_upd ;

foreach $row (@$id_array_ref) {
    &db_waybills_fields ;
    
    # if ($wb{waybillno} > 17534) { next ; }
    # if ($wb{waybillno} < 17000) { next ; }

    # if ($wb{waybillno} > 17534) { next ; }
    # if ($wb{waybillno} < 17000) { next ; }
    
    if ($wb{waybillno} > 100) { next ; }

    $get_inline_address = 1 ;
    &common_get_addresses ;
    
    $new_wb{receiver}        = $shipper{receiver} ;
    $new_wb{reccontact}      = $contact{receiver} ;
    $new_wb{recphone}        = $phoneno{receiver} ;
    $new_wb{reccellno}       = $cellno{receiver} ;
    $new_wb{recaddr1}        = $addr1{receiver} ;
    $new_wb{recaddr2}        = $addr2{receiver} ;
    $new_wb{recaddr3}        = $addr3{receiver} ;
    $new_wb{recaddr4}        = $addr4{receiver} ;
    $new_wb{recpc}           = $pc{receiver} ;
    $new_wb{recemail}        = $email{receiver} ;
    
    $new_wb{sender}          = $shipper{sender} ;
    $new_wb{sencontact}      = $contact{sender} ;
    $new_wb{senphone}        = $phoneno{sender} ;
    $new_wb{sencellno}       = $cellno{sender} ;  
    $new_wb{senaddr1}        = $addr1{sender} ;
    $new_wb{senaddr2}        = $addr2{sender} ;
    $new_wb{senaddr3}        = $addr3{sender} ;
    $new_wb{senaddr4}        = $addr4{sender} ;
    $new_wb{senpc}           = $pc{sender} ;
    $new_wb{senemail}        = $email{sender} ; 
    
    foreach (sort keys %new_wb) {
        # $pre = substr($_,0,3) ;
        # if ($pre ne $post) { print uc "<br>$pre : $wb{waybillno} <span style='color:green'>$wb{senderaddid}</span> : " ; }
        # $font = 'black' ;
        # if ($new_wb{$_} ne $wb{$_}) { $font = 'red' ; }
        # # print $_ . " = <span style='color:$font'>$new_wb{$_} : $wb{$_}</span>, " ;
        
        unless ($wb{$_}) {$wb{$_} = $new_wb{$_} ; }
        # print "<span style='color:$font'>$wb{$_}</span>, " ;
        # $post = $pre ;         
        }
        
    &update_waybills($wb{waybillno}) ; 
    
    }
   
&db_close_conn ;

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

sub update_waybills {

my ($waybillno) = @_ ;

$sql = qq(UPDATE ffwaypls_filmfreight.waybills SET 
Sender = "$wb{sender}",
Receiver = "$wb{receiver}",
RecContact = "$wb{reccontact}",
RecPhone = "$wb{recphone}",
RecAddr1 = "$wb{recaddr1}",
RecAddr2 = "$wb{recaddr2}",
RecAddr3 = "$wb{recaddr3}",
reccellno = "$wb{reccell}",
sencontact = "$wb{sencontact}",
senphone = "$wb{senphone}",
sencellno = "$wb{sencell}",
recemail = "$wb{recemail}",
senemail = "$wb{senemail}",
SenAddr1 = "$wb{senaddr1}",
SenAddr2 = "$wb{senaddr2}",
SenAddr3 = "$wb{senaddr3}",
SenAddr4 = "$wb{senaddr4}",
RecAddr4 = "$wb{recaddr4}",
RecPC    = "$wb{recpostalcode}",
SenPC    = "$wb{senpostalcode}"
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;