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



//----------------------lib-----------------------------------------
$query_device = "select * from t_".$key_table_prefix."_device ";
$result_device = mysqli_query($dbconn, $query_device);
while($line_device = mysqli_fetch_array($result_device,MYSQLI_ASSOC))
{
	
	$lib_device[$line_device["dev_android_id"]] = $line_device["dev_name"];
	
}





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



if($_REQUEST["product"]=="")
{
	$criteria1="All 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))
	{
		$criteria1="".$line_info["pdt_code"]." ".$line_info["pdt_name"];	
	}
	

}


if($_REQUEST["warehouse"]=="")
{
	$criteria2="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))
	{
		$criteria2="".$line_info["whs_code"]." ".$line_info["whs_name"];	
	}
	
	
	
}

$criteria3 = $_REQUEST["start_date"]." ~ ".$_REQUEST["end_date"];

$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_balance="select sum(bal_quantity) as bal_quantity ,
				bal_product_code
				from t_".$key_table_prefix."_balance
				";
	if($_REQUEST["warehouse"]!="")		 
	{
		$query_balance = $query_balance." where bal_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	$query_balance = $query_balance." group by bal_product_code  ";
	
	
	$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"];	
		
	}




	
	//=======================stock card to calculate back until START date=============================
	$query_transaction_start="select sum(stc_base_quantity) as total_quantity,
					stc_stock_code,
					stc_type
					from t_".$key_table_prefix."_stock_card
					where stc_date > '".$_REQUEST["balance_date"]."'  
					";
	if($_REQUEST["warehouse"]!="")		 
	{
		$query_transaction_start = $query_transaction_start." and stc_warehouse_code = '".$_REQUEST["warehouse"]."' ";
	}
	
	$query_transaction_start = $query_transaction_start." group by  stc_stock_code ,stc_type ";
	$result_transaction_start = mysqli_query($dbconn,$query_transaction_start) or die("error");
	while($line_transaction_start = mysqli_fetch_array($result_transaction_start,MYSQLI_ASSOC))
	{
		if($line_transaction_start["stc_type"]=="PLUS")
		{
			
			$bal_data[$line_transaction_start["stc_stock_code"]] = $bal_data[$line_transaction_start["stc_stock_code"]] - abs($line_transaction_start["total_quantity"]);
			
		}
		
		if($line_transaction_start["stc_type"]=="MINUS")
		{
			$bal_data[$line_transaction_start["stc_stock_code"]] = $bal_data[$line_transaction_start["stc_stock_code"]] + abs($line_transaction_start["total_quantity"]);
		}
		
	}	
		
		
		
		








	
	$data[$i][0] = "";
	$i++;
	
	$data_width_this_part[0]=20;
	$data_width_this_part[1]=25;
	$data_width_this_part[2]=25;
	$data_width_this_part[3]=30;
	$data_width_this_part[4]=30;
	$data_width_this_part[5]=20;
	$data_width_this_part[6]=20;
	$data_width_this_part[7]=20;
	
	
	$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]="L";
	$data_align_this_part[5]="R";
	$data_align_this_part[6]="R";
	$data_align_this_part[7]="R";
	
	
	//---------------------------------------------------------------------------
	//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] = "Date";
	$data[$i][1] = "Type";
	$data[$i][2] = "Document";
	$data[$i][3] = "Color/Size/Pattern";
	$data[$i][4] = "Device";
	$data[$i][5] = "Plus(+)";
	$data[$i][6] = "Minus(-)";
	$data[$i][7] = "Balance";
	
	$i++;
		
		
	$data[$i][0] = "<line>";
	$i++;	
	
	
	
	
				
	$query_pdt="select * from t_".$key_table_prefix."_product  ";		
	$query_pdt=$query_pdt." where pdt_code = '".$_REQUEST["product"]."' ";
	$result_pdt =mysqli_query($dbconn,$query_pdt);
	if($line_pdt = mysqli_fetch_array($result_pdt,MYSQLI_ASSOC))
	{
		
		$this_product_decimal = 5;
		
		
		$this_balance =0 ;
		
		$query_balance="select 
					sum(bal_quantity) as total_quantity 
					from t_".$key_table_prefix."_balance
					where bal_product_code = '".$_REQUEST["product"]."'
					";
		if($_REQUEST["warehouse"]!="ALL")		 
		{
			$query_balance = $query_balance." and bal_warehouse_code = '".$_REQUEST["warehouse"]."' ";
		}
		
		
		$result_balance=mysqli_query($dbconn,$query_balance);
		if($line_balance=mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
		{
			$this_balance = $line_balance["total_quantity"];
			
			
		}
		
		
		$total_cards_quantity=0;
		
		$query_card="select 
				sum(abs(stc_base_quantity)) as total_quantity,
				stc_type
		
				from t_".$key_table_prefix."_stock_card 
				where  stc_date >= '".$_REQUEST["start_date"]."'
				and  stc_date <= '".$_REQUEST["end_date"]."'
				
				and stc_warehouse_code = '".$_REQUEST["warehouse"]."'
				and stc_stock_code = '".$_REQUEST["product"]."'
				
				group by stc_type 
				";
		
		$result_card =mysqli_query($dbconn,$query_card);
		while($line_card = mysqli_fetch_array($result_card,MYSQLI_ASSOC))
		{
			if($line_card["stc_type"]=="PLUS")
			{
				$total_cards_quantity  =  $total_cards_quantity + $line_card["total_quantity"];
			}
			
			if($line_card["stc_type"]=="MINUS")
			{
				$total_cards_quantity  =  $total_cards_quantity - $line_card["total_quantity"];
			}
			
			
		}
		
		
		$this_open_balance = $this_balance - $total_cards_quantity;
		$this_running_balance = $this_open_balance;
		
		
		
		
		//============openning balance row========================================		
				
		$data_width[$i] = $data_width_this_part;
		$data_align[$i] = $data_align_this_part;
		
		$data_bold[$i]="YES";
		$data[$i][0] = $_REQUEST["start_date"];
		$data[$i][1] = "Openning";
		$data[$i][2] = "";
		
		$data[$i][3] = "";
		$data[$i][4] = "";
		$data[$i][5] = "";
		$data[$i][6] = "";

		$data[$i][7] = number_format($this_open_balance,$this_product_decimal,".",",");
		
		$counter++;
		$i++;
		
		
		
		//============stock card row========================================		
		$query_card="select * from t_".$key_table_prefix."_stock_card 
				where  stc_date >= '".$_REQUEST["start_date"]."'
				and  stc_date <= '".$_REQUEST["end_date"]."'
				
				and stc_warehouse_code = '".$_REQUEST["warehouse"]."'
				and stc_stock_code = '".$_REQUEST["product"]."'
				
				order by stc_date , stc_id 
				";
		
		$result_card =mysqli_query($dbconn,$query_card);
		while($line_card = mysqli_fetch_array($result_card,MYSQLI_ASSOC))
		{
			
			$data_width[$i] = $data_width_this_part;
			$data_align[$i] = $data_align_this_part;
			
			$data_bold[$i]="";
			$data[$i][0] = $line_card["stc_date"];
			$data[$i][1] = $line_card["stc_module"];
			$data[$i][2] = $line_card["stc_doc_no"];
			
			
			if(($line_card["stc_color"]!="")or($line_card["stc_size"]!="")or($line_card["stc_pattern"]!=""))
			{
				$data[$i][3] = $line_card["stc_color"]." / ".$line_card["stc_size"]." / ".$line_card["stc_pattern"];
			}
			else {
				$data[$i][3] = "";
			}
			
			if(isset($lib_device[$line_card["device_id"]]))
			{
				$data[$i][4] = $lib_device[$line_card["device_id"]];
			}
			else {
				$data[$i][4] = $line_card["device_id"];
			}
			
			
			
			if($line_card["stc_type"]=="PLUS")
			{
				$data[$i][5] = number_format($line_card["stc_base_quantity"],$this_product_decimal,".",",");
				$data[$i][6] = "";
				$this_running_balance = $this_running_balance  + abs($line_card["stc_base_quantity"]);
			}
			
			if($line_card["stc_type"]=="MINUS")
			{
				$data[$i][5] = "";
				$data[$i][6] = number_format($line_card["stc_base_quantity"],$this_product_decimal,".",",");
				$this_running_balance = $this_running_balance  - abs($line_card["stc_base_quantity"]);
			}
			
	
			$data[$i][7] = number_format($this_running_balance,$this_product_decimal,".",",");
			
			$counter++;
			$i++;
			
			
		}
		
				
	}
			
			
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
	//footer----
	
	
	
	$data_width[$i] = $data_width_this_part;
	$data_align[$i] = $data_align_this_part;
	
	$data_bold[$i]="YES";
	$data[$i][0] = $_REQUEST["end_date"];
	$data[$i][1] = "Closing";
	$data[$i][2] = "";
	
	$data[$i][3] = "";
	$data[$i][4] = "";
	$data[$i][5] = "";
	$data[$i][6] = "";

	$data[$i][7] = number_format($this_running_balance,$this_product_decimal,".",",");
	
	$counter++;
	$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 ;
			 
		 	 
			//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,"Product","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,"Warehouse","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,"Period ","0",0,'L',true);
			$this->SetFont('Arial','',8);
			$this->Cell(160,5,": ".$criteria3,"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->SetTextColor(0);
			$this->SetFont('Arial','',9);
			
			
			
			$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)
						{
								$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();




?>