<?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_facture 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["idd"];
				//=========== 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"];
					
				}	
			}	

?>


<html>
<body>
<div>
	<div style="width:100%;float:left;text-align:center;margin-top:100px;"><h2>BL / Facture N° <? echo $idd; ?>&nbsp;&nbsp;&nbsp;<? echo date('d/m/Y',$row_fact["date_facture"]); ?></h2></br></div>
	
	<div style="width:40%;float:left;    font-size: 12px;">
		Remarque : <? echo $row_fact["remarque"]; ?><br>
		<!--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>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;
			$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_facture 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 = ".$array_bl[$x] ;
					// 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;	
						$prix_ht = 0;	
						$remise  = 0;	
						$qte = $row_2["qte"];	
						$prix_ht = $row_2["prix_ht"];	
						$remise = $row_2["remise"];
						//=====================
						$prix_ht_bl = $row_2["prix_ht1"];	
						$remise_bl = $row_2["remise1"];	
						if($prix_ht_bl){
							$prix_ht_new = $prix_ht_bl;
						}else{
							$prix_ht_new = $prix_ht;
						}
						if($remise_bl){
							$remise_new = $remise_bl;
						}else{
							$remise_new = $remise;
						}	
						$total_ht = $qte  * $prix_ht_new ;
						$remise_ht = ($remise_new * $total_ht)/100;
						$total_ht_net = $total_ht- $remise_ht;
						
						$array_tva_qte[$row_p["tva"]] += $total_ht_net;
				?>
				<tr>
					<td align="left"><? echo $row_2["code_produit"]; ?></td>
					<td align="left"><? echo $row_2["name"]; ?></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_new,3) ; ?></td>
					<td align="center"><? echo number_format($remise_new,2); ?>%</td>
					<td align="right"><? echo number_format($total_ht_net,3); ?></td>
				</tr>

				<?	
					$qte_all 	 = $qte_all + $qte;
					$total_ht = intval($qte) * $prix_ht_new ;
					// TOTAUX ==============
				  $pu_all += floatval($prix_ht_new);
				  $total_ht_all += $total_ht;
				  $remise_all += floatval($remise_new);
				  $remise_ht_all += $remise_ht;
				  $total_ht_net_all += $total_ht_net;
				  if($cat_client <> 2){
					$total_ttc = $total_ht_net + (($total_ht_net*$row_p["tva"])/100);
				  }else{
						$total_ttc = $total_ht_net;
				  }
				  // echo "momo >>>".$total_ttc."<br>";
				  $total_ttc_all += $total_ttc;
					$i++;
						}
					}
				}
			}	
				//================ TOTAL =====================
			?>				
	</table>

</div>
<div class="tableContent2">
	<div style="width:40%;float:left;">
		<table border="1" width="100%" cellpadding="2">
			<tr>
				<td align="left"><b>Taux TVA</b></td>
				<td align='right'><b>Montant TVA</b></td>
			</tr>
			
<? 		
	
		if($cat_client <> 2){
			$tvaa_all = 0;
			$arr = array_unique($array_tva); 
				foreach ($arr as &$value) {
					$tvaaa = ($array_tva_qte[$value] * $value)/100 ;
					$tvaa_all += $tvaaa;
					echo "<tr>
							<td align='left'>TVA ".$value." %<br>
							<td align='right'>".number_format($tvaaa,3)."</td>
						</tr>";
				}
		}else{
					echo "<tr>
							<td align='left'>TVA 0 %<br>
							<td align='right'>0.000</td>
						</tr>";		
		
		}
								
?>			
			<tr>
				<td align='left'><b>Total TVA</b></td>
				<td align='right'><b><? echo number_format($tvaa_all,3); ?></b></td>
			</tr>
		</table>
	</div>
	<div style="width:50%;float:right;">
		<table border="1" width="100%" cellpadding="2">
			<tr>
				<td align="left"><b>Total HT brute</b></td>
				<td align="right"><? echo number_format($total_ht_all,3); ?></td>
			</tr>
			<tr>
				<td align="left"><b>Montant remise</b></td>
				<td align="right"><? echo number_format($remise_ht_all,3); ?></td>
			</tr>	
			<tr>
				<td align="left"><b>Total HT net</b></td>
				<td align="right"><? echo number_format($total_ht_net_all,3); ?></td>
			</tr>	
			<tr>
				<td align="left"><b>Total TVA</b></td>
				<td align="right"><? echo number_format($tvaa_all,3); ?></td>
			</tr>	
			<? if($cat_client <> 2){ ?>
			<tr>
				<td align="left"><b>Timbre</b></td>
				<td align="right">1.000</td>
			</tr>
			<? } ?>
			<tr>
				<td align="left"><b>Total TTC</b></td>
				<td align="right"><b><? 
				if($cat_client <> 2){
					$total = number_format($total_ttc_all + 1,3);
					$total_a = $total_ttc_all + 1;
				}else{
					$total = number_format($total_ttc_all,3);
					$total_a = $total_ttc_all;				
				}
				$total_view = number_format($total_a ,3,".","");
				echo $total; ?></b></td>
			</tr>					
		</table>
	</div>
</div>
<div class="tableContent2">
<p>Arrêtée la présente facture à la somme de : <b><? echo int2str($total_view)."."; ?></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>
</body>
</html>

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

<?
function int2str($a)
{
$convert = explode('.',$a);
if (isset($convert[1]) && $convert[1]!=''){
return int2str($convert[0]).' Dinars '.' et '.int2str($convert[1]).' millimes' ;
}
if ($a<0) return 'moins '.int2str(-$a);
if ($a<17){
switch ($a){
case 0: return 'zero';
case 1: return 'un';
case 2: return 'deux';
case 3: return 'trois';
case 4: return 'quatre';
case 5: return 'cinq';
case 6: return 'six';
case 7: return 'sept';
case 8: return 'huit';
case 9: return 'neuf';
case 10: return 'dix';
case 11: return 'onze';
case 12: return 'douze';
case 13: return 'treize';
case 14: return 'quatorze';
case 15: return 'quinze';
case 16: return 'seize';
}
} else if ($a<20){
return 'dix-'.int2str($a-10);
} else if ($a<100){
if ($a%10==0){
switch ($a){
case 20: return 'vingt';
case 30: return 'trente';
case 40: return 'quarante';
case 50: return 'cinquante';
case 60: return 'soixante';
case 70: return 'soixante-dix';
case 80: return 'quatre-vingt';
case 90: return 'quatre-vingt-dix';
}
} elseif (substr($a, -1)==1){
if( ((int)($a/10)*10)<70 ){
return int2str((int)($a/10)*10).'-et-un';
} elseif ($a==71) {
return 'soixante-et-onze';
} elseif ($a==81) {
return 'quatre-vingt-un';
} elseif ($a==91) {
return 'quatre-vingt-onze';
}
} elseif ($a<70){
return int2str($a-$a%10).'-'.int2str($a%10);
} elseif ($a<80){
return int2str(60).'-'.int2str($a%20);
} else{
return int2str(80).'-'.int2str($a%20);
}
} else if ($a==100){
return 'cent';
} else if ($a<200){
return int2str(100).' '.int2str($a%100);
} else if ($a<1000){
return int2str((int)($a/100)).' '.int2str(100).' '.int2str($a%100);
} else if ($a==1000){
return 'mille';
} else if ($a<2000){
return int2str(1000).' '.int2str($a%1000).' ';
} else if ($a<1000000){
return int2str((int)($a/1000)).' '.int2str(1000).' '.int2str($a%1000);
}
else if ($a==1000000){
return 'millions';
}
else if ($a<2000000){
return int2str(1000000).' '.int2str($a%1000000).' ';
}
else if ($a<1000000000){
return int2str((int)($a/1000000)).' '.int2str(1000000).' '.int2str($a%1000000);
}
}
?>

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