<?php
include("include/data.php");
Init_boutique();
global $base, $MySQL_Host, $MySQL_User, $MySQL_Passw, $myart, $A_NumArt,$i, $result, $NbLigne, 
$mygroupeart, $mysousgroupeart, $semaine, $limitesuivante, $limiteprecedente, $limite, $total, 
$page, $Nom_Client, $NumAbat;
// entete de la page

$date = date("d-m-Y");
$timestamp = date('Y-m-d G:i:s');
// menu haut new-supp-modif

echo "<center><br>";

/*
ajout_entre_stock.php?
num_client_entre=8
num_lot_entre=H24141817
num_trace_entre=165T
valid=0
*/

if (isset($_GET['num_client_entre'])){
  $num_client_entre =$_GET['num_client_entre'];
} else {
  $num_client_entre = '';
}

if (isset($_GET['num_lot_entre'])){
  $num_lot_entre =$_GET['num_lot_entre'];
} else {
  $num_lot_entre = '';
}

if (isset($_GET['num_trace_entre']) ){
$num_trace_entre = $_GET['num_trace_entre'];
} else {
$num_trace_entre = '';
}

if (isset($_GET['date_lot_entre']) ){
$date_lot_entre = $_GET['date_lot_entre'];
} else {
$date_lot_entre = $date ;
}

if (isset($_GET['valid']) ){
$valid = $_GET['valid'];
} else {
$valid = '';
}
$i=0;
if ($valid==1) {
// valide donc on ajourt les lignes et efface la table temporaire	
echo "Valider";
// process des requettes
$totalPoids=0;
$totalPi=0;

	$sql="select num_plu, SUM(poids), SUM(nb_pi) FROM temp_entre_stock" ;
	$sql.=" WHERE num_client='".$num_client_entre."'";
	$sql.=" AND num_lot='".$num_lot_entre."'";
	$sql.=" AND num_traca='".$num_trace_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" GROUP BY num_plu  ASC";
	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["SUM(poids)"];
	        $nb_pi_temp = $row["SUM(nb_pi)"];
					
			$totalPoids = $totalPoids + $poids_temp ;
			$totalPi = $totalPi + $nb_pi_temp ;
		
					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu_temp."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$nom_plu_temp = $row["PLU"];
						$dlc_plu_temp = $row["dlc_plu"];
						} 
								
$poids_temp = number_format($poids_temp, 3, '.', ' ')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', ' ')	;
$totalPi = number_format($totalPi, 0, '.', ' ')	;
$totalPoids = number_format($totalPoids, 3, '.', ' ')	;

$date_sais_temp = date('d-m-Y', strtotime($date_lot_entre)) ;
$semaine_saisie = date('W', strtotime($date_lot_entre)) ;
$date_dlc_temp = date("Y-m-d", strtotime($date_lot_entre)+ (24 * 3600 * $dlc_plu_temp));
$date_dlc_temp = date('d-m-Y', strtotime($date_dlc_temp)) ;
/*
echo "<table border='1' cellpadding='1' cellspacing='1' width='100%'>";
echo "<tr>";
echo "<td width='100' align='left'></td>" ;
echo "<td width='100' align='left'>$date_sais_temp</td>" ;
echo "<td width='50' align='center'>$num_plu_temp</td>" ;
echo "<td width='200' align='left'>$nom_plu_temp</td>" ;
echo "<td width='100' align='left'>$date_dlc_temp</td>" ;
echo "<td width='100' align='right'> Kg</td>" ;
echo "<td width='50' align='right'>$nb_pi_temp Pi</td>" ;
echo "</tr></table>" ;
*/
// Ajout table Stock
$last_num_entre = 0 ;
$sql2 = "SELECT max(IdNumStock) FROM stock";  
$req2 = mysqli_query($link, $sql2); 
$row = mysqli_fetch_row($req2);
$last_num_entre = $row[0];
$last_num_entre_stock = $last_num_entre + 1 ;

$date_lot_entre = date("Y-m-d", strtotime($date_lot_entre)) ;
$date_dlc_temp = date("Y-m-d", strtotime($date_dlc_temp)) ;
$date = date("Y-m-d");

$sql_ajout_stock = "INSERT INTO stock VALUES ( 
'".$last_num_entre_stock."', 
'".$IdNumClient."', 
'".$date."',
'".$semaine_saisie."',
'".$num_client_entre."',
'".$num_lot_entre."',
'".$num_trace_entre."',
'".$num_plu_temp."',
'".$date_lot_entre."',  
'".$date_dlc_temp."', 
'".$poids_temp."',
'".$nb_pi_temp."',
'0',
'1',
'',
'".$timestamp."')";

$Req_ajout_stock = mysqli_query($link, $sql_ajout_stock); 

echo "$sql_ajout_stock | $Req_ajout_stock<br>";

// Ajout table lot en position sortie
$last_num_entre = 0 ;
$sql2 = "SELECT max(num_ligne_lot) FROM lot";  
$req2 = mysqli_query($link, $sql2); 
$row = mysqli_fetch_row($req2);
$last_num_entre = $row[0];
$last_num_entre = $last_num_entre + 1 ;

$nom_plu_temp = htmlspecialchars($nom_plu_temp, ENT_QUOTES);

$sql_ajout_lot_sortie = "INSERT INTO lot VALUES ( 
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$num_client_entre."',  
'".$date_lot_entre."',  
'".$num_lot_entre."',
'".$num_trace_entre."',
'6',
'".$poids_temp."',
'1',
'0',
'".$last_num_entre_stock."',
'".$nom_plu_temp." | Nb Pi: ".$nb_pi_temp."')";

$Req_ajout_lot = mysqli_query($link, $sql_ajout_lot_sortie); 

echo "$sql_ajout_lot_sortie | $Req_ajout_lot<br>";

	}	
					 
$sql_delete_stock_temp="DELETE FROM temp_entre_stock 
WHERE IdNumClient='".$IdNumClient."' AND num_lot='".$num_lot_entre."' AND num_traca='".$num_trace_entre."'" ;


$Req_delete_stock_temp =  mysqli_query($link, $sql_delete_stock_temp); 
echo "$sql_delete_stock_temp | $Req_delete_stock_temp<br>";

Header("Location: stock.php");

} else {
// Non validé donc on demande la validation

include 'menu/header.php';

Echo "<center><font color='red'>verification de la saisie</font></center><br>";

echo "<table width='750' id='customers'>";
echo "<tr>";
echo "<th width='100'>Date</th>" ;
echo "<th width='50'&gt;N&deg; Plu</th>" ;
echo "<th width='200'>Nom PLu</th>" ;
echo "<th width='100'>dlc</th>" ;
echo "<th width='150'>Poids</th>" ;
echo "<th width='150'>Nb Pi</th>" ;
echo "</tr>";

$totalPoids=0;
$totalPi=0;

	$sql="select num_plu, SUM(poids), SUM(nb_pi) FROM temp_entre_stock" ;
	$sql.=" WHERE num_client='".$num_client_entre."'";
	$sql.=" AND num_lot='".$num_lot_entre."'";
	$sql.=" AND num_traca='".$num_trace_entre."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" GROUP BY num_plu  ASC";
	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {
	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["SUM(poids)"];
	        $nb_pi_temp = $row["SUM(nb_pi)"];
					
			$totalPoids = $totalPoids + $poids_temp ;
			$totalPi = $totalPi + $nb_pi_temp ;
		
					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu_temp."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$nom_plu_temp = $row["PLU"];
						$dlc_plu_temp = $row["dlc_plu"];
						} 
								
$poids_temp = number_format($poids_temp, 2, '.', ' ')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', ' ')	;
$totalPi = number_format($totalPi, 0, '.', ' ')	;
$totalPoids = number_format($totalPoids, 2, '.', ' ')	;

$date_sais_temp = date('d-m-Y', strtotime($date_lot_entre)) ;
$date_dlc_temp = date("Y-m-d", strtotime($date_lot_entre)+ (24 * 3600 * $dlc_plu_temp));
$date_dlc_temp = date('d-m-Y', strtotime($date_dlc_temp)) ;

echo "<tr>";
echo "<td width='50' align='left'>$date_sais_temp</td>" ;
echo "<td width='50' align='center'>$num_plu_temp</td>" ;
echo "<td width='200' align='left'>$nom_plu_temp</td>" ;
echo "<td width='50' align='left'>$date_dlc_temp</td>" ;
echo "<td width='150' align='right'>$poids_temp Kg</td>" ;
echo "<td width='150' align='right'>$nb_pi_temp Pi</td>" ;
echo "</tr>" ;
	}					 
echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=4 align='right'>Total :</td>" ;
echo "<td width='150' align='right'>$totalPoids Kg</td>" ;
echo "<td width='150' align='right'>$totalPi Pi</td>" ;
echo "</tr>" ;
echo "</table>";        

echo "<form id='IcrStock' name ='IcrStock' method='GET' action='ajout_entre_stock.php'>";
echo "<input type='hidden' name='date_lot_entre' value='$date_lot_entre'/>";  
echo "<input type='hidden' name='num_client_entre' value='$num_client_entre'/>";  
echo "<input type='hidden' name='num_lot_entre' value='$num_lot_entre'/>";  
echo "<input type='hidden' name='num_trace_entre' value='$num_trace_entre'/>"; 
echo "<input type='hidden' name='valid' value='1'/>";  
echo "<br><br><input type='submit' value='Ajout entre Stock'><br><br>";
echo "</form>";

}

?>