<?php

ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
define("TITLE_ELEMENT", 'Statistiques articles');
if(!isset($_SESSION['user'])){
    header("Location: ../../login.php");
    exit();
}
require_once("../../includes/fn_module.php"); 

header('Content-Type: text/html; charset=utf-8');
ini_set('default_charset', 'utf-8');
setlocale(LC_ALL, 'fr_FR');
setlocale(LC_TIME, "fr_FR");

?>
<!DOCTYPE html>
<head>
    <?php include("../../includes/inc_header_common.php"); ?>
    <?php include("../../includes/functions.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-6">
                            <h1><i class="fa fa-registered"></i> Statistiques articles</h1>
                        </div>
						<div class="col-md-6 text-right" style="margin-top:15px;">
							<a href="#" target="_blank"><button class="btn btn-default" type="button" id="print_page"><i class="fa fa-print"></i> Imprimer</button></a>
						</div>
                    </div>
                </div>
            </div>
            <div class="container-fluid">

                <div class="row">	
			
				
				<div class="table-responsive">
				
				
			    <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    
									<thead>
										<tr>
											<th></th>
											<th><b>Janvier</b></th>
											<th><b>Février</b></th>
											<th><b>Mars</b></th>
											<th><b>Avril</b></th>
											<th><b>Mai</b></th>
											<th><b>Juin</b></th>
											<th><b>Juillet</b></th>
											<th><b>Août</b></th>
											<th><b>Septembre</b></th>
											<th><b>Octobre</b></th>
											<th><b>Novembre</b></th>
											<th><b>Décembre</b></th>
											<th><b>Total<br>de l'année</b></th>	
										<tr>										
									</thead>	
                                    <tbody>
									<tr>
									
									<?
									
									$source = $_GET["source"];						
									
									$annee = $_GET["annee"];
									switch ($source) {
										case "BF":
											$type = "ACHAT/VENTE";
											$prod_table = "pm_produitsachat,pm_produits";
											$source = " type = 'BF' ";
											break;
										case "BE_BS":
											$type = "ACHAT";
											$prod_table = "pm_produitsachat";
											$source = " (type = 'BE' OR type = 'BS') ";
											break;
										case "BE":
											$type = "ACHAT";
											$prod_table = "pm_produitsachat";
											$source = " type = 'BE' ";
											break;
										case "BS":
											$type = "ACHAT";
											$prod_table = "pm_produitsachat";
											$source = " type = 'BS' ";
											break;
										case "FA":
											$type = "ACHAT";
											$prod_table = "pm_produitsachat";
											$source = " type = 'FA' ";
											break;
										case "BL_BR":
											$type = "VENTE";
											$prod_table = "pm_produits";
											$source = " (type = 'BL' OR type = 'BR') ";
									}
									
									
									if (!empty($_GET["cat_famille"])) {
										$cat_famille = $_GET["cat_famille"];
										for($i=0;$i<sizeof($cat_famille);$i++)
										{						
											$query_cat = "select * from pm_famille_classe where famille_cat = ".$cat_famille[$i];
											// echo $query_cat."<br>";
											$result_cat = @$db->query($query_cat);
												foreach($result_cat as $row_cat){
													echo "<tr><td colspan='14' align='center' style='background-color: #ff9425;color:#000'><b>".$row_cat["nom"]."</b></td>";
													
													if($_GET["trier"] == "famille"){
														get_stat_famille($row_cat["id"],$source,$type,$annee[0]);
													}else{
													
													$query_cat2 = "select * from pm_famille where famille_classe = ".$row_cat["id"];
													// echo $query_cat2."<br>";
													$result_cat2 = @$db->query($query_cat2);
														foreach($result_cat2 as $row_cat2){													
															
															echo "<tr><td colspan='14' align='center' style='background-color: #00a1af;color:#000'><b>".$row_cat2["nom"]."</b></td>";
															
															//===================================
															if($_GET["trier"] == "sous_famille"){
																get_stat_sous_famille($row_cat2["id"],$source,$type,$annee[0]);
															}elseif( $_GET["trier"] == "article"){																
															
																$query_cat3 = "select * from ".$prod_table." where famille = ".$row_cat2["id"];
																// echo $query_cat3."<br>";
																$result_cat3 = @$db->query($query_cat3);
																	foreach($result_cat3 as $row_cat3){
																		// echo $row_cat3["id"] ."<br>";
																		get_stat_article($row_cat3["id"],$source,$type,$annee[0]);
																	}
															}	
															//==================================
															
														}
													}
												}
											
										}
										
									}elseif(!empty($_GET["famille"])) {
										$famille = $_GET["famille"];
										for($i=0;$i<sizeof($famille);$i++)
										{
											$query_cat2 = "select * from pm_famille where famille_classe = ".$famille[$i];
													// echo $query_cat2."<br>";
													$result_cat2 = @$db->query($query_cat2);
														foreach($result_cat2 as $row_cat2){
															echo "<tr><td colspan='14' align='center' style='background-color: #00a1af;color:#000'><b>".$row_cat2["nom"]."</b></td>";
															
															//===================================
															if($_GET["trier"] == "sous_famille"){
																// echo $row_cat2["id"]." >>>>>>><br>";
																get_stat_sous_famille($row_cat2["id"],$source,$type,$annee[0]);
															}elseif( $_GET["trier"] == "article"){
															
																$query_cat3 = "select * from ".$prod_table." where famille = ".$row_cat2["id"];
																// echo $query_cat3."<br>";
																$result_cat3 = @$db->query($query_cat3);
																	foreach($result_cat3 as $row_cat3){
																		// echo $row_cat3["id"] ."<br>";
																		get_stat_article($row_cat3["id"],$source,$type,$annee[0]);
																	}
															}
															//===================================	
														}
										}				
										
									}elseif(!empty($_GET["sous_famille"])) {
										$sous_famille = $_GET["sous_famille"];
										for($i=0;$i<sizeof($sous_famille);$i++)
										{
											
											//===================================
												if($_GET["trier"] == "sous_famille"){
													
													get_stat_sous_famille($sous_famille[$i],$source,$type,$annee[0]);
													
												}elseif( $_GET["trier"] == "article"){
													
													$query_cat3 = "select * from ".$prod_table." where famille = ".$sous_famille[$i];
															// echo $query_cat3."<br>";
															$result_cat3 = @$db->query($query_cat3);
																foreach($result_cat3 as $row_cat3){
																	// echo $row_cat3["id"] ."<br>";
																	get_stat_article($row_cat3["id"],$source,$type,$annee[0]);
																}
												}
												//=================================		
										}				
										
									}elseif(!empty($_GET["article"])) {
										$article = $_GET["article"];
										for($i=0;$i<sizeof($article);$i++)
										{						
									
												get_stat_article($article[$i],$source,$type,$annee[0]);
											
										}				
										
									}else{
										echo "<h3 style='text-align:center;color:red;'>Merci de choisir une catégorie famille / Famille / Sous-famille / Article(s)</h3>";
									}

									
									
									?>
									
                                    </tbody>
                                </table>
																
								
                </div>
				
				<br><br>

			
					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<style>
@media print{

@page {size: landscape}
#wrapper {    margin-left: 0px;}
#page-wrapper{
	padding : 0px;
}
body {
	padding : 20px;
	
}
table{
	border-collapse:collapse !important;
	font-size: 7px;
	border-spacing: 2px !important;
}
h1{
	font-size: 14px;
	margin: 0;
}
table td, table th
{
    padding: 2px !important;
}
}
</style>

<?

function  get_stat_article($article,$source,$type,$annee)
{
	global $db;
	
	if($type == "ACHAT"){
		$prod_table = "pm_produitsachat";
		$query_prod = "select code_produit,desingnation,coulissage,unite from ".$prod_table." where id =".$article;
	}elseif($type == "VENTE"){
		$prod_table = "pm_produits";
		$query_prod = "select code_produit,desingnation,coulissage,unite from ".$prod_table." where id =".$article;
	}else{
		$prod_table = "pm_produitsachat,pm_produits";
		$query_prod = "select code_produit,desingnation,coulissage,unite from ".$prod_table." where id =".$article;
	}
		
	
	// echo $query_prod ."<br>";
	$result_prod = @$db->query($query_prod);
	foreach($result_prod as $row_prod){
		$code_produit = $row_prod["code_produit"];
		$name_prod = $row_prod["desingnation"];
		$coulissage = $row_prod["coulissage"];
		$unite = $row_prod["unite"];
	}
	
	echo "<tr><td colspan='14' align='center' style='background-color: #dadada;color:#000'><b>$code_produit - $name_prod - Colisage: $coulissage - unité: $unite</b></td>";
	echo "<tr>";
	echo "<td><b>2018 / variation</b></td>";
									
									
	$mois = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
	if($type == ""){
		$query = " select * from stat_article where id_product = ".$article." AND ".$source." AND annee = ".$annee;
	}else{		
		$query = " select * from stat_article where id_product = ".$article." AND ".$source." AND type_product = '".$type."' AND annee = ".$annee;
	}
	// echo $query."<br>" ;
	$result = @$db->query($query);
	if($result !== false){
		foreach($result as $row){
			$mois[$row["mois"]] += $row["qte"];
		}
	}
										
	for ($x = 1; $x <= 12; $x++) {
		$qte_val =  number_format($mois[$x], 0, '.', ' ');
		echo "<td align='center'><b>$qte_val</b></td>";
	}
	$all_qte = number_format(array_sum($mois), 0, '.', ' ');
	echo "<td align='center'><b style='color:blue;'>".$all_qte."</b></td>";
	echo "</tr>";		
}


function  get_stat_sous_famille($famille,$source,$type,$annee)
{
	global $db;
	
	if($type == "ACHAT"){
		$prod_table = "pm_produitsachat";
	}else{
		$prod_table = "pm_produits";
	}

	$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 "<tr><td colspan='14' align='center' style='background-color: #dadada;color:#000'><b>$name_famille</b></td>";
	echo "<tr>";
	echo "<td><b>2018 / variation</b></td>";
									
									
	$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 ".$source." AND type_product = '".$type."' AND annee = ".$annee;
	// echo $query."<br>" ;
	$result = @$db->query($query);
	if($result !== false){
		foreach($result as $row){
			$mois[$row["mois"]] += $row["qte"];
		}
	}
										
	for ($x = 1; $x <= 12; $x++) {
		$qte_val =  number_format($mois[$x], 0, '.', ' ');
		echo "<td align='center'><b>$qte_val</b></td>";
	}
	$all_qte = number_format(array_sum($mois), 0, '.', ' ');
	echo "<td align='center'><b style='color:blue;'>".$all_qte."</b></td>";
	echo "</tr>";		
}

function  get_stat_famille($famille,$source,$type,$annee)
{
	global $db;
	
	if($type == "ACHAT"){
		$prod_table = "pm_produitsachat";
	}else{
		$prod_table = "pm_produits";
	}


	
	echo "<tr><td colspan='14' align='center' style='background-color: #dadada;color:#000'><b>$name_famille</b></td>";
	echo "<tr>";
	echo "<td><b>2018 / variation</b></td>";
									
									
	$mois = array(0,0,0,0,0,0,0,0,0,0,0,0,0) ;
									
	$query = " select * from stat_article where famille = ".$famille." AND ".$source." AND type_product = '".$type."' AND annee = ".$annee;
	// echo $query."<br>" ;
	$result = @$db->query($query);
	if($result !== false){
		foreach($result as $row){
			$mois[$row["mois"]] += $row["qte"];
		}
	}
										
	for ($x = 1; $x <= 12; $x++) {
		$qte_val =  number_format($mois[$x], 0, '.', ' ');
		echo "<td align='center'><b>$qte_val</b></td>";
	}
	$all_qte = number_format(array_sum($mois), 0, '.', ' ');
	echo "<td align='center'><b style='color:blue;'>".$all_qte."</b></td>";
	echo "</tr>";		
}

?>


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