<?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"];

$qte_array = array();
//======================================================
$query = "SELECT * FROM pm_bc_product where id_bc = ".$id;
$result = @$db->query($query);
if($result !== false){
	echo '<table align="center" cellpadding="6" cellspacing="6" border="1" width="100%"><tr style=" background-color: orange; "><th>Désingnation</th><th>Qte<br>commandée</th><th>Unité</th><th>Solde</th></tr>';
	foreach($result as $row){
		$query_p = "SELECT desingnation FROM pm_produitsachat where id = ".$row["name"];
		$result_p = @$db->query($query_p);
		if($result_p !== false){
			$row_p = $result_p->fetch();
			$desingnation = $row_p["desingnation"];
		}	
		
		$query_c = "SELECT name FROM pm_unite where id = ".$row["unite"];
		$result_c = @$db->query($query_c);
		if($result_c !== false){
			$row_c = $result_c->fetch();
			$unite = ucfirst($row_c["name"]);
		}
      	
		$qte = $row["qte"];
		$qte_array[$row["name"]] = $row["qte"];
		$coulissage = $row["coulissage"];
		// $unite = $row["unite"];
		

		 echo '<tr style=" background-color: orange; "><td>'.$desingnation.'</td><td style="text-align:center;">'.$qte.'</td><td style="text-align:center;">'.$unite.'</td><td></td></tr>';
     } 

	

}	

//======================================================
//======================================================
//======================================================
$str = "";
$count_prod = 0;
$count_be = array();
$query = "SELECT * FROM pm_be where ref_bc = ".$id;
$result = @$db->query($query);
if($result !== false){
	// $str = '<table align="center" cellpadding="6" cellspacing="6" border="1" width="100%">';
	foreach($result as $row){
		
		$str .= "<tr><td colspan='4' style='text-align:center;background-color: #cccccc;'><b>Bon d'entré N° BE_00".$row["id"]."/ ".$row["ref_bl"]." / ".date('d-m-Y', $row["date_entre"])."</b></td></tr>";
		$str .="<tr><th>Désingnation</th><th>Qte</th><th>Unité</th><th>Solde</th></tr>";
		$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"];					
				}				
				
			// =============
			$query_p = "SELECT desingnation FROM pm_produitsachat where id = ".$row_list["name"];
			$result_p = @$db->query($query_p);
			if($result_p !== false){
				$row_p = $result_p->fetch();
				$desingnation = $row_p["desingnation"];
			}
			//=============
			$query_c = "SELECT name FROM pm_unite where id = ".$row_list["unite"];
			$result_c = @$db->query($query_c);
			if($result_c !== false){
				$row_c = $result_c->fetch();
				$unite = ucfirst($row_c["name"]);
			}			
			
			$qte = $row_list["qte"];
			$coulissage = $row_list["coulissage"];
			// $unite = $row_list["unite"];			
			$solde = ($qte_array[$row_list["name"]] - $count_be[$row_list["name"]]);
			

			 $str .= '<tr><td >'.$desingnation.'</td><td align="center" >'.$qte.'</td><td align="center" >'.$unite.'</td><td align="center" >'.$solde.'</td></tr>';
					
			// =============	
			$count_prod++;	
			}
		}		
	} 
	 $str .= '</table>';
	
	if($count_prod <> 0){
		echo $str ;
	}
	
}	




?>