<?php
require_once 'ApiGlobalFunctions.php';

$usuario=isset($_GET['uid']) ? $_GET['uid'] : $_POST['uid'];
$password=isset($_GET['key']) ? $_GET['key'] : $_POST['key'];
$url=isset($_GET['url']) ? $_GET['url'] : $_POST['url'];
$pckid=isset($_GET['cID']) ? $_GET['cID'] : $_POST['cID'];
$include=includec($url);
$EstadoAsset=diccionario($url,'EstadoAsset');
$codproduct=diccionario($url,'codproduct');
$tipolote=diccionario($url,'LoteB');
$cf_1967=diccionario($url,'AssetResv');
$cantReserva=diccionario($url,'CantReser');
$cf_1890=diccionario($url,'FIngresoB');
$cf_1896=diccionario($url,'QtyAsset');
$SlotAsset=diccionario($url,'SlotAsset');

error_log("BOxASSETS. ".$usuario."--".$password."--".$url."--".$codproduct."\n", 3, "php.log");
include $include;
//http://wshub.globalb.co/SlotINOUT.php?uid=18&key=RsFWUWIJfXhi6XRm&url=https://hoserdev.globalb.co&cID=310654
$validate=loginkey($usuario,$password,$url);
if($validate==1){
    
    list($productos['productos'],$excluidos) = getProductos($pckid,$link, $codproduct);//obtenemos los productos de la caja
	/*mysqli_set_charset($link, "utf8");
    $strExcluir = count($excluidos) > 0 ? ' and vtiger_products.productid not in ('.implode(',', $excluidos).')' : '';
    $query=mysqli_query($link, "SELECT cajaid,assetstatus,vtiger_assets.assetsid,serialnumber,product,qtyinstock,slots_tks_codigo,".$cf_1967." as 'reserva',".$cf_1890.",".$cf_1896.", ".$codproduct.", vtiger_bodegas.bodegasid,".$tipolote."
    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 $strExcluir
    INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid
    INNER JOIN caja_productos ON caja_productos.productid=vtiger_products.productid
    INNER JOIN vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_assets.assetsid
    INNER JOIN vtiger_slots ON vtiger_crmentityrel.crmid=vtiger_slots.slotsid
    INNER JOIN vtiger_paquetes ON vtiger_paquetes.paquetesid=caja_productos.cajaid
    INNER JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_paquetes.paquetes_tks_bodega    
    WHERE deleted='0' AND cajaid='$pckid' AND assetstatus='$EstadoAsset' and ".$cf_1896." > 0 AND (qtyinstock>0 OR ".$cantReserva." > 0) GROUP BY serialnumber ORDER BY ".$cf_1890);*/
    
    $query=mysqli_query($link, "SELECT cajaid,assetstatus,vtiger_assets.assetsid,serialnumber,product,qtyinstock,  ".$SlotAsset." as 'slots_tks_codigo',".$cf_1967." as 'reserva',".$cf_1890.",".$cf_1896.", ".$codproduct.", ' ' as 'bodegasid',".$tipolote."
    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
    INNER JOIN caja_productos ON caja_productos.productid=vtiger_products.productid
    WHERE deleted='0' AND cajaid='$pckid' AND assetstatus='$EstadoAsset' and ".$cf_1896." > 0 AND qtyinstock>0 AND cantidad<>recibido GROUP BY serialnumber ORDER BY ".$cf_1890);
    
    error_log("SELECT cajaid,assetstatus,vtiger_assets.assetsid,serialnumber,product,qtyinstock,  ".$SlotAsset." as 'slots_tks_codigo',".$cf_1967." as 'reserva',".$cf_1890.",".$cf_1896.", ".$codproduct.", ' ' as 'bodegasid',".$tipolote."
    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
    INNER JOIN caja_productos ON caja_productos.productid=vtiger_products.productid
    WHERE deleted='0' AND cajaid='$pckid' AND assetstatus='$EstadoAsset' and ".$cf_1896." > 0 AND qtyinstock>0 AND cantidad<>recibido GROUP BY serialnumber ORDER BY ".$cf_1890."\n", 3, "php.log");
    
    

    $num=mysqli_num_rows($query);
	$z=0;                  
    while ($row = mysqli_fetch_array($query)) {
        $assetsid=$row['assetsid'];
        $serialnumber=$row['serialnumber'];
        $assetstatus=$row['assetstatus'];
        $product=$row['product'];
        $cantidad=$row['qtyinstock'];
        $productcode=$row[$codproduct];
        $osReservado=$row['reserva'];
        $bodega=$row['bodegasid'];
        $fechaIngreso=$row[$cf_1890];
        $cantidadLote=$row[$cf_1896];
        $slot=$row['slots_tks_codigo'];
        $tlote=$row[$tipolote];
        

$docs[$z]=array('cajaID'=>$pckid,'assetstatus'=>$assetstatus,'assetsid'=>$assetsid,'serialnumber'=>$serialnumber,'product'=>$product,'productcode'=>$productcode,'cantidad'=>$cantidad,'slot'=>$slot,'osReservado'=>$osReservado,'fechaIngreso'=>$fechaIngreso,'cantidadLote'=>$cantidadLote,'bodegasid'=>$bodega);

$docs[$z]=array('serialnumber'=>$serialnumber,'productcode'=>$productcode,'cantidad'=>$cantidad,'slot'=>$slot,'osReservado'=>$osReservado,'fechaIngreso'=>$fechaIngreso,'cantidadLote'=>$cantidadLote,'tipolote'=>$tlote,'recorid'=>$assetsid);
        $z++;
    }

    /*if(count($excluidos)==count($productos['productos'])){ //si los excluidos son la misma cantidad de productos, es porque ya se han procesado todos.
        $docs = [];
    }
    */$data = array_merge(setDataJSON($docs,1), $productos);
    echo json_encode($data);
    
    
    //echo json_encode(setDataJSON($docs,1));
    
}

/**
 * obtener los datos de cada equipo, de cada asset
 */

function getBodegaSlot($serialnumber, $link)
{	
    $query=mysqli_query($link,"SELECT * FROM vtiger_assets INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_assets.assetsid
			INNER JOIN vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_assets.assetsid AND module='Slots'
			INNER JOIN vtiger_slots ON vtiger_slots.slotsid=vtiger_crmentityrel.crmid
			INNER JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_slots.slots_tks_bodega WHERE serialnumber='$serialnumber'");
    $num=mysqli_num_rows($query);
	$z=0;                  
	$data=[];
    while ($row = mysqli_fetch_array($query)) {
        $data['bodegasid'] = $row['bodegasid'];
        //$data['slotsCodigo'] = $row['slots_tks_codigo'];
        $data['assetno'] = $row['asset_no'];
    }
    return $data;
}

/**
 * obtener los productos de la caja
 */
function getProductos($cajaid,$link, $campoCodigo)
{
	$query=mysqli_query($link,"SELECT vtiger_products.productid, cantidad, $campoCodigo as 'productcode', recibido FROM vtiger_products INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid INNER JOIN caja_productos ON caja_productos.productid=vtiger_products.productid INNER JOIN vtiger_productcf on vtiger_products.productid = vtiger_productcf.productid WHERE deleted='0' AND cajaid='$cajaid'");
    $num=mysqli_num_rows($query);
	$z=0;
	$data=[];
    $excluidos=[];
    while ($row = mysqli_fetch_array($query)) {
        $data[$z]['productid'] = $row['productid'];
        $data[$z]['productcode'] = $row['productcode'];
        $data[$z]['cantidad'] = $row['cantidad'];
        $data[$z]['pendiente'] = $row['cantidad'] - $row['recibido'];        
        if($data[$z]['pendiente']<=0){
            array_push($excluidos, $row['productid']);
        }
        $data[$z]['cajaID'] = $cajaid;
        $z++;
    }
    return [$data, $excluidos];
}
