<?php


include "start.php";
include "../includes/function.php";

$CurrentUserID=0;


$main_array = array();
$save_array = array();


$query_get="select * from t_".$key_table_prefix."_user_device
	    where dusr_id='".$_REQUEST["u"]."'
	    and dusr_session_id ='".$_REQUEST["s"]."'
		";


$result_get = mysqli_query($dbconn, $query_get) or die("Error".mysqli_error($dbconn));
if($line_get = mysqli_fetch_array($result_get,MYSQLI_ASSOC))
{
	$this_data = array(
		"status" => "YES",
		"message" => "Active"
	     );

	   array_push($main_array, $this_data);
	    
	    
	    
	    
	    
	     //=================================plus type===========================================
		    
	   	$this_doc_prefix="ST";
	   	$this_doc_number_module_code="ST"; //stock transfer
	   	$this_module="STOCK_TRANSFER";
   
   
	   	$this_inventory_posting_type="MINUS";
	   	$this_inventory_posting_type2="PLUS";
	   
	   	$this_warehouse_from_id=0;
	   	$this_warehouse_to_id=0;
	   
	   
		
		$first_entry=true;
		   
	   
	   
	   
	   	$query_get_entry="select * from  t_".$key_table_prefix."_delegate_entry
				where ent_session = '".$_REQUEST["session"]."'
				and device_id = '".$_REQUEST["d"]."'
				and ent_type = 'PLUS'
				
		    		";
		$result_get_entry = mysqli_query($dbconn, $query_get_entry) or die("Error".mysqli_error($dbconn));
		while($line_get_entry = mysqli_fetch_array($result_get_entry,MYSQLI_ASSOC))
		{
			
			if($first_entry)
			{
				$first_entry=false;
				
				
				
				$this_posting_date = $line_get_entry["ent_date"];
				
				
				//get info:
				$query_whs="select * from t_".$key_table_prefix."_warehouse 
				     where whs_code  ='".$line_get_entry["ent_warehouse_code_from"]."' ";
				$result_whs  = mysqli_query($dbconn,$query_whs);
				if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
				{
				     $this_warehouse_from_id = $line_whs["whs_id"];
				}
				
				//get info:
				$query_whs="select * from t_".$key_table_prefix."_warehouse 
				     where whs_code  ='".$line_get_entry["ent_warehouse_code_to"]."' ";
				$result_whs  = mysqli_query($dbconn,$query_whs);
				if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
				{
				     $this_warehouse_to_id = $line_whs["whs_id"];
				}
				
				
				
				
				$this_doc_no = mon_register_document_number($this_doc_number_module_code);
				
				
				
				
				$query="insert into t_".$key_table_prefix."_stock_transaction_header 
						(
						sth_doc_prefix  ,
						sth_doc_no  ,
						sth_module  ,
						sth_date ,
						
						sth_warehouse_code  ,
						sth_warehouse_id  ,
						
						sth_warehouse_code2  ,
						sth_warehouse_id2  ,
						
						sth_customer_code  ,
						sth_customer_name  ,
						sth_vendor_code  ,
						sth_vendor_name  ,
						sth_reference_no  ,
						sth_remark  ,
						
						sth_category_code  ,
						
						
						sth_status  ,
						sth_before_gst_amount ,
						sth_gst_amount  ,
						sth_total_amount  ,
						
						
						sth_driver_code, 
						sth_staff1_code, 
						sth_staff2_code, 
						
						created_date  ,
						created_by  ,
						modified_date  ,
						modified_by  ,
						client_id  
						)
						values
						(
						'".$this_doc_prefix."'  ,
						'".$this_doc_no."'  ,
						'".$this_module."'  ,
						'".$this_posting_date."' ,
						
						'".$line_get_entry["ent_warehouse_code_from"]."' ,
						'".$this_warehouse_from_id."'  ,
						
						'".$line_get_entry["ent_warehouse_code_to"]."' ,
						'".$this_warehouse_to_id."'  ,
						
						
						'' ,
						''  ,
						'' ,
						''  ,
						'Daily Stock Delegation'  ,
						'Daily Stock Delegation'  ,
						
						'".$_REQUEST["ent_category"]."'  ,
						
						
						'POSTED'  ,
						'0' ,
						'0' ,
						'0' ,
						
						'".$line_get_entry["ent_driver"]."' ,
						'".$line_get_entry["ent_staff1"]."' ,
						'".$line_get_entry["ent_staff2"]."' ,
						
						
						'".date("Y-m-d H:i:s")."'  ,
						'".$CurrentUserID."'  ,
						'".date("Y-m-d H:i:s")."'  ,
						'".$CurrentUserID."'  ,
						'0'  	
						)
				
					"; 
				$result = mysqli_query($dbconn,$query) or die("Error insert Header".mysqli_error($dbconn));
				$header_id  = mysqli_insert_id($dbconn);
				
				
			}
			
			
			
			
				
				$query_product = "select * 
							from t_".$key_table_prefix."_product 
							where pdt_id = '".$line_get_entry["ent_product_id"]."' ";
				$result_product = mysqli_query($dbconn,$query_product);
				if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
				{
					
					
					
					
					$query_item="insert into t_".$key_table_prefix."_stock_transaction_item 
							(
								
							  sti_header_id  ,
							  sti_doc_prefix  ,
							  sti_doc_no  ,
							  sti_module  ,
							  sti_date  ,
							  sti_warehouse_code  ,
							  sti_warehouse_id  ,
							  
							  sti_warehouse_code2  ,
							  sti_warehouse_id2  ,
							  
							  
							  sti_stock_code  ,
							  sti_barcode  ,
							  sti_factory_code  ,
							  sti_stock_name  ,
							  sti_color  ,
							  sti_size  ,
							  sti_pattern  ,
							  sti_uom  ,
							  sti_base_uom  ,
							  sti_quantity  ,
							  sti_base_quantity  ,
							  sti_conversion  ,
							  sti_unit_cost  ,
							  sti_base_unit_cost  ,
							  sti_total_cost  ,
							  sti_gst_mode  ,
							  sti_gst_code  ,
							  sti_gst_percentage  ,
							  sti_remark  ,
							  sti_status  ,
							  sti_before_gst_amount  ,
							  sti_gst_amount  ,
							  sti_total_amount  ,
							  
							  sti_storage_location  ,
							  sti_storage_location2  ,
							  
							  
							  created_date  ,
							  created_by  ,
							  modified_date  ,
							  modified_by  ,
							  client_id  
							)
							values
							(
							'".$header_id."'  ,
							
							'".$this_doc_prefix."'  ,
							'".$this_doc_no."'  ,
							'".$this_module."'  ,
							'".$this_posting_date."' ,
							'".$line_get_entry["ent_warehouse_code_from"]."' ,
							'".$this_warehouse_from_id."'  ,
							
							'".$line_get_entry["ent_warehouse_code_to"]."' ,
							'".$this_warehouse_to_id."'  ,
							
							
							  '".$line_product["pdt_code"]."'  ,
							  '".$line_product["pdt_barcode"]."'  ,
							  ''  ,
							  '".$line_product["pdt_name"]."'  ,
							  ''  ,
							  ''  ,
							  ''  ,
							  '".$line_product["pdt_uom"]."'  ,
							  '".$line_product["pdt_uom"]."'  ,
							  '".($line_get_entry["ent_quantity"]+0)."' ,
							  '".($line_get_entry["ent_quantity"]+0)."' ,
							  '1'  ,
							  '".($line_product["pdt_cost"]+0)."'  ,
							  '".($line_product["pdt_cost"]+0)."'  ,
							  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
							  ''  ,
							  ''  ,
							  '".(0)."'  ,
							  ''  ,
							  'POSTED'  ,
							  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
							  '".(0)."'  ,
							  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
							  
							  
							  ''  ,
							  ''  ,
							  
							  '".date("Y-m-d H:i:s")."'  ,
							  
							  '".$CurrentUserID."'  ,
							  '".date("Y-m-d H:i:s")."'  ,
							  '".$CurrentUserID."'  ,
							  '0'  	
								
							);
							";
					
					$result_item = mysqli_query($dbconn,$query_item) or die("Error Insert ITEM".mysqli_error($dbconn));
					$last_item_id = mysqli_insert_id($dbconn);
					
					
					
					//------------------------Part 1: minus:-------------------------
					post_inventory_balance(
							'SERVER',
							$this_posting_date,
							$this_doc_no, 
							$last_item_id,
							
							$this_module, 
							
							$this_inventory_posting_type, 
							$line_get_entry["ent_warehouse_code_from"], 
							$line_product["pdt_code"], 
							$line_product["pdt_uom"], 
							$line_get_entry["ent_quantity"], 
							"",
							"", 
							""
							);
							
									
									
									
					//------------------------Part 2: plus:-------------------------
					post_inventory_balance(
							'SERVER',
							$this_posting_date,
							$this_doc_no, 
							$last_item_id,
							
							$this_module, 
							
							$this_inventory_posting_type2, 
							$line_get_entry["ent_warehouse_code_to"], 
							$line_product["pdt_code"], 
							$line_product["pdt_uom"], 
							$line_get_entry["ent_quantity"], 
							"",
							"", 
							""
							);
					
					
					
					
				}
			
			
			
			$query_update="update t_".$key_table_prefix."_delegate_entry
						set ent_posted = 'POSTED'
				    	where ent_id='".$line_get_entry["ent_id"]."'
					";

			$result_update = mysqli_query($dbconn, $query_update) or die("Error".mysqli_error($dbconn));
		
		}
		
		
		
		
		//=================================minus type===========================================
		
		
		
		
		$this_posting_date = $_SESSION["stock_delegate_calendar_y"]."-".$_SESSION["stock_delegate_calendar_m"]."-".$_SESSION["stock_delegate_calendar_d"]."";
		

		$this_warehouse_from_id=0;
		$this_warehouse_to_id=0;
	
  		
  		$first_entry=true;
		
		
		
		
		$query_get_entry="select * from  t_".$key_table_prefix."_delegate_entry
				where ent_session = '".$_REQUEST["session"]."'
				and device_id = '".$_REQUEST["d"]."'
				and ent_type = 'MINUS'
				
		    		";
		$result_get_entry = mysqli_query($dbconn, $query_get_entry) or die("Error".mysqli_error($dbconn));
		while($line_get_entry = mysqli_fetch_array($result_get_entry,MYSQLI_ASSOC))
		{
			
			if($first_entry)
			{
				$first_entry=false;
				
				
				
				$this_posting_date = $line_get_entry["ent_date"];
				
				//get info:
				$query_whs="select * from t_".$key_table_prefix."_warehouse 
					where whs_code  ='".$line_get_entry["ent_warehouse_code_from"]."' ";
				$result_whs  = mysqli_query($dbconn,$query_whs);
				if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
				{
					$this_warehouse_from_id = $line_whs["whs_id"];
				}
				
				//get info:
				$query_whs="select * from t_".$key_table_prefix."_warehouse 
					where whs_code  ='".$line_get_entry["ent_warehouse_code_to"]."' ";
				$result_whs  = mysqli_query($dbconn,$query_whs);
				if($line_whs = mysqli_fetch_array($result_whs,MYSQLI_ASSOC))
				{
					$this_warehouse_to_id = $line_whs["whs_id"];
				}
				
				
				
				
				$this_doc_no = mon_register_document_number($this_doc_number_module_code);
				
				
				
				$query="insert into t_".$key_table_prefix."_stock_transaction_header 
						(
						sth_doc_prefix  ,
						sth_doc_no  ,
						sth_module  ,
						sth_date ,
						
						sth_warehouse_code  ,
						sth_warehouse_id  ,
						
						sth_warehouse_code2  ,
						sth_warehouse_id2  ,
						
						sth_customer_code  ,
						sth_customer_name  ,
						sth_vendor_code  ,
						sth_vendor_name  ,
						sth_reference_no  ,
						sth_remark  ,
						
						sth_category_code  ,
						
						sth_status  ,
						sth_before_gst_amount ,
						sth_gst_amount  ,
						sth_total_amount  ,
						
						
						sth_driver_code, 
						sth_staff1_code, 
						sth_staff2_code, 
						
						
						created_date  ,
						created_by  ,
						modified_date  ,
						modified_by  ,
						client_id  
						)
						values
						(
						'".$this_doc_prefix."'  ,
						'".$this_doc_no."'  ,
						'".$this_module."'  ,
						'".$this_posting_date."' ,
						
						
						
						'".$line_get_entry["ent_warehouse_code_to"]."' ,
						'".$this_warehouse_to_id."'  ,
						
						'".$line_get_entry["ent_warehouse_code_from"]."' ,
						'".$this_warehouse_from_id."'  ,
						
						
						'' ,
						''  ,
						'' ,
						''  ,
						'Daily Stock Delegation (Return)'  ,
						'Daily Stock Delegation (Return)'  ,
						
						'".$_REQUEST["ent_category"]."'  ,
						
						'POSTED'  ,
						'0' ,
						'0' ,
						'0' ,
						
						
						
						'".$line_get_entry["ent_driver"]."' ,
						'".$line_get_entry["ent_staff1"]."' ,
						'".$line_get_entry["ent_staff2"]."' ,
						
						
						'".date("Y-m-d H:i:s")."'  ,
						'".$CurrentUserID."'  ,
						'".date("Y-m-d H:i:s")."'  ,
						'".$CurrentUserID."'  ,
						'0'  	
						)
				
					"; 
				$result = mysqli_query($dbconn,$query) or die("Error insert Header".mysqli_error($dbconn));
				$header_id  = mysqli_insert_id($dbconn);
				
				
				
			}
			
			
			
			
			
			
			$query_product = "select * 
						from t_".$key_table_prefix."_product 
						where pdt_id = '".$line_get_entry["ent_product_id"]."' ";
			$result_product = mysqli_query($dbconn,$query_product);
			if($line_product = mysqli_fetch_array($result_product,MYSQLI_ASSOC))
			{
				
				
				
				
				$query_item="insert into t_".$key_table_prefix."_stock_transaction_item 
						(
							
						  sti_header_id  ,
						  sti_doc_prefix  ,
						  sti_doc_no  ,
						  sti_module  ,
						  sti_date  ,
						  sti_warehouse_code  ,
						  sti_warehouse_id  ,
						  
						  sti_warehouse_code2  ,
						  sti_warehouse_id2  ,
						  
						  
						  sti_stock_code  ,
						  sti_barcode  ,
						  sti_factory_code  ,
						  sti_stock_name  ,
						  sti_color  ,
						  sti_size  ,
						  sti_pattern  ,
						  sti_uom  ,
						  sti_base_uom  ,
						  sti_quantity  ,
						  sti_base_quantity  ,
						  sti_conversion  ,
						  sti_unit_cost  ,
						  sti_base_unit_cost  ,
						  sti_total_cost  ,
						  sti_gst_mode  ,
						  sti_gst_code  ,
						  sti_gst_percentage  ,
						  sti_remark  ,
						  sti_status  ,
						  sti_before_gst_amount  ,
						  sti_gst_amount  ,
						  sti_total_amount  ,
						  
						  sti_storage_location  ,
						  sti_storage_location2  ,
						  
						  
						  created_date  ,
						  created_by  ,
						  modified_date  ,
						  modified_by  ,
						  client_id  
						)
						values
						(
						'".$header_id."'  ,
						
						'".$this_doc_prefix."'  ,
						'".$this_doc_no."'  ,
						'".$this_module."'  ,
						'".$this_posting_date."' ,
						
						
						'".$line_get_entry["ent_warehouse_code_to"]."' ,
						'".$this_warehouse_to_id."'  ,
						
						'".$line_get_entry["ent_warehouse_code_from"]."' ,
						'".$this_warehouse_from_id."'  ,
						
						
						  '".$line_product["pdt_code"]."'  ,
						  '".$line_product["pdt_barcode"]."'  ,
						  ''  ,
						  '".$line_product["pdt_name"]."'  ,
						  ''  ,
						  ''  ,
						  ''  ,
						  '".$line_product["pdt_uom"]."'  ,
						  '".$line_product["pdt_uom"]."'  ,
						  '".($line_get_entry["ent_quantity"]+0)."'  ,
						  '".($line_get_entry["ent_quantity"]+0)."'  ,
						  '1'  ,
						  '".($line_product["pdt_cost"]+0)."'  ,
						  '".($line_product["pdt_cost"]+0)."'  ,
						  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
						  ''  ,
						  ''  ,
						  '".(0)."'  ,
						  ''  ,
						  'POSTED'  ,
						  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
						  '".(0)."'  ,
						  '".(($line_product["pdt_cost"]*$line_get_entry["ent_quantity"])+0)."'  ,
						  
						  
						  ''  ,
						  ''  ,
						  
						  '".date("Y-m-d H:i:s")."'  ,
						  
						  '".$CurrentUserID."'  ,
						  '".date("Y-m-d H:i:s")."'  ,
						  '".$CurrentUserID."'  ,
						  '0'  	
							
						);
						";
				
				$result_item = mysqli_query($dbconn,$query_item) or die("Error Insert ITEM".mysqli_error($dbconn));
				$last_item_id = mysqli_insert_id($dbconn);
				
				
				
				//------------------------Part 1: minus:-------------------------
				post_inventory_balance(
						'SERVER',
						$this_posting_date,
						$this_doc_no, 
						$last_item_id,
						
						$this_module, 
						
						$this_inventory_posting_type, 
						$line_get_entry["ent_warehouse_code_to"], 
						$line_product["pdt_code"], 
						$line_product["pdt_uom"], 
						$line_get_entry["ent_quantity"], 
						"",
						"", 
						""
						);
						
								
								
								
				//------------------------Part 2: plus:-------------------------
				post_inventory_balance(
						'SERVER',
						$this_posting_date,
						$this_doc_no, 
						$last_item_id,
						
						$this_module, 
						
						$this_inventory_posting_type2, 
						$line_get_entry["ent_warehouse_code_from"], 
						$line_product["pdt_code"], 
						$line_product["pdt_uom"], 
						$line_get_entry["ent_quantity"], 
						"",
						"", 
						""
						);
				
				
				
				
			}
			
			
			
			
			
			
			
			
		}
		
		
		
		
		
		


























	  
	    
		
			 $this_data = array(
				 "status" => "YES",
				 "message" => "Active"
				 );

			    array_push($save_array, $this_data);
				
				
		    
	    
	    
	    
	    
}
else {

    	$this_data = array(
		  "status" => "NO",
		  "message" => "Invalid Login Information"
   	);

   array_push($main_array, $this_data);
}
 


$content_array["login"] = $main_array;
$content_array["save"] = $save_array;



$json = json_encode($content_array);

echo $json;






?>
