<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$producto=$_POST['ProductCode'];
$caja=$_POST['Caja_ID'];
$include=includec($url);
include $include;

$validate=loginkey($usuario,$password,$url);
if($validate==1){
    mysqli_set_charset($link, "utf8");
    $campo=diccionario($url,'codproduct');        
    $FIngresoB=diccionario($url,'FIngresoB');  
    $cantasset=diccionario($url,'QtyAsset');
    $AssetResv=diccionario($url,'AssetResv');
    $EstadoAsset=diccionario($url,'EstadoAsset');

     $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='$caja'");
    $data= mysqli_fetch_array($query);
    $OS=$data['paquetes_tks_ordendesalida'];        




    $query=mysqli_query($link, "SELECT bodegas_tks_nombre,vtiger_slots.slotsid,secciones_tks_seccion, racksno, slots_tks_codigo, slots_tks_cantidad,slots_tks_fila, slots_tks_columna,serialnumber,".$FIngresoB." as FIB, ".$cantasset." as cantidad, if(".$AssetResv.">1 && assetstatus='$EstadoAsset','1','0') as 'ReservFlag',
if(".$AssetResv.">1 && assetstatus='".$EstadoAsset."',".$AssetResv.",'0') as 'ReservID'
        FROM vtiger_slots 
        INNER JOIN vtiger_slotscf ON vtiger_slotscf.slotsid=vtiger_slots.slotsid 
        INNER JOIN vtiger_crmentity CRM1 ON CRM1.crmid=vtiger_slots.slotsid 
        INNER JOIN vtiger_crmentityrel ON vtiger_crmentityrel.crmid=vtiger_slots.slotsid
        RIGHT JOIN vtiger_assets ON vtiger_assets.assetsid=vtiger_crmentityrel.relcrmid
        RIGHT JOIN vtiger_assetscf ON vtiger_assetscf.assetsid=vtiger_assets.assetsid
        INNER JOIN vtiger_crmentity CRM2 ON CRM2.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 vtiger_racks ON vtiger_racks.racksid=vtiger_slots.slots_tks_rack 
        INNER JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_slots.slots_tks_bodega
        INNER JOIN vtiger_secciones ON vtiger_secciones.seccionesid=vtiger_slots.slots_tks_seccion
        WHERE CRM1.deleted='0' AND CRM2.deleted='0' AND ".$campo."='$producto' AND  (".$AssetResv."='$OS' OR ".$AssetResv."='') GROUP by serialnumber ORDER BY 12 DESC, ".$FIngresoB." ASC");       
    $z=0;              
    while ($row = mysqli_fetch_array($query)) {           
        $slotid=$row['slotsid']; 
        $query2=mysqli_query($link,"SELECT *FROM vtiger_crmentityrel WHERE crmid='$slotid'");
        $num2=mysqli_num_rows($query2);
        $bodega=$row['bodegas_tks_nombre'];            
        $seccion=$row['secciones_tks_seccion'];            
        $rack=$row['racksno'];
        $slot=$row['slots_tks_codigo'];
        $cantidad=$row['cantidad'];   
        $fila=$row['slots_tks_fila'];
        $columna=$row['slots_tks_columna'];
        $serialnumber=$row['serialnumber'];
        $FechaIngresoBod=$row['FIB'];
        $ReservFlag=$row['ReservFlag'];
        $ReservID=$row['ReservID'];
        $docs[$z]=array('bodega'=>$bodega,'seccion'=>$seccion,'rack'=>$rack,'slot'=>$slot,'cantidad'=>$cantidad,'fila'=>$fila,'columna'=>$columna,'NSerie'=>$serialnumber,'FechaIngresoBod'=>$FechaIngresoBod,'ReservFlag'=>$ReservFlag,'ReservID'=>$ReservID);
        $z++;            
        }
    echo json_encode(setDataJSON($docs,1));   
}
