<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$producto=$_POST['ProductCode'];
$include=includec($url);
include $include;
error_log("PRODUCTSSLOTS. ".$usuario."--".$password."--".$url."--".$producto."\n", 3, "php.log");
$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');
	$LoteB=diccionario($url,'LoteB');
	$EstadoAsset=diccionario($url,'EstadoAsset');
	$zonaPickeo=diccionario($url,'ZonaPickeo');
	$BodAsset=diccionario($url,'BodAsset');
    $SlotAsset=diccionario($url,'SlotAsset');
    $RackAsset=diccionario($url,'RackAsset');
    $SeccAsset=diccionario($url,'SeccAsset');
    
    $query=mysqli_query($link, "SELECT ".$BodAsset." as 'bodegas_tks_nombre', ".$SeccAsset." as 'secciones_tks_seccion', ".$RackAsset." as 'racksno', ".$SlotAsset." as 'slots_tks_codigo', '' as 'slots_tks_cantidad', '' as 'slots_tks_fila', '' as 'slots_tks_columna',serialnumber,".$FIngresoB." as FIB, ".$cantasset." as cantidad, if(".$AssetResv.">1 && assetstatus='$EstadoAsset','1','0') as 'ReservFlag', ".$zonaPickeo." as ZonaPickeo, ".$LoteB." as lote, salesorder_no
        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 AND CRM1.deleted='0'
        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_slots ON vtiger_slots.slots_tks_codigo=vtiger_assetscf.$SlotAsset
        INNER JOIN vtiger_slotscf ON vtiger_slotscf.slotsid=vtiger_slots.slotsid
        LEFT JOIN vtiger_salesorder ON vtiger_salesorder.salesorderid=vtiger_assetscf.$AssetResv
		WHERE ".$campo."='$producto' AND ".$cantasset.">0  GROUP by serialnumber ORDER BY 14 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'];
		$TipoLote=$row['lote'];
		$zonaPick=$row['ZonaPickeo'];
		/*if(!empty($ReservID)){
		    $querySO=mysqli_query($link, "SELECT salesorder_no FROM vtiger_salesorder WHERE salesorderid='$ReservID'");
		    $dataSO= mysqli_fetch_array($querySO);   
		    $so_no=$dataSO['salesorder_no'];
		}*/
		$so_no=$row['salesorder_no'];
        $docs[$z]=array('bodega'=>$bodega,'seccion'=>$seccion,'rack'=>$rack,'slot'=>$slot,'cantidad'=>$cantidad,'fila'=>$fila,'columna'=>$columna,'NSerie'=>$serialnumber,'FechaIngresoBod'=>$FechaIngresoBod,'ReservFlag'=>$ReservFlag,'ReservID'=>$so_no,'ZonaPickeo'=>$zonaPick,'TipoLote'=>$TipoLote);
        $z++;            
        }
     echo json_encode(setDataJSON($docs,1));
}
