<?php
require_once 'ApiGlobalFunctions.php';

$usuario=isset($_POST['uid']) ? $_POST['uid'] : $_GET['uid'];
$password=isset($_POST['key']) ? $_POST['key'] : $_GET['key'];
$url=isset($_POST['url']) ? $_POST['url'] : $_GET['url'];

//?uid=18&key=RsFWUWIJfXhi6XRm&url=http://hoserdev.globalb.co
//?uid=18&key=RsFWUWIJfXhi6XRm&url=http://hoserdev.globalb.co&Slot=S414002
error_log("ASSETSLOTS. ".$usuario."--".$password."--".$url."--"."\n", 3, "php.log");
$slotcode=isset($_POST['Slot']) ? $_POST['Slot'] : $_GET['Slot'];
$include=includec($url);
include $include;

$validate=loginkey($usuario,$password,$url); 

if($validate>=1){
    mysqli_set_charset($link, "utf8");
    $codprod=diccionario($url,'codproduct');    
    $QtyAsset=diccionario($url,'QtyAsset');    
    $FIngresoB=diccionario($url,'FIngresoB');  
    $LoteB=diccionario($url,'LoteB');
    $UltInv=diccionario($url,'UltInv');
    $AssetResv=diccionario($url,'AssetResv');
    $EstadoAsset=diccionario($url,'EstadoAsset');

    
    $query=mysqli_query($link,"SELECT serialnumber,vtiger_assets.assetsid,".$LoteB." as lote,".$QtyAsset." AS cantidad,".$FIngresoB." as FIB, vtiger_products.productid ,productname,".$codprod." as codprod, productcategory,CRM2.description, qtyinstock,registrosinventario_tks_invent,if(".$AssetResv.">1 && assetstatus='".$EstadoAsset."','1','0') as 'ReservFlag',
if(".$AssetResv.">1 && assetstatus='".$EstadoAsset."',".$AssetResv.",'0') as 'ReservID'
        FROM vtiger_assets 
        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_assets.assetsid
        INNER JOIN vtiger_assetscf ON vtiger_assetscf.assetsid=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 vtiger_crmentity CRM2 ON CRM2.crmid=vtiger_products.productid
        INNER JOIN vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_assets.assetsid
        INNER JOIN vtiger_slots ON vtiger_slots.slotsid=vtiger_crmentityrel.crmid
        INNER JOIN vtiger_crmentity CRM3 ON CRM3.crmid=vtiger_slots.slotsid AND CRM3.deleted='0'
        INNER JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_slots.slots_tks_bodega
        INNER JOIN vtiger_racks ON vtiger_racks.racksid=vtiger_slots.slots_tks_rack
        LEFT JOIN vtiger_registrosinventario ON vtiger_registrosinventario.registrosinventarioid=vtiger_assets.".$UltInv."
        WHERE vtiger_crmentity.deleted='0' AND slots_tks_codigo='$slotcode' AND qtyinstock>0  AND assetstatus='$EstadoAsset' GROUP by serialnumber ORDER BY qtyinstock DESC ");
$num=mysqli_num_rows($query);    

    $z=0;
    $docs = array();
    while ($row = mysqli_fetch_array($query)) {        
        $NSerie=$row['serialnumber'];
        $AssetID=$row['assetsid'];
        $TipoLote=$row['lote'];
        $CantLote=$row['cantidad'];
        $FechaIngresoBod=$row['FIB'];
        $ProductCode=$row['productid'];
        $ProductoNombre=$row['productname'];
        $ProductoCodigo=$row['codprod'];
        $ProductoCategoria=$row['productcategory'];
        $ProductoDescription=$row['description'];
        $ProductoStock=$row['qtyinstock'];
        $rinventario=$row['registrosinventario_tks_invent'];
        $ReservFlag=$row['ReservFlag'];
        $ReservID=$row['ReservID'];
     

        $docs[$z]=array('NSerie'=>$NSerie,'AssetID'=>$AssetID,'TipoLote'=>$TipoLote,'CantLote'=>$CantLote,'FechaIngresoBod'=>$FechaIngresoBod,'ProductCode'=>$ProductCode,'ProductoNombre'=>$ProductoNombre,'ProductoCodigo'=>$ProductoCodigo,'ProductoCategoria'=>$ProductoCategoria,'ProductoDescription'=>$ProductoDescription,'ProductoStock'=>$ProductoStock,'UltimoInvID'=>$rinventario,'ReservFlag'=>$ReservFlag,'ReservID'=>$ReservID);
        $z++;            
    }
    echo json_encode(setDataJSON($docs, 1));
}
?>
