<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$ocid=$_POST['ocid'];
$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 ".$campo." , productname,quantity
            FROM vtiger_inventoryproductrel
            INNER JOIN vtiger_products ON vtiger_products.productid=vtiger_inventoryproductrel.productid
            INNER JOIN vtiger_productcf ON vtiger_productcf.productid=vtiger_products.productid
            WHERE id='$ocid'");
    $z=0;
    while ($row = mysqli_fetch_array($query)) {           
	    $productcode=$row[0];
        $productname=$row['productname'];
        $qtyinstock=stock($row['quantity']);
        $docs[$z]=array('ProductCode'=>$productcode,'ProductName'=>$productname,'Pqty'=>$qtyinstock);
        $z++; 

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

function loginkey2($usuario,$key,$url){
    if(empty($usuario) || empty($key) || empty($url)){
        return 0;
    }else{
        $include=includec($url);
        include ($include);
        $query=mysqli_query($link,"SELECT *FROM vtiger_users where id='$usuario' AND accesskey='$key'");
        $data= mysqli_num_rows($query);    
        if ($data>0) {
            return 1;    
        }else{
            return 0;    
        }
    }
}


?>
