<?php

ini_set('display_errors', 1);
define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'doc_achat');
if(!isset($_SESSION['user'])){
    header("Location: /admin/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-random"></i> Objectif</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" >				
			

		<div class="panel panel-default">
		
		 <? if( isset($_POST['submit']) ){
				$product = $_POST['prod'];
				$sous_famille = $_POST['sous_famille'];
				$famille = $_POST['famille'];
				$type = $_POST['type'];
				$mois_1 = $_POST['mois_1'];
				$mois_2 = $_POST['mois_2'];
				$mois_3 = $_POST['mois_3'];
				$mois_4 = $_POST['mois_4'];
				$mois_5 = $_POST['mois_5'];
				$mois_6 = $_POST['mois_6'];
				$mois_7 = $_POST['mois_7'];
				$mois_8 = $_POST['mois_8'];
				$mois_9 = $_POST['mois_9'];
				$mois_10 = $_POST['mois_10'];
				$mois_11 = $_POST['mois_11'];
				$mois_12 = $_POST['mois_12'];
				$totalUsername = sizeof($product);

				for($i=0;$i<$totalUsername;$i++) {

					$InsertProduct = $product[$i];
					$InsertSous_famille = $sous_famille[$i];
					$InsertFamille = $famille[$i];
					$InsertType = $product[$i];					
					$InsertMois_1 = $mois_1[$i];
					$InsertMois_2 = $mois_2[$i];
					$InsertMois_3 = $mois_3[$i];
					$InsertMois_4 = $mois_4[$i];
					$InsertMois_5 = $mois_5[$i];
					$InsertMois_6 = $mois_6[$i];
					$InsertMois_7 = $mois_7[$i];
					$InsertMois_8 = $mois_8[$i];
					$InsertMois_9 = $mois_9[$i];
					$InsertMois_10 = $mois_10[$i];
					$InsertMois_11 = $mois_11[$i];
					$InsertMois_12 = $mois_12[$i];


					// echo $InsertProduct." >> ".$InsertMois_1."<br>";	
					$delete = "DELETE FROM `pm_objectif` WHERE id_product =$InsertProduct AND type='$type'";
					$query_insert = "INSERT INTO `pm_objectif`(`type`, `famille`,`sous_famille`,`id_product`, `mois_1`, `mois_2`, `mois_3`, `mois_4`, `mois_5`, `mois_6`, `mois_7`, `mois_8`, `mois_9`, `mois_10`, `mois_11`, `mois_12`)".
					" VALUES ('$type','$InsertFamille','$InsertSous_famille','$InsertProduct','$InsertMois_1','$InsertMois_2','$InsertMois_3','$InsertMois_4','$InsertMois_5','$InsertMois_6','$InsertMois_7','$InsertMois_8','$InsertMois_9','$InsertMois_10','$InsertMois_11','$InsertMois_12') ";
					$result_delete = @$db->query($delete);
					$result_insert = @$db->query($query_insert);
					// echo $query_insert.'<br>';
					
				}
				echo "<br><h2 style='color:green;text-align: center;'>Mise à jour avec succès !</h2></br></br>";
			}	
		?>
		
			<div class="table-responsive">
				<table class="table table-bordered table-hover table-striped" id="listing_base">
                    <thead>
						<tr>
							<th><b>Article</b></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>
						</tr>
                    </thead>
					<tbody>
					<form method="POST" action="#">
					<?
					
					// =================================
					switch ($_GET["cat"]) {
						case 1:
							$type = "ACHAT";
							$table = "pm_produitsachat";
							break;
						case 2:
							$type = "VENTE";
							$table = "pm_produits";
							break;
						case 3:
							$type = "ACHAT";
							$table = "pm_produitsachat";
							break;
						case 4:
							$type = "ACHAT";
							$table = "pm_produitsachat";
					}
					// =================================
					if($db !== false){
						
						$q_famille = "select * from pm_famille_classe where  famille_cat = ".$_GET["cat"];
						
						$result_famille = @$db->query($q_famille);
                            if($result_famille !== false){
								foreach($result_famille as $row_famille){
									echo "<tr><td  colspan='13' align='center' style='background-color: #ffc000;color:#000'><b>".$row_famille["nom"]."</b></td></tr>";
									$q_famille_sous = "select * from pm_famille where  famille_classe = ".$row_famille["id"];
									$result_famille_sous = @$db->query($q_famille_sous);
										if($result_famille_sous !== false){
											foreach($result_famille_sous as $row_famille_sous){
												echo "<tr><td  colspan='13' align='center' style='background-color: #dadada;color:#000'><b>".$row_famille_sous["nom"]."</b></td></tr>";
											
							$query = "SELECT * FROM `".$table."` where famille = ".$row_famille_sous["id"]." order by desingnation ASC";
							// echo $query."<br>";
							$result = @$db->query($query);
                            if($result !== false){
								$mois_1_all = $mois_2_all = $mois_3_all = $mois_4_all = $mois_5_all = $mois_6_all  = 0 ;
								$mois_7_all = $mois_8_all = $mois_9_all = $mois_10_all = $mois_11_all = $mois_12_all = 0;
								foreach($result as $row){
									$mois_1 = $mois_2 = $mois_3 = $mois_4 = $mois_5 = $mois_6  = 0 ;									
									$mois_7 = $mois_8 = $mois_9 = $mois_10 = $mois_11 = $mois_12 = 0;
									
									$query_obj = "SELECT * FROM `pm_objectif` where id_product =".$row["id"]." AND type ='".$type."'";
									// echo $query_obj ."<br>";
									$result_obj  = @$db->query($query_obj );
									if($result_obj !== false){
										foreach($result_obj as $row_obj){
											if( isset($row_obj["type"]) ){
												$type = $row_obj["type"];
											}
											$mois_1 = $row_obj["mois_1"];
											$mois_1_all += $row_obj["mois_1"];
											$mois_2 = $row_obj["mois_2"];
											$mois_2_all += $row_obj["mois_2"];
											$mois_3 = $row_obj["mois_3"];
											$mois_3_all += $row_obj["mois_3"];
											$mois_4 = $row_obj["mois_4"];
											$mois_4_all += $row_obj["mois_4"];
											$mois_5 = $row_obj["mois_5"];
											$mois_5_all += $row_obj["mois_5"];
											$mois_6 = $row_obj["mois_6"];
											$mois_6_all += $row_obj["mois_6"];
											$mois_7 = $row_obj["mois_7"];
											$mois_7_all += $row_obj["mois_7"];
											$mois_8 = $row_obj["mois_8"];
											$mois_8_all += $row_obj["mois_8"];
											$mois_9 = $row_obj["mois_9"];
											$mois_9_all += $row_obj["mois_9"];
											$mois_10 = $row_obj["mois_10"];
											$mois_10_all += $row_obj["mois_10"];
											$mois_11 = $row_obj["mois_11"];
											$mois_11_all += $row_obj["mois_11"];
											$mois_12 = $row_obj["mois_12"];
											$mois_12_all += $row_obj["mois_12"];
										}
									}	
					
					?>
					<tr>
							<td><b><? echo $row['desingnation']; ?></b></td>
							<input type="hidden" name="prod[]" value="<? echo $row['id']; ?>" />
							<input type="hidden" name="type" value="<? echo $type; ?>" />
							<input type="hidden" name="famille[]" value="<? echo $row_famille["id"]; ?>" />
							<input type="hidden" name="sous_famille[]" value="<? echo $row_famille_sous["id"]; ?>" />
							<td><input name="mois_1[]" type="text" value="<? echo $mois_1; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_2[]" type="text" value="<? echo $mois_2; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_3[]" type="text" value="<? echo $mois_3; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_4[]" type="text" value="<? echo $mois_4; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_5[]" type="text" value="<? echo $mois_5; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_6[]" type="text" value="<? echo $mois_6; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_7[]" type="text" value="<? echo $mois_7; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_8[]" type="text" value="<? echo $mois_8; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_9[]" type="text" value="<? echo $mois_9; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_10[]" type="text" value="<? echo $mois_10; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_11[]" type="text" value="<? echo $mois_11; ?>" style="width:70px;text-align:center;" /></td>
							<td><input name="mois_12[]" type="text" value="<? echo $mois_12; ?>" style="width:70px;text-align:center;" /></td>
		
							
							
						</tr>
						
					<?
					
								}
					?>
					<tr>
							<td align='right' style="background-color: #ffbebe;"><b>Total</b></td>
		
							<td style="background-color: #ffbebe;"><input name="mois_1[]" type="text" value="<? echo $mois_1_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_2[]" type="text" value="<? echo $mois_2_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_3[]" type="text" value="<? echo $mois_3_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_4[]" type="text" value="<? echo $mois_4_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_5[]" type="text" value="<? echo $mois_5_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_6[]" type="text" value="<? echo $mois_6_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_7[]" type="text" value="<? echo $mois_7_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_8[]" type="text" value="<? echo $mois_8_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_9[]" type="text" value="<? echo $mois_9_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_10[]" type="text" value="<? echo $mois_10_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_11[]" type="text" value="<? echo $mois_11_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
							<td style="background-color: #ffbebe;"><input name="mois_12[]" type="text" value="<? echo $mois_12_all; ?>" style="width:70px;text-align:center;" disabled="disabled" /></td>
		
							
							
						</tr>
						
					<?
								
							}	
							
						} 
					
											}
										}	
								}
							}	
						
					?>		
					
					
					</tbody>
				</table>	
				
			<div class="panel-body">
			   <label class="col-lg-4 control-label " ></label>
			   <div class="col-lg-12">
				  <div class=" form-inline" style="text-align:  center;">
						<button type="submit" name="submit" class="btn btn-default mt15" data-toggle="tooltip" data-placement="bottom" title="Metre à jour">
							<i class="fa fa-search"></i><span class="hidden-sm hidden-xs"> Metre à jour</span>
						</button>
				  </div>
			   </div>
		 </div>
		 
		 </form>
	</div> 
		
		</div> 	
	




	
	
	




	

									
                    <?php
                    if($db !== false){
                        
                    } ?>
					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<style>
input{
	font-weight: bold;
	    color: #2f9df2;
}
</style>
<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; ?>
