<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];

$password=$_POST['key'];
$url=$_POST['url'];
$serieBI=$_POST['NSerie'];
$include=includec($url);
include $include;
error_log("ASSETDETAIL. ".$usuario."--".$password."--".$url."--".$serieBI."\n", 3, "php.log");
$validate=loginkey($usuario,$password,$url);

if($validate==1){
    mysqli_set_charset($link, "utf8");
    $codprod=diccionario($url,'codproduct');    
    $lote=diccionario($url,'LoteB');
    $cantasset=diccionario($url,'QtyAsset');
    $fingreso=diccionario($url,'FIngresoB');
    $BodAsset=diccionario($url,'BodAsset');
    $SlotAsset=diccionario($url,'SlotAsset');
    $SeccAsset=diccionario($url,'SeccAsset');
    $RackAsset=diccionario($url,'RackAsset');
    $MovAsset=diccionario($url,'MovAsset');
    $UltInv=diccionario($url,'UltInv');
	$AssetResv=diccionario($url,'AssetResv');
    $EstadoAsset=diccionario($url,'EstadoAsset');
//error_log("ASSETDETAIL. cantasset".$cantasset." - codprod: ".$codprod." fingreso: ".$fingreso." - lote. ".$lote." - BodAsset: ".$BodAsset
   	$query=mysqli_query($link,"SELECT vtiger_assets.assetsid,assetstatus,product,productcategory, bodegas_tks_nombre,slots_tks_seccion,racks_tks_rack,slots_tks_fila,slots_tks_columna,slots_tks_codigo,".$cantasset." as QtyAsset,serialnumber,".$codprod." as codprod,".$fingreso." as FIngresoB,".$lote." as lote, productname, registrosinventario_tks_invent, ".$BodAsset." as bodega, ".$SlotAsset." as slot, ".$RackAsset." as rack, ".$SeccAsset." as seccion,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
        LEFT JOIN vtiger_crmentityrel ON vtiger_crmentityrel.relcrmid=vtiger_assets.assetsid
        LEFT JOIN vtiger_slots ON vtiger_slots.slotsid=vtiger_crmentityrel.crmid
        LEFT JOIN vtiger_bodegas ON vtiger_bodegas.bodegasid=vtiger_slots.slots_tks_bodega
        LEFT JOIN vtiger_racks ON vtiger_racks.racksid=vtiger_slots.slots_tks_rack
        LEFT JOIN vtiger_registrosinventario ON vtiger_registrosinventario.registrosinventarioid=vtiger_assets.".$UltInv."
        WHERE deleted='0' AND serialnumber='$serieBI'  ORDER BY vtiger_slots.slotsid DESC limit 1");


    $data= mysqli_fetch_array($query);      
error_log("ASSETDETAIL. ".print_r($data,true)."\n", 3, "php.log");
    $assetsid=$data['assetsid'];
    $estadoBI=$data['assetstatus'];
    $productid=$data['product'];
    /*$bodega=$data['bodegas_tks_nombre'];
    $seccion=$data['slots_tks_seccion'];
    $rack=$data['racks_tks_rack'];
    $fila=$data['slots_tks_fila'];
    $slot=$data['slots_tks_codigo'];
    $cantidad=$data['QtyAsset'];
    $lotebi=$data['lote'];  */
     
    $bodega=$data['bodega'];
    $productcategory=$data['productcategory'];
    $seccion=$data['seccion'];
    $rack=$data['rack'];
    $slot=$data['slot'];
    $cantidad=$data['QtyAsset'];
    $lotebi=$data['lote'];
    
    $fila=$data['slots_tks_fila'];
    $Columna=$data['slots_tks_columna'];        
    $productcode=$data['codprod'];    
    $FIngreso=$data['FIngresoB'];        
    $productname=$data['productname'];  
    $rinventario=$data['registrosinventario_tks_invent'];  
	$ReservFlag=$data['ReservFlag'];
    $ReservID=$data['ReservID'];

    if(!empty($ReservID)){
      $query2=mysqli_query($link,"SELECT *
      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
      WHERE assetstatus='$EstadoAsset' AND ".$AssetResv."='$ReservID' AND ".$codprod."='$productcode'");
      $num=mysqli_num_rows($query2);
      if($num>=1){
        $cnReserva=1;
      }else{
        $cnReserva=0;
      }
    }

    $estadoAsset=diccionario($url,'EstadoAsset');  
    $description=$data['description'];
    
    $queryfecha=mysqli_query($link, "SELECT ".$fingreso." as fecha, vtiger_assets.assetsid as asset
            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
            WHERE deleted='0' AND  product='$productid' AND assetstatus='".$estadoAsset."' AND ".$cantasset.">0 AND ".$fingreso." IS NOT NULL ORDER BY 1 ASC LIMIT 1");
    $datafecha= mysqli_fetch_array($queryfecha);     
    $assetfecha=$datafecha['asset'];
    
    if($assetfecha==$assetsid){
        $oldest='1';//la mas vieja
    }else{
        $oldest='0';//no es la mas vieja
    }




    $docs[0]=array('Bodega'=>$bodega,'Seccion'=>$seccion,'Rack'=>$rack,'Fila'=>$fila,'Columna'=>$Columna,'Slot'=>$slot,'Cantidad'=>$cantidad,'NSerie'=>$serieBI,'AssetStatus'=>$estadoBI,'AssetID'=>$assetsid,'ProductCode'=>$productcode,'FechaIngresoBod'=>$FIngreso,'TipoLote'=>$lotebi,'ProductName'=>$productname,'UltimoInvID'=>$rinventario,'Oldest'=>$oldest,'ReservFlag'=>$ReservFlag,'ReservID'=>$ReservID,'ProductCategory'=>$productcategory,'description'=>$description,'OsResv'=>$cnReserva);

       echo json_encode(setDataJSON($docs, 1));
}
?>
