<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$inventario=$_POST['InvID'];
$tipo=$_POST['Tipo'];
$include=includec($url);
include $include;
$validate=loginkey($usuario,$password,$url);
$validate=1;
if($validate==1){
    $codprod=diccionario($url,'codproduct');
    $catinv=diccionario($url,'CatInventario');
    $bodega_field=diccionario($url,'BodAsset');
    $slot_field=diccionario($url,'SlotAsset');
    $rack_field=diccionario($url,'RackAsset');
    $seccion_field=diccionario($url,'SeccAsset');
    $cantasset_field=diccionario($url,'QtyAsset');
    $estadoAsset=diccionario($url,'EstadoAsset');
    $seccionrack=diccionario($url,'SeccionRack');
    mysqli_set_charset($link, "utf8");
    $query=mysqli_query($link, "SELECT inventario_tks_bodega, bodegas_tks_nombre
    FROM vtiger_inventario 
    INNER JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_inventario.inventario_tks_bodega
    WHERE inventarioid='$inventario'");
    $data=mysqli_fetch_array($query);  
    $bodega=$data['inventario_tks_bodega'];
    $bodegaNombre=$data['bodegas_tks_nombre'];
    if($tipo=='Productos'){
        $query=mysqli_query($link, "SELECT rel_inventario.*
        FROM rel_inventario 
        INNER JOIN vtiger_products ON vtiger_products.productid=rel_inventario.productid
        WHERE rel_inventario.inventarioid='$inventario' AND asignado='$usuario'");
        $num=mysqli_num_rows($query); 
        if($num>0){
            $z=0;
            while ($row = mysqli_fetch_array($query)) {           
                $producto=$row['productid'];
                $querySlots=mysqli_query($link, "SELECT ".$slot_field." as slot, SUM(".$cantasset_field.") as 'cantidad', ".$bodega_field." as bodega, ".$rack_field." as rack, ".$seccion_field." as seccion, productname, ".$codprod." as productcode
                FROM vtiger_assets
                INNER JOIN vtiger_assetscf ON vtiger_assets.assetsid=vtiger_assetscf.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 vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_assets.assetsid
                INNER JOIN vtiger_slots ON vtiger_slots.slotsid=vtiger_crmentityrel.crmid
                WHERE deleted='0' AND product='$producto' AND assetstatus='$estadoAsset' AND slots_tks_bodega='$bodega'
                GROUP BY 1");  
                $num2=mysqli_num_rows($querySlots);
                while ($row2 = mysqli_fetch_array($querySlots)) { 
                    $producto=$row2['productname'];
                    $codigo=$row2['productcode'];
                    $bodega=$row2['bodega'];
                    $seccion=$row2['seccion'];
                    $rack=$row2['rack'];
                    $slot=$row2['slot'];
                    $cantidad=$row2['cantidad'];
                    $slotreg=mysqli_query($link, "SELECT *FROM registros_inventario WHERE slot='$slot' AND inventarioid='$inventario'");
                    $numslotreg=mysqli_num_rows($slotreg);
                    if($numslotreg>0){
                        $slotProccess='1';
                    }else{
                        $slotProccess='0';
                    }
                    $docs[$z]=array('productid'=>$producto,'productname'=>$producto,'productcode'=>$codigo,'bodega'=>$bodegaNombre,'seccion'=>$seccion,'rack'=>$rack,'slot'=>$slot,'cantidad'=>$cantidad,'slotProccess'=>$slotProccess);
                    $z++;  
                }    
            }    
        }/*else{
            $docs[0]=array('productid'=>'','productname'=>'','productcode'=>'','bodega'=>'','seccion'=>'','rack'=>'','slot'=>'','cantidad'=>'');
        }  */  
    }else if($tipo=='Slots'){
        /*$query=mysqli_query($link, "SELECT vtiger_slots.slotsid, slots_tks_codigo,slotsno, bodegas_tks_nombre,secciones_tks_seccion, racks_tks_rack
        FROM rel_inventario 
        INNER JOIN vtiger_slots ON vtiger_slots.slotsid=rel_inventario.slotsid
        INNER JOIN vtiger_slotscf ON vtiger_slotscf.slotsid=vtiger_slots.slotsid 
        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_slots.slotsid 
        LEFT 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 vtiger_crmentity.deleted='0' AND  inventarioid='$inventario' AND asignado='$usuario' AND slots_tks_bodega='$bodega'");*/
        $query=mysqli_query($link, "SELECT vtiger_slots.slotsid, slots_tks_codigo,slotsno,secciones_tks_seccion, racks_tks_rack
        FROM rel_inventario 
        INNER JOIN vtiger_slots ON vtiger_slots.slotsid=rel_inventario.slotsid
        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_slots.slotsid 
        INNER JOIN vtiger_racks ON vtiger_racks.racksid=vtiger_slots.slots_tks_rack         
        INNER JOIN vtiger_secciones ON vtiger_secciones.seccionesid=vtiger_slots.slots_tks_seccion
        WHERE vtiger_crmentity.deleted='0' AND  inventarioid='$inventario' AND asignado='$usuario' AND slots_tks_bodega='$bodega'");
        $z=0;
        $num=mysqli_num_rows($query);
        if($num>0){
            while ($row2 = mysqli_fetch_array($query)) { 
                $slotid=$row2['slotsid'];
                $codigo=$row2['slots_tks_codigo'];
                $slotno=$row2['slotsno'];
                $seccion=$row2['secciones_tks_seccion'];
                $rack=$row2['racks_tks_rack'];
                $slotreg=mysqli_query($link, "SELECT *FROM registros_inventario WHERE slot='$codigo' AND inventarioid='$inventario' AND userid='$usuario'");
                $numslotreg=mysqli_num_rows($slotreg);
                if($numslotreg>0){
                    $slotProccess='1';
                }else{
                    $slotProccess='0';
                }
                $docs[$z]=array('slotid'=>$slotid,'codigo'=>$codigo,'slot_no'=>$slotno,'bodega'=>$bodegaNombre,'seccion'=>$seccion,'rack'=>$rack,'slotProccess'=>$slotProccess);
                $z++;  
            }
        }else{
            $docs[0]=array('slotid'=>'','codigo'=>'','slot_no'=>'','bodega'=>'','seccion'=>'','rack'=>'');
        }    
    }else if($tipo=='Racks'){
        $query=mysqli_query($link, "SELECT rel_inventario.slotsid, slots_tks_codigo, slotsno, vtiger_racks.racksid, racksno, secciones_tks_seccion, vtiger_racks.racks_tks_rack
        FROM rel_inventario 
        INNER JOIN vtiger_slots ON vtiger_slots.slotsid=rel_inventario.slotsid
        INNER JOIN vtiger_racks ON vtiger_racks.racksid=rel_inventario.rackid
        INNER JOIN vtiger_rackscf ON vtiger_rackscf.racksid=vtiger_racks.racksid
        INNER JOIN vtiger_seccionescf ON vtiger_seccionescf.seccionesid=vtiger_rackscf.$seccionrack
        INNER JOIN vtiger_secciones ON vtiger_secciones.seccionesid=vtiger_seccionescf.seccionesid
        WHERE inventarioid='$inventario' AND asignado='$usuario' AND racks_tks_bodega='$bodega'");
        $z=0;
        $num=mysqli_num_rows($query); 
        if($num>0){
            while ($row2 = mysqli_fetch_array($query)) { 
                $slotid=$row2['slotsid'];
                $slot_code=$row2['slots_tks_codigo'];
                $slotsno=$row2['slotsno'];
                $rackid=$row2['racksid'];
                $codigorack=$row2['racks_tks_rack'];
                $rackno=$row2['racksno'];
                $seccion=$row2['secciones_tks_seccion'];
                $slotreg=mysqli_query($link, "SELECT *FROM registros_inventario WHERE slot='$slot_code' AND inventarioid='$inventario' AND userid='$usuario'");
                $numslotreg=mysqli_num_rows($slotreg);
                if($numslotreg>0){
                    $slotProccess='1';
                }else{
                    $slotProccess='0';
                }
                $docs[$z]=array('slotid'=>$slotid,'codigo'=>$slot_code,'slot_no'=>$slotsno,'bodega'=>$bodegaNombre,'seccion'=>$seccion,'rack'=>$codigorack,'slotProccess'=>$slotProccess);
                $z++;
            }    
        }else{
            $docs[0]=array('slotid'=>'','codigo'=>'','slot_no'=>'','bodega'=>'','seccion'=>'','rack'=>'');
        }
    }
	echo json_encode(setDataJSON($docs,1));
}
