<?php

// ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'ca');
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 vente</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">				


	<form >				
<div class="table-responsive">
<b>Client :</b> </br>
<?
$clients = implode(',', $_GET['client']); // Convertir le tableau en chaîne "1,2,3"
							$clients_name = "";
							$query = "SELECT name FROM `pm_client` where id IN (".$clients.")";
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row0){
									echo "* ".$row0['name']."<br>";
								}
							}

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


														
										<tr>
											<th class="text-center"><b>
											<?
												if (!empty($_GET['product'])) {
													echo "Produits" ;
												}else{
													echo "Familles";
												}
											?>
											
											</b></th>
											<th class="text-center"><b>Qte par unité</b></th>
											<th class="text-center"><b>Qte Emballé</b></th>
											<th class="text-center"><b>HT</b></th>
											<th class="text-center"><b>TTC</b></th>

										</tr>
									</thead>
									<tbody>
									    
						<?php


						$all_qte += 0 ;									
						$all_qte_c += 0 ;									
						$all_ht += 0;									
						$all_ttc += 0 ;	
						
						if (!empty($_GET['product'])) {
							$produits = implode(',', $_GET['product']); // Convertir le tableau en chaîne "1,2,3"
						}else if(!empty($_GET['famille'])) {
							$produits = "";
							$famille = implode(',', $_GET['famille']);
							$query = "SELECT id FROM `pm_produits` where famille IN (".$famille.")";
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row0){
									$produits .= $row0['id'].",";
								}
							}
							$produits = substr($produits, 0, -1);
							
							$familles = "";
							$query = "SELECT nom FROM `pm_famille` where id IN (".$famille.")";
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row0){
									$familles .= $row0['nom']."<br>";
								}
							}
							
						}else{
							$produits = "";
						}
						
						
						
						if($db !== false){
							//========
$query = "	SELECT  
                f.id_client,
                c.name AS client_name,
                fam.nom AS famille_produit,
                COUNT(DISTINCT f.id) AS nombre_factures,
                GROUP_CONCAT(DISTINCT bp.name SEPARATOR '<br> ') AS produits,
                bp.name AS produit,
                bp.coulissage AS coulissage,
                SUM(bp.qte) AS total_qte,
                SUM(bp.qte * (bp.prix_ht1 - (bp.prix_ht1 * bp.remise1 / 100))) AS total_montant_ht,
                SUM(bp.qte * (bp.prix_ht1 - (bp.prix_ht1 * bp.remise1 / 100)) * 1.19) AS total_montant_ttc,
                (SELECT SUM(f2.total_ttc) 
                 FROM pm_facture f2 
                 WHERE f2.id_client = f.id_client 
                 AND f2.date_facture BETWEEN '".strtotime($_GET["date_in"])."' AND '".( strtotime($_GET["date_out"]) + 86400 )."'
                ) AS total_montant_ttc_factures
            FROM pm_facture f
            JOIN pm_bl_product bp ON FIND_IN_SET(bp.id_bl, f.ref_bl) > 0
            JOIN pm_produits p ON bp.id_produits = p.id
            JOIN pm_famille fam ON p.famille = fam.id
            JOIN pm_client c ON f.id_client = c.id
            WHERE f.id_client IN (".$clients.")  
            AND f.date_facture BETWEEN '".strtotime($_GET["date_in"])."' AND '".( strtotime($_GET["date_out"]) + 86400 )."'";

if($produits <> ""){		
    $query .= "	AND bp.id_produits IN (".$produits.")";
}

if (!empty($_GET['product'])) {
	$query .= " GROUP BY bp.id_produits";
}else{
	$query .= " GROUP BY fam.nom";
}


$query .= " ORDER BY f.id_client ASC;";

							// echo $query;
							// exit();
							$result = @$db->query($query);
                            if($result !== false){
								foreach($result as $row){	
									if($familles <> ""){
										$produitso = $familles;
									}else{
										$produitso = $row["produit"] ;
									}
									echo '<tr>
									
											<td class="text-center date_sort"  ><b>'.$row["produit"].'</b></td>
											<td class="text-center"><b>'.$row["total_qte"].'</b></td>
											<td class="text-center"><b>'.number_format($row["total_qte"]/$row["coulissage"], 0,'', '').'</b></td>
											<td class="text-center"><b>'.number_format($row["total_montant_ht"],3).'</b></td>
											<td class="text-center"><b>'.number_format($row["total_montant_ttc"]+$row["nombre_factures"],3).'</b></td>
										  </tr>
										';
									
										$all_qte += $row["total_qte"] ;									
										$all_qte_c += $row["total_qte"]/$row["coulissage"] ;									
										$all_ht += $row["total_montant_ht"] ;									
										$all_ttc += $row["total_montant_ttc"]+$row["nombre_factures"] ;									
	
	
								}	
							
									echo '<tr>
											<td class="text-center date_sort" ><b>TOTAL</b></td>
											<td class="text-center"><b>'.$all_qte.'</b></td>
											<td class="text-center"><b>'.number_format($all_qte_c, 0,'', '').'</b></td>
											<td class="text-center"><b>'.number_format($all_ht,3).'</b></td>
											<td class="text-center"><b>'.number_format($all_ttc,3).'</b></td>
										  </tr>
										';
							}	
							
							
							
														
						} ?>									


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

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

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


?>



