<?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">
						<?
						
							if($_GET["id_depot"] == '1'){
								$name_depot = "Dépôt Principal";
							}else if($_GET["id_depot"] == '3'){
								$name_depot = "Dépôt Export";
							}else{
								$name_depot = "Tout les Dépôt";
							}
							
						?>
                            <h1><i class="fa fa-random"></i> Inventaire PF - <? echo $name_depot; ?></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 famille_'.$row['id'].'" 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;
														$qte_emballe_total = 0;
														$qte_palette_total = 0;
														foreach($result_2 as $row_2){	
															
															// if($row['type'] == "vente"){
																$query_3 = "SELECT * FROM pm_produits WHERE famille = ".$row_2['id'];
																$query_3 .= " AND status = 1 ";
															// }else{
																// $query_3 = "SELECT * FROM pm_produitsachat WHERE famille = ".$row_2['id'];
																// $query_3 .= " AND status = 1 ";
															// }
															$result_3 = @$db->query($query_3);
															if($result_3 !== false){
																
																
																$qte_total = 0;
																$qte_emballe = 0;
																$qte_palette = 0;
																
																$cnt =  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"];
																		
																	}
																	//=============================
																	// $query_4 = "SELECT qte FROM pm_depot_stock_pf where id_produits = ".$row_3["id"]." AND id_depot IN (".$_GET["id_depot"].")" ;
																	$query_4 = "SELECT SUM(qte) as qte FROM pm_depot_stock_pf where id_produits = ".$row_3["id"]." AND id_depot IN (".$_GET["id_depot"].")" ;
																	// echo $query_4;
																	// exit();
																	$result_4 = @$db->query($query_4);
																	if($result_4 !== false){
																		$row_4 = $result_4->fetch();
																		
																		$qte = $row_4["qte"];
																			
																	}
																	

																	$qte_all = number_format($qte,2, ',', '') ;
																	//================================
																	if($qte_all <= 0){
																		$color = 'style="color:red;"';
																	}else{
																		$color = 'style="color:green;"';
																	}


																	$qte_emb = number_format($qte_all*$row_3["coulissage"],2, ',', '') ;
																	if($qte_emb < 0){
																		$color1 = 'style="color:red;"';
																	}else if($qte_emb > 0){
																		$color1 = 'style="color:green;"';	
																	}else{
																		$color1 = 'style="color:black;"';
																	}

																	
																	//================================
																	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 = "-" ;
																	// Affichage du titre de la sous-catégorie AVANT les produits
																	if ($cnt == 0){
																		
																		
																		echo '<tr><td class="text-center" colspan="6" style="background-color: #dadada; color: #000;"><b>'.$row_2['nom'].'</b></td></tr>';
															
																		echo '<tr class="nodrop nodrag">
																				<th width="100">                                                
																						Code article                                              
																				</th>
																				 <th width="130">                                                
																						Désignation Article                                              
																				</th>

																				<th width="70">                                                
																						Colisage                                               
																				</th>
																				<th width="70">                                                
																						Qte emballée                                               
																				</th>
																							<th width="20">                                                
																						Colisage palette                                               
																				</th>
																				<th width="70">                                                
																						Qte palette                                               
																				</th>
																
																		</tr>';
																		$cnt++;	
																	}	
																	// Calcul des quantités emballées et palette
																	$qte_emb = number_format($qte_all/$row_3["coulissage"],2, ',', '') ;
																	$qte_pal= number_format($qte_all/$row_3["coulissage_palette"],2, '.', '') ;
																		// Affichage du produit (toujours affiché)
																	echo '<tr>
																		<td class="text-center">'.$row_3["code_produit"].'</td>
																		<td class="text-center">'.$row_3["desingnation"].'</td>
																	
																		
																		<td class="text-center">'.$row_3["coulissage"].'</td>
																		<td class="text-center"><b '.$color1.'>'.$qte_all.'</b></td>
																		<td class="text-center">'.$row_3["coulissage_palette"].'</td>
																		<td class="text-center">'.$qte_pal.'</td>
																		
																	</tr>';
																	
																	// Calcul des totaux
																	$qte_total = $qte_total + $qte_all;
																	$qte_emballe += number_format($qte_all/$row_3["coulissage"],1, '.', '') ;
																	$qte_palette += $qte_pal ;
																		
																		
																	
																}
																$qte_all_total += $qte_total;
																
																$qte_emballe_total += $qte_total;
																$qte_palette_total += $qte_palette;
																
																
																if($qte_total > 0){
																echo '<tr style="background-color: #ffbebe;color:#000">
																			<td class="text-center" style="background-color: #ffbebe;"colspan="2"><b>Total sous-famille</b></td>
																			<td class="text-center" style="background-color: #ffbebe;" colspan="1"></td>
																			<td class="text-center" style="background-color: #ffbebe;"><b>'.$qte_total.'</b></td>
																			<td class="text-center" style="background-color: #ffbebe;"></td>
																			<td class="text-center" style="background-color: #ffbebe;"><b>'.$qte_palette.'</b></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>
																			// <td class="text-center" style="background-color: #bec0ff;"></td>
																			// <td class="text-center" style="background-color: #bec0ff;"></td>
																			
																		// </tr>';														
																if($qte_all_total > 0){
																	echo '<tr style="background-color: #bec0ff;color:#000">
																			<td class="text-center" style="background-color: #bec0ff;"colspan="2"><b>Total Famille</b></td>
																			<td class="text-center" style="background-color: #bec0ff;" colspan="1"></td>
																			<td class="text-center" style="background-color: #bec0ff;"><b>'.$qte_emballe_total.'</b></td>
																			<td class="text-center" style="background-color: #bec0ff;"></td>
																			<td class="text-center" style="background-color: #bec0ff;"><b>'.$qte_palette_total.'</b></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'] = ""; ?>
