<?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, $s, $ordre, $num_bl_complet,
$limiteprecedente, $limite, $total, $page, $Nom_Client, $semaine,$txt, $puht ;

// http://192.168.1.200/horius/detail_bl_liste.php?ordre=1&num_bl=01-59&numligne=1226

include 'menu/header_lt.php';

$date = date("Y-m-d");
$Valid_ok=0;
$refere = $_SERVER['HTTP_REFERER'] ;

if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		if (isset($_POST['ordre'])) { $num_ordre=$_POST['ordre']; }  else {$num_ordre=''; }
		if (isset($_POST['num_bl'])) { $num_bl_base=$_POST['num_bl']; }  else {$num_bl_base=''; }
		if (isset($_POST['numligne'])) { $num_li_bl=$_POST['numligne']; }  else {$num_li_bl=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		if (isset($_GET['ordre'])) { $num_ordre=$_GET['ordre']; }  else {$num_ordre=''; }
		if (isset($_GET['num_bl'])) { $num_bl_base=$_GET['num_bl']; }  else {$num_bl_base=''; }
		if (isset($_GET['numligne'])) { $num_li_bl=$_GET['numligne']; }  else {$num_li_bl=''; }
		$valForm="GET" ;
}
 $sql1 = "SELECT * FROM gestion_bl_haut WHERE num_bl='$num_bl_base' AND num_li_bl='$num_li_bl'"; 
		$req1 = mysqli_query($link, $sql1);
		while ($row = mysqli_fetch_assoc($req1)) {
				$num_bl_ligne = $row["num_li_bl"];
        $date_bl = $row["date_bl"];
        $date_livraison_bl = $row["date_livraison"];
        $num_client_entre = $row["num_client_bl"];
        $CodeTrsp = $row["num_trsp"];
        $CodePaie = $row["num_paie"]; 
    }  
// formation du numéro de BL

$date_bl_e=date("d/m/Y", strtotime($date_bl));
$date_livraison_bl_e = date("d/m/Y", strtotime($date_livraison_bl));
$Anne_bl=date("y", strtotime($date_bl));
$num_bl_complet ="$Anne_bl-$num_bl-".$num_bl_base ;
$date_livraison_bl_e = date("d/m/Y", strtotime($date_livraison_bl)) ;
$num_bl_complet ="$Anne_bl-$num_bl-".$num_bl_base ;

		$sql = "SELECT * FROM clients_base WHERE Num_Client='$num_client_entre'"; 
		$sql.=" AND IdNumClient='".$IdNumClient."'";
		$req = mysqli_query($link, $sql);
		while ($row = mysqli_fetch_assoc($req)) {
		        $nom_Client = $row["Nom_Client"];
		        $Code_tarif_Client = $row["Code_tarif"];
		 }  
    
    $sql1 = "SELECT * FROM gestion_bl_haut WHERE num_li_bl='$num_bl_ligne'"; 
		$req1 = mysqli_query($link, $sql1);
		while ($row = mysqli_fetch_assoc($req1)) {
        $date_livraison_bl = $row["date_livraison"];
        $CodeTrsp = $row["num_trsp"];
        $CodePaie = $row["num_paie"]; 
    }  
    $sql2 = "SELECT `Code_Trsp` FROM categorie_trsp WHERE `Num_Ligne_Cate_Trsp`=$CodeTrsp";  
		$req2 = mysqli_query($link, $sql2);  
		while ($row = mysqli_fetch_assoc($req2)) {$Code_Trsp_texte = $row["Code_Trsp"];} 
		
		$sql3 = "SELECT  `Code_Paiement` FROM categorie_paiement WHERE `Num_Ligne_Cate_Paiement`=$CodePaie";  
		$req3 = mysqli_query($link, $sql3);  
		while ($row = mysqli_fetch_assoc($req3)) {$Code_Paiement_texte = $row["Code_Paiement"];} 
		
$date_livraison_bl_e = date("d/m/Y", strtotime($date_livraison_bl));
/*
echo "$sql1 | $date_livraison_bl | $CodeTrsp | $CodePaie <br>";
echo "$sql2 <br> $sql3 <br>";
echo "$CodeTrsp | $Code_Trsp_texte //  | $Code_Paiement_texte<br>";
*/
// titre page
echo "<div class='titre-text'><center>Bon de livraison N&deg; $num_bl_complet </div>" ;
echo "<HR size='1' width='500'><center>";
echo "<table border='0' cellpadding='1' cellspacing='1' width='750'><tr>";
echo "<tr>";
echo "<td class='t-text12' width='100'><center><label>Date BL : </label>$date_bl_e</label></center></td>";
echo "<td class='t-text12' width='150'><center><label>Client : </label>$nom_Client</label></center></td>";
echo "<td class='t-text12' width='150'><center><label>Date Livraison : </label>$date_livraison_bl_e</label></center></td>";
echo "<td class='t-text12' width='30'><center><a href='maj_haut_bl.php?ordre=3&num_bl=$num_bl_ligne'><center><img src='images/redo.png'></center></a></td>";
echo "<td class='t-text12' width='30'><center><a href='edition_bl_nc_imp.php?num_entre=$num_bl_ligne' TARGET=_BLANK><img src='images/print.png' width='20' height='20'></a></center></td>";
echo "<td class='t-text12' width='30'><center><a href='edition_bl_imp.php?num_entre=$num_bl_ligne' TARGET=_BLANK><img src='images/money_bag.png' width='20' height='20'></a></center></td>";
echo "</tr>";
echo "<td class='t-text12' colspan='2'><center>Livraison : $CodeTrsp | $Code_Trsp_texte</center></a></center></td>";
echo "<td class='t-text12' colspan='5'><center>Paiement : $CodePaie | $Code_Paiement_texte</center></a></center></td>";
echo "</table><br>";

// detail bon de livraison

echo "<center>" ;
echo "<table width='750' id='customers'>";
echo "<tr>";
echo "<th width='50'>Num ligne</th>" ;
echo "<th width='50'>Num PLu</th>" ;
echo "<th width='200'>Nom PLu</th>" ;
echo "<th width='180'>Num Lot</th>" ;
echo "<th width='100'>Nb Pi</th>" ;
echo "<th width='100'>Poids</th>" ;
echo "<th width='100'>Pu Ht</th>" ;
echo "<th width='100'>Montant Ht</th>" ;
echo "</tr>";

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

	$sql="select * FROM gestion_bl_detail" ;
	$sql.=" WHERE num_bl_ligne='".$num_bl_ligne."'";
	$sql.=" AND IdNumClient='".$IdNumClient."'";  
	$sql.=" ORDER BY `num_plu` ASC";

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

	        $num_plu_temp = $row["num_plu"];
	        $poids_temp = $row["poids"];
	        $nb_pi_temp = $row["nb_pi"];
	        $num_lot_plu = $row["num_lot"];
	        $num_traca_plu = $row["num_traca"];
	        $puht = $row["puht"];
	        $n_ligne = $row["num_ligne"];
	        $Nun_lot_bl = $row["num_lot"];
	        $Nun_traca_bl = $row["num_traca"];
	
	$li=$li+1 ;        
		
					$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"];
						} 
						
					$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"];
						} 	

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

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

switch ($PrixPoid) { 	
	case 0: 		  // Prix Piece 	
	$montantht = $totalPi*$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, 2, '.', ' ')	;
$nb_pi_temp = number_format($nb_pi_temp, 0, '.', ' ')	;

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


echo "<tr>";
echo "<td width='50' style='text-align:center'>$li</td>" ;
echo "<td width='50' style='text-align:center'>$num_plu_temp </td>" ;
echo "<td width='200' style='text-align:left'>$nom_plu_temp</td>" ;
echo "<td width='180' style='text-align:left'>$Nun_lot_bl - $Nun_traca_bl </td>" ;
echo "<td width='100' style='text-align:right'>$nb_pi_temp Pi</td>" ;
echo "<td width='100' style='text-align:right'>$poids_temp Kg</td>" ;
echo "<td width='100' style='text-align:right'>$puht &euro;</td>" ;
echo "<td width='100' style='text-align:right'>$montantht &euro;</td>" ;
echo "</tr>" ;
	}					 

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

	        $poids_total_bl = $row["SUM(poids)"];
	        $nb_pi_total_bl = $row["SUM(nb_pi)"];
	        $montant_total_bl = $row["SUM(montant_ht)"];
	}
$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, '.', '')	;	

echo "<tr style='background-color:lightgrey;'>" ;
echo "<td colspan=4 style='text-align:right'>Total :</td>" ;
echo "<td width='100' style='text-align:right'>$nb_pi_total_bl Pi</td>" ;
echo "<td width='100' style='text-align:right'>$poids_total_bl Kg</td>" ;
echo "<td width='100' style='text-align:left'>&nbsp;</td>" ;
echo "<td width='100' style='text-align:right'>$montant_total_bl &euro;</td>" ;
       
echo "</tr>" ;
echo "</table>";  

echo "</tr> <input type='submit' value='Fermer' onclick='window.close()' ></table><br>";

include 'menu/footer_lt.php';

?>
