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

exit ;

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

sub load_waybills {

&db_open_ro ;

my $sql = qq(SELECT waybillno, invno FROM waybills WHERE InvNo = '' AND WaybillNo < 15878 ORDER BY WaybillNo DESC) ;

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

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

$sth->finish();

&db_close_conn ;

# &db_open_upd ;

foreach $row (@$array_ref) {
    $waybillno       = @$row[0] ;
    $invoiceno       = @$row[1] ;
    # unless ($invoiceno) { next ; }
    &update_waybills($waybillno,'NC') ;
    }
    
# &db_close_conn ;  

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

sub update_waybills {

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

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

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

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

# sub load_invoices {

# &db_open_ro ;

# my $sql = qq(SELECT * FROM invoices) ;

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

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

# $sth->finish();

# &db_close_conn ;

# # &db_open_upd ;

# foreach $row (@$inv_array_ref) {
    # $waybillno       = @$row[0] ;
    # $invoiceno       = @$row[1] ;
    # unless ($invoiceno) { next ; }
    # &update_waybills($waybillno,$invoiceno) ;
    # }
    
# # &db_close_conn ;  

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

use db ;
use today ;
use common ;

1;