<?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');


$query_project_data = "select * from t_".$key_table_prefix."_project ; ";
$result_project_data = mysqli_query($dbconn, $query_project_data);
while($line_project_data = mysqli_fetch_array($result_project_data,MYSQLI_ASSOC))
{
	$project_data[$line_project_data["prj_id"]]=$line_project_data;
}




//============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 (Single Product) by Customer/Ref/Project 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["product"]=="")
{
	
}
else {
	
	$query_info="select * from t_".$key_table_prefix."_product where pdt_code = '".$_REQUEST["product"]."' ; ";
	$result_info =mysqli_query($dbconn,$query_info);
	
	if($line_info = mysqli_fetch_array($result_info,MYSQLI_ASSOC))
	{
		$criteria2="".$line_info["pdt_code"]." ".$line_info["pdt_name"];	
		
		if($line_info["pdt_uom"]!="")
		{
			$criteria2=$criteria2." (".$line_info["pdt_uom"].")";	
		}
		
	}
	

}




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

	




$i=0;


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



	$query_category="select *
				from t_".$key_table_prefix."_category
				 ; ";
	$result_category =mysqli_query($dbconn,$query_category);
	while($line_category = mysqli_fetch_array($result_category,MYSQLI_ASSOC))
	{
		$category_name[$line_category["cat_code"]]=$line_category["cat_name"];
	}

	
	$query_warehouse="select *
				from t_".$key_table_prefix."_warehouse
				 ; ";
	$result_warehouse =mysqli_query($dbconn,$query_warehouse);
	while($line_warehouse = mysqli_fetch_array($result_warehouse,MYSQLI_ASSOC))
	{
		$warehouse_name[$line_warehouse["whs_code"]]=$line_warehouse["whs_name"];
	}






/*
	$query_balance="select * from t_".$key_table_prefix."_balance
				";
	if($_REQUEST["warehouse"]!="")		 
	{
		$query_balance = $query_balance." where bal_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	$result_balance=mysqli_query($dbconn,$query_balance);
	while($line_balance=mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
	{
		$bal_data[$line_balance["bal_product_code"]] = $bal_data[$line_balance["bal_product_code"]] +  $line_balance["bal_quantity"];	
	}
	
	
	
	
	
	$query_transaction="select sum(stc_base_quantity) as total_quantity,
					stc_module ,
					stc_type ,
					stc_date,
					stc_warehouse_code	 
	 				from t_".$key_table_prefix."_stock_card
					where stc_date >= '".$_REQUEST["start"]."'  and stc_date <= '".$_REQUEST["end"]."'
					and stc_module = 'STOCK_OUT'
				";
	if($_REQUEST["warehouse"]!="")		 
	{
		$query_transaction = $query_transaction." and stc_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	if($_REQUEST["product"]!="")		 
	{
		$query_transaction = $query_transaction." and stc_stock_code = '".$_REQUEST["product"]."' ";
	}
	$query_transaction = $query_transaction." group by  stc_warehouse_code, stc_date , stc_module, stc_type  ";
	$query_transaction = $query_transaction." order by  stc_warehouse_code, stc_date  ";
	
	
	
	$result_transaction = mysqli_query($dbconn,$query_transaction);
	while($line_transaction = mysqli_fetch_array($result_transaction,MYSQLI_ASSOC))
	{
		$tran_data[$line_transaction["stc_warehouse_code"]][$line_transaction["stc_date"]][$line_transaction["stc_module"]][$line_transaction["stc_type"]] = $tran_data[$line_transaction["stc_warehouse_code"]][$line_transaction["stc_stock_code"]][$line_transaction["stc_module"]][$line_transaction["stc_type"]] +  $line_transaction["total_quantity"];	
		$tran_unique_date[$line_transaction["stc_warehouse_code"]][$line_transaction["stc_date"]] = $line_transaction["stc_date"];
		
		
	}
*/

	
	
	
	



	
	//$data[$i][0] = "";
	//$i++;
	
	$data_width_this_part[0]=20;
	$data_width_this_part[1]=20;
	$data_width_this_part[2]=20;
	$data_width_this_part[3]=100;
	$data_width_this_part[4]=20;
	$data_width_this_part[5]=10;
	
	
	$data_align_this_part[0]="L";
	$data_align_this_part[1]="L";
	$data_align_this_part[2]="L";
	$data_align_this_part[3]="L";
	$data_align_this_part[4]="R";
	$data_align_this_part[5]="L";
	
	//$data_width_this_part[9]=20;
	
	
	//---------------------------------------------------------------------------
	//title:
	/*
	$data_width[$i] = $data_width_this_part;
	$data_align[$i] = $data_align_this_part;
	
	$data_font_size[$i]="BIG";
	$data_bold[$i]="YES";
	$data[$i][0] = "Stock Balance";
	$i++;
	*/
	
	//$data[$i][0] = "<line>";
	//$i++;	
	
	//header----
	//$data_bold[$i]="YES";
	//$data_width[$i] = $data_width_this_part;
	//$data_align[$i] = $data_align_this_part;
	
	/*
	$data[$i][0] = "Product Code";
	$data[$i][1] = "Product Name";
	$data[$i][2] = "Sales";
	
	$data[$i][3] = "Return";
	$data[$i][4] = "Stock In";
	$data[$i][5] = "Stock Out";
	$data[$i][6] = "Transfer In";
	$data[$i][7] = "Transfer Out";
	$data[$i][8] = "Adjustment";
	*/
	
	$header_row[0] = "Warehouse";
	$header_row[1] = "Date";
	$header_row[2] = "Doc No.";
	
	$header_row[3] = "Detail (Ref/Remark/Project)";
	$header_row[4] = "Stock Out Qty";
	$header_row[5] = "UOM";
	
	$header_row_data_width = $data_width_this_part;
	$header_row_data_align = $data_align_this_part;
	
	
	
	
	
		
		
	//$data[$i][0] = "<line>";
	//$i++;	
	
	
	
	$query_pdt="select * from t_".$key_table_prefix."_product 
			where pdt_id> 0 
			";
			//where pdt_code = '".$_SESSION["BALANCE_PDT_CODE"]."'
	if($_REQUEST["pdt_code"]!="")
	{
		$query_pdt=$query_pdt." and pdt_code = '".$_REQUEST["product"]."' ";
	}	 
	$query_pdt=$query_pdt." order by product, pdt_name ";
	
	$result_pdt =mysqli_query($dbconn,$query_pdt);
	
	$sub_total_value=0;
	$temp_cat_name="";
	
	while($line_pdt = mysqli_fetch_array($result_pdt,MYSQLI_ASSOC))
	{
		
	}
	
	
	
	
	
	
	
	$query_transaction="select * 
	 				from 
					t_".$key_table_prefix."_stock_transaction_header, t_".$key_table_prefix."_stock_transaction_item
					where sth_id = sti_header_id
					and sth_date >= '".$_REQUEST["start"]."'  and sth_date <= '".$_REQUEST["end"]."'
					and sth_module = 'STOCK_OUT'
				";
	if($_REQUEST["warehouse"]!="")		 
	{
		$query_transaction = $query_transaction." and sth_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	if($_REQUEST["product"]!="")		 
	{
		$query_transaction = $query_transaction." and sti_stock_code = '".$_REQUEST["product"]."' ";
		
	}
	
	$query_transaction = $query_transaction." order by sth_date, sth_doc_no , sti_id ";
	
	
	$sub_total_warehouse=0;
	$sub_total_date=0;
	
	
	
	$result_transaction = mysqli_query($dbconn,$query_transaction);
	while($line_transaction = mysqli_fetch_array($result_transaction,MYSQLI_ASSOC))
	{
		
		/*
		if($line_transaction["sth_date"]!=$temp_date)
		{
			if($temp_date!="")
			{
			
				
				$data_width[$i] = $data_width_this_part;
				$data_align[$i] = $data_align_this_part;
				
				$data_bold[$i]="YES";
				$data[$i][0] = "";
				$data[$i][1] = "Sub-total for ".$temp_date."";
				$data[$i][2] = "";
				$data[$i][3] = "";
				$data[$i][4] = number_format(abs($sub_total_date),$decimal_count,".",","); 
				
				$counter++;
				$i++;	
				
				$sub_total_date=0;
				
				
			}
			
		}	
		*/
		
		
		
		if($line_transaction["sth_warehouse_code"]!=$temp_warehouse)
		{
			
			if($temp_warehouse!="")
			{
				
				$data_width[$i] = $data_width_this_part;
				$data_align[$i] = $data_align_this_part;
				
				$data_bold[$i]="YES";
				
				$data[$i][0] = "Total for ".$temp_warehouse." ".$warehouse_name[$temp_warehouse];
				$data[$i][1] = "";
				$data[$i][2] = "";
				$data[$i][3] = "";
				$data[$i][4] = number_format(abs($sub_total_warehouse),$decimal_count,".",","); 
				
				$counter++;
				$i++;
				
				$sub_total_warehouse=0;
				$sub_total_date=0;
				
				
				$data[$i][0] = "<line>";
				$i++;	
				
				
			}
			
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="YES";
			$data[$i][0] = $line_transaction["sth_warehouse_code"]." ".$warehouse_name[$line_transaction["sth_warehouse_code"]];
			
			$counter++;
			$i++;
			
			$sub_total_warehouse=0;
			$sub_total_date=0;
			
		}	
			
			
			
		
			
			
				
				
		     
		     
			$decimal_count = $line_pdt["pdt_decimal_quantity"]+0;	
			$decimal_count=3;

			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$this_project="";
			
			$query_project="select * 
				from t_".$key_table_prefix."_project_link 
				where plk_module='".$line_transaction["sth_module"]."'
				and plk_object_id = '".$line_transaction["sth_id"]."'  
				";
			$result_project = mysqli_query($dbconn, $query_project);
			if($line_project = mysqli_fetch_array($result_project,MYSQLI_ASSOC))
			{
				
				
					
				$this_project=$project_data[$line_project["plk_project_id"]]["prj_name"];
				
			}	
			
			
			
			
			
			
			$data[$i][0] = "";
			$data[$i][1] = $line_transaction["sth_date"];
			$data[$i][2] = $line_transaction["sth_doc_no"];
			$data[$i][3] = $line_transaction["sth_reference_no"]." ".$line_transaction["sth_remark"]." ".$this_project;
			$data[$i][4] = number_format(abs($line_transaction["sti_quantity"]),$decimal_count,".",",");
			$data[$i][5] = $line_transaction["sti_uom"];
			
			
			
			
			$sub_total_warehouse = $sub_total_warehouse +  abs($line_transaction["sti_quantity"]);
			$sub_total_date = $sub_total_date  +  abs($line_transaction["sti_quantity"]);
			
			$all_total =  $all_total +  abs($line_transaction["sti_quantity"]);
			
			$counter++;
			$i++;
			
			
			
			$temp_warehouse  = $line_transaction["sth_warehouse_code"];
			$temp_date  = $line_transaction["sth_date"];
		
	
		
	}	
	
	
	/*
	
	if($line_transaction["sth_date"]!=$temp_date)
	{
		if($temp_date!="")
		{
		
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="YES";
			$data[$i][0] = "";
			$data[$i][1] = "Sub-total for ".$temp_date."";
			$data[$i][2] = "";
			$data[$i][3] = "";
			$data[$i][4] = number_format(abs($sub_total_date),$decimal_count,".",","); 
			
			$counter++;
			$i++;	
			
			$sub_total_date=0;
		}
		
	}
	*/
	
	
	
	if($line_transaction["sth_warehouse_code"]!=$temp_warehouse)
	{
		
		if($temp_warehouse!="")
		{
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="YES";
			
			$data[$i][0] = "Total for ".$temp_warehouse." ".$warehouse_name[$temp_warehouse];
			$data[$i][1] = "";
			$data[$i][2] = "";
			$data[$i][3] = "";
			$data[$i][4] = number_format(abs($sub_total_warehouse),$decimal_count,".",","); 
			
			$counter++;
			$i++;
			
			$sub_total_warehouse=0;
			$sub_total_date=0;
			
			$data[$i][0] = "<line>";
			$i++;	
			
			
		}
		
		
	
	
		
		
	}	
		
		
		
	//----------------all total------------------------------------------------------	
		
	$data_width[$i] = $data_width_this_part;
	$data_align[$i] = $data_align_this_part;
	
	$data_bold[$i]="YES";
	
	$data[$i][0] = "Total";
	$data[$i][1] = "";
	$data[$i][2] = "";
	$data[$i][3] = "";
	$data[$i][4] = number_format(abs($all_total),$decimal_count,".",","); 
	
	$counter++;
	$i++;



	//---------------last line-------------------------------------------------
		
	$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,"Product","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,"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();




?>