<?php
   
include("include/data.php");
require('invoice.php');
Init_boutique();
global $base, $MySQL_Host, $MySQL_User, $MySQL_Passw, $myart, $A_NumArt,$i, $result, $NbLigne, 
$mygroupeart, $mysousgroupeart, $semaine, $limitesuivante, $limiteprecedente, $limite, $total, $rendement, 
$page, $Nom_Client, $NumAbat, $mrs, $total_entre, $total_sortie, $total_entre_cass, $total_sortie_cass, 
$total_entre_cass, $total_sortie_cass, $total_entre_decoupe, $total_sortie_decoupe, $total_entre_stockPf, $total_sortie_stockPf,
$total_entre_transf, $total_sortie_transf, $total_entre_congel, $total_sortie_congel ;

$date = date("Y/m/d");
$timestamp = date('Y-m-d G:i:s');
// entete de la page


/* http://192.168.1.200/horius/import_bl.php
ordre=1
date_bl=2018-07-25
date_bl_livr=2018-07-26
ok=Valider

*/ 
unset($arr); 

if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['ok'])) { $ok=$_POST['ok']; } else {$ok=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['ok'])) { $ok=$_GET['ok']; } else {$ok=''; }
		
		$arr = array(); 

		$i=0;
		foreach($_GET['NumBl_li'] as $valeur)
		{ $i=$i+1 ;
		$arr[$i] = $valeur ;
		}
			
		$valForm="GET" ;
}
$b=0;
/*
// TITRE Page //
echo "<HR size='2' width='500'><div class='titre-text'><center>Cr&#233;ation Bon de livraison apr&#232;s imports</div></center><HR size='2' width='500'>" ;
echo "<div class='t-text12'><center>Date BL : $date_bl - Date Livraison : $date_bl_livr <HR size='2' width='500'></div>" ;
*/

$Nb_ValTbl = sizeof ($arr) ;

for ($f=1; $f <= $Nb_ValTbl ; $f++) {

 $num_bl_ligne = substr_replace($arr[$f],'',-1,1);
 //$num_bl_complet = substr_replace($arr1[$f],'',-1,1);
 //$num_bl = substr_replace($arr2[$f],'',-1,1);

  $sqla = "SELECT * FROM gestion_bl_haut WHERE num_li_bl='$num_bl_ligne'"; 
		$reqa = mysqli_query($link, $sqla);
		while ($row = mysqli_fetch_assoc($reqa)) {
        $date_livraison_bl = $row["date_livraison"];
        $CodeTrsp = $row["num_trsp"];
        $CodePaie = $row["num_paie"]; 
        $etat_bl = $row["etat"];
        $num_client_entre = $row["num_client_bl"];
        $num_bl = $row["num_bl"];
    }  

$sqlb = "SELECT * FROM clients_base WHERE Num_Client='$num_client_entre'"; 
$reqb = mysqli_query($link, $sqlb);
while ($row = mysqli_fetch_assoc($reqb)) {
        $Nom_Client = $row["Nom_Client"];
        $Code_tarif_Client = $row["Code_tarif"];
    }  

$Anne_bl=date("y", strtotime($date_livraison_bl));
$num_bl_complet ="$Anne_bl-$IdNumClient-".$num_bl ;
/*
echo $f." $Nom_Client -  values : ".$num_bl_ligne.' - BL Complet : '.$num_bl_complet.' - BL base : '.$num_bl.' - date_livraison_bl : '.$date_livraison_bl.' - CodeTrsp : '.$CodeTrsp.' - CodePaie : '.$CodePaie.' - etat_bl : '.$etat_bl.'<br>' ; 
echo "Num ligne : $num_bl_ligne |Num Bl $num_bl |Ctarif : $Code_tarif_Client |BL : $num_bl_complet <br>$ok<br>";
*/
if ($ok=='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_bl" ;
	$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_bl`.`num_plu` ASC";

	$req=mysqli_query($link, $sql);  
// Test
//Echo "<br> $sql | $resq<br>";
//
	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"];
						} 
// test //
//echo "<br>$sql1 | $sql2 | $sql3<br>";
//
$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_bl_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_bl_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 "Rq ajout stock : $sql_ajout_stock | $Req_ajout_stock<br>";

// Ajout table stock en position sortie
$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."',
'1',
'1',
'".$num_bl_complet."',
'".$timestamp."')";

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

//echo "Rq ajout lot sortie : $sql_ajout_lot_sortie | $Req_ajout_lot<br>";

	}	

	$sql3 ="SELECT SUM(poids), SUM(nb_pi), SUM(montant_ht) FROM gestion_bl_detail";
	$sql3.=" WHERE num_bl='".$num_bl."'";
	$sql3.=" AND num_bl_ligne='".$num_bl_ligne."'";
	$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, 3, '.', '')	;	
$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_bl_haut SET 
`poids_livre` = '$poids_total_bl', 
`nb_pi_livre` = '$nb_pi_total_bl', 
`montant_ht` = '$montant_total_bl',
`etat`= 1  
WHERE `num_li_bl` = $num_bl_ligne 
AND `IdNumClient` ='$IdNumClient' ";

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

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

$Req_delete_stock_temp =  mysqli_query($link, $sql_delete_stock_temp); 
//echo "Rq del temps : $sql_delete_stock_temp | $Req_delete_stock_temp<br>";

// maj total apres ajour

$sql3 ="SELECT SUM(poids), SUM(nb_pi), SUM(montant_ht) FROM gestion_bl_detail";
	$sql3.=" WHERE num_bl='".$num_bl."'";
	$sql3.=" AND num_bl_ligne='".$num_bl_ligne."'";
	$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_bl_haut SET 
`poids_livre` = '$poids_total_bl', 
`nb_pi_livre` = '$nb_pi_total_bl', 
`montant_ht` = '$montant_total_bl'

WHERE `num_li_bl` = $num_bl_ligne 
AND `IdNumClient` ='$IdNumClient' ";  

$Req_maj_ht_bl = mysqli_query($link, $sql_maj_ht_bl); 

//echo "$sql_maj_ht_bl | $Req_maj_ht_bl<br><br><br>";

} 

if ($ok=="ExportPdf") {
//echo $f." $Nom_Client -  values : ".$num_bl_ligne.' - BL Complet : '.$num_bl_complet.' - BL base : '.$num_bl.' - date_livraison_bl : '.$date_livraison_bl.' - CodeTrsp : '.$CodeTrsp.' - CodePaie : '.$CodePaie.' - etat_bl : '.$etat_bl.'<br>' ; 
//echo "Num ligne : $num_bl_ligne |Num Bl $num_bl |Ctarif : $Code_tarif_Client |BL : $num_bl_complet <br>$ok<br>";
//echo "<br>src='edition_bl_pdf.php?ordre=1&i=2&num_entre='".$num_bl_ligne."'<br>";

echo "<iframe src='edition_bl_pdf.php?ordre=1&i=2&num_entre=".$num_bl_ligne." name='zone' height='0%' width='0%'>></iframe><br>$zoby";
//
}
$zoby=1 ;
}
$zoby=2 ;
echo "<br>$zoby";

if ($zoby==2) {
$refere = $_SERVER['HTTP_REFERER'] ;
echo "<br>$refere<br>";
sleep(60);
Header("Location:  $refere") ;	
}

?>
