<?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"];
$id_be = $_GET["id_be"];

$qte_sortie = 0 ;
$qte_valide = 0 ;



	//====================== New stock 

	$cont = 0 ;
	$outp = array();

	
	$query_sorti = "SELECT qte FROM pm_depot_stock WHERE `id_depot` = 1 AND id_produits  = ".$id." AND ref_id = ".$id_be;
	// echo $query_sorti;
	$result_sorti = @$db->query($query_sorti);
	if($result_sorti !== false){
	$outp_sorti = $result_sorti->fetch();
		$outp[$cont]["qte"] = $outp_sorti["qte"];
	}	
	//======================
echo json_encode($outp);


exit();







//====================== QTE utiliser
$query_sorti = "SELECT SUM(qte) as all_qte FROM pm_bf_product WHERE id_be = ".$id_be." AND id_produits = ".$id;
// echo $query_sorti;
$result_sorti = @$db->query($query_sorti);
if($result_sorti !== false){
$outp_sorti = $result_sorti->fetch();
	$qte_sortie += $outp_sorti["all_qte"];
}	
//======================
	//====================== BS
	
	$query_sorti = "SELECT SUM(qte) as all_qte FROM pm_br_product WHERE id_be = ".$id_be." AND name = ".$id;
	// echo $query_sorti;
	$result_sorti = @$db->query($query_sorti);
	if($result_sorti !== false){
	$outp_sorti = $result_sorti->fetch();
		$qte_sortie += $outp_sorti["all_qte"];
	}	
	//======================
	
	

	$cont = 0 ;
	$outp = array();



if($id_be > 276 ){
	
	$query = "SELECT qte FROM pm_be_product WHERE id_be = ".$id_be." AND name = ".$id;
	$result = @$db->query($query);
	if($result !== false){
		// $outp = $result->fetch();
		foreach($result as $row){
			
			$qte_valide = $row["qte"] - $qte_sortie;
	
		} 	
	}
	
}else{
	
//====================== QTE ecart

$query_e = "SELECT SUM(qte) as all_qte FROM pm_be_ecart WHERE id_be = ".$id_be." AND id_produit = ".$id;
// echo $query_e;
$result_e = @$db->query($query_e);
if($result_e !== false){
$outp_e = $result_e->fetch();
	// $qte_sortie += $outp_sorti["all_qte"];
	$qte_valide = $outp_e["all_qte"] - $qte_sortie;
}	
//======================	
	
}

if($qte_valide > 0){
	
	$outp[$cont]["qte"] = $qte_valide ;
	
}else{
	$outp[$cont]["qte"] = 0 ;
}



	
	echo json_encode($outp);
	





?>