<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$cajaid=$_POST['cajaID'];
$include=includec($url);
include $include;

$validate=loginkey($usuario,$password,$url);
if($validate==1){
	mysqli_set_charset($link, "utf8");
    $campo=diccionario($url,'codproduct');
    $AssetResv=diccionario($url,'AssetResv');
    $EstadoAsset=diccionario($url,'EstadoAsset');
    $SlotAsset=diccionario($url,'SlotAsset');
    $QtyAsset=diccionario($url,'QtyAsset');
    //echo $campo;
	$query=mysqli_query($link,"SELECT paquetes_tks_ordendesalida
	FROM vtiger_paquetes
	INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_paquetes.paquetes_tks_ordendesalida
	WHERE deleted='0' AND paquetesid='$cajaid'");
	$data= mysqli_fetch_array($query);
    $OS=$data['paquetes_tks_ordendesalida'];	

   $query=mysqli_query($link, "SELECT vtiger_products.productid, productname, ".$campo." as 'codprod' ,cantidad,recibido, estado
    FROM vtiger_paquetes 
    INNER JOIN caja_productos ON vtiger_paquetes.paquetesid=caja_productos.cajaid 
    INNER JOIN vtiger_products ON caja_productos.productid=vtiger_products.productid 
    INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid
    LEFT JOIN vtiger_assets ON vtiger_assets.assetsid=caja_productos.asset
    LEFT JOIN vtiger_assetscf ON vtiger_assetscf.assetsid=vtiger_assets.assetsid
    WHERE vtiger_paquetes.paquetesid='$cajaid'");
	$z=0;                  
	$h=0;
    //print_r( mysqli_fetch_array($query));
    while ($row = mysqli_fetch_array($query)) {
	$cnReserva=0;
	$productoid=$row['productid'];
        $productname=$row['productname'];            
        $productcode=$row['codprod'];
        $cantidad=$row['cantidad'];
        $recibido=$row['recibido'];
        $estado=$row['estado'];
        $restantes=$cantidad-$recibido;
      
		if(!empty($OS)){	
			$query2=mysqli_query($link,"SELECT *
			FROM vtiger_assets
			INNER JOIN vtiger_assetscf ON vtiger_assetscf.assetsid=vtiger_assets.assetsid
			INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_assets.assetsid
			INNER JOIN vtiger_products ON vtiger_products.productid=vtiger_assets.product
			INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid
			WHERE assetstatus='$EstadoAsset' AND ".$AssetResv."='$OS' AND ".$campo."='$productcode'");
			$num=mysqli_num_rows($query2);  		
			if($num>=1){
				$cnReserva=1;
			}else{
				$cnReserva=0;
			}
		}
                
        $cajaprods[$z]=array('ProductName'=>$productname,'ProductCODE'=>$productcode,'ProductQTY'=>$cantidad,'ProductQrest'=>$restantes,'ProductQdone'=>$recibido,'ProductQstatus'=>$estado,'cnReserva'=>$cnReserva);
        $z++;  
	$queryr=mysqli_query($link,"SELECT serialnumber, ".$SlotAsset." as 'slot', ".$QtyAsset." as 'qtyasset', ".$campo." as 'codeprod'  
					FROM vtiger_assets
					INNER JOIN vtiger_assetscf ON vtiger_assetscf.assetsid=vtiger_assets.assetsid
					INNER JOIN vtiger_crmentity CRM1 ON CRM1.crmid=vtiger_assets.assetsid
					INNER JOIN vtiger_products ON vtiger_products.productid=vtiger_assets.product
					INNER JOIN vtiger_crmentity CRM2 ON CRM2.crmid=vtiger_products.productid
					WHERE CRM1.deleted='0' AND CRM2.deleted='0' AND product='$productoid' AND assetstatus='$EstadoAsset'");

	while ($row2= mysqli_fetch_array($queryr)) {
		$serialnumber=$row2['serialnumber'];
		$slot=$row2['slot'];
		$qtyasset=$row2['qtyasset'];
		$codeprod=$row2['codeprod'];
		
		$assetsall[$h]=array('serialnumber'=>$serialnumber,'productcode'=>$codeprod,'slotid'=>$slot,'qtyasset'=>$qtyasset);
		$h++;
	}	         
   }

$array= array
  (
    	"CajaProds"=>$cajaprods,
        "EquiposAll"=>$assetsall,
 );


echo json_encode(setDataJSON($array,1));




}
