<?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
//Include 'menu/header.php';

$date = date("d-m-Y");

// menu haut new-supp-modif

echo "<center><br>";

/*
ordre=1
num_cate_intrant=4
Date_Entre_d=17
Date_Entre_m=02
Date_Entre_y=2016
Num_Plu=3
Date_DLC_d=20
Date_DLC_m=02
Date_DLC_y=2016
id_presta=8
Num_Bl=1551%2F7474
Poids_intrant=6.50
num_unite=1
cloture=0
observations=Pot+au+feu
*/


$Date_Entre_y = $_POST['Date_Entre_y'];
$Date_Entre_m = $_POST['Date_Entre_m'];
$Date_Entre_d = $_POST['Date_Entre_d'];

$Date_DLC_d = $_POST['Date_DLC_d'];
$Date_DLC_m = $_POST['Date_DLC_m'];
$Date_DLC_y = $_POST['Date_DLC_y'];

$num_cate_intrant=htmlentities( $_POST['num_cate_intrant']);
$Num_Plu=htmlentities($_POST['Num_Plu']);
$id_presta=htmlentities($_POST['id_presta']);
$Num_Bl=htmlentities($_POST['Num_Bl']);
$Poids_intrant=htmlentities($_POST['Poids_intrant']);
$num_unite=htmlentities($_POST['num_unite']);
$cloture=htmlentities($_POST['cloture']);
$observations=htmlentities($_POST['observations']);


$Date_Entre_sql=date("Y-m-d", mktime(0,0,0,$Date_Entre_m,$Date_Entre_d,$Date_Entre_y));
$Date_Entre=date("d/m/y", mktime(0,0,0,$Date_Entre_m,$Date_Entre_d,$Date_Entre_y));

$Date_DLC_sql=date("Y-m-d", mktime(0,0,0,$Date_DLC_m,$Date_DLC_d,$Date_DLC_y));
$Date_DLC=date("d/m/y", mktime(0,0,0,$Date_DLC_m,$Date_DLC_d,$Date_DLC_y));

$semaine_intrant=date("W", mktime(0,0,0,$Date_Entre_m,$Date_Entre_d,$Date_Entre_y));
$anne_lot_intrant=date("y", mktime(0,0,0,$Date_Entre_m,$Date_Entre_d,$Date_Entre_y));

//$req2 = mysql_query("SELECT Num_Lot Num_entre_intrant FROM table WHERE id=LAST_INSERT_ID()");

$last_num_entre = 0 ;
$sql = "SELECT max(Num_entre_intrant) FROM intrants";  
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$Num_entre_intrant_last= $row[0];
$Num_entre_intrant_last_sel = $Num_entre_intrant_last + 1 ;
$nb_car_enrg =strlen($Num_entre_intrant_last_sel) ;
switch ($nb_car_enrg) {
    case 1:
        $Num_lot_intrant = $anne_lot_intrant."/000".$Num_entre_intrant_last_sel ;
		break;
    case 2:
        $Num_lot_intrant = $anne_lot_intrant."/00".$Num_entre_intrant_last_sel ;
		break;
    case 3:
         $Num_lot_intrant = $anne_lot_intrant."/0".$Num_entre_intrant_last_sel ;
		 break;
}

Echo "<center><font color='red'>Enregistrement</font></center>";

echo "<br>";
echo "num_ligne intrant dernier : ".$Num_entre_intrant_last." <br>";
echo "num_ligne intrant Nouveau :<font color='red'> ".$Num_entre_intrant_last_sel."</font> <br>";
echo "Date entre : ".$Date_Entre." date sql : ".$Date_Entre_sql." /y= ".$Date_Entre_y."  /m= ".$Date_Entre_m." /d= ".$Date_Entre_d."<br>";
echo "Date DLC : ".$Date_DLC." date sql : ".$Date_DLC_sql." /y= ".$Date_DLC_y."  /m= ".$Date_DLC_m." /d= ".$Date_DLC_d."<br>";
echo "Num semaine : ".$semaine_intrant." <br>";
echo "Num fournisseur : ".$id_presta." <br>";
echo "Num_Categorie PLu: ".$num_cate_intrant." <br>";
echo "Num Lot : ".$Num_lot_intrant." | nb car : $nb_car_enrg<br>";
echo "Poids Froid : ".$Poids_intrant." | Num_bl : $Num_Bl<br>";
echo "Unite : ".$num_unite." <br>";
echo "Observations : ".$observations." <br>";
echo "clos ? : ".$cloture." <br>";

// Ajout table Decoupe

$sql1 = "INSERT INTO intrants VALUES (
'".$Num_entre_intrant_last_sel."',
 '".$IdNumClient."', 
'".$id_presta."', 
'".$Date_Entre_sql."', 
'".$Date_DLC_sql."', 
'".$Num_Bl."', 
'".$Poids_intrant."', 
'".$num_unite."', 
'".$num_cate_intrant."', 
'".$observations."', 
'".$Num_Plu."',
'".$Num_lot_intrant."',
'".$semaine_intrant."',
'".$cloture."')";

echo $sql1."&nbsp;";

$res = mysqli_query($link, $sql1);

echo "resultat requet : ".$res." <br>";

$last_num_entre = 0 ;
$sqla = "SELECT max(Num_entre_intrant) FROM intrants_stock";  
$reqa = mysqli_query($link, $sqla); 
$row = mysqli_fetch_row($reqa);
$last_num_entre= $row[0];
$last_num_entre = $last_num_entre + 1 ;

$sql2 = "INSERT INTO intrants_stock VALUES (
'".$last_num_entre."', 
'".$IdNumClient."', 
'0', 
'".$Num_lot_intrant."', 
'".$Date_Entre_sql."',
'".$semaine_intrant."',
'".$Poids_intrant."',
 NULL ) ";

echo $sql2."&nbsp;";

$res = mysqli_query($link, $sql2);

echo "resultat requet : ".$res." <br>";

Header("Location: entre_intrants.php?limite=0");
?>