#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
use CGI qw( :standard );
use CGI::Carp qw(fatalsToBrowser);
    
require cfg ;

print header; 

use Date::Calc qw(:all);
use POSIX;

use PDF::API2::Lite;
use constant mm => 25.4/72;
use constant in =>    1/72;
use constant pt =>    1;

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

@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});

my $cl_id    = $ARGV[0] ;
my $wb_list  = $ARGV[1] ;

my @waybills = split(/\|/,$wb_list);

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

unless ($wb_list) { print "ERROR" ; exit ; }

&today ;

&db_load_airports ;

&start_pdf ;

foreach my $wb_no (@waybills) {
	&db_load_waybill($wb_no) ;
	@parcels = () ;
	&db_load_parcels($wb_no) ;
	&build_pdf($wb_no) ;
	}

&finish_pdf ;

&screen2 ;
    
exit ;

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

sub start_pdf {

my  $invpdfpath = "$pdfpath/invoice" ;
our $pdfname = "INV-$cl_id-" . $now_ccyymmdd . '.pdf' ;

&pdf_delete_previous_pdf($invpdfpath) ;
&pdf_initialise($invpdfpath,$pdfname,'Film Freight Waybill') ; ;
    
} #------------------------------------------------------------------------------------------

sub build_pdf {

my ($wb_no) = @_ ;

&pdf_initialise_page ;

$up     = 285 ;

&invoice_header($wb_no) ;
&invoice_out_to_pdf($wb_no) ;

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

sub invoice_header {

my ($wb_no) = @_ ;

if (($coldatedd ne '00') and ($coldatedd ne '')) {
    $invoive_date = "$coldatedd-$coldatemm-$coldateccyy";
    }
else
    {
    $invoive_date = "$now_dd-$now_mm-$now_year";
    }

&pdf_nl_gap_2 ;
&pdf_xx_large_bold_text (77, $up, 'Commercial Invoice') ;
&pdf_nl_gap_2 ;
&pdf_nl ;
&pdf_medium_bold_text (10, $up, "Invoice # : ") ;
&pdf_large_text (28, $up, $wb_no) ; # Invoice Number
&pdf_medium_bold_text (170, $up, "Date : ") ;
&pdf_medium_text (182, $up, $invoive_date) ;
&pdf_nl_t ;
&pdf_black_line(10,$up,200,$up) ; # horizontal
&pdf_nl_t ;

} #-------------------------------------------------------------------------------
   
sub invoice_out_to_pdf {

my ($wb_no) = @_ ;

&pdf_check_value_of_up(40) ;

&pdf_nl;
$top=$up;
&pdf_black_line(10,$up,200,$up) ; # horizontal
&pdf_nl_s;

our @colstart = (10,106,200) ;
our @txtstart_1 = (12,108) ;
our @txtstart_2 = (12,197) ; # include align right values

&pdf_nl_tt ;
&pdf_small_bold_text ($txtstart_1[0], $up, "From :") ;
&pdf_text_block ($txtstart_1[0]+12,$wb{sender},50,4) ; $up = $up + 4 ;
&pdf_small_bold_text ($txtstart_1[1], $up, "To :") ;
&pdf_text_block ($txtstart_1[1]+8,$wb{receiver},50,4) ;

$get_inline_address = 1 ;
&common_get_addresses ;

$senderup = $up ; $receiverup = $up ;

@senderaddresses = split(/<br>/,$address_for_email{sender}) ;
foreach (@senderaddresses) {
    &pdf_small_text ($txtstart_1[0]+12,$senderup,$_) ;
    $senderup-=3;
    }

@receiveraddresses = split(/<br>/,$address_for_email{receiver}) ;
foreach (@receiveraddresses) {
    &pdf_small_text ($txtstart_1[1]+8,$receiverup,$_) ;
    $receiverup-=3;
    }

if ($receiverup < $senderup) { $up = $receiverup ; } else { $up = $senderup ; }

&pdf_nl_tt;

unless ($wb{sencontact}) { $wb{sencontact} = $contact{sender} ; }
unless ($wb{senphone})   { $wb{senphone}   = $phoneno{sender} ; }
unless ($wb{sencellno})  { $wb{sencellno}  = $cellno{sender}  ; }

&pdf_nl_t;
&pdf_small_text ($txtstart_1[0], $up, "Contact : $wb{sencontact}") ;
&pdf_small_text ($txtstart_1[1], $up, "Contact : $wb{reccontact}") ;
&pdf_nl_t;
&pdf_small_text ($txtstart_1[0], $up, "Phone   : $wb{senphone}      $wb{sencellno}") ;           
&pdf_small_text ($txtstart_1[1], $up, "Phone   : $wb{recphone}      $wb{reccellno}") ;
&pdf_nl;

if ($airport_code{$wb{originid}}){ $print_originopshub = qq($airport_code{$wb{originid}} - ) ; }
if ($airport_code{$wb{destid}})  { $print_destopshub   = qq($airport_code{$wb{destid}} - )   ; }

&pdf_small_bold_text ($txtstart_1[0],  $up, "Origin :") ;
&pdf_medium_bold_text ($txtstart_1[0]+11,  $up, "$print_originopshub$wb{'origin'}") ;
&pdf_small_bold_text ($txtstart_1[1], $up, "Destination :") ;
&pdf_medium_bold_text ($txtstart_1[1]+18, $up, "$print_destopshub$wb{'dest'}") ;

&pdf_nl_s;

&pdf_black_line(10,$up,200,$up) ; # horizontal

&vertical_lines_table ;	

$top=$up;

&pdf_nl_gap_3 ;
&pdf_nl_gap_3 ;
# &pdf_nl_gap_3 ;
# &pdf_nl_gap_3 ;

&pdf_black_line(10,$up,200,$up) ; # horizontal

&pdf_black_line ($colstart[0],$top,$colstart[0],$up) ; # vertical
&pdf_black_line ($colstart[2],$top,$colstart[2],$up) ; # vertical

$top=$up;

&pdf_nl_gap_1;

# &pdf_small_bold_text ($txtstart_1[0], $up, "Contents :") ;
&pdf_small_bold_text ($txtstart_1[0], $up, "CONTENTS") ;
&pdf_small_bold_text_right ($txtstart_2[1], $up, "VALUE FOR CUSTOMS") ;

&pdf_nl_s;
&pdf_black_line(10,$up,200,$up) ; # horizontal
&pdf_nl_gap_1;
&pdf_nl_gap_1;

foreach (@parcels) {
    my ($pcs,$desc,$actkgs,$volkgs) = split(/\|/) ;    
    # &pdf_small_text ($txtstart_1[0]+15, $up, "$pcs x $desc") ;
    &pdf_small_text ($txtstart_1[0], $up, "$desc") ;
	last;
    }
	
$disp_cust_val = sprintf("%.2f",$wb{customsvalue}) ; 

if ($wb{custvalcurr}) {
	# &pdf_medium_bold_text (170, $up, "$wb{custvalcurr} $disp_cust_val") ;
	&pdf_small_bold_text_right ($txtstart_2[1], $up, "$wb{custvalcurr} $disp_cust_val") ;
	}
	
&pdf_nl_gap_3 ;
&pdf_nl_gap_3 ;

&pdf_nl_s;

&pdf_black_line(10,$up,200,$up) ; # horizontal

&vertical_lines_table ;	

$top=$up;

&pdf_nl_gap_3 ;
&pdf_nl_s ;

&pdf_small_text (86, $up, 'No Financial Transaction Involved') ;
&pdf_nl;
&pdf_small_text (86, $up, 'Value for Customs Purposes Only') ;
&pdf_nl;
&pdf_small_text (86, $up, 'Item Strictly not for resale') ;
&pdf_nl;
&pdf_nl;

&pdf_black_line(10,$up,200,$up) ; # horizontal

&pdf_black_line ($colstart[0],$top,$colstart[0],$up) ; # vertical
&pdf_black_line ($colstart[2],$top,$colstart[2],$up) ; # vertical

$top=$up;

&pdf_nl_gap_1;

&pdf_small_bold_text ($txtstart_1[0], $up, "Origin : South Africa") ;

&pdf_nl_s;

&pdf_black_line(10,$up,200,$up) ; # horizontal

&vertical_lines_table ;	



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

sub text_block {

my ($along,$text_block,$tlock_length,$grid,$align_right) = @_ ;

my $lines = 1;

my @words_array = split (/ /, $text_block) ;
my $wrapped_line   = '' ;
my $block_start_up = $up ;

foreach my $word(@words_array) {
    my $word_length = length($word) ;
    my $wrapped_line_length = length($wrapped_line) ;

    if ( ($wrapped_line_length + $word_length) > $tlock_length ) {        
        &text_block_line($along,$grid,$wrapped_line,$align_right) ;
        &pdf_nl_s;   
        $lines++;
        $wrapped_line = '' ;
        }

    $wrapped_line .= $word . ' ' ;
    }
    
&text_block_line($along,$grid,$wrapped_line,$align_right) ;
 
&pdf_nl_s;

return ($lines) ;

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

sub text_block_line {

my ($along,$grid,$wrapped_line,$align_right) = @_ ;

if ($align_right) {
	&pdf_tiny_text_right ($along,$up,$wrapped_line) ;
	}
else
	{
	&pdf_tiny_text  ($along,$up,$wrapped_line) ; 
	}

# &pdf_small_text  ($along,$up,$wrapped_line) ;     

&check_value_of_up(20,1) ;
    
} #-------------------------------------------------------------------------------

sub vertical_lines_table {

&pdf_black_line ($colstart[0],$top,$colstart[0],$up) ; # vertical
&pdf_black_line ($colstart[1],$top,$colstart[1],$up) ; # vertical
&pdf_black_line ($colstart[2],$top,$colstart[2],$up) ; # vertical
	
} #------------------------------------------------------------------------------------------

sub check_value_of_up {

my ($value,$intxtblock) = @_ ;

if ($up < $value) {
    if ($intxtblock) { &pdf_nl; &pdf_black_line(10,$up,200,$up) ; }
	&vertical_lines_table ;
    &pdf_initialise_page ;
    $up     = 290 ;
    $across = 1 ;
	$top    = $up; $lowest_up = $up ;
    &pdf_black_line(10,$up,200,$up) ; # horizontal
	if ($intxtblock) { &pdf_nl; $start_up = $up ; }
    }

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

sub screen2 {

print <<ENDOFTEXT;
$dialog{'common'}{'head'}    
    <div id="content" class="col-lg-12 col-sm-12">                       
        <CENTER>
        <table height='100%' width='100%'>
        <tr>
            <td align='center'>
                <iframe src='$useropts{'$domain'}/pdf/invoice/$pdfname' width='100%' height='100%' frameborder='0'></iframe>
            </td>
        </tr>
        </table>    
    </div>
</body>
</html>
ENDOFTEXT
#

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

use dialog ;
use today ;
use common ;
use common_min ;
use db_min ;
use pdf ;

1;