<?php
ini_set('display_errors', 0);

require_once("../../common/lib.php");
require_once("../../common/define.php");

if(!isset($_SESSION['user'])){
    header("Location: login.php");
    exit();
}

$id = $_GET["id"];

$query_be = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produitsachat as a,pm_be_product as b where a.id = b.name AND a.id = ".$id;
$query_retour = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produitsachat as a,pm_br_product as b where a.id = b.name AND a.id = ".$id;
$query_fabri = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produitsachat as a,pm_bf_product as b where a.id = b.id_produits AND a.id = ".$id;

$result_be = @$db->query($query_be);
$result_retour = @$db->query($query_retour);
$result_fabri = @$db->query($query_fabri);

if($result_be !== false){
	$row_be = $result_be->fetch();																		
	$qte_be = $row_be["cnt"];
}

if($result_retour !== false){
	$row_retour = $result_retour->fetch();																		
	$qte_retour = $row_retour["cnt"];
}

if($result_fabri !== false){
	$row_fabri = $result_fabri->fetch();																		
	$qte_fabri = $row_fabri["cnt"];
}

$qte_all = $qte_be - ($qte_retour + $qte_fabri) ;

	if(isset($qte_all)){
        $data = array(
            "id"     => $id,
            "qte_en_stock"     => $qte_all,
        );
        echo json_encode($data);
    }



?>