<?php

session_start();

include_once '../../conexao/index.php';
date_default_timezone_set('Africa/Maputo');

$data_hora = date("Y-m-d H:i:s");

$userID = $_SESSION['idUsuario'];

$userNOME = $_SESSION['nomeUsuario'];

$userCATE = $_SESSION['categoriaUsuario'];


$sql = "SELECT MAX(idperiodo) as id FROM periodo WHERE usuario = '$userID'";
$rs = mysqli_query($db, $sql);
$dados = mysqli_fetch_array($rs);
$id = $dados['id'];
?>
<html>
<head>
	<title>Fecho de Caixa</title>
	<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
	<style type="text/css">
		*{
			font-size: 8pt;
		}
	</style>
</head>
<body onload="window.print()">
<div class="fatura">
<div class="header" style="text-align: center;">
	<p>Tete - Cidade de Moatize</p>
	<p>Estrada Nacional NR 7</p>
	<p>Cell:855213692  -  NUIT: 401058303</p>
</div>
<div class="fact" style="text-align: center;">
	<p><span>Fecho</span><span class="pd" style="margin-left: 100px;"><?php echo $id; ?></span></p>
</div>
<div class="body" style="text-align: center;">
	<p class="p"><span>Documento Original</span><span style="margin-left: 10px;"><?php echo $data_hora; ?></span></p>
</div>
<table class="prodFactura" style="width: 95%;margin: 3px auto;text-align:center;">
	<thead class="titulo">
		<th>Data</th>
		<th>Abertura (Mt)</th>
		<th>Fecho (Mt)</th>
	</thead>
	<tbody>
		<?php 
			$q = "SELECT * FROM periodo WHERE idperiodo = '$id'";
			$c = mysqli_query($db, $q);
			while ($a = mysqli_fetch_array($c)) {
			$ab = $a['aberturaperiodo'];
			$fc = $a['fechoperiodo'];
		?>
			<tr>
				<td><?php echo $a['datafechoperiodo']; ?></td>
				<td><?php echo "".number_format($ab,2,".",","); ?></td>
				<td><?php echo "".number_format($fc,2,".",","); ?></td>
			</tr>
		<?php

			}
		?>
	</tbody>
</table>
<table class="prodFactura" style="width: 95%;margin: 3px auto;text-align:center;">
	<thead class="titulo">
		<th>Modo de Pagamento</th>
		<th>Valor (Mt)</th>
	</thead>
	<tbody>
		<?php 
			$q = "SELECT DISTINCT modo, SUM(pagamento+iva) as val FROM `pos_mesas` WHERE periodo = '$id' GROUP BY modo";
			$c = mysqli_query($db, $q);
			while ($a = mysqli_fetch_array($c)) {
			$modo= $a['modo'];
			$val = $a['val'];
		?>
			<tr>
				<td><?php echo $modo; ?></td>
				<td><?php echo "".number_format($val,2,".",","); ?></td>
			</tr>
		<?php

			}
		?>
	</tbody>
</table>
<table class="prodFactura" style="width: 95%;margin: 3px auto;text-align:center;">
	<thead class="titulo">
		<th>Artigo</th>
		<th>Qtd</th>
		<th>Valor</th>
	</thead>
	<tbody>
		<?php 
			$q = "SELECT MIN(data) as DataInicial, MAX(data) as DataFinal FROM pos_mesas WHERE periodo ='$id'";
			$c = mysqli_query($db, $q);
			while ($a = mysqli_fetch_array($c)) {
				$DataInicial = $a['DataInicial'];
				$DataFinal = $a['DataFinal'];
				$q = "SELECT DISTINCT(SELECT nomeproduto FROM produto as p WHERE p.idproduto = ps.artigo) as atg, sum(qtd) as qtds, sum(total) as tot FROM pos_mesas_artigos as ps WHERE data BETWEEN '$DataInicial' AND '$DataFinal' GROUP BY artigo";
				$d = mysqli_query($db, $q);
				while ($b = mysqli_fetch_array($d)) {
					$atg = $b['atg'];
					$qtds = $b['qtds'];
					$tot = $b['tot'];
					?>
						<tr>
							<td><?php echo $atg; ?></td>
							<td><?php echo $qtds; ?></td>
							<td><?php echo "".number_format($tot,2,".",","); ?></td>
						</tr>
					<?php
				}
			}
		?>
	</tbody>
</table>
<div class="processo" style="text-align: center;">
	<p>iVone - Processado por programa</p>
	<p style="margin: 10px 0px;">Operador: <?php echo $_SESSION['nomeUsuario']; ?></p>
</div>

</div>
<script type="text/javascript">

	$(function() {

		window.print();

	});

</script>
</body>
</html>