<?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";


include $addon_path."plugin/phpqrcode/qrlib.php";



$addon_path="";


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


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


//formating-----------------------------------------------------------------------------------
$doc_paper_size=$_REQUEST["s"]; //A4 or Letter

//parameter-----------------------------------------------------------------------------------	
$selected_date_start_simple = $_REQUEST["start"];
$selected_date_end_simple = $_REQUEST["end"];


$selected_date_start=$selected_date_start_simple." 00:00:00";
$selected_date_end=$selected_date_end_simple." 23:59:59";



//title---------------------------------------------------------------------------------------
$doc_title="Stock Balance Report";




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



$total_column=5;

$i=1;
$query_info="select * from t_".$key_table_prefix."_product 
 		where pdt_id in (".implode($_SESSION["PDT_MARKING"],",").") ; ";
		
$result_info =mysqli_query($dbconn,$query_info);

while($line_info = mysqli_fetch_array($result_info,MYSQLI_ASSOC))
{
	
	if($line_info["pdt_barcode"]!="")
	{
		
		$product[$i] = $line_info["pdt_barcode"];
		$product_name[$i]=$line_info["pdt_name"];
		$i++;	
	}
	
}




$col_counter=0;
$row_counter=0;
foreach($product as $key => $value)
{
	
	if($col_counter>=$total_column)
	{
		$col_counter=0;
		$row_counter++;
	}
	
	$data[$row_counter][$col_counter] = $product[$key];
	$data_name[$row_counter][$col_counter] = $product_name[$key];
	
	$col_counter++;
}








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


$page_y_position=0;

//============printing start===================================================================
class PDF extends FPDF
{
		
		//Page header
		function Header()
		{
			//$this->Image("http://localhost/pcloud/p_server/pcartserver/plugin/phpqrcode/image.php?qt=".$data[$row_key][$c],10+(($col_width-30)/2)+($c*$col_width),5+($row_key*30),30,30,'png');
			
			global $page_y_position;
    		     $page_y_position=5;
		}

		//Page footer
		function Footer()
		{	
		 
			global $page_y_position;
			$page_y_position=5;
		 
		 	//$this->SetY(-10);
			//$this->Cell(10,5,"test","1",0,"C",$fill);	
		}
		

		
		function render_grid()
		{
			global $data,$data_name;	
			global $total_column;		
			global $page_y_position;
			
			$this->SetLineWidth(.3);
			$this->SetFillColor(245,245,245);
			$this->SetTextColor(0);
			$this->SetFont('Arial','',9);
			
			
			
			$fill=false;
			
			$col_width = round(190/$total_column);
			
		
			$page_y_position=5;
			
			foreach($data as $row_key=>$row)
			{
				
				
					
					
					if($this->y+40>$this->PageBreakTrigger)
					{
						$this->AddPage($this->CurOrientation,$this->CurPageFormat);
						$page_y_position=5;
					}
					
					
					
					for($c=0;$c<$total_column;$c++)
					{
						
						if($data[$row_key][$c]!="")
						{
								
								
							$this_img_x = 10+(($col_width-30)/2)+($c*$col_width);
							$this_img_y = $page_y_position+6;
							
							
							//$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
							
							
							/*
							ob_start();
							QRCode::png('1231231312312', null);
							$imageString = ob_get_contents();
							ob_end_clean();
							//echo $imageString;
							$this->Image($imageString,$this_img_x,$this_img_y,30,30,'qr');
							*/
							
							
							//$this->Image("http://localhost/pcloud/p_server/pcartserver/plugin/phpqrcode/image.php?qt=".$data[$row_key][$c],$this_img_x,$this_img_y,30,30,'png');
							//$this->Image("http://".$_SERVER[HTTP_HOST].str_replace("print/product_barcode/index.php","",$_SERVER[REQUEST_URI])."plugin/phpqrcode/image.php?qr=".$data[$row_key][$c],$this_img_x,$this_img_y,30,30,'jpg');
							//$this->Image("",$this_img_x,$this_img_y,30,30,'png');
							
							$filename=realpath("../../")."/qr_codes/".$data[$row_key][$c].".png";
							if (file_exists($filename)) {
							    
							    $this->Image($filename,$this_img_x,$this_img_y,30,30,'png');
							    
							} else {
							    
							    QRcode::png($data[$row_key][$c],realpath("../../")."/qr_codes/".$data[$row_key][$c].".png");
							    
							    $this->Image($filename,$this_img_x,$this_img_y,30,30,'png');
							}


							
							
							
							
							$this->Cell($col_width,28,"","TLR",0,"C",$fill);	
							
							
							
							//$this->Image("http://localhost/pcloud/p_server/pcartserver/plugin/phpqrcode/image.php?qt=".$data[$row_key][$c],10+(($col_width-30)/2)+($c*$col_width),null,30,30,'png');
							//$this->Image("http://localhost/pcloud/p_server/pcartserver/plugin/phpqrcode/image.php?qt=".$data[$row_key][$c],null,null,30,30,'png');
						}
					}
					$this->Ln();	
					
					
					
					$this->SetFont('Arial','B',9);
					for($c=0;$c<$total_column;$c++)
					{
						if($data[$row_key][$c]!="")
						{
							$this->Cell($col_width,4,$data[$row_key][$c],"LR",0,"C",$fill);			
						}
						
					}
					$this->Ln();	
					
					$this->SetFont('Arial','',7);
					for($c=0;$c<$total_column;$c++)
					{
						if($data[$row_key][$c]!="")
						{
							$this->Cell($col_width,4,$data_name[$row_key][$c],"LR",0,"C",$fill);		
						}
					}
					$this->Ln();	
					
					for($c=0;$c<$total_column;$c++)
					{
						if($data[$row_key][$c]!="")
						{
							$this->Cell($col_width,4,"","BLR",0,"C",$fill);		
						}
					}
					
					
					
					$page_y_position = $page_y_position + 40;
					$this->Ln();	
					
			}
			
		}

}

//Instanciation of inherited class


$pdf=new PDF();

$pdf->AliasNbPages();
$pdf->AddPage('P',$doc_paper_size);
$pdf->SetFont('Arial','',8);



$pdf->SetAutoPageBreak(true , 10);


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


$pdf->render_grid();



$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);
$pdf->Cell($w[0],7,$table_footer[0],"T",0,'R',true);
$pdf->Cell($w[1],7,$table_footer[1],"T",0,'R',true);
$pdf->Ln(7);
*/		    
		    
$pdf->Ln(10);
    
		    
$pdf->Output();




?>