<?php

include "start.php";


$main_array = array();
$product_array = array();
$warehouse_array = array();
$staff_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);
	    
	    
	    
	    
	    
	    
	    
	    //---------staff-------------------------------------------------------------------
	    
    	    $query_get_staff="select * from t_".$key_table_prefix."_staff
    	    		";


    	    $result_get_staff = mysqli_query($dbconn, $query_get_staff) or die("Error".mysqli_error($dbconn));
    	    while($line_get_staff= mysqli_fetch_array($result_get_staff,MYSQLI_ASSOC))
    	    {
    	    	     $this_data = array(
    	    			  "id" => "".$line_get_staff["stf_id"],
    	    			  "code" => "".$line_get_staff["stf_code"],
    				  "name" => "".$line_get_staff["stf_name"]
    	    	     );

    	    	    array_push($staff_array, $this_data);
    	    	    
    	    	    
    	    }
    	    
	    	    
	    	    
		    
		    
		    
		    
	    
	    //--------------warehouse-----------------------------------------------
	    $query_get_warehouse="select * from t_".$key_table_prefix."_warehouse
	    		";


	    $result_get_warehouse = mysqli_query($dbconn, $query_get_warehouse) or die("Error".mysqli_error($dbconn));
	    while($line_get_warehouse= mysqli_fetch_array($result_get_warehouse,MYSQLI_ASSOC))
	    {

	    	
	    	
	    	
	    	     $this_data = array(
	    			  "id" => "".$line_get_warehouse["whs_id"],
	    			  "code" => "".$line_get_warehouse["whs_code"],
				  "name" => "".$line_get_warehouse["whs_name"]
	    	     );

	    	    array_push($warehouse_array, $this_data);
	    	    
	    	    
	    	    
	    	    
	    }
	    
	    
	    
	    
	    
	    //----------------------------------------------------------------------------
	    
	    
	    
	    $query_balance="select 
				    sum(bal_quantity) as total_quantity ,
				    bal_product_code 
				    from t_".$key_table_prefix."_balance
				    where  bal_warehouse_code = '".$_REQUEST["warehouse_to"]."' 
				    group by bal_product_code 
				    ";
	    $result_balance=mysqli_query($dbconn,$query_balance);
	    while($line_balance=mysqli_fetch_array($result_balance,MYSQLI_ASSOC))
	    {
		    $lib_balance[$line_balance["bal_product_code"]] = $line_balance["total_quantity"];				
		    $lib_open_balance[$line_balance["bal_product_code"]] = $line_balance["total_quantity"];				
	    }
	    

	    
	    $query_card="select 
			    sum(abs(stc_base_quantity)) as total_quantity,
			    stc_type,
			    stc_stock_code,
			    stc_doc_no
	    
			    from t_".$key_table_prefix."_stock_card 
			    where  stc_date = '".$_REQUEST["date"]."'
			    and stc_warehouse_code = '".$_REQUEST["warehouse_to"]."'
			    group by stc_type ,stc_stock_code,stc_doc_no
			    ";
	    
	    $result_card =mysqli_query($dbconn,$query_card);
	    while($line_card = mysqli_fetch_array($result_card,MYSQLI_ASSOC))
	    {	
		    $lib_stock_cart[$line_card["stc_stock_code"]][$line_card["stc_doc_no"]][$line_card["stc_type"]] = $line_card["total_quantity"]+0;
		    
		    
		    if($line_card["stc_type"]=="PLUS")
		    {
			    $lib_open_balance[$line_card["stc_stock_code"]] = $lib_open_balance[$line_card["stc_stock_code"]] - $line_card["total_quantity"];
			    
		    }
		    if($line_card["stc_type"]=="MINUS")
		    {
			    $lib_open_balance[$line_card["stc_stock_code"]] = $lib_open_balance[$line_card["stc_stock_code"]] + $line_card["total_quantity"];
			    
		    }
		    
		    
	    }
	    
	    
	    
	    
	    
	    
	    
	    $query_card_after_end ="select 
			    sum(abs(stc_base_quantity)) as total_quantity,
			    stc_type,
			    stc_stock_code
			    
	    
			    from t_".$key_table_prefix."_stock_card 
			    where  stc_date > '".$_REQUEST["date"]."'
			    and stc_warehouse_code = '".$_REQUEST["warehouse_to"]."'
			    group by stc_type ,stc_stock_code 
			    ";
	    
	    
	    $result_card_after_end =mysqli_query($dbconn,$query_card_after_end);
	    while($line_card_after_end = mysqli_fetch_array($result_card_after_end,MYSQLI_ASSOC))
	    {	
		  
		    
		    if($line_card_after_end["stc_type"]=="PLUS")
		   {
			   $lib_balance[$line_card_after_end["stc_stock_code"]] = $lib_balance[$line_card_after_end["stc_stock_code"]] - $line_card_after_end["total_quantity"];
			   $lib_open_balance[$line_card_after_end["stc_stock_code"]] = $lib_open_balance[$line_card_after_end["stc_stock_code"]] - $line_card_after_end["total_quantity"];
			   
			   
		   }
		   if($line_card_after_end["stc_type"]=="MINUS")
		   {
			   $lib_balance[$line_card_after_end["stc_stock_code"]] = $lib_balance[$line_card_after_end["stc_stock_code"]] + $line_card_after_end["total_quantity"];
			   $lib_open_balance[$line_card_after_end["stc_stock_code"]] = $lib_open_balance[$line_card_after_end["stc_stock_code"]] + $line_card_after_end["total_quantity"];
			   
		   }
		   
		   
		   
		    
		    
	    }
	    

	    



	    //----------------------------------------------------------------------------
	    
	    
	    
	    

	    $query_get_product="select * from t_".$key_table_prefix."_product
	    		";


	    $result_get_product = mysqli_query($dbconn, $query_get_product) or die("Error".mysqli_error($dbconn));
	    while($line_get_product = mysqli_fetch_array($result_get_product,MYSQLI_ASSOC))
	    {

	    	
	    		$history="";
		
		    foreach($lib_stock_cart[$line_get_product["pdt_code"]] as $doc_no => $data1)
		    {
				    
			    if($data1["PLUS"]>0)
			    {
				    $history= $history." +".($lib_stock_cart[$line_get_product["pdt_code"]][$doc_no]["PLUS"]+0);
				    
			    }
			    
			    if($data1["MINUS"]>0)
			    {
				    $history= $history." -".($lib_stock_cart[$line_get_product["pdt_code"]][$doc_no]["MINUS"]+0);
				    
			    }
			    
		    }
		    
	    
		
		
	    	
	    	     $this_data = array(
	    			  "id" => "".$line_get_product["pdt_id"],
	    			  "code" => "".$line_get_product["pdt_code"],
				  "name" => "".$line_get_product["pdt_name"],
				  "uom" => "".$line_get_product["pdt_uom"],
				  
				  
				  "open_balance" => "".$lib_open_balance[$line_get_product["pdt_code"]],
				  "history" => "".$history,
				  "closing_balance" => "".$lib_balance[$line_get_product["pdt_code"]]
				  
				  
				  
	    	     );

	    	    array_push($product_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["product"] = $product_array;
$content_array["warehouse"] = $warehouse_array;
$content_array["staff"] = $staff_array;



$json = json_encode($content_array);

echo $json;






?>
