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


$cat = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
$total = 0;
$tab[1] = array() ;
$tab[2] = array() ;
$tab[3] = array() ;
$tab[4]= 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"];
													$query_cat2 = "select * from pm_famille where famille_classe = ".$row_cat["id"];
													// echo $query_cat2."<br>";
													$result_cat2 = @$db->query($query_cat2);
													// echo "data: [";
														
													
													
														// $tab_1[0] = 13000; 
														$b = 0;
														foreach($result_cat2 as $row_cat2){													
															$famille = $row_cat2["id"];
															//===================================
																$source = "type = 'BF' AND (type_product= 'VENTE' OR type_product= 'ACHAT')";
																$type = "ACHAT/VENTE";
																$annee = date('o');
																
																$listage = "mensuel";
	
	// $moiss = 4 ;
	$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;

	// echo $query."<br>" ;
	$qte_valooo = 0;
	
	$result = @$db->query($query);
	if($result !== false){
		foreach($result as $row){
			if($row["all_qte"]){
			// $qte_valooo= $row["all_qte"] / $row["coulissage"];
			$qte_valooo= number_format($row["all_qte"] / $row["coulissage"],3, '.','');
			}
			if($row_cat["id"] == 1){
				$tab[1][$b] = $qte_valooo;
			}elseif($row_cat["id"]== 2){
				$tab[2][$b] = $qte_valooo;
			}elseif($row_cat["id"]== 3){
				$tab[3][$b] = $qte_valooo;
			}else{
				$tab[4][$b] = $qte_valooo;
			}	
			$total += $qte_valooo;
			// echo $qte_valooo."<br>";
			$b++;
		}
	}
																
																
																
															//==================================
															
														}
													
													// echo "]";
												$q++;
													
												}
	//=========================
	$data_1 = "data: [";
	for ($x = 1; $x <= 4; $x++) {
		if(isset($tab[$x][0])){
			$data_1 .= $tab[$x][0].",";	 
		}else{
			$data_1 .= "0,";
		}
	}
	$data_1 .= "]";
	//=========================
	$data_2 = "data: [";
	for ($x = 1; $x <= 4; $x++) {
		if(isset($tab[$x][1])){
			$data_2 .= $tab[$x][1].",";
		}else{
			$data_2 .= "0,";
		}
	}
	$data_2 .= "]";

	echo "series: [{
        name: '15g',
        ".str_replace(",]","]",$data_1)."

    }, {
        name: '70g',
        ".str_replace(",]","]",$data_2)."

    }],";
	echo "    subtitle: {
        text: 'TOTAL : ".$total."'
    },";
	
	// echo str_replace(",]","]",$data_1)."<br>";
	// echo str_replace(",]","]",$data_2) ;
	
	
	// echo json_encode($tab_1)."<br>";
	// echo json_encode($tab_2)."<br>";
	// echo json_encode($tab_3)."<br>";
	// echo json_encode($tab_4)."<br>";
	



?>