<?php
// ini_set('display_errors', 1);

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

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

$id = $_GET["id"];
$reste = $_GET["reste"];

if($reste == 1){
//=================================================
$str = "";
$count_prod = 0;
$count_be = array();
$query = "SELECT * FROM pm_be where ref_bc = ".$id;
$result = @$db->query($query);
if($result !== false){
	foreach($result as $row){
		$query_list = "select * from pm_be_product where id_be =". $row["id"];
		$result_list = @$db->query($query_list);
		if($result_list !== false){
			
			foreach($result_list as $row_list){
				
				$query_bep = "SELECT SUM(qte) as cnt from pm_be_product WHERE name = ".$row_list["name"]." AND id_be = ".$row["id"];
				// echo $query_bep ."<br>";
				$result_bep = $db->query($query_bep);
				if($result_bep !== false){
					$row_bep = $result_bep->fetch();
					
					$count_be[$row_list["name"]] += $row_bep["cnt"];					
				}				
					
			
			$qte = $row_list["qte"];
			$coulissage = $row_list["coulissage"];
			// $unite = $row_list["unite"];
			// $solde = $count_be[$row_list["name"]];
			// echo $row_list["name"]." : ".$solde."<br>" ;
			}
		}		
	} 
}	

}

//============================================================================
//============================================================================
//============================================================================
$query = "SELECT * FROM pm_bc_product where id_bc = ".$id;
$result = @$db->query($query);
$cont = 0 ;
$outp = array();
if($result !== false){
	// $outp = $result->fetch();
	foreach($result as $row){
		
		if($reste == 1){
			$outp[$cont]["qte"] = abs($count_be[$row["name"]]-$row["qte"]);
		}else{
			$outp[$cont]["qte"] = $row["qte"] ;
		}
		$outp[$cont]["name"] = $row["name"];
		$outp[$cont]["prix"] = $row["prix"];
		$outp[$cont]["coulissage"] = $row["coulissage"];
		$outp[$cont]["unite"] = $row["unite"];
		
		
		$cont++;	
	} 
	
	echo json_encode($outp);
	
}	




?>