<?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 = "SELECT * from pm_bc where valider = 0 AND fournisseur = ".$id;
// echo $query;
$result = @$db->query($query);
$cont = 0 ;
$outp = array();
if($result !== false){
	// $outp = $result->fetch();
	foreach($result as $row){
		
		$count_be = 0 ;
		$query_bc = "SELECT SUM(qte) as bc FROM `pm_bc_product` WHERE id_bc = ".$row["id"];
		$result_bc = $db->query($query_bc);
		if($result_bc !== false && $db->last_row_count() > 0){
			$row_bc = $result_bc->fetch();																
			$count_bc = $row_bc['bc'];
			}
		$query_be = "SELECT * from pm_be WHERE ref_bc = ".$row["id"];
		// echo $query_be."<br>";
		$result_be = $db->query($query_be);
		if($result_be !== false){
			foreach($result_be as $row_be){
				$query_bep = "SELECT qte from pm_be_product WHERE id_be = ".$row_be["id"];
				// echo $query_bep.'<br>';
				$result_bep = $db->query($query_bep);
				if($result_bep !== false){
					foreach($result_bep as $row_bep){
					$count_be = $count_be + $row_bep["qte"];
					}
				}	
																
			}
		}
		$status = ($count_be*100) / $count_bc;	
		// echo $row["id"]."==>".$status."<br>";
		if($status < 100){	
			$outp[$cont]["id"] = $row["id"];
			
			$cont++;
		} 	
	} 
	
	echo json_encode($outp);
	
}	




?>