<?php

// ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");

if(!isset($_SESSION['user'])){
    header("Location: ../login.php");
    exit();
}
require_once("../../includes/fn_module.php"); ?>

<?
$id_facture = $_GET["id"];

$query_fact = "select * from pm_bl where id =".$id_facture ;
			// echo "====>".$query_fact;
			$result_fact = @$db->query($query_fact);
			if($result_fact !== false){
				$row_fact = $result_fact->fetch();
				$bl = explode(",", $row_fact["ref_bl"]);
				$idd = $row_fact["id"];
				//=========== info client =======
				$id_client = $row_fact["id_client"];
				$query_client = "select * from pm_client where id = ".$id_client;
				$result_client = @$db->query($query_client);
				if($result_client !== false){
					$row_client = $result_client->fetch();
					$nom 		= $row_client["name"];
					$adress 	= $row_client["adresse"];
					$tel 		= $row_client["tel"];
					$tva 		= $row_client["tva"];
					$cat_client 		= $row_client["cat_client"];
					
				}	
			}


	$query_update = "UPDATE `pm_bl` SET `printed`='1' WHERE id =".$id_facture ;	
	$result_update = @$db->query($query_update);

?>


<html>
<body>
	<div><img src="../../images/entete.jpg" width="100%" /></div>
<div>
	<div style="width:100%;float:left;text-align:center;"><h2>Bon de livraison<br>N° BL<? echo $idd; ?><br><? echo date('d/m/Y',$row_fact["date_livraison"]); ?></h2></br></div>
	
	<div style="width:40%;float:left;border:1px #000 solid;padding:1%;">
		<b>ORIENTAL SNACKS</b><br>
		Zone Industrielle Mghira 3<br>
		2082 - Fouchana 
		<!--Réf bon de livraison : -->
		<? 
			// foreach ($bl as &$value) {
				// echo "<b>BL000".$value." - </b> ";
			// }
		?><br>
	</div>
	<div style="width:40%;float:right;border:1px #000 solid;padding:1%;">
		<b>Client :</b><br>
		<? echo $nom ; ?><br>
		<? echo $adress ; ?><br>
		<? echo $tel ; ?><br>
		<? echo $tva ; ?><br>
	</div>
</div>

<div class="tableContent">
	<table border="1" width="100%" cellpadding="2">
		<tr>
			<th>Code article</th>
			<th>Designation article</th>
			<th>Colisage</th>
			<th>Qté Carton</th>
			<th>Colisage palette</th>
			<th>Qté palette</th>

		</tr>
		 <?
			$qte_all 	 = 0;
			$qte_all_palette = 0;
			
			$pu_all  = 0;
			$total_ht_all = 0;
			$remise_all = 0;
			$remise_ht_all = 0;
			$total_ht_net_all = 0;
			$total_ttc_all = 0;
			
			$array_tva = array();
			$array_tva_qte = array();
			$i = 0;
			
			$query_fact = "select * from pm_bl where id =".$id_facture ;
			// echo "====>".$query_fact;
			$result_fact = @$db->query($query_fact);
			if($result_fact !== false){
				$row_fact = $result_fact->fetch();
				$ref_bl = $row_fact["ref_bl"];
				$array_bl = explode(",", $ref_bl);
				$count_bl = count($array_bl);
	

				//========= Recuperé les produits =====
				// for ($x = 0; $x < $count_bl; $x++)
				// {	
					// echo "====>".$count_bl[$x];
					// $query_prod = "SELECT * FROM `pm_produits`as a , pm_bl_product as b WHERE a.id = b.id_produits AND b.id_bl = ".$array_bl[$x] ;
					// $query_prod = "SELECT * FROM `pm_facture_gen` WHERE id_facture = ".$id_facture ;
					$query_prod = "SELECT * FROM `pm_produits`as a , pm_bl_product as b WHERE a.id = b.id_produits AND b.id_bl = ".$id_facture  ;
					// echo "====>".$query_prod."<br>";
					$result_prod = @$db->query($query_prod);
					if($result_prod !== false){
						foreach($result_prod as $row_2){
						//======== get coulissage ======	
						$queryP = "SELECT * FROM `pm_produits` WHERE `code_produit` = '".$row_2["code_produit"]."'";	
						// echo $queryP."<br>" ;
						$result_p = @$db->query($queryP);
						if($result_p !== false){
							$row_p = $result_p->fetch();
							$array_tva[$i] = $row_p["tva"];
						}
						//======== get uniter ======	
						$queryU = "SELECT * FROM `pm_unite` WHERE `id` = ".$row_p["unite"];	
						$result_u = @$db->query($queryU);
						if($result_u !== false){
							$row_u = $result_u->fetch();
						}						
						$qte     = 0;	
						$qte_palette     = 0;	
						$prix_ht = 0;	
						$remise  = 0;	
						$qte = $row_2["qte"] / $row_p["coulissage"];	
						$qte_palette = $qte / $row_p["coulissage_palette"];	
						
				?>
				<tr>
					<td align="center"><? echo $row_2["code_produit"]; ?></td>
					<td align="center"><? echo $row_2["name"]; ?></td>
					<td align="center"><? echo $row_p["coulissage"]; ?></td>
					<td align="center"><? echo $qte; ?></td>
					<td align="center"><? echo $row_p["coulissage_palette"]; ?></td>
					<td align="center"><? echo round($qte_palette); ?></td>
					
				</tr>

				<?	
					$qte_all +=  $qte;
					$qte_all_palette +=  round($qte_palette);
					
					
					$i++;
						}
					}
				// }
			}	
				//================ TOTAL =====================
			?>
				<tr>
					<td align="center" colspan="3"><b>TOTAL</b></td>

					<td align="center"><b><? echo $qte_all; ?></b></td>
					<td align="center" ></td>
					<td align="center"><b><? echo $qte_all_palette; ?></b></td>
				</tr>
			
	</table>

</div>
<div class="tableContent2">
<?
$matricule = "";
						//======== get coulissage ======	
						$queryP = "SELECT * FROM `pm_vehicule` WHERE `id` = '".$row_fact["vehicles"]."'";	
						// echo $queryP."<br>" ;
						$result_p = @$db->query($queryP);
						if($result_p !== false){
							$row_p = $result_p->fetch();
							$matricule = $row_p["matricule"];
						}
		
		echo "Véhicule : ".$matricule."<br>";
		
$chauffeur = "";
						//======== get coulissage ======	
						$queryP = "SELECT * FROM `pm_chauffeur` WHERE `id` = '".$row_fact["chauffeur"]."'";	
						// echo $queryP."<br>" ;
						$result_p = @$db->query($queryP);
						if($result_p !== false){
							$row_p = $result_p->fetch();
							$chauffeur = $row_p["firstname"];
						}
		
		echo "Chauffeur : ".$chauffeur;		
?>


<p>Remarque : <? echo $row_fact["remarque"]; ?></b></p>
</div>

<div class="tableContent2">
	<div style="width:40%;float:left;text-align:center;">
		Cachet et signature<br>
		de client
	</div>
	<div style="width:40%;float:right;text-align:center;">
		Cachet et signature<br><br>
	</div>
</div>

<div style="position: absolute;bottom: 100px;"><? echo date("d/m/Y H:i:s"); ?></div>
	<div class="divFooter"><img src="../../images/footer.jpg" width="100%" /></div>
</body>
</html>

<style>
@media print {
    div.divFooter {
        position: fixed;
        background: url('../../images/footer.jpg');
       /* height: 100px;  put the image height here */
        width: 100%; /* put the image width here */
        bottom: 0;
    }
}


body{
    font-size: 11px;
    font-family: arial;
}
.tableContent{
	margin-top:20px;
	float:left;
	width:100%
}
.tableContent2{
	margin-top:20px;
	float:left;
	width:100%
}
table{
	border-collapse:collapse;
	font-size: 11px;
}
</style>

<script>
window.print();
</script>