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

$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 Out By Customer Report";


if($_REQUEST["warehouse"]=="")
{
	$criteria1="All Warehouse";
}
else {
	
	$query_info="select * from t_".$key_table_prefix."_warehouse where whs_code = '".$_REQUEST["warehouse"]."' ; ";
	$result_info =mysqli_query($dbconn,$query_info);
	
	if($line_info = mysqli_fetch_array($result_info,MYSQLI_ASSOC))
	{
		$criteria1="".$line_info["whs_code"]." ".$line_info["whs_name"];	
	}
		
	
}

/*
if($_REQUEST["category"]=="")
{
	$criteria2="All Category";
}
else {
	
	$query_info="select * from t_".$key_table_prefix."_category where cat_code = '".$_REQUEST["category"]."' ; ";
	$result_info =mysqli_query($dbconn,$query_info);
	
	if($line_info = mysqli_fetch_array($result_info,MYSQLI_ASSOC))
	{
		$criteria2="".$line_info["cat_code"]." ".$line_info["cat_name"];	
	}
	

}
*/


if($_REQUEST["customer"]=="")
{
	$criteria3="All Customer";
}
else {
	
	
	$query_info="select * from t_".$key_table_prefix."_customer where cus_code = '".$_REQUEST["customer"]."' ; ";
	$result_info =mysqli_query($dbconn,$query_info);
	
	if($line_info = mysqli_fetch_array($result_info,MYSQLI_ASSOC))
	{
		$criteria3="".$line_info["cus_code"]." ".$line_info["cus_name"];	
	}
	


}



$criteria4="".$_REQUEST["start"]." to ".$_REQUEST["end"];	

	




$i=0;


//=================================================================================================



	$query_customer="select *
				from t_".$key_table_prefix."_customer
				 ; ";
	$result_customer =mysqli_query($dbconn,$query_customer);
	while($line_customer = mysqli_fetch_array($result_customer,MYSQLI_ASSOC))
	{
		$customer_name[$line_customer["cus_code"]]=$line_customer["cus_name"];
	}



	$query_product="select *
				from t_".$key_table_prefix."_product
				 ; ";
	$result_product =mysqli_query($dbconn,$query_product);
	while($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
	{
		$product_name[$line_product["pdt_code"]]=$line_product["pdt_name"];
		$product_decimal[$line_product["pdt_code"]]=$line_product["pdt_decimal_quantity"]+0;
	}




	//$data[$i][0] = "";
	//$i++;
	
	$data_width_this_part[0]=20;
	$data_width_this_part[1]=20;
	$data_width_this_part[2]=70;
	$data_width_this_part[3]=20;
	$data_width_this_part[4]=20;
	$data_width_this_part[5]=20;
	$data_width_this_part[6]=20;
	
	
	
	
	
	$data_align_this_part[0]="L";
	$data_align_this_part[1]="L";
	$data_align_this_part[2]="L";
	$data_align_this_part[3]="R";
	$data_align_this_part[4]="R";
	$data_align_this_part[5]="R";
	$data_align_this_part[6]="R";
	
	
	
	
	
	$header_row[0] = "Date";
	$header_row[1] = "Code";
	$header_row[2] = "Description";
	
	$header_row[3] = "Quantity";
	$header_row[4] = "Amount";
	$header_row[5] = "Tax";
	$header_row[6] = "Amount+Tax";
	
	
	$header_row_data_width = $data_width_this_part;
	$header_row_data_align = $data_align_this_part;
	
	
	
	
	
		
		
	//$data[$i][0] = "<line>";
	//$i++;	
	
	
	
	$query_sth = "select 
	
			sum(sti_before_gst_amount) as before_gst_amount,  
			sum(sti_gst_amount) as gst_amount, 
			sum(sti_total_amount) as total_amount, 
			sum(sti_quantity) as total_quantity, 
			
			
			sth_customer_code, 
			sth_date, 
			sti_stock_code 
			
	
		
			from	t_".$key_table_prefix."_stock_transaction_header , t_".$key_table_prefix."_stock_transaction_item
			where sth_status = 'POSTED' 
			and sth_module ='STOCK_OUT'
			and sth_id = sti_header_id
			
			and sth_date >= '".$selected_date_start_simple."'
			and sth_date <= '".$selected_date_end_simple."'
			
			";
	
	if($_REQUEST["customer"]!="")
	{
		$query_sth=$query_sth." and sth_customer_code = '".$_REQUEST["customer"]."' ";
	}
	
	if($_REQUEST["warehouse"]!="")
	{
		$query_sth=$query_sth." and sth_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	
	
	$query_sth=$query_sth." group  by sth_customer_code, sth_date , sti_stock_code  ";
	
	$query_sth=$query_sth." order by sth_customer_code, sth_date , sti_stock_code  ";
	
	$result_sth = mysqli_query($dbconn,$query_sth);
	//echo mysqli_error($dbconn);
	while($line_sth = mysqli_fetch_array($result_sth,MYSQLI_ASSOC))
	{
	
		if(($temp_header_code != $line_sth["sth_customer_code"])and($temp_header_code!=""))
		{
					
			
			
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="YES";
			$data[$i][0] = "";
			$data[$i][1] = "";
			$data[$i][2] = "Sub-Total";
			
		
			$data[$i][3] = number_format($sub_total_value1,5,".",",");	
			$data[$i][4] = number_format($sub_total_value2,2,".",",");	
			$data[$i][5] = number_format($sub_total_value3,2,".",",");	
			$data[$i][6] = number_format($sub_total_value4,2,".",",");	
			$i++;
			
			$sub_total_value1=0;
			$sub_total_value2=0;
			$sub_total_value3=0;
			$sub_total_value4=0;
			
			
			//--------------------------
			
			$data[$i][0] = "<line>";
			$i++;	
			
			
			
		}
		
		
		if($temp_header_code != $line_sth["sth_customer_code"])
		{
			
			
			$sub_total_value=0;
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="YES";
			$data[$i][0] = $line_sth["sth_customer_code"]." :  ".$customer_name[$line_sth["sth_customer_code"]];
			
			$counter++;
			$i++;
			
		}
	
	
		$decimal_count = $product_decimal[$line_sth["sti_stock_code"]];
		
		
		

		$data_width[$i] = $data_width_this_part;
		$data_align[$i] = $data_align_this_part;
		
		
		$data[$i][0] = $line_sth["sth_date"];
		$data[$i][1] = $line_sth["sti_stock_code"];
		$data[$i][2] = $product_name[$line_sth["sti_stock_code"]];
		
	
		$data[$i][3] = number_format($line_sth["total_quantity"],5,".",",");	
		$data[$i][4] = number_format($line_sth["before_gst_amount"],2,".",",");	
		$data[$i][5] = number_format($line_sth["gst_amount"],2,".",",");	
		$data[$i][6] = number_format($line_sth["total_amount"],2,".",",");	
		
		
		
		
		
		
		
		
		$counter++;
		$i++;
		
		$total_value1 = $total_value1 + ($line_sth["total_quantity"]);
		$total_value2 = $total_value2 + ($line_sth["before_gst_amount"]);
		$total_value3 = $total_value3 + ($line_sth["gst_amount"]);
		$total_value4 = $total_value4 + ($line_sth["total_amount"]);
		
		$sub_total_value1 = $sub_total_value1 + ($line_sth["total_quantity"]);
		$sub_total_value2 = $sub_total_value2 + ($line_sth["before_gst_amount"]);
		$sub_total_value3 = $sub_total_value3 + ($line_sth["gst_amount"]);
		$sub_total_value4 = $sub_total_value4 + ($line_sth["total_amount"]);
	
	
		$temp_header_code = $line_sth["sth_customer_code"];
		
		
		
	}	
	
	
	if(($temp_header_code!=""))
	{
		
		$data_width[$i] = $data_width_this_part;
		$data_align[$i] = $data_align_this_part;
		
		$data_bold[$i]="YES";
		$data[$i][0] = "";
		$data[$i][1] = "";
		$data[$i][2] = "Sub-Total";
		
	
		$data[$i][3] = number_format($sub_total_value1,5,".",",");	
		$data[$i][4] = number_format($sub_total_value2,2,".",",");	
		$data[$i][5] = number_format($sub_total_value3,2,".",",");	
		$data[$i][6] = number_format($sub_total_value4,2,".",",");	
		
		$i++;
		
		
		//--------------------------
		
		$data[$i][0] = "<line>";
		$i++;	
		
	}
	
	
	if(($temp_header_code!=""))
	{
		
		$data_width[$i] = $data_width_this_part;
		$data_align[$i] = $data_align_this_part;
		
		$data_bold[$i]="YES";
		$data[$i][0] = "";
		$data[$i][1] = "";
		$data[$i][2] = "Total";
		
	
		$data[$i][3] = number_format($total_value1,5,".",",");	
		$data[$i][4] = number_format($total_value2,2,".",",");	
		$data[$i][5] = number_format($total_value3,2,".",",");	
		$data[$i][6] = number_format($total_value4,2,".",",");	
		$i++;
		
		
		//--------------------------
		
		$data[$i][0] = "<line>";
		$i++;	
		
	}
	
		
	$data[$i][0] = "<line>";
	$i++;	
	
	
	
	






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






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




//============printing start===================================================================
class PDF extends FPDF
{
		
		//Page header
		function Header()
		{
			global $sys_system_master_setting_line;
			global $doc_debug_line;
			global $doc_title;
			global $selected_date_start_simple, $selected_date_end_simple;
		 	
			global $dbconn;
		 	global $table_header;
		 	global $key_table_prefix;
		 	global $data,$data_bold;
		 	 
			 global $criteria1, $criteria2, $criteria3,$criteria4;
			 
			 global $w, $align;
			 global $header_row_data_width;
			 global $header_row_data_align;
			 global $header_row;
		 	 
			//include '../letter_head.php';    
			//$this->Ln(5);
		
		    //line 1======================
			
		   	$this->SetFont('Arial','',13);
		   	$this->SetFont('','B');
			$this->Cell(190,5,$doc_title,$doc_debug_line,10,'L');
			$this->SetFont('','');
			$this->Ln(0);
		    
		  
			 
			//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

			
			
			$this->Ln(0);
			$this->SetFont('Arial','B',8);
			$this->Cell(30,5,"Warehouse","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".$criteria1,"0",0,'L',true);
			
			$this->Ln(4);
			$this->SetFont('Arial','B',8);
			$this->Cell(30,5,"Category","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".$criteria2,"0",0,'L',true);
			$this->Ln(4);
			
			$this->SetFont('Arial','B',8);
			$this->Cell(30,5,"Vendor","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".$criteria3,"0",0,'L',true);
			$this->Ln(4);
			
			
			$this->SetFont('Arial','B',8);
			$this->Cell(30,5,"Date","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".$criteria4,"0",0,'L',true);
			$this->Ln(4);
			
			 
			
			
			$w = $header_row_data_width;
			$align = $header_row_data_align;
			
			$this->Ln(5);
			$this->Cell($length,0,"","B",0,'L',$fill);
			$this->Ln();
			
			$this->SetFont('Arial','B',7);
			foreach($header_row as $col => $col_value)
			{
				
				
					$this->SetTextColor(0,0,0);
					$this->Cell($w[$col],5,"".$header_row[$col],"0",0,$align[$col],$fill);
				
				
				
			}
			$this->Ln(5);
			$this->Cell($length,0,"","B",0,'L',$fill);
			$this->Ln(0);
			
			
			
			/*
			
			$this->SetFont('Arial','B',8);
			$this->Cell(30,5,"Balance as at","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".date("Y-m-d H:i:s"),"0",0,'L',true);
			$this->Ln(4);
			
			*/
		    
		    
		}

		//Page footer
		function Footer()
		{	
		 
			global $doc_time_stamp,$doc_user,$doc_print_count;
		 	
			//Position at 1.5 cm from bottom
			$this->SetY(-15);
		    
			//Arial italic 8
			$this->SetFont('Arial','I',8);
			
			//Footer (Page Number / timestamp)
			$this->Cell(100,10,"Printed on ".$doc_time_stamp." ".$doc_user.'',0,0,'L');
			$this->Cell(90,10,"Page: ".$this->PageNo()." of {nb}",0,0,'R');
		    
		}
		

		
		function render_grid()
		{
			global $data;			
			global $data_bold;
			global $data_font_size;
			global $data_width;
			global $data_align;
			
			
			
			
			
			$this->SetLineWidth(.3);
			$this->SetFillColor(245,245,245);
			
			$this->SetFont('Arial','',9);
			
			
			//$this->Ln(5);
			$fill=false;
			foreach($data as $key=>$row)
			{
				
				$w = $data_width[$key];
				$align = $data_align[$key];
				
				
				
				
				if($row[0]=="<line>")
				{
					if($row[0]!="")
					{
						$length = $row[1];	
					}
					else {
						$length = 190;
					}
					
					$fill=false;
					$this->Cell($length,0,"","B",0,'L',$fill);
					$this->Ln();	
					$item_text_line++;
				}
				else
				{
					
					if($data_bold[$key]=="YES")
					{
						$this->SetFont('Arial','B',7);
						if($data_font_size[$key] =="SMALL")
						{
							$this->SetFont('Arial','B',6);	
						}
						if($data_font_size[$key] =="BIG")
						{
							$this->SetFont('Arial','B',10);	
						}
					}
					else
					{
						$this->SetFont('Arial','',7);
						if($data_font_size[$key] =="SMALL")
						{
							$this->SetFont('Arial','',6);	
						}
						if($data_font_size[$key] =="BIG")
						{
							$this->SetFont('Arial','',10);	
						}
					}
					
					
					
					
					foreach($row as $col => $col_value)
					{
						//if($w[$col]>0)
						{
							
							if(($row[$col]=="0") or ($row[$col]=="0.0") or ($row[$col]=="0.00") or ($row[$col]=="0.000"))
							{
								$this->SetTextColor(230,230,230);
							}
							else {
								$this->SetTextColor(0,0,0);
							}
							
							$this->Cell($w[$col],5,$row[$col],"0",0,$align[$col],$fill);
						}
						
						
					}
					
					$this->Ln();	
					
							
					$item_text_line++;
					//$fill=!$fill;
				}
							
				
					
			
			
			  
			}
			//$this->Cell(array_sum($w),0,'','T');
		}

}

//Instanciation of inherited class


$pdf=new PDF();

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





$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();




?>