<?php

$addon_path="../../";
include $addon_path."includes/config.php";
include $addon_path."includes/database.php";
include $addon_path."includes/keyword.php";
include $addon_path."includes/function.php";
include $addon_path."includes/session.php";

$addon_path="";


require('../fpdf16/fpdf.php');


//============Date Gathering start===================================================================


//formating-----------------------------------------------------------------------------------
$doc_paper_size=$_REQUEST["s"]; //A4 or Letter
if($_REQUEST["s"]=="A4")
{
	$doc_table_format=''; //COLOR or blank
}
else
{
	$doc_table_format=''; //COLOR or blank
}


//header--------------------------------------------------------------------------------------


$query_doc="select * from t_".$key_table_prefix."_invoice_header where ivh_id ='".$_REQUEST["ivh_id"]."' ";
$result_doc=mysqli_query($dbconn,$query_doc);
if($line_doc=mysqli_fetch_array($result_doc,MYSQLI_ASSOC))
{
	
	//$doc_title="Sales Issue Note";
	$doc_title="Picking List";
	
	
	//left panel-----------------------------------------------------------------------------------
	
        
        $address_array = explode(chr(10),$line_doc["ivh_customer_address"]);
        
        
	$doc_header_1_label="Deliver To :";
	$doc_header_1_value="".$line_doc["ivh_customer_name"];
	
	$doc_header_2_label="";
	$doc_header_2_value="".$address_array[0];
	
	$doc_header_3_label="";
	$doc_header_3_value="".$address_array[1];
	
	$doc_header_4_label="";
	$doc_header_4_value="".$address_array[2];
	
        $doc_header_5_label="";
	$doc_header_5_value="".$address_array[3]." ".$address_array[4]." ".$address_array[5];
	
	
	//right panel-----------------------------------------------------------------------------------
	
	$doc_info_1_label="Document No.";
	$doc_info_1_value=$line_doc["ivh_doc_no"];
	
	$doc_info_2_label="Date";
	$doc_info_2_value=$line_doc["ivh_date"];
	
	$doc_info_3_label="Page";
	$doc_info_3_value="of";
	
	$doc_info_4_label="Reference No";
	$doc_info_4_value=$line_doc["ivh_reference_no"];



	//Invoice Item--------------------------------------------------------------------------
	$table_header=array('No.','Item Description','Quantity','UOM','Unit Price(RM)','GST Code','GST(RM)','Total(RM)');
	
	$i=1;
	$total_amount=0;
	$query_doc_item="select * 
			
			from t_".$key_table_prefix."_invoice_item 
			where ivi_doc_no = '".$line_doc["ivh_doc_no"]."'
			order by ivi_id  ; ";
	$result_doc_item=mysqli_query($dbconn,$query_doc_item);
	while($line_doc_item=mysqli_fetch_array($result_doc_item,MYSQLI_ASSOC))
	{
	 	
		
		$data[$i]=array($i,
                                $line_doc_item["ivi_product_code"]." - ".$line_doc_item["ivi_product_name"].chr(10),
				
                                ($line_doc_item["ivi_quantity"]+0),
				$line_doc_item["ivi_uom"],
				
                                number_format($line_doc_item["ivi_price"]+0,2,".",","),
                                $line_doc_item["ivi_gst_code"],
				number_format($line_doc_item["ivi_gst_amount"]+0,2,".",","),
				number_format($line_doc_item["ivi_total_amount"]+0,2,".",",")
				
				
                                );
		//$data2[$i]=array("","","".$line_doc_item["ivi_product_description"],"","");	
		
			
		$i++;
	
	}
	

	$table_footer=array('Item Total',$line_doc["ivh_before_gst_amount"],'');
	//$table_footer2=array('Tax',$line_doc["ivh_tax_amount"],'');
	$table_footer3=array('GST',$line_doc["ivh_gst_amount"],'');
	$table_footer4=array('Total',$line_doc["ivh_amount"],'');
	
	

}

//bottom----------------------------------------------------------------------------------------
$doc_signature_area_left="";
$doc_signature_area_middle="";
$doc_signature_area_right="Issued by";

$doc_time_stamp=date("d-m-Y H:m:s");
$doc_user=$CurrentUserFullName;




//technical--------------------------------------------------------------------------------------
$doc_debug_line=0;

//============printing start===================================================================

class PDF extends FPDF
{
		
		//Page header
		function Header()
		{
			
			global $doc_branch;
		 	global $doc_debug_line;
			global $doc_title;
		 	
		 	global $table_header;
		 	 global $key_table_prefix;
		 	global $sys_system_master_setting_line;
		 	 
			 
			global $doc_header_1_value, $doc_header_1_label;
		 	global $doc_header_2_value, $doc_header_2_label;		 	 
		 	global $doc_header_3_value, $doc_header_3_label;
		 	global $doc_header_4_value, $doc_header_4_label;
		 	global $doc_header_5_value, $doc_header_5_label;
			
			
		 	 
		 	global $doc_info_1_value, $doc_info_1_label;
		 	global $doc_info_2_value, $doc_info_2_label;		 	 
		 	global $doc_info_3_value, $doc_info_3_label;
		 	global $doc_info_4_value, $doc_info_4_label;
		 	global $doc_info_5_value, $doc_info_5_label;
		 	 
		 	 
			include '../letter_head.php';    
		    $doc_debug_line=0;
		    
		    $this->Ln(0);
		
		
		    //line 1======================
			$this->SetFont('Arial','',8);
			$this->SetFont('','I');
			$this->Cell(130,8,'',$doc_debug_line,0,'BL');
		   	$this->SetFont('Arial','',16);
		   	$this->SetFont('','B');
			$this->Cell(60,8,$doc_title,$doc_debug_line,10,'R');
			$this->SetFont('','');
			$this->Ln(0);
		    
		    //line 2======================
			$this->SetFont('Arial','',8); 
			$this->SetFont('','');
			$this->Cell(30,4,$doc_header_1_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(90,4,$doc_header_1_value,$doc_debug_line,0,'L');
			
			$this->SetFont('','');
			$this->Cell(30,4,$doc_info_1_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(40,4,": ".$doc_info_1_value,$doc_debug_line,10,'L');
			$this->Ln(0);
		   
		    
		    //line 3======================
			$this->SetFont('Arial','',8); 
			$this->Cell(30,4,$doc_header_2_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(90,4,$doc_header_2_value,$doc_debug_line,0,'L');
			
			$this->SetFont('','');
			$this->Cell(30,4,$doc_info_2_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(40,4,": ".$doc_info_2_value,$doc_debug_line,10,'L');
			$this->Ln(0);
		   	
		   	
		    //line 4======================
			$this->SetFont('Arial','',8); 
			$this->Cell(30,4,$doc_header_3_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(90,4,$doc_header_3_value,$doc_debug_line,0,'L');
			
			$this->SetFont('','');
			$this->Cell(30,4,$doc_info_3_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(40,4,": ".$this->PageNo().' '.$doc_info_3_value.' {nb}',$doc_debug_line,10,'L');
			$this->Ln(0);
		
		    //line 4======================
			$this->SetFont('Arial','',8); 
			$this->Cell(30,4,$doc_header_4_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(90,4,$doc_header_4_value,$doc_debug_line,0,'L');
			
			$this->SetFont('','');
			$this->Cell(30,4,$doc_info_4_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(40,4,": ".$doc_info_4_value,$doc_debug_line,10,'L');
			$this->Ln(0);
			
		
			//line 5======================
			$this->SetFont('Arial','',8); 
			$this->Cell(30,4,$doc_header_5_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');
			$this->Cell(90,4,$doc_header_5_value,$doc_debug_line,0,'L');
			
			$this->SetFont('','');
			$this->Cell(30,4,$doc_info_5_label,$doc_debug_line,0,'L');
			$this->SetFont('','B');			
			$this->Cell(40,4,"".$doc_info_5_value,$doc_debug_line,10,'L');
			$this->Ln(1);

			 
			 
			//Draw Line Item Table Header====================================== 
		 	$this->SetFillColor(255,255,255);
		    $this->SetTextColor(0);
			//$this->SetDrawColor(128,0,0);
		    $this->SetDrawColor(0,0,0);		    
		    
		    $this->SetLineWidth(.3);
		    $this->SetFont('','B');
		    //Header

//------> header width:		    
		    $w=array(10,60,20,20,20,20,20,20);
		    $this->Cell($w[0],5,$table_header[0],"B",0,'L',true);
		    $this->Cell($w[1],5,$table_header[1],"B",0,'L',true);
		    $this->Cell($w[2],5,$table_header[2],"B",0,'R',true);
		    $this->Cell($w[3],5,$table_header[3],"B",0,'L',true);
		    $this->Cell($w[4],5,$table_header[4],"B",0,'R',true);
		    $this->Cell($w[5],5,$table_header[5],"B",0,'R',true);
		    $this->Cell($w[6],5,$table_header[6],"B",0,'R',true);
		    $this->Cell($w[7],5,$table_header[7],"B",0,'R',true);
		    $this->Ln(7);
		    
		    
		}

		//Page footer
		function Footer()
		{	
		 
		     global $doc_time_stamp,$doc_user,$doc_print_count;
		 	
		    //Position at 1.5 cm from bottom
			$this->SetY(-8);
		
		    
		    
		    //Arial italic 8
		    $this->SetFont('Arial','I',8);
		    //Page number
		    $this->Cell(0,5,"Issued on ".$doc_time_stamp." by ".$doc_user.'',0,0,'R');
		    
		    
		    
		     
		    if($doc_print_count>1)
		    {
			$this->Cell(0,5,"Re-print: Issued on ".$doc_time_stamp." by ".$doc_user.'',0,0,'R');
		    }
		    else{
			$this->Cell(0,5,"Issued on ".$doc_time_stamp." by ".$doc_user.'',0,0,'R');
		    }
		    
		    
		    
		}
		

		
		//Load data
		function LoadData($file)
		{
		    //Read file lines
		    $lines=file($file);
		    $data=array();
		    foreach($lines as $line)
		        $data[]=explode(';',chop($line));
		    return $data;
		}
/*
		//Simple table
		function BasicTable($header,$data)
		{
		    //Header
		    foreach($header as $col)
		        $this->Cell(40,7,$col,1);
		    $this->Ln();
		    //Data
		    foreach($data as $row)
		    {
		        foreach($row as $col)
		            $this->Cell(40,6,$col,1);
		        $this->Ln();
		    }
		}
				
		//Better table
		function ImprovedTable($table_header,$data)
		{
		    //Column widths
		    $w=array(40,35,40,45);
		    //Header
		    for($i=0;$i<count($table_header);$i++)
		        $this->Cell($w[$i],7,$table_header[$i],1,0,'C');
		    $this->Ln();
		    //Data
		    foreach($data as $row)
		    {
		        $this->Cell($w[0],6,$row[0],'LR');
		        $this->Cell($w[1],6,$row[1],'LR');
		        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
		        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
		        $this->Ln();
		    }
		    //Closure line
		    $this->Cell(array_sum($w),0,'','T');
		}
*/
		//Colored table
		function FancyTable($table_header,$data)
		{
		    //Colors, line width and bold font
		    $this->SetFillColor(255,0,0);
		    $this->SetTextColor(255);
		    $this->SetDrawColor(128,0,0);
		    $this->SetLineWidth(.3);
		    $this->SetFont('','B');
		    $w=array(10,100,20,20,20,20);
		    //Header
		    /*
		    
		    for($i=0;$i<count($table_header);$i++)
		        $this->Cell($w[$i],7,$table_header[$i],1,0,'C',true);
		    
		    */
		    //$this->Ln();
		    //Color and font restoration
		    $this->SetFillColor(224,235,255);
		    $this->SetTextColor(0);
		    $this->SetFont('');
		    //Data
		    $fill=false;
		    foreach($data as $row)
		    {
		        $this->Cell($w[0],4,$row[0],'LR',0,'L',$fill);
		        $this->Cell($w[1],4,$row[1],'LR',0,'L',$fill);
		        $this->Cell($w[2],4,$row[2],'LR',0,'C',$fill);
		        $this->Cell($w[3],4,$row[3],'LR',0,'R',$fill);
		        $this->Cell($w[4],4,$row[4],'LR',0,'R',$fill);
                        $this->Cell($w[5],4,$row[5],'LR',0,'R',$fill);
		        $this->Ln();
		        $fill=!$fill;
		    }
		    $this->Cell(array_sum($w),0,'','T');
		}
//Colored table
		function FancyTable_non_color($table_header,$data)
		{
			
			
			
			global $data2;
		    //Colors, line width and bold font
		   // $this->SetFillColor(255,0,0);
		    //$this->SetTextColor(255);
		    //$this->SetDrawColor(128,0,0);
		    $this->SetLineWidth(.3);
		    $this->SetFont('','B');
		   	$w=array(10,60,20,20,20,20,20,20);
		    //Header
		    /*
		    
		    for($i=0;$i<count($table_header);$i++)
		        $this->Cell($w[$i],7,$table_header[$i],1,0,'C',true);
		    
		    */
		    //$this->Ln();
		    //Color and font restoration
		    $this->SetFillColor(224,235,255);
		    $this->SetTextColor(0);
		    $this->SetFont('');
		    //Data
		    $fill=false;
		    foreach($data as $key=>$row)
		    {
		        $item_text_line=0;
			$product_text_array = explode(chr(10),$row[1]);
			foreach($product_text_array as $product_text_value)
			{
				
				if($product_text_value!="")
				{
					if($item_text_line==0)
					{
						
						$this->Cell($w[0],4,$row[0],"0",0,'L',$fill);
						$this->Cell($w[1],4,$product_text_value,"0",0,'L',$fill);
						$this->Cell($w[2],4,$row[2],"0",0,'R',$fill);
						$this->Cell($w[3],4,$row[3],"0",0,'L',$fill);
						$this->Cell($w[4],4,$row[4],"0",0,'R',$fill);
						$this->Cell($w[5],4,$row[5],"0",0,'R',$fill);
						$this->Cell($w[6],4,$row[6],"0",0,'R',$fill);
						$this->Cell($w[7],4,$row[7],"0",0,'R',$fill);
						$this->Ln();	
						
					}else
					{
						$this->Cell($w[0],4,"","0",0,'L',$fill);
						$this->Cell($w[1],4,$product_text_value,"0",0,'L',$fill);
						
						$this->Ln();	
						
					}
				
					
					
					$item_text_line++;
				}
			}
			
			
			
			
		     //   $fill=!$fill;
		    }
		    $this->Cell(array_sum($w),0,'','T');
		}

}

//Instanciation of inherited class


$pdf=new PDF();

$pdf->AliasNbPages();



$doc_paper_size="A4";
$pdf->AddPage('P',$doc_paper_size);
$pdf->SetFont('Arial','',8);

$pdf->SetAutoPageBreak(true,10);



$pdf->SetFont('Arial','',8);

if($doc_table_format=="COLOR")
{
 $pdf->FancyTable($table_header,$data);
}
else
{
 $pdf->FancyTable_non_color($table_header,$data);
}



$pdf->Ln();
//Draw Line Item Table Header====================================== 
$pdf->SetFillColor(255,255,255);
$pdf->SetTextColor(0);
    //$this->SetDrawColor(128,0,0);
$pdf->SetDrawColor(0,0,0);		    

$pdf->SetLineWidth(.3);
$pdf->SetFont('','B');
//Header
$w=array(170,20,20);
$pdf->Cell($w[0],5,$table_footer[0],"T",0,'R',true);
$pdf->Cell($w[1],5,number_format($table_footer[1]+0,2,".",","),"T",0,'R',true);
//$pdf->Cell($w[2],5,"","T",0,'R',true);
$pdf->Ln(5);
/*
$pdf->Cell($w[0],5,$table_footer2[0],"0",0,'R',true);
$pdf->Cell($w[1],5,number_format($table_footer2[1]+0,2,".",","),"0",0,'R',true);
$pdf->Cell($w[2],5,"","0",0,'R',true);
$pdf->Ln(5);
*/

$pdf->Cell($w[0],5,$table_footer3[0],"0",0,'R',true);
$pdf->Cell($w[1],5,number_format($table_footer3[1]+0,2,".",","),"0",0,'R',true);
//$pdf->Cell($w[2],5,"","0",0,'R',true);
$pdf->Ln(5);
$pdf->Cell($w[0],5,$table_footer4[0],"0",0,'R',true);
$pdf->Cell($w[1],5,number_format($table_footer4[1]+0,2,".",","),"BT",0,'R',true);
//$pdf->Cell($w[2],5,"","0",0,'R',true);


$pdf->Ln(7);
		    
		    
		    
$pdf->Ln(0);

		    
//bottom area:



	    
		    
//bottom area:
$pdf->Ln(0);


$pdf->Cell(10,5,'',0,0,'C');
$pdf->Cell(50,5,"Acknowledged by","0",0,'L');
$pdf->Cell(60,5,'',0,0,'C');  
$pdf->Cell(60,5,"Issued by","0",0,'L');
 $pdf->Cell(20,5,'',0,0,'C');  
	 		    

$pdf->Ln(18);
	
	
$pdf->Cell(10,4,'',0,0,'C');  
$pdf->Cell(50,4,"Signature","T",0,'L');	  
$pdf->Cell(60,4,'',0,0,'C');
$pdf->Cell(60,4,"Signature","T",0,'L');
$pdf->Cell(20,4,'',0,0,'C'); 
	 		    
$pdf->Ln(5);

$pdf->Cell(10,3,'',0,0,'C');  
$pdf->Cell(50,3,"Date:","0",0,'L');	  
$pdf->Cell(60,3,'',0,0,'C');
$pdf->Cell(60,3,"Date: ","0",0,'L');
$pdf->Cell(20,3,'',0,0,'C'); 
		    
		    
		    
		    
$pdf->Output();




?>