<?
include("ApiGlobalFunctions.php");

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$invid=$_POST['InventarioID'];
$cbslot=$_POST['SlotID'];
$productcodea=$_POST['ProductCode'];
$cantidad=$_POST['CantidadInv'];
$resultado=$_POST['Resultado'];


$username=username($usuario,$url);

$include=includec($url);
include ($include);
$asignado="19x".$usuario;
$campo=diccionario($url,'Inventario');
$inventario=$campo.$invid;


//error_log("Usuario: ".$usuario." Pass: ".$password." url: ".$url." Inventairo: ".$invid." Slot: ".$cbslot." Assets ".$productcode." cantidad ".$cantidad." Resultado ".$resultado."\n", 3, "php.log");

//echo $inventario;

$query=mysqli_query($link,"SELECT slotsid FROM vtiger_slots INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_slots.slotsid  WHERE deleted='0' AND slots_tks_codigo='$cbslot'");
$data=mysqli_fetch_array($query);
$slotid=$data['slotsid'];
//echo $slotid;

$campo=diccionario($url,'codproduct');
$query=mysqli_query($link,"SELECT vtiger_products.productid, assetsid,serialnumber,".$campo." as prodcod
    FROM vtiger_products 
    INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid     
    INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid  
    INNER JOIN vtiger_assets ON vtiger_assets.product=vtiger_products.productid
    WHERE deleted='0'  AND serialnumber='$productcodea'");
$data=mysqli_fetch_array($query);
$productid=$data['productid'];
$assetsid=$data['assetsid'];
$prodcod=$data['prodcod'];
$producto="14x".$productid;
$baseins="28x".$assetsid;
//echo $productid."--".$assetsid."--".$prodcod;/*

$cantasset=diccionario($url,"QtyAsset");
$queryr=mysqli_query($link,"SELECT ".$cantasset." as cant FROM vtiger_assetscf WHERE assetsid='$assetsid'");
$datar=mysqli_fetch_array($queryr);
$qtyasset=$datar["cant"];
//echo $qtyasset;

if($cantidad==$qtyasset){
    $resultado='Correcto';
}else{
    $resultado='Desigual';
}

error_log("Cantidad: ".$cantidad." Cantasset: ".$qtyasset."\n", 3, "php.log");

$includenat=includenative($usuario,$password,$url);
chdir($includenat);   

include_once 'vtwsclib/Vtiger/WSClient.php';
$userws=diccionario($url,'userws');
$passws=diccionario($url,'keyws');
$user = $userws;
$key  = $passws;
$url  = $url;
$client = new Vtiger_WSClient($url);
$login = $client->doLogin($user, $key);
if(!$login) {
    echo 'Fallo';
}else {          
    //$field=$client->doDescribe('Registrosinventario');
    //print_r($field);
    $RegInvAsset=diccionario($url,'RegInvAsset');
    $resultadori=diccionario($url,'Resultadori');
    $record=$client->doCreate('Registrosinventario',array('registrosinventario_tks_invent'=>$inventario,'registrosinventario_tks_produc'=>$producto,'registrosinventario_tks_codigo'=>$prodcod,'registrosinventario_tks_cantid'=>$cantidad,'registrosinventario_tks_slot'=>$cbslot,'assigned_user_id'=>$asignado,$RegInvAsset=>$baseins,$resultadori=>$resultado));            
    if ($record) {            
        $recordid = $client->getRecordId($record['id']);     
        $date=date('Y-m-d');
        $UltInv=diccionario($url,"UltInv");
        mysqli_query($link,"UPDATE vtiger_assets SET ".$UltInv."='$recordid' WHERE assetsid='$assetsid'");

        mysqli_query($link,"UPDATE vtiger_slots SET slots_tks_fechaultimoinventari='$date' WHERE slotsid='$slotid' ");
        $docs[0]=array('Result'=>'1', 'id'=>$recordid);   
        echo json_encode(setDataJSON($docs,1)); 
    }else{
         $error=$client->lastError();
        $docs[0]=array('Result'=>'0', 'id'=>$error);           
        echo json_encode(setDataJSON($docs,1)); 
    }
}


error_log("ID: ".$recordid."\n", 3, "php.log");


?>
