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

$json_array_document = array();
$json_array_item = array();



$query_get="select * from t_".$key_table_prefix."_invoice_header
		where ivh_operation_mode = 'ORDER'
		and not ifnull(ivh_delivery_status,'') = 'DELIVERED'
		and ivh_id = '".$_REQUEST["id"]."'
		
		";
$result_get = mysqli_query($dbconn, $query_get);
while($line_get = mysqli_fetch_array($result_get,MYSQLI_ASSOC))
{
	
	if(($_REQUEST["code"]!="")and($_REQUEST["user"]!=""))
	{
		
		$query_update_item = "update  t_".$key_table_prefix."_invoice_item
				
				set ivi_picking_status = 'PICKED',
				    ivi_picking_datetime = '".date("Y-m-d H:i:s")."',
				    ivi_picking_user = '".$_REQUEST["user"]."'
				
				where ivi_header_id = '".$line_get["ivh_id"]."'
				and  ( ivi_product_code = '".$_REQUEST["code"]."' or  ivi_product_barcode = '".$_REQUEST["code"]."' ) 
				
				";
		$result_update_item = mysqli_query($dbconn, $query_update_item);
		
		$message="Picking Status Updated".mysqli_error($dbconn);
		
		
	}
	


	
	
	
	
	
   	$this_data = array(
		"id" => "".$line_get["ivh_id"],
		"doc_no" => "".$line_get["ivh_doc_no"],
		"picking_status" => "".$line_get["ivh_picking_status"],
		"customer_code" => "".$line_get["ivh_customer_code"],
		"customer_name" => "".$line_get["ivh_customer_name"],
		"date" => "".$line_get["ivh_date"],
		"message" => "".$message
		
	  );
	array_push($json_array_document, $this_data); 
	
	
	


	$query_get_item="select * from t_".$key_table_prefix."_invoice_item
			where ivi_header_id = '".$line_get["ivh_id"]."'
			order by ivi_storage_location
			";
	$result_get_item = mysqli_query($dbconn, $query_get_item);
	while($line_get_item = mysqli_fetch_array($result_get_item,MYSQLI_ASSOC))
	{
		
		
		$this_location = "".$line_get_item["ivi_storage_location"];
		$query_internal_location="select distinct iit_location_code 
		 				from t_".$key_table_prefix."_internal_inventory_transaction 
						where iit_product_code = '".$line_get_item["ivi_product_code"]."' 
						and iit_type = 'CHECK_IN' ;";
						
		$resul_internal_location = mysqli_query($dbconn, $query_internal_location);
		while($line_internal_location = mysqli_fetch_array($resul_internal_location,MYSQLI_ASSOC))
		{
			$this_location = $this_location.", ".$line_internal_location["iit_location_code"];
			
		}				
		
	
		
	   	$this_data = array(
			"id" => "".$line_get_item["ivi_id"],
			"code" => "".$line_get_item["ivi_product_code"],
			"barcode" => "".$line_get_item["ivi_product_barcode"],
			"name" => "".$line_get_item["ivi_product_name"],
			"uom" => "".$line_get_item["ivi_uom"],
			"quantity" => "".$line_get_item["ivi_quantity"],
			"location" => "".$this_location,
			
			"picking_status" => "".$line_get_item["ivi_picking_status"],
			"picking_datetime" => "".$line_get_item["ivi_picking_datetime"],
			"picking_user" => "".$line_get_item["ivi_picking_user"]
			
		  );
		array_push($json_array_item, $this_data); 
	}




}


$content_array["document"] = $json_array_document;	
$content_array["item"] = $json_array_item;	




$json = json_encode($content_array);	
echo $json;
?>