<?php 

session_start();

include_once '../conexao/index.php';

date_default_timezone_set('Africa/Maputo');

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



/*Variaveis do Sistema*/

/*********************************************/

$_SESSION['idUsuario'] = $_SESSION['idUsuario'];

$_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];

$_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 



$userID = $_SESSION['idUsuario'];

$userNOME = $_SESSION['nomeUsuario'];

$userCATE = $_SESSION['categoriaUsuario'];

/*********************************************/



$mesa = $_GET['mesa'];

$cliente = $_GET['cliente'];

$modo = $_GET['modo'];

$valor = $_GET['valor'];

$status = 'Aberto';



$sqlBoxx = "SELECT idperiodo FROM periodo WHERE diaperiodo = '$status'";

$rsBoxx = mysqli_query($db, $sqlBoxx);

if (mysqli_num_rows($rsBoxx) > 0) {

	$d = mysqli_fetch_assoc($rsBoxx);

	$periodo = $d['idperiodo'];

}



$sqlq = "SELECT SUM(total) as totc FROM pos_mesas_temp_artigos WHERE mesa = '$mesa'";

$rsv = mysqli_query($db, $sqlq);

$dadosx = mysqli_fetch_array($rsv);

$v = $dadosx['totc'];

$troco = $valor - $v;





$sql = "INSERT INTO `pos_mesas`(`descricao`, `pagamento`, `modo`, `troco`, `cliente`, `user`, `periodo`, `mesa`) VALUES('$data_hora', '$v', '$modo', '$troco', '$cliente', '$userID', '$periodo', '$mesa')";

$rs = mysqli_query($db, $sql);

if ($rs>0) {

	$sql = "SELECT MAX(id) as idm FROM pos_mesas WHERE user = '$userID' AND mesa = '$mesa' AND periodo = '$periodo'";

	$rs = mysqli_query($db, $sql);

	while ($dados = mysqli_fetch_assoc($rs)) {

		$idm = $dados['idm'];

		$sqlll = "SELECT * FROM pos_mesas_temp_artigos WHERE mesa = '$mesa'";

		$rsss = mysqli_query($db, $sqlll);

		while ($d = mysqli_fetch_assoc($rsss)) {

			$produto = $d['artigo'];

			$qtd = $d['qtd'];

			$preco = $d['preco'];

			$total = $d['total'];

			$sl = "INSERT INTO `pos_mesas_artigos`(`artigo`, `qtd`, `preco`, `total`, `cliente`, `user`, `pos`, `mesa`) VALUES('$produto', '$qtd', '$preco', '$total', '$cliente', '$userID', '$idm', '$mesa')";

			$rd = mysqli_query($db, $sl);

			if ($rd > 0) {

				$sql = "UPDATE produto SET stock = stock - '$qtd' WHERE nomeproduto = '$produto'";

                $rsA = mysqli_query($db, $sql);


			}

		}

	}

}

?>

<!DOCTYPE html>

<html>

<head>

	<title>VD #<?php echo $idm; ?></title>

	<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
	<style type="text/css">
		*{
			font-size: 8pt;
		}
	</style>

</head>

<body>

<div class="fatura">

	<div class="header" style="text-align: center;">

		<img src="logo_pedaco.png" style="width: 110px;">
		<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>Recibo</span><span class="pd" style="margin-left: 40px;"><?php echo $idm; ?></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;">

		<thead class="titulo">

			<th>Cod</th>

			<th>Descricao</th>

			<th>Qtd</th>

			<th>Preco</th>

			<th>Total</th>

		</thead>

		<tbody>

			<?php 

				$q = "SELECT * FROM pos_mesas_temp_artigos WHERE mesa = '$mesa' AND user = '$userID'";

				$c = mysqli_query($db, $q);

				while ($a = mysqli_fetch_array($c)) {

			?>

				<tr>

					<td><?php echo $a['id']; ?></td>

					<td><?php echo $a['artigo']; ?></td>

					<td><?php echo $a['qtd']; ?></td>

					<td><?php echo $a['preco']; ?></td>

					<td><?php echo $a['total']; ?></td>

				</tr>

			<?php

				}

			?>

		</tbody>

	</table>

	

	<div class="totall">

		<?php 

			$sql = "SELECT SUM(total) as totc FROM pos_mesas_temp_artigos WHERE mesa = '$mesa' AND user = '$userID'";

			$rs = mysqli_query($db, $sql);

			if (mysqli_num_rows($rs>0)) {

				$dados = mysqli_fetch_array($rs);

				$v = $dados['totc'];

				$troco = $valor - $v;

			}

		?>

		<p style="font-size: 18pt;display: flex;flex-direction: row;justify-content: space-between;"><span>Total</span><span><?php echo $v; ?></span></p>

		<p style="font-size: 18pt;display: flex;flex-direction: row;justify-content: space-between;"><span>Troco</span><span><?php echo $troco; ?></span></p>

	</div>

	<div class="processo" style="text-align: center;">

		<p>iVone - Processado por programa</p>

		<p style="margin: 10px 0px;">Operador: <?php echo $_SESSION['nomeUsuario']; ?></p>

		<p>Obrigado pela sua preferencia!</p>

	</div>

</div>

<?php 

	$k = "DELETE FROM pos_mesas_temp_artigos WHERE mesa = '$mesa' AND user = '$userID'";

	$b = mysqli_query($db, $k);

	if ($b>0) {

		$k = "UPDATE mesas SET status = 0 WHERE numero = '$mesa'";

		$b = mysqli_query($db, $k);

	}

?>

<script type="text/javascript">

	$(function() {

		window.print();

	});

</script>

</body>

</html>