<?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>";

/*
http://192.168.1.200/orius/ajout_saisie_fabrication_haut.php?ordre=1
Cate_Fabric=2
Num_plu_Fabric=18
dlc_plu_Fabric=
Num_Client_Fabric=15
Date_Fabric=2017-07-19
Poid_Fabric=168
num_unitee_plu_intran=1
Observation_Fabric=
ok=Valider
*/

// Get ou Post existe ou pas ?
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['Cate_Fabric'])) { $Cate_Fabric=$_POST['Cate_Fabric']; }  else {$Cate_Fabric=''; }
		if (isset($_POST['Num_plu_Fabric'])) { $Num_plu_Fabric=$_POST['Num_plu_Fabric']; } else {$Num_plu_Fabric=''; }
		if (isset($_POST['Num_Client_Fabric'])) { $Num_Client_Fabric=$_POST['Num_Client_Fabric']; } else {$Num_Client_Fabric=''; }
		if (isset($_POST['Date_Fabric'])) { $Date_Fabric=$_POST['Date_Fabric']; } else {$Date_Fabric=''; }
		if (isset($_POST['Poid_Fabric'])) { $Poid_Fabric=$_POST['Poid_Fabric']; } else {$Poid_Fabric=''; }
		if (isset($_POST['num_unitee_plu_intran'])) { $num_unitee_plu_intran=$_POST['num_unitee_plu_intran']; } else {$num_unitee_plu_intran=''; }
		if (isset($_POST['Observation_Fabric'])) { $observations=$_POST['Observation_Fabric']; } else {$observations=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['Cate_Fabric'])) { $Cate_Fabric=$_GET['Cate_Fabric']; }  else {$Cate_Fabric=''; }
		if (isset($_GET['Num_plu_Fabric'])) { $Num_plu_Fabric=$_GET['Num_plu_Fabric']; } else {$Num_plu_Fabric=''; }
		if (isset($_GET['Num_Client_Fabric'])) { $Num_Client_Fabric=$_GET['Num_Client_Fabric']; } else {$Num_Client_Fabric=''; }
		if (isset($_GET['Date_Fabric'])) { $Date_Fabric=$_GET['Date_Fabric']; } else {$Date_Fabric=''; }
		if (isset($_GET['Poid_Fabric'])) { $Poid_Fabric=$_GET['Poid_Fabric']; } else {$Poid_Fabric=''; }
		if (isset($_GET['num_unitee_plu_intran'])) { $num_unitee_plu_intran=$_GET['num_unitee_plu_intran']; } else {$num_unitee_plu_intran=''; }
		if (isset($_GET['Observation_Fabric'])) { $observations=$_GET['Observation_Fabric']; } else {$observations=''; }
		$valForm="GET" ;
}

$Semaine_Fabric =date("W", strtotime($Date_Fabric));

$sql = "SELECT Let_Client FROM clients WHERE Num_Client='$Num_Client_Fabric'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Let_Client_Fabric = $row["Let_Client"];
}

$sql = "SELECT attribu_plu FROM plu WHERE Num_PLU='$Num_plu_Fabric'";   
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
        $Let_Plu_Fabric = $row["attribu_plu"];
}

$sql = "SELECT dlc_plu FROM plu WHERE Num_PLU='$Num_plu_Fabric'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
       $dlc_Plu_Fabric = $row["dlc_plu"];
}

$sql = "SELECT * FROM categorie_fabrications WHERE num_cate_fabric='$Cate_Fabric'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) {
       $Let_Cate_Fabric = $row["Let_cate_fabric"];
}

$Anne_Fabric=date("y", strtotime($Date_Fabric));
$Traca_Fabric_Cal=date("z", strtotime($Date_Fabric));
$Traca_Fabric_Cal=$Traca_Fabric_Cal+1;

$Num_Lot_Calc=$Let_Client_Fabric.$Semaine_Fabric.$Let_Plu_Fabric.$Anne_Fabric ;

 $Date_Fabric_y=date("Y", strtotime($Date_Fabric));
 $Date_Fabric_m=date("m", strtotime($Date_Fabric));
 $Date_Fabric_d=date("d", strtotime($Date_Fabric));
 
$Date_dlc_calc = date("Y-m-d", mktime(0,0,0,$Date_Fabric_m,$Date_Fabric_d,$Date_Fabric_y)+ 24*3600*($dlc_Plu_Fabric+1));

$Car_traca_cal = strlen (trim ($Traca_Fabric_Cal));
switch ($Car_traca_cal) {
    case 1:
        $car_trac_deb="00".$Traca_Fabric_Cal ;
        break;
    case 2:
        $car_trac_deb="0".$Traca_Fabric_Cal ;
        break;
    case 3:
        $car_trac_deb=$Traca_Fabric_Cal ;
        break;
}

$Traca_Fabric_Cal.=$Let_Cate_Fabric ;


$last_num_entre = 0 ;
$sql = "SELECT max(Num_Fabric) FROM fabrications_haut";
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$last_num_entre = $row[0];
$last_num_entre = $last_num_entre + 1 ;
$last_num_entre_sel = $last_num_entre;

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

echo "<br>";
echo "Date fabric : $Date_Fabric  | DLC $dlc_Plu_Fabric jrs : $Date_dlc_calc <br>";
echo "Num semaine : ".$Semaine_Fabric." <br>";
echo "Num client : ".$Num_Client_Fabric." <br>";
echo "Num_Categorie PLu: ".$Num_plu_Fabric." <br>";
echo "Num Lot : ".$Num_Lot_Calc." <br>";
echo "Num Lot tace : ".$Traca_Fabric_Cal." <br>";
echo "Poids Froid : ".$Poid_Fabric." <br>";
echo "Num Unitee : ".$num_unitee_plu_intran." <br>";
echo "Observations : ".$observations." <br>";
echo "Enrg Encours : ".$last_num_entre." <br>";
echo "Time Stamps : ".$timestamp." <br>";
echo "verif Time Stamps : ".date("Y-m-d H:i:s", $timestamp)." <br>";


// Ajout table fabrications_haut

$sql1 = "INSERT INTO fabrications_haut VALUES (
'".$last_num_entre."', 
'".$IdNumClient."',  
'".$Num_plu_Fabric."', 
'".$Cate_Fabric."', 
'".$Num_Client_Fabric."', 
'".$Semaine_Fabric."', 
'".$Date_Fabric."', 
'".$Date_dlc_calc."', 
'".$Num_Lot_Calc."', 
'".$Traca_Fabric_Cal."',
'".$Poid_Fabric."', 
'".$num_unitee_plu_intran."', 
'".$observations."', 
'0', 
'".$today."')";

echo "requet : ".$sql1." <br>";

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

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

// Ajout table lot

$last_num_entre = 0 ;
$sql = "SELECT max(num_ligne_lot) FROM lot";  
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$last_num_entre = $row[0];
$last_num_entre = $last_num_entre + 1 ;

$sql2 = "INSERT INTO lot VALUES ( 
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$Num_Client_Fabric."',  
'".$Date_Fabric."',
'".$Num_Lot_Calc."', 
'".$Traca_Fabric_Cal."', 
'3', 
'".$Poid_Fabric."', 
'0', 
'0', 
'".$last_num_entre_sel."',
'".$observations."')";

echo "requet : ".$sql2." <br>";

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

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



Header("Location: saisie_fabrications_detail.php?ordre=1&Num_Fabric=$last_num_entre");
?>