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

&get_db_shipper_details ;

print $json ;

exit ;

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

sub get_db_shipper_details {

&db_open_ro ;

my $sql = qq(SELECT * FROM ffwaypls_filmfreight.shippers ORDER BY receiver) ;

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

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

$sth->finish();

&db_close_conn ;

foreach $row (@$senders_array_ref) {

    &db_shipper_fields ;
    
    $sn_id             =~ s/^\s+|\s+$//g ; # trim both ends whitespace
    $sn_receiver       =~ s/^\s+|\s+$//g ;
    $sn_addr1          =~ s/^\s+|\s+$//g ;
    $sn_addr2          =~ s/^\s+|\s+$//g ;
    $sn_addr3          =~ s/^\s+|\s+$//g ;
    $sn_addr4          =~ s/^\s+|\s+$//g ;
    $sn_pc             =~ s/^\s+|\s+$//g ;
    $sn_contact        =~ s/^\s+|\s+$//g ;
    $sn_phoneno        =~ s/^\s+|\s+$//g ; 
    
    &update_shipper($sn_id,$sn_receiver,$sn_addr1,$sn_addr2,$sn_addr3,$sn_addr4,$sn_pc,$sn_contact,$sn_phoneno) ;
    }
    
} #------------------------------------------------------------------------------------------

sub update_shipper {

my ($id,$receiver,$addr1,$addr2,$addr3,$addr4,$pc,$contact,$phoneno) = @_ ;

&db_open_upd ;

my $sql = qq(UPDATE ffwaypls_filmfreight.shippers 
SET

Receiver = "$receiver",
Addr1 = "$addr1",
Addr2 = "$addr2",
Addr3 = "$addr3",
Addr4 = "$addr4",
PC = "$pc",
Contact = "$contact",
PhoneNo = "$phoneno"

WHERE id = "$id") ;

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

&db_close_conn ;

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

use db ;
use today ;

1;