#!/usr/bin/perl

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

use DBI;
use CGI::Carp qw(fatalsToBrowser);
use Date::Manip;
use Date::Calc qw(:all);

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 $qt_id     = $ARGV[0] ;

# our $debug = 1 ;
our $label = 1 ; # used in pdf_check_value_of_up

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

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

&today ;
&pdf_curr_symbols;

my $table = 'quotes' ;

&db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	&prep_pdf ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

&exclusions ;
&start_pdf ;
&build_pdf ;
&finish_pdf ;
&screen2 ;
    
exit ;

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

sub prep_pdf {

my @cols = () ;

&tab_hash ;

&db_min_ro($table,"*","`id`='$qt_id'",'','') ;

our $custid = $db{$table}{$qt_id}{deliverynote_to} ;
&db_min_ro('customers','id,name,physical_address,contact_email,phone,contact_name,vat_nr',"`id`='$custid'",'','') ;
$customer_name{$custid}         = $db{customers}{$custid}{name} ; 
$customer_address{$custid}      = $db{customers}{$custid}{physical_address} ; 
$customer_email{$custid}        = $db{customers}{$custid}{contact_email} ; 
$customer_phone{$custid}        = $db{customers}{$custid}{phone} ; 
$customer_contact_name{$custid} = $db{customers}{$custid}{contact_name} ; 
$customer_vat_nr{$custid}       = $db{customers}{$custid}{vat_nr} ; 

&db_min_ro('price_list','id,description','','','') ;
foreach my $id (keys %{$db{price_list}}) { $price_list{$id} = $db{price_list}{$id}{description} ; }

our $countryid = $db{$table}{$qt_id}{country_shipped} ;
&db_min_ro('countries','*',"`id`='$countryid'",'','') ;
$country{$countryid} = $db{countries}{$countryid}{name} ;

our $camera_system_id = $db{$table}{$qt_id}{camera_system_id} ;
&db_min_ro('camera_systems','*',"`id`='$camera_system_id'",'','') ;
$camera_system{$camera_system_id} = $db{camera_systems}{$camera_system_id}{name} ;
    
} #------------------------------------------------------------------------------------------
    
sub exclusions {

our $exclude_rate = 0 ;
our $exclude_vat  = 0 ;
	
if ($db{$table}{$qt_id}{currency} ne 'ZAR') {
	$exclude_vat = 1 ;
}
	
} #------------------------------------------------------------------------------------------
    
sub start_pdf {

my  $qtpdfpath = "$pdfpath/deliverynote" ;
our $pdfname = "EW-$qt_id-" . $now_ccyymmdd . '.pdf' ;

&pdf_delete_previous_pdf($qtpdfpath) ;
&pdf_initialise($qtpdfpath,$pdfname,'ITV Extended Warranty') ;
    
} #------------------------------------------------------------------------------------------
    
sub build_pdf {

&tab_curr_symbols;

&pdf_initialise_page ;

$up = 292 ;
# &deliverynote_pdf_page_header ;

&pdf_box_block_top(5,205,'#009bc8') ;
	
	$up       = 260 ;
	$across   = 1 ;
	$blue_box = 1 ;
	
	&pdf_page_header_del ;
		
	&deliverynote_pdf_vars ;

	&deliverynote_out_to_pdf ;
	#&deliverynote_pdf_footer ;

$up = 5 ;	
&pdf_box_block_bottom(5,205,'#009bc8') ;

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

# sub deliverynote_pdf_page_header {

# $page_num++ ;

# my $image1 = $page->gfx(1) ;
# my $image1_thumb = "$htmlpath/img/pdf_logo.png";
# $pdf_image1_file = $pdf->image_png($image1_thumb);
# $image1->image($pdf_image1_file, 7/mm, $up/mm, 90/mm, 29/mm);

# $up+=18 ;

# &pdf_nl_gap_3 ;
# &pdf_xx_large_bold_text (156,$up,"QUOTE") ;
# &pdf_nl_gap_1 ;

# } #-------------------------------------------------------------------------------
    
sub deliverynote_out_to_pdf {

$up = $up - 5;
$up = $up - 5;



$top = $up;


&pdf_box_block_top(10,200,'#009bc8') ;

$up = $up - 5;


&pdf_small_bold_text ($boxtxt[1],$up,'Invoice To');
&pdf_small_bold_text ($boxtxt[2],$up,'Pull from Customer');
&pdf_small_bold_text ($boxtxt[3],$up,'Shipped To');
&pdf_small_bold_text ($boxtxt[4],$up,'Pull from Quote');

$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$up, $blocks[3],$up,'#009bc8') ;
&pdf_thin_black_line ($blocks[4],$up, 200,$up,'#009bc8') ;


$up = $up - 5;
$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$up, $blocks[3],$up,'#009bc8') ;
&pdf_thin_black_line ($blocks[4],$up, 200,$up,'#009bc8') ;


$up = $up - 5;
$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$up, $blocks[3],$up,'#009bc8') ;
&pdf_thin_black_line ($blocks[4],$up, 200,$up,'#009bc8') ;

$up = $up - 5;
$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$up, $blocks[3],$up,'#009bc8') ;
&pdf_thin_black_line ($blocks[4],$up, 200,$up,'#009bc8') ;

$up = $up - 5;
$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$up, $blocks[3],$up,'#009bc8') ;
&pdf_thin_black_line ($blocks[4],$up, 200,$up,'#009bc8') ;

$up = $up - 5;
$up = $up - 3;
&vertical_lines_inner_box;
&pdf_box_block_bottom(10,200,'#009bc8') ; 


$up = $up - 5;
$up = $up - 3;

$top = $up;
&pdf_box_block_top(10,200,'#009bc8') ;
$up = $up - 5;

&pdf_small_bold_text ($boxtxt[1],$up,'Item Code');
&pdf_small_bold_text ($boxtxt[2],$up,'Description');
&pdf_small_bold_text ($boxtxt[3],$up,'Qty');
&pdf_small_bold_text ($boxtxt[4],$up,'Serial Number');

$up = $up - 3;

&vertical_lines_inner_box;



&pdf_box_block_bottom(10,200,'#009bc8') ; 

$up = $up - 5;

&pdf_small_text($boxtxt[1], $up, 'All Items received in good order');

$up = $up - 1;


$top = $up;
&pdf_box_block_top(10,200,'#009bc8') ;

$up = $up - 5;

&pdf_small_text($boxtxt[1], $up, 'Date');

$up = $up - 3;

&pdf_thin_black_line ($blocks[1],$up, $blocks[3],$up,'#009bc8') ;

$up = $up - 5;

&pdf_small_text($boxtxt[1], $up, 'Name');

$up = $up + 4;

&pdf_small_text($boxtxt[3], $up, 'Signature');

$up = $up - 4;

$up = $up - 3;

&vertical_lines_inner_box;

&pdf_box_block_bottom(10,200,'#009bc8') ; 

$up = $up - 5;

$up = $up - 3;

$top = $up;

&pdf_box_block_top(10,200,'#009bc8') ;

$up = $up - 5;

$up = $up - 3;

$up = $up - 5;
&pdf_small_text($boxtxt[1], $up, 'Remarks');

$up = $up - 3;

$up = $up - 5;

$up = $up - 3;

&pdf_thin_black_line ($blocks[2],$top,$blocks[2],$up,'#009bc8') ;

&pdf_box_block_bottom(10,200,'#009bc8') ; 





} #------------------------------------------------------------------------------------------
#&pdf_nl_gap_3;
#&pdf_box_block_top($blocks[1],200,'#009bc8')

#&vertical_lines_inner_box

sub vertical_lines_inner_box {
	
&pdf_thin_black_line ($blocks[2],$top,$blocks[2],$up,'#009bc8') ; # vertical
&pdf_thin_black_line ($blocks[3],$top,$blocks[3],$up,'#009bc8') ; # vertical
&pdf_thin_black_line ($blocks[4],$top,$blocks[4],$up,'#009bc8') ; # vertical
#&pdf_thin_black_line ($blocks[6],$top,$blocks[6],$up,'#009bc8') ; # vertical
	
} #----------------------------------------------------------------------------------------------------



sub deliverynote_pdf_footer {
	
&pdf_check_value_of_up(70);
&pdf_nl_gap_3;&pdf_nl_gap_3;&pdf_nl_gap_3; 
#&pdf_nl_gap_3;&pdf_nl_gap_3;&pdf_nl_gap_3; 


&pdf_tiny_bold_text (10, $up, "Name:")  ; &pdf_tiny_text (30, $up, "__________________________________________") ;
&pdf_tiny_bold_text (140, $up, "Date:") ; &pdf_tiny_text (155, $up, "__________________________") ;
&pdf_nl;
&pdf_tiny_text (10, $up, "(Duly Authorised)") ;
&pdf_nl_gap_3;
&pdf_tiny_bold_text (10, $up, "Company:") ; &pdf_tiny_text (30, $up, "__________________________________________") ;
&pdf_tiny_bold_text (140, $up, "Signature:") ; &pdf_tiny_text (155, $up, "__________________________") ;
&pdf_nl_gap_2;
&pdf_tiny_bold_text (10, $up, "Name:")  ; &pdf_tiny_text (30, $up, "__________________________________________") ;
&pdf_tiny_bold_text (140, $up, "Date:") ; &pdf_tiny_text (155, $up, "__________________________") ;
&pdf_nl;
&pdf_tiny_text (10, $up, "(Duly Authorised on behalf of Interactive Television Africa)") ;
&pdf_nl;
&pdf_nl;
&pdf_tiny_bold_text (140, $up, "Signature:") ; &pdf_tiny_text (155, $up, "__________________________") ;
	
} #------------------------------------------------------------------------------------------

sub deliverynote_pdf_vars {

# our @htxt   = (0,0,115,0,146,172,0) ;
# our @boxtxt = (0,12,102,117,142,167) ;
# our @blocks = (0,10,100,115,140,165) ;

# our @rboxtxt = (0,97,122,147,172,197) ;
# our @boxtxt  = (0,12,102,127,152,177) ;
# our @blocks  = (0,10,100,125,150,175) ;

our @rboxtxt  = (0,30,103,116,143,170,197) ;
our @boxtxt   = (0,12,42,107,137,157,174) ;
our @blocks   = (0,10,40,105,135,155,172) ;

# our @tboxrtxt = (0,77,107,137,167,197) ;
# our @tboxtxt  = (0,12,82,112,142,172) ;
# our @tblocks  = (0,10,80,110,140,170) ;

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

sub custom_date_txt {

my ($date) = @_ ;

my $dt_txt = Date_to_Text(substr($date,0,4),substr($date,5,2),substr($date,8,2));

$dt_txt =  substr($dt_txt,4) ;

return ($dt_txt) ;

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

sub check_space_on_page_custom {

my ($value) = @_ ;

if ($up < $value) {
    &pdf_thin_black_line (10,$top,10,$up) ; 
	&vertical_lines_table; # draw vertical_lines on prematurely ended table
	&pdf_initialise_page ;
    $up=282 ; 
	$top=$up; 
	&pdf_thin_black_line(10,$up,200,$up) ; 
    $across = 1 ;
    }

} #----------------------------------------------------------------------------------------------------
  
sub get_val {

my ($val,$cur) = @_ ;

my $disp_val = $val ;

if ($val eq '0.00') { 
	$disp_val = '' ; 
	}
elsif ($val) {
	$disp_val = $cur . ' ' . $val ;
	}

# &common_min_debug($val);

return($disp_val);

} #------------------------------------------------------------------------------------------
  
sub vertical_lines_table {

unless ($exclude_rate) {
	&pdf_thin_black_line ($blocks[2],$top,$blocks[2],$up) ; # vertical
	&pdf_thin_black_line ($blocks[3],$top,$blocks[3],$up) ; # vertical
	}

unless ($exclude_vat) {
	&pdf_thin_black_line ($blocks[4],$top,$blocks[4],$up) ; # vertical
	}

&pdf_thin_black_line ($blocks[5],$top,$blocks[5],$up) ; # vertical
&pdf_thin_black_line ($blocks[6],$top,$blocks[6],$up) ; # vertical

} #------------------------------------------------------------------------------------------
  
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/deliverynote/$pdfname' width='100%' height='100%' frameborder='0'></iframe>
            </td>
        </tr>
        </table>    
    </div>
</body>
</html>
ENDOFTEXT
#

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

use db ;
use db_min ;
use dialog ;
use today ;
use common ;
use pdf ;
use tabs ;

1;


sub pdf_page_header_del {

$page_num++ ;

# my $image1 = $page->gfx(1) ;
# my $image1_thumb = "$htmlpath/img/pdf_logo.jpg";
# $pdf_image1_file = $pdf->image_jpeg($image1_thumb);
# $image1->image($pdf_image1_file, $across/mm, $up/mm, 80/mm, 19/mm);

my $image1 = $page->gfx(1) ;
my $image1_thumb = "$htmlpath/img/pdf_logo.png";
$pdf_image1_file = $pdf->image_png($image1_thumb);
$image1->image($pdf_image1_file, 10/mm, $up/mm, 90/mm, 29/mm);

$up+=26 ;

&pdf_nl ;
&pdf_xx_large_bold_text(127,$up,'DELIVERY NOTE') ;
#&pdf_small_text_right (198,$up,"Interactive Television Africa (Pty) Ltd") ;

#&pdf_small_text_right (198,$up,"Co Reg: 2020/535585/07") ;

#&pdf_small_text_right (198,$up,"Vat Reg: 4280292691") ;
&pdf_nl ;
&pdf_nl ;
&pdf_small_bold_text (137,$up,'Date');
#&pdf_small_text_right (198,$up,"804 Hammet Crossing Office Park") ;
&pdf_nl ;
&pdf_small_bold_text (137,$up,'Order Nr');
#&pdf_small_text_right (198,$up,"2 Selborne Street, Johannesburg") ;

&pdf_nl ;
#&pdf_small_text_right (198,$up,"South Africa") ;
&pdf_nl ;

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