<?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_bc = $_GET["id"];

$query_fact = "select * from pm_bc where id =".$id_bc ;
			// echo "====>".$query_fact;
			$result_fact = @$db->query($query_fact);
			if($result_fact !== false){
				$row_fact = $result_fact->fetch();
				// $bl = explode(",", $row_fact["ref_bl"]);
				//=========== info client =======
				$id_fournisseur = $row_fact["fournisseur"];
				$query_client = "select * from pm_fournisseurs where id = ".$id_fournisseur;
				$result_client = @$db->query($query_client);
				if($result_client !== false){
					$row_client = $result_client->fetch();
					$idfre 		= $row_client["id"];
					$nom 		= $row_client["name"];
					$adress 	= $row_client["adresse"];
					$email 		= $row_client["email"];
					$tva 		= $row_client["tva"];
					$contact 		= $row_client["contact"];
				}	
			}	

?>


<html>
<body>
<div>
	<div><img src="../../images/entete.jpg" width="100%" /></div>
	<div style="width:100%;float:left;text-align:center;margin-top:10px;"><h2>Bon de Commande N° BC00<? echo $id_bc; ?> le <? echo date('d/m/Y',$row_fact["add_date"]); ?></h2></br></br></br></div>
	
	<div style="width:35%;float:left;font-size:14px">
		Date de livraison souhaitée : <br><b><? echo date('d/m/Y',$row_fact["date_livraison"]); ?></b><br><br>
		Référance : <br><b><? echo $row_fact["ref"]; ?></b><br>
	</div>
	<div style="width:50%;float:right;border:1px #000 solid;padding:1%;">
		<b>Fournisseur :</b><br>
		FRS_00<? echo $idfre ; ?> - <b><? echo $nom ; ?></b><br>
		<? echo $adress ; ?><br>
		<? echo $tva ; ?><br>
		<? echo $contact ; ?><br>
		<? echo $email ; ?><br>
		
	</div>
</div>

<div class="tableContent">
	<table border="1" width="100%" cellpadding="5">
		<tr>
			<th>Code article</th>
			<th>Designation article</th>
			<th>Colisage</th>
			<th>Quantité</th>
			<th>Unite</th>
			<th>P.U HT</th>
			<th>Remise</th>
			<th>Montant Net HT</th>
		</tr>
		 <?
			$qte_all 	 = 0;
			$prix_ht_all = 0;
			
			$pu_all  = 0;
			$total_ht_all = 0;
			$remise_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_facture where id =".$id_bc ;
			// 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_bc_product` WHERE id_bc = ".$id_bc ;
					// 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_produitsachat` WHERE `id` = '".$row_2["name"]."'";	
						// echo $queryP ;
						$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;	
						$prix_ht = 0;	
						$remise  = 0;	
						$qte = $row_2["qte"];	
						$qte_all += $qte;
						$prix_ht = $row_2["prix"];												
						$remise = $row_p["remise"];	
						$devise = $row_p["devise"];	
						$ht_net = ($qte * $prix_ht)-(($remise/100) * $qte * $prix_ht);	
						$total_ht_all += $ht_net ;
					
						
				?>
				<tr>
					<td align="left"><? echo $row_p["code_produit"]; ?></td>
					<td align="left"><? echo $row_p["desingnation"]; ?></td>
					<td align="center"><? echo $row_p["coulissage"]; ?></td>
					<td align="center"><? echo $qte; ?></td>
					<td align="center"><? echo $row_u["name"]; ?></td>
					<td align="center"><? echo number_format($prix_ht,3) ; ?> <? echo $devise; ?></td>
					<td align="center"><? echo number_format($remise,2) ; ?>%</td>
					<td align="center"><? echo number_format($ht_net,3,".",""); ?> <? echo $devise; ?></td>
				</tr>

				<?	
	
						}
					}
				// }
			// }	
				//================ TOTAL =====================
			?>	
			<tr>
					<td align="center" colspan="2"><b>TOTAL</b></td>		
					<td align="center"></td>
					<td align="center"><? echo $qte_all; ?></td>
					<td align="center"></td>
					<td align="center"></td>
					<td align="center"></td>
					<td align="center"><b><? echo number_format($total_ht_all,3,".",""); ?> <? echo $devise; ?></b></td>
				</tr>	
							
	</table>

</div>

<div class="tableContent2">
<p><b>PS : Attention, veuillez respecter les dates de livraisons qui sont indiquées en haut, merci.</b></p>
</div>

<div class="tableContent2">
	<div style="width:40%;float:left;text-align:center;">
	</div>
	<div style="width:40%;float:right;text-align:center;">
		SERVICE ACHAT<br><br>
		<img src="../../images/signature.jpg" width="200" />
	</div>
</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: 12px;
    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: 12px;
}

@page {
    margin: 0 20px;
}
</style>


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