#!/usr/bin/perl

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

use DBI;
use CGI::Carp qw(fatalsToBrowser);
# 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;

# use GD::Barcode::UPCE;
# use GD::Barcode::Image;

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

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

my $waybillno = $ARGV[0] ;

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

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

&today ;

&db_load_airports ;
&db_load_waybill($waybillno) ;
&db_load_parcels($waybillno) ;
&build_pdf ;
&screen2 ;
    
exit ;

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

sub build_pdf {

my  $wbpdfpath = "$pdfpath/label" ;
our $pdfname = 'WB-LABEL-' . $waybillno . '-' . $now_ccyymmdd . '.pdf' ;

$layout = 'label' ;

&pdf_delete_previous_pdf($wbpdfpath) ;
&pdf_initialise($wbpdfpath,$pdfname,'Film Freight Waybill Label') ; ;
&waybill_out_to_label_pdf ;
&finish_pdf ;
    
} #------------------------------------------------------------------------------------------
    
sub label_line {

&pdf_black_line (5,$up,95,$up) ;
&pdf_nl_gap_2 ;

} #------------------------------------------------------------------------------------------
    
sub label_header {

$top = 5 ;

# end 107.5mm (label 100mm x 100mm)
&pdf_xx_large_text (5,$up,"Film Freight CC") ;

# binmode(STDOUT);
# print "Content-Type: image/png\n\n";
# my $barcode_png = GD::Barcode->new('EAN13', '123456789012')->plot->png;

# my $image1 = $page->gfx(1) ;
# my $image1_thumb = $barcode_png ;
# $pdf_image1_file = $pdf->image_jpeg($image1_thumb);
# $image1->image($pdf_image1_file, 50/mm, $up/mm, 20/mm, 5/mm);

&pdf_medium_text  (67,$up,"Wb No. : $wb{'waybillno'}") ; 
&pdf_nl_gap_2 ;

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

sub label_row_2 {

my $print_sender = uc $wb{'sender'} ;

# if ($wb{'originopshub'}){ $block_length = 28 ; } else { $block_length = 50 ; }
if ($airport_code{$wb{originid}}){ $block_length = 26 ; } else { $block_length = 34 ; }

&pdf_xx_large_bold_text  (82,$up,$airport_code{$wb{originid}}) ;
&pdf_medium_text (5,$up,"From :") ;
&pdf_text_block (16,$print_sender,$block_length,3) ;

} #------------------------------------------------------------------------------- 
  
sub label_row_3 {

my $print_receiver = uc $wb{'receiver'} ;

&pdf_medium_text  (5,$up,"To :") ;
# &pdf_medium_text  (12,$up,$print_receiver) ;
&pdf_text_block (12,$print_receiver,38,3) ;

$get_inline_address = 1 ;
&common_get_addresses ;
# &pdf_nl ;

@receiveraddresses = split(/<br>/,$address_for_email{'receiver'}) ;
foreach (@receiveraddresses) {
    &pdf_medium_text (12,$up,$_) ;
    &pdf_nl;
    }

my $print_receiver_contact   = '' ;
my $print_receiver_cellno    = '' ;
my $print_receiver_cellno_2  = '' ;

if ($wb{'reccellno'}) { $print_receiver_cellno   = ' or ' . $wb{'reccellno'} ; }
if ($wb{'reccellno'}) { $print_receiver_cellno_2 = ' : ' . $wb{'reccellno'} ; }

if (($wb{'reccontact'}) and ($wb{'recphone'})) { 
    $print_receiver_contact = $wb{'reccontact'} . " : " . $wb{'recphone'} . $print_receiver_cellno ; 
    }    
elsif ($wb{'recphone'}) { 
    $print_receiver_contact = $wb{'recphone'} . $print_receiver_cellno ; 
    }
elsif ($wb{'reccontact'}) { 
    $print_receiver_contact = $wb{'reccontact'} . $print_receiver_cellno_2 ; 
    }
    
if ($print_receiver_contact) {
    &pdf_medium_text  (12,$up,$print_receiver_contact) ;
    &pdf_nl ;
    }
    
$up=$up+4 ;
&pdf_xx_large_bold_text  (82,$up,$airport_code{$wb{destid}}) ;
&pdf_nl ;

} #------------------------------------------------------------------------------- 
  
sub label_row_4 {

my ($colccyy,$colmm,$coldd) = split(/\-/,$wb{'coldate'}) ;

$date_to_text = "$coldd-$colmm-$colccyy";
# $date_to_text = Date_to_Text($colccyy,$colmm,$coldd);
# $date_to_text =~ s/\-/ /g ;
# $date_to_text = substr($date_to_text,4) ;

&pdf_medium_text  (5,$up,"Date : $date_to_text") ;
&pdf_medium_text  (70,$up,"Pcs :") ;
&pdf_xx_large_bold_text  (80,$up,"$pccnt - $wb{'pcs'}") ;
&pdf_nl ;
&pdf_medium_text  (5,$up,"Service : $wb{'service'}") ;
&pdf_nl_gap_2 ;
&pdf_medium_text  (5,$up,"Wb No. :") ;
&pdf_xx_large_bold_text  (20,$up,$wb{'waybillno'}) ;
&pdf_medium_text  (70,$up,"Kgs :") ; 
# &pdf_xx_large_bold_text  (80,$up,$kgs) ;

if ($wb{volmass} > $wb{actmass}) { $chgmass = ceil($wb{volmass}); } else { $chgmass = ceil($wb{actmass}); }

# my $chgmass = sprintf("%.0f",($wb{chgmass})) ;
&pdf_xx_large_bold_text  (80,$up,$chgmass) ;

} #------------------------------------------------------------------------------- 
  
sub waybill_out_to_label_pdf {

$label = 1 ;

foreach (@parcels) {
    ($pcs,$desc,$kgs) = split(/\|/) ;
    for (1 .. $pcs) {
        $pccnt++ ;
        $up = 90 ;
        &pdf_initialise_page ;
        &label_header ;
        &label_line ;
        &label_row_2 ;
        &label_line ;
        &label_row_3 ;
        &label_line ;
        &label_row_4 ;
        &pdf_nl_gap_1 ;
        }
    }
                
} #------------------------------------------------------------------------------------------
                    
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/label/$pdfname' width='100%' height='100%' frameborder='0'></iframe>
            </td>
        </tr>
        </table>    
    </div>
</body>
</html>
ENDOFTEXT
#

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

use db ;
use dialog ;
use today ;
use common ;
use pdf ;

1;