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

$json_array_document = array();
$json_array_document_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  not ifnull(ivh_picking_status,'') = 'PICKED' 
		order by ivh_date desc 
		";
$result_get = mysqli_query($dbconn, $query_get);
while($line_get = mysqli_fetch_array($result_get,MYSQLI_ASSOC))
{
   	$this_data = array(
		"id" => "".$line_get["ivh_id"],
		"doc_no" => "".$line_get["ivh_doc_no"],
		"customer_code" => "".$line_get["ivh_customer_code"],
		"customer_name" => "".$line_get["ivh_customer_name"],
		"picking_status" => "".$line_get["ivh_picking_status"],
		"date" => "".$line_get["ivh_date"],
		"created_date" => "".$line_get["created_date"],
		"modified_date" => "".$line_get["modified_date"]
		
	  );
	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"]."'
			and ivi_status = 'POSTED'
			";
	$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"],
			"doc_id" => "".$line_get["ivh_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,
			
			
			
		  );
		array_push($json_array_document_item, $this_data);
		    
	}


    
}



$content_array["document"] = $json_array_document;	
$content_array["document_item"] = $json_array_document_item;	



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