<?php

// ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'Inventaire');
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-6">
                            <h1><i class="fa fa-random"></i> Inventaire</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="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 method="GET" action="stock_search.php">				
<div class="table-responsive">
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>

                                    </thead>
                                    <tbody>    
									
									    <?php
										if($db !== false){
											
											$query = "select b.id, b.nom, a.type from pm_category as a ,pm_famille_classe as b where a.stockable = 1 AND a.id = b.famille_cat ";
											if($_GET["cat"] <> ""){
												$query .= " AND a.type = '".$_GET["cat"]."' ";
											}
											// echo $query."<br>";
											$result = @$db->query($query);
											if($result !== false){
												foreach($result as $row){		
													echo '<tr><td class="text-center" colspan="6" style="background-color: #ffc000;color:#000"><b>'.$row['nom'].'</b></td></tr>';												
													$query_2 = "SELECT * FROM pm_famille WHERE famille_classe = ".$row['id'];
													// echo $query_2."<br>";
													$result_2 = @$db->query($query_2);
													if($result_2 !== false){
														$qte_all_total = 0;
														foreach($result_2 as $row_2){	
															echo '<tr><td class="text-center" colspan="6" style="background-color: #dadada;color:#000"><b>'.$row_2['nom'].'</b></td></tr>';
															
															if($row['type'] == "vente"){
																$query_3 = "SELECT * FROM pm_produits WHERE famille = ".$row_2['id'];
															}else{
																$query_3 = "SELECT * FROM pm_produitsachat WHERE famille = ".$row_2['id'];
															}
															$result_3 = @$db->query($query_3);
															if($result_3 !== false){
																echo '<tr class="nodrop nodrag">
																	<th width="100">                                                
																			Code article                                              
																	</th>
																	 <th width="130">                                                
																			Désignation Article                                              
																	</th>
																	 <th width="70">                                                
																			Quantité                                            
																	</th>
																	<th width="70">                                                
																			Unité                                              
																	</th>
																	<th width="70">                                                
																			Colisage                                               
																	</th>
																	<th width="70">                                                
																			Qte emballée                                               
																	</th>
													
																</tr>';
																
																$qte_total = 0;
																foreach($result_3 as $row_3){	
																
																	//====== unite ================
																	$query_unt = "select * from pm_unite where id =".$row_3["unite"] ;
																	$result_unt = @$db->query($query_unt);
																	if($result_unt !== false){
																		$row_unt = $result_unt->fetch();
																		
																		$unt = $row_unt["name"];
																		
																	}
																	//=============================
																	if($row['type'] == "vente"){
																		// $query_4 = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_bl_product as b where a.id = b.id_produits AND a.id = ".$row_3["id"] ;
																		$query_4 = "SELECT SUM(qte) as cnt FROM pm_bl as a LEFT JOIN pm_bl_product as b ON a.id = b.id_bl where b.id_produits = ".$row_3["id"] ;
																		
																		$query_4_retour = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_brv_product as b where a.id = b.id_produits AND a.id = ".$row_3["id"] ;
																		$query_4_fabri = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_bf as b where a.id = b.product AND a.id = ".$row_3["id"] ;
																		// echo $query_4_retour ."<br>";
																		// $query_4_cmd = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_bc_product as b where a.id = b.name AND a.id = ".$row_3["id"] ;
																	}else{
																		$query_4 = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_be_product as b where a.id = b.name AND a.id = ".$row_3["id"] ;
																		$query_4_retour = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_br_product as b where a.id = b.name AND a.id = ".$row_3["id"] ;
																		$query_4_fabri = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_bf_product as b where a.id = b.id_produits AND a.id = ".$row_3["id"] ;
																		$query_4_cmd = "SELECT *,SUM(qte) as cnt,SUM(a.coulissage) as coul FROM pm_produits as a,pm_bc_product as b where a.id = b.name AND a.id = ".$row_3["id"] ;
																	}
																	
																	// echo $query_4 ."<br>";
																	// echo $query_4_retour ."<br>";
																	// echo $query_4_retour ."<br>";
																	$qte = $coulissage = $emballe = $qte_all = $coulissage_all = 0;
																	//========== query_4 =============
																	$result_4 = @$db->query($query_4);
																	if($result_4 !== false){
																		$row_4 = $result_4->fetch();
																		
																		$qte = $row_4["cnt"];
																		$coulissage = $row_4["coul"];
																		
																	}
																	//================================
																	//========== query_4_retour ======		
																	$result_4_retour = @$db->query($query_4_retour);
																	if($result_4_retour !== false){
																		$row_4_retour = $result_4_retour->fetch();
																		
																		$qte_retour = $row_4_retour["cnt"];
																		$coulissage_retour = $row_4_retour["coul"];
																		
																	}
																	//================================
																	//========== query_4_fabri ======
																	$result_4_fabri = @$db->query($query_4_fabri);
																	if($result_4_fabri !== false){
																		$row_4_fabri = $result_4_fabri->fetch();
																		
																		$qte_fabri = $row_4_fabri["cnt"];
																		$coulissage_fabri = $row_4_fabri["coul"];
																		
																	}
																	//================================
																	//========== query_4_cmd ======
																	$result_4_cmd = @$db->query($query_4_cmd);
																	if($result_4_cmd !== false){
																		$row_4_cmd = $result_4_cmd->fetch();
																		
																		$qte_cmd = $row_4_cmd["cnt"];
																		$coulissage_cmd = $row_4_cmd["coul"];
																		
																	}
																	//================================
																	//========== Caclcule ============
																	$qte_all = ($qte_retour + $qte_fabri) - $qte ;
																	$coulissage_all = $coulissage - ($coulissage_retour + $coulissage_fabri) ;
																	if($coulissage <> ""){
																		$emballe = floatval($qte_all/$row_3["coulissage"]) ;
																	}
																	//================================
																	if($qte_all <= 0){
																		$color = 'style="color:red;"';
																	}else{
																		$color = 'style="color:green;"';
																	}
																	
																	//================================
																	if($qte_cmd == 0){
																		$cmd_encour = 0;
																	}else{
																		$cmd_encour = $qte_cmd-$qte;
																	}
																	if($cmd_encour <= 0){
																		$color_cmd = 'style="color:red;"';
																	}else{
																		$color_cmd = 'style="color:green;"';
																	}
																	//================================
																	// if($qte_all == "") $qte = "-" ;
																	// if($coulissage == "") $coulissage = "-" ;
																	if($qte_all > 0){
																		echo '<tr>
																				<td class="text-center">'.$row_3["code_produit"].'</td>
																				<td class="text-center">'.$row_3["desingnation"].'</td>
																				<td class="text-center"><b '.$color.'>'.number_format($qte_all,1, ',', ' ').'</b></td>
																				<td class="text-center">'.mb_strtoupper($unt).'</td>
																				<td class="text-center">'.$row_3["coulissage"].'</td>
																				<td class="text-center">'.number_format($qte_all/$row_3["coulissage"],1, ',', ' ').'</td>
																				
																			</tr>';
																	}	
																		$qte_total = $qte_total + $qte_all;
																}
																$qte_all_total = $qte_all_total + $qte_total;
																echo '<tr style="background-color: #ffbebe;color:#000">
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			<td class="text-center" style="background-color: #ffbebe;">Total sous-famille : '.number_format($qte_total,1, ',', ' ').' '.mb_strtoupper($unt).'</td>
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			
																		</tr>';
																
															}	
															
														}

													echo '<tr style="background-color: #bec0ff;color:#000">
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			<td class="text-center" style="background-color: #bec0ff;">Total famille : '.number_format($qte_all_total,1, ',', ' ').' '.mb_strtoupper($unt).'</td>
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			
																		</tr>';														
													}
												}	
											}
											
											
											
											// $srt = '<tr>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                    // <td class="text-center">x</td>
                                                // </tr>';
											
										} ?>
									
												                                                



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

					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; ?>
