<?php
include("include/data.php");
Init_boutique();
global $base, $MySQL_Host, $MySQL_User, $MySQL_Passw, $myart, $result, $NbLigne, $semaine, $limitesuivante, $limiteprecedente, 
$total, $page, $NumTypeLot , $timestamp ;
// entete de la page

//include 'menu/header.php';

$date = date("d-m-Y");
$timestamp = date('Y-m-d G:i:s');

/* 
http://192.168.1.200/orius/validation_fabrication.php?
ordre=1
Num_Fabric=1
valid=Validation+de+Fabricaiton
*/
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['Num_Fabric'])) { $num_fabric  =$_POST['Num_Fabric']; }  else {$num_fabric =''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['Num_Fabric'])) { $num_fabric  =$_GET['Num_Fabric']; }  else {$num_fabric =''; }
		$valForm="GET" ;
}
echo "$valForm" ;

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

// Matiere premiere dans lot
echo "<br>Matiere 1ere<br>";
$myart =new fabrications_detail;
$select = "SELECT * FROM fabrications_detail WHERE Num_Fabric='$num_fabric' AND Type_Fabric='1'";
$select.=" AND IdNumClient='".$IdNumClient."'";
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
	$myart->readrow($result) ;
	$Num_Fabric_Ligne=$myart->Num_Fabric_Haut ;
	$Num_plu_Intrant=$myart->Num_plu_Intrant;
	$Num_Lot_Intrant=$myart->Num_Lot_Intrant ;
	$Traca_Intrant=$myart->Traca_Intrant ;
	$untie_intrant=$myart->untie_intrant ;
	$Poid_Intrant=$myart->Poid_Intrant ;	
	$Date_fabric =$myart->DLC_Intrant ; 

$sql = "SELECT PLU FROM plu WHERE Num_PLU='$Num_plu_Intrant'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $Pluintrant = $row["PLU"];} 

$sql = "SELECT * FROM fabrications_haut WHERE Num_Fabric='$num_fabric'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $Num_plu_Fabric = $row["Num_plu_Fabric"];} 	

$sql = "SELECT PLU FROM plu WHERE Num_PLU='$Num_plu_Fabric'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $NomPluintrant = $row["PLU"];} 

$sql = "SELECT num_client FROM decoupe WHERE num_lot_decoupe='$Num_Lot_Intrant' AND tracabilite_decoupe='$Traca_Intrant'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $num_clientDecoupe = $row["num_client"];} 

echo "Num Fabric : $Num_Fabric_Ligne | ";
echo "Num Plu : $Num_plu_Intrant  | Num Client Lot : $num_clientDecoupe<br>";
echo "Nom plu : $Pluintrant | Num Plu Frabric : $Num_plu_Fabric | Nom plu fabric : $NomPluintrant | ";
echo "num Lot : $Num_Lot_Intrant  | traca : $Traca_Intrant | date : $Date_fabric | ";
echo "Poid plu : $Poid_Intrant<br>";
echo "";

$observations = $Num_plu_Fabric."|".$NomPluintrant ;

$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 ;

	$sql = "INSERT INTO lot VALUES (
	'".$last_num_entre."',
	'".$IdNumClient."',
	'".$num_clientDecoupe ."',  
	'".$Date_fabric ."', 
	'".$Num_Lot_Intrant."',
	'".$Traca_Intrant."',
	'3', 
	'".$Poid_Intrant."', 
	'1', 
	'0', 
	'".$Num_Fabric_Ligne."', 
	'".$observations."')";
	
$req1 = mysqli_query($link, $sql);

echo "sql : $sql | Rq : ".$req1."<br>" ;	
}
 
// Intrant dans Intrant stock

echo "<br>*-* Intrants *-*<br>";
$myart =new fabrications_detail;
$select = "SELECT * FROM fabrications_detail WHERE Num_Fabric='$num_fabric' AND Type_Fabric='2'";
$select.=" AND IdNumClient='".$IdNumClient."'";
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
	$myart->readrow($result) ;
	$Num_Fabric_Ligne=$myart->Num_Fabric_Haut ;
	$Num_plu_Intrant=$myart->Num_plu_Intrant;
	$Num_Lot_Intrant=$myart->Num_Lot_Intrant ;
	$Traca_Intrant=$myart->Traca_Intrant ;
	$untie_intrant=$myart->untie_intrant ;
	$Poid_Intrant=$myart->Poid_Intrant ;	
	$Date_fabric =$myart->DLC_Intrant ; 

$sql = "SELECT * FROM plu_intrant WHERE num_plu_intrant='$Num_plu_Intrant'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $Pluintrant = $row["nom_plu_intrant"];} 

$sql = "SELECT * FROM fabrications_haut WHERE Num_Fabric='$num_fabric'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $Num_plu_Fabric = $row["Num_plu_Fabric"];} 	

$sql = "SELECT PLU FROM plu WHERE Num_PLU='$Num_plu_Fabric'";
$sql.=" AND IdNumClient='".$IdNumClient."'";  
$req = mysqli_query($link, $sql);
while ($row = mysqli_fetch_assoc($req)) { $NomPluintrant = $row["PLU"];} 

echo "Num Fabric : $Num_Fabric_Ligne | ";
echo "Num Plu : $Num_plu_Intrant  | ";
echo "Nom plu : $Pluintrant | Num Plu Frabric : $Num_plu_Fabric | Nom plu fabric : $NomPluintrant | ";
echo "num Lot : $Num_Lot_Intrant  | traca : $Traca_Intrant | date : $Date_fabric | ";
echo "Poid plu : $Poid_Intrant<br>";
echo "";

$semainefabic = date("W", strtotime("$Date_fabric"));

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

	$sql = "INSERT INTO intrants_stock VALUES (
	'".$last_num_entre."',
	'".$IdNumClient."',  
	'1', 
	'".$Num_Lot_Intrant."',
	'".$Date_fabric."',
 	'".$semainefabic."',
	'".$Poid_Intrant."', 
	'".$Num_plu_Intrant."')";
	
$req1 = mysqli_query($link, $sql);

echo "sql : $sql | Rq : ".$req1."<br>" ;	
}

// mise a jour fabrication valide
$sql = "UPDATE fabrications_haut SET 
`Etat` = '1' 
WHERE 
`Num_Fabric` =$num_fabric";

$req2 = mysqli_query($link, $sql);

echo "sql : $sql | Rq : ".$req2."<br>" ;	

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

?>
