<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$cajaid=$_POST['cajaID'];
$include=includec($url);
include $include;
error_log("BOxASSETS. ".$usuario."--".$password."--".$url."--".$cajaid."\n", 3, "php.log");
$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');
	$OSDev=diccionario($url,'OCOSDevo');
    $assetmov=diccionario($url,'MovAsset');
    //echo $campo;
	/*$query=mysqli_query($link,"SELECT paquetes_tks_ordendesalida, paquetes_tks_ordendecompra
	FROM vtiger_paquetes
	INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_paquetes.paquetesid
	WHERE deleted='0' AND paquetesid='$cajaid'");
	$data= mysqli_fetch_array($query);
    $OS=$data['paquetes_tks_ordendesalida'];
    $OC=$data['paquetes_tks_ordendecompra'];*/	

   $query=mysqli_query($link, "SELECT paquetes_tks_ordendesalida, paquetes_tks_ordendecompra, productname, ".$campo." as pcode ,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
    WHERE vtiger_paquetes.paquetesid='$cajaid'");
	$z=0;
	$docs=array();
    //print_r( mysqli_fetch_array($query));
    while ($row = mysqli_fetch_array($query)) {
		$cnReserva=0;
		$OS=$row['paquetes_tks_ordendesalida'];
        $OC=$row['paquetes_tks_ordendecompra'];
        $productname=$row['productname'];            
        $productcode=$row['pcode'];
        $cantidad=$row['cantidad'];
        $recibido=$row['recibido'];
        $estado=$row['estado'];
        //$AssetResvs=$row['reserva'];
        $restantes=$cantidad-$recibido;
      
		if(!empty($OS)){	
			$query2=mysqli_query($link,"SELECT count(*) as count
			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
			WHERE deleted='0' AND assetstatus='$EstadoAsset' AND ".$AssetResv."='$OS'");
			$datarev= mysqli_fetch_array($query2);
            $countrev=$datarev['count'];
			if($countrev>=1){
				$cnReserva=1;
			}else{
				$cnReserva=0;
			}
		}
                
        $docs[$z]=array('ProductName'=>$productname,'ProductCODE'=>$productcode,'ProductQTY'=>$cantidad,'ProductQrest'=>$restantes,'ProductQdone'=>$recibido,'ProductQstatus'=>$estado,'cnReserva'=>$cnReserva,'AssetResv'=>$AssetResvs);
        $z++;            
        }
/*        if(!empty($OC)){
            $query=mysqli_query($link,"SELECT serialnumber, ".$campo." as CodigoProd, movimientos_tks_cantidad
            FROM vtiger_purchaseordercf
            INNER JOIN vtiger_movimientos ON vtiger_movimientos.movimientos_tks_ordendesalida=vtiger_purchaseordercf.$OSDev
            INNER JOIN vtiger_assets ON vtiger_assets.assetsid=vtiger_movimientos.$assetmov
            INNER JOIN vtiger_products ON vtiger_products.productid=vtiger_assets.product
            INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid
            WHERE purchaseorderid='$OC'");
            $devolucion=array();
            $i=0;
            while ($row = mysqli_fetch_array($query)) {
                $serial=$row['serialnumber']; 
                $productcode=$row['CodigoProd'];       
                $cantidad=$row['movimientos_tks_cantidad'];
                $devolucion[$i]=array('SerialNumber'=>$serial,'ProductCode'=>$productcode,'QTYOut'=>$cantidad);
                $i++; 
            }    
        }
        $data=array('ProductsBox'=>$docs,'DevolucionAssets'=>$devolucion);*/
//echo json_encode(setDataJSON($devolucion,1));
echo json_encode(setDataJSON($docs,1));
//echo json_encode(setDataJSON($data,1));
}