<?php
	$currDir = dirname(__FILE__);
	include("$currDir/nel.php");
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	include_once("$currDir/header.php");

	/* grant access to all users who have access to the facture table */
	$order_from = get_sql_from('factures');
	if(!$order_from) exit(error_message('Accès non autorisé!', false));

	/* get invoice */
	$order_id = intval($_REQUEST['idfactures']);
	if(!$order_id) exit(error_message('ID facture invalide!', false));
	
	/* retrieve order info */
	$order_fields = get_sql_fields('factures');
	$res = sql("select {$order_fields} from {$order_from} and idfactures={$order_id}", $eo);
	if(!($order = db_fetch_assoc($res))) exit(error_message('Aucune facture disponible', false));
	
	//var_dump($order);
	
	/* retrieve order items */
	$items = array();
	$order_total = 0;
	$item_fields = get_sql_fields('items');
	$item_from = get_sql_from('items');
	$res = sql("select {$item_fields} from {$item_from} and items.numero={$order_id}", $eo);
	while($row = db_fetch_assoc($res)){
		$row['LineTotal'] = str_replace('$', '', $row['prix']) * $row['qte'];
		$items[] = $row;
		$order_total += $row['LineTotal'];
	}
	
	//var_dump($items);
	
	?>
<img id="img1" src="images/chezfatma120.png" alt="ein" style="width: 80px; float: right;">
<!-- <h5 style="float: right;">Elève : <?php echo $order['client']; ?></h5> /-->
<h3>Chez FATMA</h3>
<p>Restaurant - Traiteur
<br>Route Hôtel Monotel
<br>Email : info@chezfatma.net
<br>Tél : +222 00 00 00
</p>
<hr>
<html>
<head>
	<title>Facture</title>
<style type="text/css" media="print">
input.imprimer
{
display:none;
}
</style>
 
</head>
 
<body>

<form method="POST" action="">
 
<input type="button" value="PRINT" class="imprimer" name="bnom" onClick="javascript:window.print()">
</form>

</body>
 
</html>

	<div class="row">
		<div class="col-sm-6">
			<!-- customer info -->
			<h5>CLIENT : <?php echo $order['client']; ?></h5>
		</div>
		<div class="col-sm-6 text-right">
			<!-- invoice info -->
			<h1></h1>
			<h5>Nouakchott, le <?php echo $order['date']; ?></h5>
			<center><u><h1>FACTURE N° <?php echo $order['idfactures']; ?></h1></u></center>
		</div>
	</div>
	
	<hr>
	
	<!-- order items -->
	<table class="table table-striped table-bordered">
		<thead>
			<th class="text-center">N°</th>
			<th>Désignation</th>
			<th class="text-center">Prix Unitaire</th>
			<th class="text-center">Quantité</th>
			<th class="text-center">Prix Total</th>
		</thead>
		
		<tbody>
			<?php foreach($items as $i => $item){ ?>
				<tr>
					<td class="text-center"><?php echo ($i + 1); ?></td>
					<td><?php echo $item['designation']; ?></td>
					<td class="text-center"><?php echo number_format($item['prix'], 0, ',', ' '); ?></td>
					<td class="text-center"><?php echo $item['qte']; ?></td>
					<td class="text-right"><?php echo number_format($item['LineTotal'], 0, ',', ' '); ?></td>
				</tr>
			<?php } ?>
		</tbody>
		
		<tfoot>


			<tr>
				<th colspan="4" class="text-right">TOTAL</th>
				<th class="text-right"> <?php echo number_format($order_total + $order['Freight'], 0, ',', ' '); ?></th>
			</tr>
		</tfoot>
	</table>
	<br><br>
	<span id="in-words"><b>Arrêté la présente facture à la somme de:</b></span>

<?php
include_once('nel.php');
echo enlettres("$order_total");
?>
<span id="in-words"><b> N-Ouguiya</b></span>

	<br><br><br>
	<span id="in-words" style="float: right;"><b>La caisse</b></span>
	<br><br><br><br><br><br><br><br>
	<hr>
	<center>
	<p>
		Adresse : Chez FATMA | Téléphone : +222 00 00 00 00 | Email : info@chezfatma<br>
		Nouakchott - Mauritanie
	</p>
	</center>
	<!-- <span id="in-words" style="color: red;"><b>NB. AUCUNE SOMME PAYEE N'EST EN AUCUN CAS REMBOURSABLE</b></span> /-->
	
	<?php

	include_once("$currDir/footer.php");

?>
