<?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;
$total_mois= array() ;
$colissage= array() ;
$tab= array() ;
$obj= array() ;

for ($moiss = 1; $moiss <= 12; $moiss++) {
$total = 0;
$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 = 2018;
																
																$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"];

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

									
									
	$mois = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
	
	
	

		// $query = " select *,SUM(qte) as all_qte from stat_article where sous_famille = ".$famille." AND type = 'BF' AND (type_product= 'VENTE' OR type_product= 'ACHAT') AND annee = ".$annee." AND mois = ".$moiss;
		$query = " select * from stat_article where sous_famille = ".$famille." AND type = 'BF' AND annee = ".$annee." AND mois = ".$moiss;

		$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, '.','');
			}
			
			$tab[$q][$row_cat2["nom"]] = $qte_valooo;
			$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++;
		}
	}
	
	$total += $qte_valooo;
																
																
																
															//==================================
															
														}
													
													// echo "</tr>";
												$q++;
												
												
												}
												
			
												
												
												foreach($tab as $ligne){
														// Lecture de chaque tableau de chaque ligne
														$pourcent = 0;
													foreach($ligne as $cle=>$valeur){
														// Affichage
														if(($valeur > 0)&&($total > 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";
													}
													
													
														
														
														
													}
												}

	
												$total_mois[$moiss] = $total;


												}
												
// === affichage ====	
$valll = "";
echo "{
        name: '".$annee."',
		 data: [
		";											
foreach ($total_mois as &$value) {
    // echo $value ."<br>";
	$valll .=  " ,".number_format(($value),3, '.','');
}

echo substr($valll, 2);
echo "]";
echo "}";
?>