<?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();
}




$cat = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
$total = 0;
$colissage= array() ;
$tab= array() ;
$obj= array() ;



$query_cat = "select * from pm_famille_classe  where famille_cat = 2";
											// echo $query_cat."<br>";
											$q = 1;
											$result_cat = @$db->query($query_cat);
												foreach($result_cat as $row_cat){												
													$cat[$row_cat["id"]] = $row_cat["nom"];
													// echo $row_cat["nom"]."<br>";
													$query_cat2 = "select * from pm_famille where famille_classe = ".$row_cat["id"];
													// echo $query_cat2."<br>";
													
													$result_cat2 = @$db->query($query_cat2);
													// echo "data: [";
														
													// echo "<tr>";
													
														// $tab_1[0] = 13000; 
														$b = 0;
														foreach($result_cat2 as $row_cat2){	
														
															$famille = $row_cat2["id"];
															// echo "<td>".$row_cat2["nom"]."</td>";
															//===================================
																$source = "type = 'BF' AND (type_product= 'VENTE' OR type_product= 'ACHAT')";
																$type = "ACHAT/VENTE";
																$annee = date('o');
																
																$listage = "mensuel";
	
	// $moiss = 8 ;
	$moiss = date("n") ;


	$query_prod = "select * from pm_famille where id =".$famille;
	// echo $query_prod ."<br>";
	$result_prod = @$db->query($query_prod);
	foreach($result_prod as $row_prod){

		$name_famille = $row_prod["nom"];

	}	
//==================================
	$query_bf = "SELECT `date_int`,`id_ref` FROM `stat_article` where type = 'BF' AND id_ref <> '' order by `id_ref` DESC LIMIT 1";
	// echo $query_bf ."<br>";
	$result_bf = @$db->query($query_bf);
	foreach($result_bf as $row_bf){

		$id_bf = $row_bf["id_ref"];
		$date_bf = date("d/m/Y",$row_bf["date_int"]);

	}		
//==================================	
	// echo "<br><b>$name_famille</b><br>";

									
	$annee = date("Y");								
	$mois = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
	
	
	

		// $query = " select * from stat_article where sous_famille = ".$famille." AND type = 'BF' AND annee = ".$annee." AND mois = ".$moiss;
		$query = " select * from stat_article where sous_famille = ".$famille." AND type = 'BF'  AND mois = ".$moiss." AND annee = ".$annee;

		$query_obj = " select *,SUM(mois_".$moiss.") as total_obj from pm_objectif where sous_famille = ".$famille;
		$result_obj = @$db->query($query_obj);
		if($result_obj !== false){
			foreach($result_obj as $row_obj){
				$total_obj = $row_obj["total_obj"];
				// echo $total_obj ;
			}	
		}
	// echo $query."<br>" ;
	$qte_valooo = 0;
	
	$result = @$db->query($query);
	if($result !== false){
		foreach($result as $row){
			if($row["qte"]){
			// $qte_valooo= $row["all_qte"] / $row["coulissage"];
			$qte_valooo += number_format($row["qte"] / $row["coulissage"],3, '.','');
			}
			
			
			$colissage[$row_cat2["nom"]] = $row["coulissage"];
			$obj[$row_cat2["nom"]] = $total_obj;
			// $name[$q][$b] = $row_cat2["nom"];	
			// $tab[$q[$b]] = $qte_valooo;	
			
			// echo "<td>".$qte_valooo."</td></tr>";
			$b++;
		}
	}
	
	$tab[$q][$row_cat2["nom"]] = $qte_valooo;
	$total += $qte_valooo;
																
																
																
															//==================================
															
														}
													
													// echo "</tr>";
												$q++;
												
												
												}
												
												echo '<table border="1" width="100%" class="table table-bordered table-hover table-striped">';
												
												echo '							<thead>
							<tr>
								<td colspan="7" align="center" style="background: #2F9DF2;color:#FFF;"><b>Production ( jusqu à LOT_'.$id_bf.' le '.$date_bf.' )</b></td>
							</tr>
							';
								echo "<tr><th><b>Sous-famille</b></th><th><b>Production (en carton)</b></th><th><b>% au total</b></th><th><b>Real / Objectif</b></th></tr>";				
												
												foreach($tab as $ligne){
														// Lecture de chaque tableau de chaque ligne
														
													foreach($ligne as $cle=>$valeur){
														// Affichage
														$pourcent = 0;
														if($valeur > 0){
															$pourcent = ($valeur/$total)*100 ;
														}
														
														if(($valeur > 0)&&($obj[$cle] > 0)){
															$objectif = ($valeur/($obj[$cle]/$colissage[$cle]))*100;
														}else{
															$objectif = 0;
														}
														
	
														$status = round($objectif);

																								
													if($status < 10){
														$color_satatus = "red";
													}elseif($status < 50){
														$color_satatus = "orange";
													}elseif($status < 100){
														$color_satatus = "blue";
													}else{
														$color_satatus = "green";
													}
													
													$prc = '
														<div class="w3-light-grey">
														  <div class="w3-container w3-round w3-'.$color_satatus.'" style="width:'.$status.'%;max-width:100%;color:#000 !important;font-weight: bold;">'.$status.'%</div>
														</div>
														';
														
														
														echo "<tr><td>".$cle."</td><td align='right'>".number_format($valeur,0, '.',' ')."</td><td align='center'>".round($pourcent)."%</td><td>".$prc."</td></tr>";
													}
												}
												
												echo "<tr><td></td><td align='right'><b>".$total."</b></td><td></td><td></td></tr>";																			
												echo "</table>";
	
	



?>