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

$date = date("Y/m/d");
$timestamp = date('Y-m-d G:i:s');
//$semaine = date("w");
// menu haut new-supp-modif

echo "<center><br>";

/*
ajout_detail_bl_valid.php?
ordre=1
num_bl_ligne=1
num_bl=1
Code_tarif_Client=1 num_bl_ref
*/
if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		if (isset($_POST['ordre'])) { $num_ordre=$_POST['ordre']; }  else {$num_ordre=''; }
		if (isset($_POST['num_bl_ligne'])) { $num_bl_ligne=$_POST['num_bl_ligne']; }  else {$num_bl_ligne=''; }
		if (isset($_POST['num_bl'])) { $num_bl_base=$_POST['num_bl']; }  else {$num_bl_base=''; }
		if (isset($_POST['num_eleveur'])) { $num_eleveur=$_POST['num_eleveur']; } else {$num_eleveur=''; }
		if (isset($_POST['Code_tarif_Client'])) { $Code_tarif_Client=$_POST['Code_tarif_Client']; }  else {$Code_tarif_Client=''; }
		if (isset($_POST['num_lot_entre'])) { $num_lot_entre=$_POST['num_lot_entre']; } else {$num_lot_entre=''; }
		if (isset($_POST['num_trace_entre'])) { $num_trace_entre=$_POST['num_trace_entre']; }  else {$num_trace_entre=''; }
		if (isset($_POST['num_bl_complet'])) { $num_bl_complet=$_POST['num_bl_complet']; }  else {$num_bl_complet=''; }
		if (isset($_POST['num_bl_ref'])) { $num_bl_ref=$_POST['num_bl_ref']; }  else {$num_bl_ref=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		if (isset($_GET['ordre'])) { $num_ordre=$_GET['ordre']; }  else {$num_ordre=''; }
		if (isset($_GET['num_bl_ligne'])) { $num_bl_ligne=$_GET['num_bl_ligne']; }  else {$num_bl_ligne=''; }
		if (isset($_GET['num_bl'])) { $num_bl_base=$_GET['num_bl']; }  else {$num_bl_base=''; }
		if (isset($_GET['num_eleveur'])) { $num_eleveur=$_GET['num_eleveur']; } else {$num_eleveur=''; }
		if (isset($_GET['num_client_entre'])) { $num_client_entre=$_GET['num_client_entre']; }  else {$num_client_entre=''; }
		if (isset($_GET['Code_tarif_Client']) ){$Code_tarif_Client = $_GET['Code_tarif_Client'];} else {$Code_tarif_Client = '';}
		if (isset($_GET['num_lot_entre'])) { $num_lot_entre=$_GET['num_lot_entre']; } else {$num_lot_entre=''; }
		if (isset($_GET['num_trace_entre'])) { $num_trace_entre=$_GET['num_trace_entre']; }  else {$num_trace_entre=''; }
		if (isset($_GET['num_bl_complet'])) { $num_bl_complet=$_GET['num_bl_complet']; }  else {$num_bl_complet=''; }
		if (isset($_GET['num_bl_ref'])) { $num_bl_ref=$_GET['num_bl_ref']; }  else {$num_bl_ref=''; }
		$valForm="GET" ;
}


// valide donc on ajourt les lignes et efface la table temporaire	
echo "$valForm | Valider";
// process des requettes

$totalPoids=0;
$totalPi=0;
$totalmontant=0;

	$sql="select num_eleveur, num_plu, num_lot, num_traca, SUM(poids), SUM(nb_pi)  FROM temp_detail_br" ;
	$sql.=" WHERE num_bl_ligne='".$num_bl_ligne."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" GROUP BY num_eleveur, num_lot, num_plu, num_traca ASC";
	$sql.=" ORDER BY `temp_detail_br`.`num_plu` ASC";

	$req=mysqli_query($link, $sql);  
	while ($row = mysqli_fetch_assoc($req)) {

	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["SUM(poids)"];
	        $nb_pi_temp = $row["SUM(nb_pi)"];
	        $num_lot_temp = $row["num_lot"];
	        $num_traca_temp = $row["num_traca"];
					$num_eleveur_base = $row["num_eleveur"];
						
					$sql1="select * FROM plu" ;
					$sql1.=" WHERE Num_PLU='".$num_plu_temp."'";
					$sql1.=" AND IdNumClient='".$IdNumClient."'";  
					$req1=mysqli_query($link, $sql1);  
					while ($row = mysqli_fetch_assoc($req1)) { 
						$nom_plu_temp = $row["PLU"];
						$dlc_plu = $row["dlc_plu"];
						} 
						
					$sql2="SELECT Num_Base_Tarifs, Num_PLU, Prix_HT, Prix_TTC, Code_Tva, PrixPoid, Base_Tarifs.Num_Code_Tarif" ;
					$sql2.=" FROM `Base_Tarifs`";
					$sql2.=" INNER JOIN categorie_tarif";
					$sql2.=" ON categorie_tarif.Num_Code_Tarif = Base_Tarifs.Num_Code_Tarif AND categorie_tarif.IdNumClient = Base_Tarifs.IdNumClient";
					$sql2.=" WHERE Num_PLU='".$num_plu_temp."'";
					$sql2.=" AND Base_Tarifs.Num_Code_Tarif='".$Code_tarif_Client."'";
					$sql2.=" AND Base_Tarifs.IdNumClient='".$IdNumClient."'";  
					$req2=mysqli_query($link, $sql2);  
					while ($row = mysqli_fetch_assoc($req2)) { 
						$puht = $row["Prix_HT"];
						$PrixPoid= $row["PrixPoid"];
						} 	

					$sql3="select * FROM stock" ;
					$sql3.=" WHERE Num_Lot='".$num_lot_temp."'";
					$sql3.=" AND EtatES=0";
					$sql3.=" AND Num_Traca='".$num_traca_temp."'";
					$sql3.=" AND IdNumClient='".$IdNumClient."'";  
					$req3=mysqli_query($link, $sql3);  
					while ($row = mysqli_fetch_assoc($req3)) { 
						$date_lot_entre = $row["Date_Etiq"];
						} 
$dlc_temp = date("Y-m-d", strtotime($date_lot_entre)+ (24 * 3600 * $dlc_plu));

$totalPoids = $totalPoids + $poids_temp ;
$totalPoids = number_format($totalPoids, 3, '.', '')	;

$totalPi = $totalPi + $nb_pi_temp ;						
$totalPi = number_format($totalPi, 0, '.', '')	;

switch ($PrixPoid) { 	
	case 0: 		  // Prix Piece 	
	$montantht = $nb_pi_temp*$puht ;
	$montantht = number_format($montantht, 2, '.', '')	;		
	break; 	
	
	case 1: 		  // Prix Poids 	
	$montantht = $poids_temp*$puht ;
	$montantht = number_format($montantht, 2, '.', '')	;	
	break; 	
	}
							

$poids_temp = number_format($poids_temp, 3, '.', '')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', '')	;

$totalmontant= $totalmontant + $montantht ;
$totalmontant = number_format($totalmontant, 2, '.', '')	;


// Ajout table Stock
$last_num_entre = 0 ;
$sql2 = "SELECT max(num_ligne) FROM gestion_br_detail";  
$req2 = mysqli_query($link, $sql2); 
$row = mysqli_fetch_row($req2);
$last_num_entre = $row[0];
$last_num_entre_stock = $last_num_entre + 1 ;


$sql_ajout_stock = "INSERT INTO gestion_br_detail VALUES ( 
'".$last_num_entre_stock."', 
'".$IdNumClient."', 
'".$num_bl_ligne."',
'".$num_bl_base."',
'".$num_eleveur_base."', 
'".$num_lot_temp."',
'".$num_traca_temp."',
'".$num_plu_temp."',
'".$puht."',  
'".$poids_temp."', 
'".$nb_pi_temp."',
'".$montantht."',
'".$timestamp."')";

$Req_ajout_stock = mysqli_query($link, $sql_ajout_stock); 

echo "$sql_ajout_stock | $Req_ajout_stock<br>";

// Ajout table stock en position entre
$last_num_entre = 0 ;
$sql2 = "SELECT max(IdNumStock) FROM stock";  
$req2 = mysqli_query($link, $sql2); 
$row = mysqli_fetch_row($req2);
$last_num_entre = $row[0];
$last_num_entre = $last_num_entre + 1 ;

$sql_ajout_lot_sortie = "INSERT INTO stock VALUES ( 
'".$last_num_entre."', 
'".$IdNumClient."', 
'".$date."',  
'".$semaine."',
'".$num_eleveur_base."',
'".$num_lot_temp."',
'".$num_traca_temp."',
'".$num_plu_temp."',
'".$date_lot_entre."',
'".$dlc_temp."',
'".$poids_temp."',
'".$nb_pi_temp."',
'0',
'1',
'".$num_bl_complet."',
'".$timestamp."')";

$Req_ajout_lot = mysqli_query($link, $sql_ajout_lot_sortie); 

echo "$sql_ajout_lot_sortie | $Req_ajout_lot<br>";

	}	

$sql3 ="SELECT SUM(poids), SUM(nb_pi), SUM(montant_ht) FROM gestion_br_detail";
	$sql3.=" WHERE num_bl='".$num_bl."'";
	$sql3.=" AND IdNumClient='".$IdNumClient."'";  
	$req3=mysqli_query($link, $sql3);  
	while ($row = mysqli_fetch_assoc($req3)) {

	        $poids_total_bl = $row["SUM(poids)"];
	        $nb_pi_total_bl = $row["SUM(nb_pi)"];
	        $montant_total_bl = $row["SUM(montant_ht)"];
	}
	echo "<br>sql : $sql3<br>";
$poids_total_bl = number_format($poids_total_bl, 2, '.', '')	;	
$montant_total_bl = number_format($montant_total_bl, 2, '.', '')	;
$nb_pi_total_bl = number_format($nb_pi_total_bl, 0, '.', '')	;	
	
// mise a jour haut bl montant et qte
$sql_maj_ht_bl = "UPDATE gestion_br_haut SET 
`poids_retour` = '$poids_total_bl', 
`nb_pi_retour` = '$nb_pi_total_bl', 
`montant_ht` = '$montant_total_bl' 
WHERE `num_br` = '$num_bl'";

$Req_maj_ht_bl = mysqli_query($link, $sql_maj_ht_bl); 
echo "$sql_maj_ht_bl | $Req_maj_ht_bl<br>";

// delette bon temporaire
					 
$sql_delete_stock_temp="DELETE FROM temp_detail_br 
WHERE IdNumClient='".$IdNumClient."' AND num_bl_ligne='".$num_bl_ligne."'" ;

$Req_delete_stock_temp =  mysqli_query($link, $sql_delete_stock_temp); 
echo "$sql_delete_stock_temp | $Req_delete_stock_temp<br>";

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