<?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');


/*
http://192.168.1.200/horius/ajout_ligne_stock.php
ordre=0
num_eleveur=12
num_lot_entre=L09817919
position=p
Date_Mvt=2019-03-05
num_plu_mvt=38
traca_mvt=64D
poids_mvt=3.68
NbPi_mvt=19
*/

if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['position'])) { $position=$_POST['position']; }  else {$position=''; }
		if (isset($_POST['num_eleveur'])) { $num_eleveur=$_POST['num_eleveur']; }  else {$num_eleveur=''; }
		if (isset($_POST['num_lot_entre'])) { $num_lot_entre=$_POST['num_lot_entre']; }  else {$num_lot_entre=''; }
		if (isset($_POST['Date_Mvt'])) { $Date_Mvt=$_POST['Date_Mvt']; }  else {$Date_Mvt=''; }
		if (isset($_POST['num_plu_mvt'])) { $num_plu_mvt=$_POST['num_plu_mvt']; }  else {$num_plu_mvt=''; }
		if (isset($_POST['traca_mvt'])) { $traca_mvt=$_POST['traca_mvt']; }  else {$traca_mvt=''; }
		if (isset($_POST['poids_mvt'])) { $poids_mvt=$_POST['poids_mvt']; }  else {$poids_mvt=''; }
		if (isset($_POST['NbPi_mvt'])) { $NbPi_mvt=$_POST['NbPi_mvt']; }  else {$NbPi_mvt=''; }
		if (isset($_POST['observations'])) { $observations=$_POST['observations']; }  else {$observations=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['position'])) { $position=$_GET['position']; }  else {$position=''; }
		if (isset($_GET['num_eleveur'])) { $num_eleveur=$_GET['num_eleveur']; }  else {$num_eleveur=''; }
		if (isset($_GET['num_lot_entre'])) { $num_lot_entre=$_GET['num_lot_entre']; }  else {$num_lot_entre=''; }
		if (isset($_GET['Date_Mvt'])) { $Date_Mvt=$_GET['Date_Mvt']; }  else {$Date_Mvt=''; }
		if (isset($_GET['num_plu_mvt'])) { $num_plu_mvt=$_GET['num_plu_mvt']; }  else {$num_plu_mvt=''; }
		if (isset($_GET['traca_mvt'])) { $traca_mvt=$_GET['traca_mvt']; }  else {$traca_mvt=''; }
		if (isset($_GET['poids_mvt'])) { $poids_mvt=$_GET['poids_mvt']; }  else {$poids_mvt=''; }
		if (isset($_GET['NbPi_mvt'])) { $NbPi_mvt=$_GET['NbPi_mvt']; }  else {$NbPi_mvt=''; }
		if (isset($_GET['observations'])) { $observations=$_GET['observations']; }  else {$observations=''; }
		$valForm="POST" ;
		$valForm="GET" ;
}

					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu_mvt."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
//echo "<br>".$sql1 ;
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$dlc_plu_temp = $row["dlc_plu"];
						} 


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

$poids_mvt = number_format($poids_mvt, 3, '.',' ')	;
$NbPi_mvt = number_format($NbPi_mvt, 0, '.', ' ')	;

// 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_Mvt)) ;
$date_dlc_temp = date("Y-m-d", strtotime($date_dlc_temp)) ;
$date = date("Y-m-d");

switch ($position) {
	case 'p':

$sql_ajout_stock = "INSERT INTO stock VALUES ( 
'".$last_num_entre_stock."', 
'".$IdNumClient."', 
'".$date."',
'".$semaine_saisie."',
'".$num_eleveur."',
'".$num_lot_entre."',
'".$traca_mvt."',
'".$num_plu_mvt."',
'".$date_lot_entre."',  
'".$date_dlc_temp."', 
'".$poids_mvt."',
'".$NbPi_mvt."',
'0',
'1',
'".$observations."',
'".$timestamp."')";

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

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

	case 'm':

$sql_ajout_stock = "INSERT INTO stock VALUES ( 
'".$last_num_entre_stock."', 
'".$IdNumClient."', 
'".$date."',
'".$semaine_saisie."',
'".$num_eleveur."',
'".$num_lot_entre."',
'".$traca_mvt."',
'".$num_plu_mvt."',
'".$date_lot_entre."',  
'".$date_dlc_temp."', 
'".$poids_mvt."',
'".$NbPi_mvt."',
'1',
'1',
'".$observations."',
'".$timestamp."')";

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

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

$refere = $_SERVER['HTTP_REFERER'] ;
Header("Location:  $refere") ;



?>