<?php

// ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'Inventaire');
if(!isset($_SESSION['user'])){
    header("Location: ../login.php");
    exit();
}
require_once("../includes/fn_module.php"); ?>
<!DOCTYPE html>
<head>
    <?php include("../includes/inc_header_common.php"); ?>

</head>
<body>
    <div id="wrapper">
        <?php include("../includes/inc_top.php"); ?>
        <div id="page-wrapper">
            <div class="page-header">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-8">
                            <h1><i class="fa fa-line-chart"></i> Analyse de la consommation</h1>
                        </div>
                    </div>
                </div>
            </div>
            <div class="container-fluid">
                <div class="alert-container">
                    <div class="alert alert-success alert-dismissable"></div>
                    <div class="alert alert-warning alert-dismissable"></div>
                    <div class="alert alert-danger alert-dismissable"></div>
                </div>
                <div class="row">				

<?php
									$prod = explode("_", $_GET["product"]);
									$id_product = $prod[1];
									
							$query = "SELECT code_produit,desingnation FROM `pm_produits` where id = ".$id_product;
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row0){
									$code_produit = $row0['code_produit'];
									$name_produit = $row0['desingnation'];

								}
							}	
									
?>		
	<h3><? echo $name_produit; ?></h3>
				
	<form method="GET" action="stock_search.php">				
<div class="table-responsive">

                                <table class="table table-bordered table-hover table-striped tablesorter" id="tablesorter">
                                    
                                    <thead>	
										


														
										<tr>
											<th class="text-center date_sort"  ><i class="fa fa-filter"></i> <b>Code article</b></th>
											<th class="text-center"><b>ID BF</b></th>
											<th class="text-center"><b>DATE BF</b></th>
											<th class="text-center"><b>QTE PF emballé</b></th>
											<th class="text-center"><b>QTE PF Unite</b></th>
											
						<?php
						$id_produits_array = array();
						if($db !== false){
							//========
							// $query = "SELECT id_produits FROM `pm_produits_product` where id_mp  = ".$id_product." AND status = 1";
							
$query = "
    SELECT pa.code_produit ,pp.id_produits, pa.desingnation
    FROM pm_produits_product pp
    JOIN pm_produitsachat pa ON pp.id_produits = pa.id
    WHERE pp.id_mp = ".$id_product." 
    AND pp.status = 1
	ORDER BY 
	  CASE
		WHEN pa.code_produit LIKE 'P%' THEN 1
		WHEN pa.code_produit LIKE 'M%' THEN 2
		WHEN pa.code_produit LIKE 'H%' THEN 3
		ELSE 4
	  END
";
// echo $query;
						
							$test_value = 0;
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row1){	

								echo '<th class="text-center" style="background-color: #fffde8;"><b title="'.$row1["desingnation"].'">'.$row1["code_produit"].'</b></th>';
								$id_produits_array[] = $row1["id_produits"];

								if ((strpos($row1["code_produit"], 'P') === 0 || strpos($row1["code_produit"], 'M') === 0)&&($test_value == 0)) {
									echo '<th class="text-center" style="background-color: #fffde8;"><b>CTN/PELLET</b></th>';
									$test_value = $row1["id_produits"];
								}

								}
							}
														
						} ?>	

										
											
										</tr>
									</thead>
									<tbody>
									    
						<?php
						if($db !== false){
							//========
							$query = "SELECT * FROM `pm_bf` where product = ".$id_product." ";
							$query .= " AND date_bf >= '".strtotime($_GET["date_in"])."' AND date_bf <= '".strtotime($_GET["date_out"])."'";
							$query .= " order by date_bf ASC";
							// echo $query;
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row){	
									echo '<tr>
											<td class="text-center date_sort"  ><b>'.$code_produit.'</b></td>
											<td class="text-center"><b>LOT_'.$row["id"].'</b></td>
											<td class="text-center"><b>'.date("d-m-Y", $row["date_bf"]).'</b></td>
											<td class="text-center"><b>'.$row["qte_embal"].'</b></td>
											<td class="text-center"><b>'.($row["qte"]+$row["qte_lot"]).'</b></td>
										';
										


							//========
							foreach ($id_produits_array as $id_produit) {
								$rpw = '<td class="text-center"><b>0.000</b></td>';
							// echo '<td class="text-center"><b>'.$id_produit.'</b></td>';
								$queryProd = "SELECT SUM(qte) as all_qte
								FROM pm_bf_product
								WHERE id_bf = " . $row["id"] . "
								AND id_produits = " . $id_produit;

								// echo $queryProd."<br>";
									$resultProd = @$db->query($queryProd);
									if($resultProd !== false){
										foreach($resultProd as $row_Prod){
											$valeur = number_format($row_Prod["all_qte"]/($row["qte"]+$row["qte_lot"]), 4);
											// $rpw = '<td class="text-center"><b>'.$row_Prod["qte"].'/'.$row["qte"].'</b></td>';
											$rpw = '<td class="text-center"><b>'.$valeur.'</b></td>';
										}
									}
									
									echo $rpw;
									
									if($id_produit == $test_value){
											$valeur2 = (($row["qte"]+$row["qte_lot"])/$row["Colisage"])/$row_Prod["qte"];
											echo '<td class="text-center"><b>'.number_format($valeur2, 4).'</b></td>';										
									} 
									
									
							}
														
											
										
									echo "</tr>";	
								}	
							}	
							
														
						} ?>									


                                    </tbody>
                                </table>
                            </div>
									

					
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; 


?>



