<?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');
/* Pour Matiere Premiere
ajout_fabrication_detail.php
ordre=1
Num_Fabric=2
TypeFabric=1
Plu_Fabric=98
PoidLot=25
num_unitee_plu_intran=1
ok=%2B
*/
/* => Intrants
ajout_fabrication_detail.php
ordre=1
Num_Fabric=2
TypeFabric=2
PoidLot=.250
plu_intrant=1
num_unitee_plu_intran=1
categorie_plu_intran=5
num_ligne_lot_intrant=12
ok=%2B
*/
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['Num_Fabric'])) { $num_fabric  =$_POST['Num_Fabric']; }  else {$num_fabric =''; }
		if (isset($_POST['TotalLot'])) { $TotalLot =$_POST['TotalLot']; }  else {$TotalLot=''; }
		if (isset($_POST['PoidLot'])) { $PoidLot = $_POST['PoidLot']; } else {$PoidLot=''; }
		if (isset($_POST['TypeFabric'])) { $TypeFabric=$_POST['TypeFabric']; }  else {$TypeFabric=''; }
		if (isset($_POST['Plu_Fabric'])) { $Plu_Fabric=$_POST['Plu_Fabric']; } else {$Plu_Fabric=''; }
		if (isset($_POST['Cate_intrant'])) { $Cate_intrant=$_POST['Cate_intrant']; } else {$Cate_intrant=''; }
		if (isset($_POST['num_unitee_plu_intran'])) { $num_unitee_plu=$_POST['num_unitee_plu_intran']; } else {$num_unitee_plu_intran=''; }
		if (isset($_POST['plu_intrant'])) { $Plu_Intrant=$_POST['plu_intrant']; } else {$Plu_Intrant=''; }
		if (isset($_POST['categorie_plu_intran'])) { $categorie_plu_intran=$_POST['categorie_plu_intran']; } else {$categorie_plu_intran=''; }		
		if (isset($_POST['num_ligne_lot_intrant'])) { $num_ligne_lot_intrant=$_POST['num_ligne_lot_intrant']; } else {$num_ligne_lot_intrant=''; }		
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['Num_Fabric'])) { $num_fabric  =$_GET['Num_Fabric']; }  else {$num_fabric =''; }
		if (isset($_GET['TotalLot'])) { $TotalLot =$_GET['TotalLot']; }  else {$TotalLot=''; }
		if (isset($_GET['PoidLot'])) { $PoidLot = $_GET['PoidLot']; } else {$PoidLot=''; }
		if (isset($_GET['TypeFabric'])) { $TypeFabric=$_GET['TypeFabric']; }  else {$TypeFabric=''; }
		if (isset($_GET['Plu_Fabric'])) { $Plu_Fabric=$_GET['Plu_Fabric']; } else {$Plu_Fabric=''; }
		if (isset($_GET['Cate_intrant'])) { $Cate_intrant=$_GET['Cate_intrant']; } else {$Cate_intrant=''; }
		if (isset($_GET['num_unitee_plu_intran'])) { $num_unitee_plu=$_GET['num_unitee_plu_intran']; } else {$num_unitee_plu_intran=''; }
		if (isset($_GET['plu_intrant'])) { $Plu_Intrant=$_GET['plu_intrant']; } else {$Plu_Intrant=''; }
		if (isset($_GET['categorie_plu_intran'])) { $categorie_plu_intran=$_GET['categorie_plu_intran']; } else {$categorie_plu_intran=''; }		
		if (isset($_GET['num_ligne_lot_intrant'])) { $num_ligne_lot_intrant=$_GET['num_ligne_lot_intrant']; } else {$num_ligne_lot_intrant=''; }
		$valForm="GET" ;
}
echo "$TypeFabric" ;
$PoidLot=number_format($PoidLot, 3, '.', ' ')	;

switch ($TypeFabric) {
	case 1:
	
	$Cate_intrant=11;
	$myart =new lot;
	$select = "SELECT * FROM lot WHERE `num_ligne_lot`='$Plu_Fabric'";
	$result = mysqli_query($link, $select);
	$myart->getnum($result);
	while ($myart->z < $myart->num) {
	$myart->readrow($result);
	$Num_ligne_plu=$myart->num_ligne_lot ;
	$Num_lot_plu=$myart->num_lot_decoupe ;
	$Num_lot_plu_trace=$myart->num_lot_tace_decoupe ;
	$Date_Lot=$myart->date_lot_decoupe ;
}
	
	$sql = "SELECT Num_categoriePlu FROM entreabat WHERE `Num_Lot`='$Num_lot_plu'";    
	$req = mysqli_query($link, $sql);
	while ($row = mysqli_fetch_assoc($req)) {$NumTypeLot = $row["Num_categoriePlu"];}  
	$Plu_Intrant = "1000".$NumTypeLot ;

	

// conversion unites tout en Kg ou Litres
	$myart =new unitee;
	$select = "SELECT * FROM unitee WHERE `num_unite`='$num_unitee_plu'";
	$result = mysqli_query($link, $select);
	$myart->getnum($result);
	while ($myart->z < $myart->num) {
	$myart->readrow($result);
	$num_unitee_plu=$myart->num_unite ;
  $nom_unite_long=$myart->nom_unite_long  ;
  $nom_unite_ini=$myart->nom_unite_ini  ;
  $conversion=$myart->conversion  ;
}

echo "<br>Unite : $num_unitee_plu | $nom_unite_long | $nom_unite_ini | $conversion <br>" ;

switch ($num_unitee_plu) {
case 1: // deja en Kilo
break ;
case 2: // en Gramme => Kilo
$PoidLot=$PoidLot * $conversion ;
$num_unitee_plu = 1 ;
break ;
case 3:
break ;
case 4:
break ;
case 5:
}
echo "Apres conversion : $PoidLot | $conversion <br>" ;

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


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

echo "<br>Num Type Lot : $NumTypeLot <br>";

echo "Ordre : ".$num_ordre."  | Num enrg : ".$last_num_entre."<br>";
echo "Num Fabric : ".$num_fabric." <br>";
echo "Type Fabric : ".$TypeFabric." <br>";
echo "Cate Intran : ".$Cate_intrant." <br>";
echo "Num Lot : ".$Num_lot_plu." | Cate Plu : $NumTypeLot <br>";
echo "Traca Lot : ".$Num_lot_plu_trace." <br>";
echo "Poids Fabric : ".$PoidLot." <br>";
echo "Totalite plu Fabric : ".$TotalLot." <br>";
echo "Plu Fabric : ".$Plu_Fabric." <br>";
echo "Plu intrant : ".$Plu_Intrant." <br>";
echo "Unite : ".$num_unitee_plu." <br>";
echo "date Lot : ".$Date_Lot." <br>";

	$sql_Ajt = "INSERT INTO fabrications_detail VALUES (
	'".$last_num_entre."',
	'".$IdNumClient."',  
	'".$num_fabric."', 
	'".$TypeFabric."',
	'".$Cate_intrant."',
	'".$Plu_Intrant."', 
	'".$Date_Lot."', 
	'".$Num_lot_plu."', 
	'".$Num_lot_plu_trace."', 
	'".$num_unitee_plu."', 
	'".$PoidLot."', 
	'".$today."')";
	
echo $sql_Ajt." <br>";

$req_Ajt = mysqli_query($link, $sql_Ajt);

echo "resultat requet : ".$req_Ajt ;
break ;
	
	case 2:

$Plu_Intrant=$Plu_Intrant;	
$Cate_intrant=$categorie_plu_intran ;
$Num_lot_plu_trace='';

	$sql = "SELECT * FROM intrants WHERE `Num_entre_intrant`='$num_ligne_lot_intrant'";    
	$req = mysqli_query($link, $sql);
	while ($row = mysqli_fetch_assoc($req)) {$Num_lot_plu = $row["Num_Lot"];}  

	$sql = "SELECT * FROM fabrications_haut WHERE `Num_Fabric`='$num_fabric'";    
	$req = mysqli_query($link, $sql);
	while ($row = mysqli_fetch_assoc($req)) {
		$Date_Lot = $row["Date_Fabric"];

		}  



// conversion unites tout en Kg ou Litres
	$myart =new unitee;
	$select = "SELECT * FROM unitee WHERE `num_unite`='$num_unitee_plu'";
	$result = mysqli_query($link, $select);
	$myart->getnum($result);
	while ($myart->z < $myart->num) {
	$myart->readrow($result);
	$num_unitee_plu=$myart->num_unite ;
  $nom_unite_long=$myart->nom_unite_long  ;
  $nom_unite_ini=$myart->nom_unite_ini  ;
  $conversion=$myart->conversion  ;
}

echo "<br>Unite : $num_unitee_plu | $nom_unite_long | $nom_unite_ini | $conversion <br>" ;

switch ($num_unitee_plu) {
case 1: // deja en Kilo
break ;
case 2: // en Gramme => Kilo
$PoidLot=$PoidLot * $conversion ;
$num_unitee_plu = 1 ;
break ;
case 3:
break ;
case 4:
break ;
case 5:
}
echo "Apres conversion : $PoidLot | $conversion <br>" ;

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

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


echo "Ordre : ".$num_ordre."  | Num enrg : ".$last_num_entre."<br>";
echo "Num Fabric : ".$num_fabric." <br>";
echo "Type Fabric : ".$TypeFabric." <br>";
echo "Cate Intran : ".$Cate_intrant." <br>";
echo "Num Lot : ".$Num_lot_plu." | Cate Plu : $NumTypeLot <br>";
echo "Traca Lot : ".$Num_lot_plu_trace." <br>";
echo "Poids Fabric : ".$PoidLot." <br>";
echo "Totalite plu Fabric : ".$TotalLot." <br>";
echo "Plu Fabric : ".$Plu_Fabric." <br>";
echo "Plu intrant : ".$Plu_Intrant." <br>";
echo "Unite : ".$num_unitee_plu." <br>";
echo "date Lot : ".$Date_Lot." <br>";

	$sql = "INSERT INTO fabrications_detail VALUES ('".$last_num_entre."',
	'".$IdNumClient."',  
	'".$num_fabric."', 
	'".$TypeFabric."',
	'".$Cate_intrant."',
	'".$Plu_Intrant."', 
	'".$Date_Lot."', 
	'".$Num_lot_plu."', 
	'".$Num_lot_plu_trace."', 
	'".$num_unitee_plu."', 
	'".$PoidLot."', 
	'".$today."')";
	
echo $sql." <br>";

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

echo "resultat requet : ".$req ;	
	
	break ;
}	

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

?>
