<?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;
/*
http://192.168.1.200/orius/ajout_abattoir.php?
id_abat=1
DateEntre=2017-07-10
Num_Client=8
DateAbattage=2017-07-07
Num_CatPlu=1
plu=950
DateDecoupe=2017-07-11
NumIdent=FR4639332339
NumAbat=6828
NumTicket=22193
qtv=0
mrs=1
Poids=379.55
Poids_chd=387.20
Observations=ras
*/
// entete de la page

//include 'menu/header.php';

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

// menu haut new-supp-modif

echo "<center><br>";

$id_abat=htmlentities($_POST['id_abat']);

$DateEntre=htmlentities($_POST['DateEntre']);
$DateAbattage=htmlentities($_POST['DateAbattage']);
$DateDecoupe=htmlentities($_POST['DateDecoupe']);

$Num_ClientForm=htmlentities($_POST['Num_Client']);
$NumIdent=htmlentities($_POST['NumIdent']);
$NumAbat=htmlentities($_POST['NumAbat']);

$Poids=htmlentities($_POST['Poids']);
$Poids_chd=htmlentities($_POST['Poids_chd']);
$TempRecep=htmlentities($_POST['TempRecep']);
$Num_CatPlu=htmlentities($_POST['Num_CatPlu']);
$Observations=htmlentities($_POST['Observations']);
$plu=htmlentities($_POST['plu']);
$NumTicket=htmlentities($_POST['NumTicket']);
$mrs=htmlentities($_POST['mrs']);
$qtv=htmlentities($_POST['qtv']);

$myart =new clients;
$select = "SELECT * FROM clients WHERE Num_Client='$Num_ClientForm'";
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $Num_Client=$myart->Num_Client ;
  $Let_Client=$myart->Let_Client ;
  $Nom_Client=$myart->Nom_Client ;
  $Adr_Client1=$myart->adresse_1 ;
  $Adr_Client2=$myart->Adresse_2 ;
  $Adr_Client3=$myart->Adresse_3 ;
  $Telephone=$myart->Telephone ;
  $Portable=$myart->Portable ;
} 
$sql = "SELECT max(Num_entre) FROM entreabat";  
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$last_num_entre = $row[0];

$NumIdent=strtoupper($NumIdent);
$ident=substr($NumIdent, -4);
//$Num_Lot = printf("%s%s%s%s",$Let_Client,$semaine,$ident,$anne_a);
$DateAbattage_a = strtotime($DateAbattage);
$anne_a = date("y", $DateAbattage_a);
$semaine = date("W", $DateAbattage_a );
$Num_Lot = $Let_Client.$semaine.$ident.$anne_a ;

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


echo $id_abat." <br>";
echo "Date Es :".$DateEntre." <br>";
echo "Date As :".$DateAbattage." <br>";
echo "Date Ds :".$DateDecoupe." <br>";
echo "Num Client ".$Num_Client." <br>";
echo "Let Client ".$Let_Client." <br>";
echo "Nom Client ".$Nom_Client." <br>";
echo "Num lot : ".$Num_Lot."<br>";
echo "Num Ident :".$NumIdent." <br>";
echo "Num Abat : ".$NumAbat." <br>";
echo "Poids Froid : ".$Poids." <br>";
echo "Poids Chaud : ".$Poids_chd." <br>";
echo "Temp Recep : ".$TempRecep ." <br>";
echo "Categorie PLU : ".$Num_CatPlu." <br>";
echo "Observations : ".$Observations." <br>";
echo "Plu : ".$plu." <br>";
echo "Mrs : ".$mrs." <br>";
echo "Qtv : ".$qtv." <br>";
echo "IdNumClient : ".$IdNumClient." <br>";
echo "Last Enrg : ".$last_num_entre." <br>";

$num_entre_abat = $last_num_entre+1 ;


$sql = "INSERT INTO entreabat VALUES (
'".$num_entre_abat."', 
'".$IdNumClient."', 
'".$id_abat."', 
'".$DateEntre."', 
'".$DateAbattage."', 
'".$DateDecoupe."',
'".$NumTicket."', 
'".$semaine."', 
'".$NumIdent."', 
'".$NumAbat."', 
'".$Poids."', 
'".$Poids_chd."', 
'".$TempRecep."', 
'".$Num_CatPlu."', 
'".$Observations."', 
'".$Num_Client."', 
'".$plu."', 
'".$Num_Lot."',
'0',
'".$Poids."',
'".$mrs."',
'".$qtv."',
'0')";

echo $sql." <br>";

$res="";

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

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


/*
SELECT id FROM monTable
ORDER BY id DESC
LIMIT 1;
*/

$sql = "SELECT max(Num_entre) FROM entreabat";  
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$last_num_entre = $row[0];

echo "requette : ".$sql."<br>  relt : ?? ".$last_num_entre."<br>";

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

$sql1 = "INSERT INTO lot_dispo VALUES ('".$last_num_entre."','".$IdNumClient."', '".$Num_Lot."', '', '0')";

$res5="";
echo "requet : ".$sql1." <br>";
$res5 =  mysqli_query($link, $sql1);
echo "resultat requet : ".$res5." <br>";

$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."', 
'".$DateDecoupe."', 
'".$Num_Lot."', 
'', 
'4', 
'".$Poids."', 
'0', 
'1', 
'".$num_entre_abat."', '')";

$res2 = "";
echo "requet : ".$sql2." <br>";
$res2 =  mysqli_query($link, $sql2);
echo "resultat requet : ".$res2." <br>";

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

if ($mrs==1) {
echo "mrs ? : Oui  =>".$mrs." <br>";
$sql = "INSERT INTO table_mrs VALUES (
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$NumIdent."', 
'".$Num_Client."', 
'".$DateEntre."', 
'".$DateDecoupe."', 
'".$Poids."', NULL, NULL, NULL, NULL, NULL, '0')";

$res6="";
echo $sql." <br>";
$res6 =  mysqli_query($link, $sql); 
echo "resultat requet : ".$res6." <br>";
} else {
echo "mrs ? : Non  =>".$mrs." <br>";
}

if ($qtv==1) {
	
$last_num_entre = 0 ;
$sql = "SELECT max(Num_ligne_qtv) FROM quantitative_haut";  
$req = mysqli_query($link, $sql); 
$row = mysqli_fetch_row($req);
$last_num_entre = $row[0];
$last_num_entre = $last_num_entre + 1 ;

echo "qtv ? : Oui  =>".$qtv." <br>";
$sql = "INSERT INTO quantitative_haut VALUES (
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$DateDecoupe."', 
'".$semaine."', 
'".$Num_Client."', 
'".$Num_Lot."', 
'', 
'".$Poids."', 
'0', 
'".$Observations."')";

$res7="";
echo $sql." <br>";
$res7 =  mysqli_query($link, $sql);
echo "resultat requet : ".$res7." <br>";
} else {
echo "Qtv ? : Non  =>".$qtv." <br>";
}


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