<?php

include("ApiGlobalFunctions.php");

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];

/*
$host="localhost";
$user="globa279_master";
$pass="123*master2";
$bd="globa279_smxdev";

@$link = mysql_connect($host,$user,$pass);
mysql_select_db($bd, $link);
mysql_query("SET NAMES utf8");

mysql_query("SET NAMES 'utf8'");*/
mysqli_set_charset($link, "utf8");
$include=includec($url);
include $include;
$validate=loginkey($usuario,$password,$url);

if($validate==1){
mysqli_set_charset($link, "utf8");
$campo=diccionario($url,'codproduct');
$query=mysqli_query($link,"SELECT crmid,productname, ".$campo. ",productcategory,qtyinstock,createdtime,description 
        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 
        WHERE deleted='0' AND discontinued='1' AND qtyinstock>0  ORDER BY modifiedtime DESC");    
 $num=mysqli_num_rows($query);
$z=0;
while ($row = mysqli_fetch_array($query)) {           
            $productid=$row['crmid'];            
            $productname=$row['productname'];            
            $productcode=$row[2];
            $productcategory=$row['productcategory'];
            $qtyinstock=stock($row['qtyinstock']);                        
            $fechacreacion=$row['createdtime'];
            $fechacreacion=explode(" ",$fechacreacion);
            $fechacreacion=$fechacreacion[0];
            $description=$row['description'];
            $docs[$z]=array('productid'=>$productid,'productname'=>$productname,'productcode'=>$productcode,'productcategory'=>$productcategory,'qtyinstock'=>$qtyinstock,'fechacreacion'=>$fechacreacion,'descripcion'=>$description);
            $z++;            
        }    
//print_r($docs);
}
$docs = isset($docs) ? $docs : ['Result'=>0];
echo json_encode(setDataJSON($docs,1));

?>
