<?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, $res;
// entete de la page
//include 'menu/header.php';

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

// menu haut new-supp-modif

echo "<center>";
Echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
Echo "<tbody>";
echo "<a href='saisie_ajout_abattoir.php'><b>Nouveau</b></a> <b> | </b>";
echo "<a href=''><b>Consulter</b></a> <b> | </b>";
echo "<a href=''><b>Supprimer</b></a> <b> | </b>";
echo "<a href=''><b>Modifier</b></a>";
Echo "</td></tr>";
Echo "</tbody>";
Echo "</table><br>";

$Num_entre=$_GET['Num_entre'];
echo "<br>Num Entre : ".$Num_entre."<br>" ;

$id_abat=htmlentities($_POST['id_abat']);
$DateEntre_y = $_POST['DateEntre_y'];
$DateEntre_m = $_POST['DateEntre_m'];
$DateEntre_d = $_POST['DateEntre_d'];

$DateAbat_y = $_POST['DateAbat_y'];
$DateAbat_m = $_POST['DateAbat_m'];
$DateAbat_d = $_POST['DateAbat_d'];

$Date_decoupe_y = $_POST['Date_decoupe_y'];
$Date_decoupe_m = $_POST['Date_decoupe_m'];
$Date_decoupe_d = $_POST['Date_decoupe_d'];

$Num_Client=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']);
$Num_Lot = htmlentities($_POST['Num_Lot']);
$Num_Lot = strtoupper($Num_Lot) ;

$NumTicket=htmlentities($_POST['NumTicket']);
$mrs=htmlentities($_POST['mrs']);
$qtv=htmlentities($_POST['qtv']);

$Date_Entre=date("d/m/y", mktime(0,0,0,$DateEntre_m,$DateEntre_d,$DateEntre_y));
$DataAbat=date("d/m/y", mktime(0,0,0,$DateAbat_m,$DateAbat_d,$DateAbat_y));
$DataDecoup=date("d/m/y", mktime(0,0,0,$Date_decoupe_m,$Date_decoupe_d,$Date_decoupe_y));

$Date_Es=date("Y-m-d", mktime(0,0,0,$DateEntre_m,$DateEntre_d,$DateEntre_y));
$Date_As=date("Y-m-d", mktime(0,0,0,$DateAbat_m,$DateAbat_d,$DateAbat_y));
$Date_Ds=date("Y-m-d", mktime(0,0,0,$Date_decoupe_m,$Date_decoupe_d,$Date_decoupe_y));

$Date_Entre=date("Y-m-d", mktime(0,0,0,$DateEntre_m,$DateEntre_d,$DateEntre_y));
$DataAbat=date("Y-m-d", mktime(0,0,0,$DateAbat_m,$DateAbat_d,$DateAbat_y));

$SemaineAbat=date("W", mktime(0,0,0,$DateAbat_m,$DateAbat_d,$DateAbat_y));
$SemaineDec=date("W", mktime(0,0,0,$Date_decoupe_m,$Date_decoupe_d,$Date_decoupe_y));
//$Date_Entre=date("Y-m-d",strtotime($Date_Entre));
//$DataAbat=date("Y-m-d",$DataAbat);

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

echo "Num Entre :".$Num_entre." <br>";
echo "Id Abat : ".$id_abat." <br>";
echo "Date Es :".$Date_Es." <br>";
echo "Date As :".$Date_As." <br>";
echo "Date Ds :".$Date_Ds." <br>";
echo "Num Client ".$Num_Client." <br>";
echo "Num Ident :".$NumIdent." <br>";
echo "Num Abat :".$NumAbat." <br>";
echo "Poid Fd : ".$Poids." <br>";
echo "Poid Chd : ".$Poids_chd." <br>";
echo "Temps Entre : ".$TempRecep." <br>";
echo "Num Plu :".$Num_CatPlu." <br>";
echo "Observation : ".$Observations." <br>";
echo "Plu :".$plu." <br>";
echo "Num lot :".$Num_Lot." <br>";
echo "Semaine Abat :".$SemaineAbat." <br>";
echo "Semaine Dec  :".$SemaineDec." <br>";
echo "`IdNumClient`  :".$IdNumClient."<br>";
if ($mrs==1)
echo "mrs ? : Oui  =>".$mrs." <br>";
else
echo "mrs ? : Non  =>".$mrs." <br>";
if ($qtv==1)
echo "qtv ? : Oui  =>".$qtv." <br>";
else
echo "qtv ? : Non  =>".$qtv." <br>";

// Maj Lot dispo
// recheche dans lot dispo le num lot a modifier
echo "<br><br> MAj Lot dispo<br>";

$sql = "SELECT Num_Lot FROM entreabat WHERE `Num_entre`='$Num_entre'";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $num_lot_avmodif = $row["Num_Lot"];
    }  
echo $sql." | ".$num_lot_avmodif ;
echo "<br>*-*-*<br>";

$sql1 = "SELECT num_ligne_lot_dispo FROM lot_dispo WHERE `num_lot_dispo`='$num_lot_avmodif'";  
$req = mysqli_query($link, $sql1);  
while ($row = mysqli_fetch_assoc($req)) {
        $num_ligne_lot_dispo = $row["num_ligne_lot_dispo"];
    }  
echo $sql1." | ".$num_ligne_lot_dispo ;

echo "<br>*-*-*<br>";
$sql = "UPDATE lot_dispo SET 
`num_lot_dispo`='$Num_Lot' WHERE `num_ligne_lot_dispo`='$num_ligne_lot_dispo' AND `IdNumClient`=$IdNumClient";

echo $sql."<br>";
$res = mysqli_query($link, $sql);  
echo "Resultat Sql :".$res."<br>";

echo "<br>*-*-*<br>";


//$sql = "INSERT INTO entreabat VALUES ('', '".$id_abat."', '".$Date_Es."', '".$Date_As."', '".$Date_Ds."','".$NumTicket."', '".$SemaineAbat."', '".$NumIdent."', '".$NumAbat."','".$Poids."', '".$Poids_chd."', '".$Num_CatPlu."', '".$Observations."', '".$Num_Client."', '".$plu."', '".$Num_Lot."','0','".$Poids."','".$mrs."')";

$sql = "UPDATE entreabat SET 
`Num_Abattoir`='$id_abat', `Date_Entre`='$Date_Es', `DataAbat`='$Date_As', 
`DateDecoupe`='$Date_Ds', `NumTicket`='$NumTicket', `SemaineAbat`='$SemaineAbat', 
`NumIdentAn`='$NumIdent', `Num_Abatage`='$NumAbat', `Poid`='$Poids', `TempRecep`='$TempRecep',
`Poid_Chaud`='$Poids_chd', `Num_categoriePlu`='$Num_CatPlu', `Observations`='$Observations', 
`Num_Client`='$Num_Client', `Num_Plu`='$plu', `Num_Lot`='$Num_Lot', 
`Poid_saisie`='0', `Poid_entre`='$Poids', `Mrs`='$mrs', `qtv`='$qtv' WHERE `Num_entre`='$Num_entre' AND `IdNumClient`=$IdNumClient ";

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

$res =0;
$sql = "UPDATE lot SET 
`date_lot_decoupe` = '$Date_Ds', 
`num_lot_decoupe` = '$Num_Lot', 
`num_cate_decoupe` = '4', 
`poid_decoupe` = '$Poids', 
`etat_decoupe` = '0', 
`solde_decoupe` = '0',
`Num_Client`='$Num_Client',
`observations` = '$Observations' 
WHERE `num_ligne_detail` = '$Num_entre' AND `IdNumClient`=$IdNumClient";

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

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

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



// M.R.S.

if ($mrs==1) {
echo "mrs ? : Oui  =>".$mrs." <br>";

$myart =new table_mrs;
$select = "SELECT  max(Num_ligne_mrs) FROM table_mrs WHERE Num_ident_Animal='$NumIdent'";
$req = mysqli_query($link, $select);
$row = mysqli_fetch_row($req);
$res = $row[0];
echo "mrs existe ? =>".$res." <br>";

if(isset($res)) {  // l'url existe déjà, on  met a jour les valeurs
echo "resiltat : ".$res." <br>";

$sql = "UPDATE table_mrs SET 
`Num_ident_animal`='$NumIdent', 
`Num_Clients`='$Num_Client', 
`Date_Entre`='$Date_Es', 
`Date_Decoupe`='$Date_As', 
`Poids_carcasse`='$Poids'  
WHERE `Num_ligne_mrs`='$res' AND `IdNumClient`=$IdNumClient ";

echo $sql."<br>";
$res = mysqli_query($link, $sql);  
echo "Resultat Sql : $res <br>";


 } else {  // l'url existe pas , on  ajoute les valeurs
echo "mrs ? : Oui  =>".$mrs." <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 ;

$sql = "INSERT INTO table_mrs VALUES (
'".$last_num_entre."', 
'".$IdNumClient."',  
'".$NumIdent."',
'".$Num_Client."', 
'".$Date_Es."', 
'".$Date_Ds."', 
'".$Poids."', NULL, NULL, NULL, NULL, NULL, '0')";

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

}
} else {
echo "mrs ? : Non  =>".$mrs." <br>";
}

// Qtv



if ($qtv==1) {
echo "Qtv ? : Oui => ".$qtv."<br>";
$sql5 = "SELECT Num_ligne_qtv FROM quantitative_haut WHERE `num_lot`='$Num_Lot'";  
$req2 = mysqli_query($link, $sql5);  
while ($row = mysqli_fetch_assoc($req2)) {
        $Num_ligne_qtv = $row["Num_ligne_qtv"];
    }  

echo $sql5."<br>";
if (isset($Num_ligne_qtv)) {
echo "Ligne : ".$Num_ligne_qtv." => Mise a jour<br>";

$sql = "UPDATE quantitative_haut SET 
`Date_decoupe`='$Date_Ds', 
`semaine_decoupe`='$SemaineDec', 
`num_Client`='$Num_Client', 
`num_lot`='$Num_Lot', 
`poid_decoupe`='$Poids', 
`Observations`='$Observations' 
WHERE `Num_ligne_qtv`='$Num_ligne_qtv' AND `IdNumClient`=$IdNumClient ";

echo $sql."<br>";
$res = mysqli_query($link, $sql);  
echo "Resultat Sql : $res <br>";

}	else {
echo "Ligne : n'exite pas donc on la cree <br>";

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

$sql = "INSERT INTO quantitative_haut VALUES (
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$Date_Ds."', 
'".$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");
?>
