<?php 
session_start();
if(!isset($_SESSION['idUsuario'])){
	header("location:../");
}
include_once '../conexao/index.php';

$id_factura = $_GET['id_fatura'];

$sql = "SELECT MAX(ano_fiscal) as serie FROM serie_factura";
$rs = mysqli_query($db, $sql) or die(mysqli_error($db));
if (mysqli_num_rows($rs) > 0) {
	$dados = mysqli_fetch_array($rs);
	$serie = $dados['serie'];
}

error_reporting(E_ALL);

$_SESSION['idUsuario'] = $_SESSION['idUsuario'];
$_SESSION['nomeUsuario'] = $_SESSION['nomeUsuario'];
$_SESSION['categoriaUsuario'] = $_SESSION['categoriaUsuario']; 
/*Variaveis do Sistema*/
/*********************************************/
?>
<!DOCTYPE html>
<html>
<head>
	<title>Factura <?php echo "$serie/$id_factura"; ?></title>
	<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
	<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
</head>
<body>
<?php 
	$sql = "SELECT * FROM empresa";
	$rs = mysqli_query($db, $sql);
	while ($dados = mysqli_fetch_array($rs)) {
?>
<div class="row">
	<div class="col-md-8">
		<h5><b><?php echo $dados['nome']; ?></b></h5>
		<p>Contribuinte: <?php echo $dados['nuit']; ?></p>
	</div>
	<div class="col-md-4">
		<img src="">
	</div>
</div>
<br>
<div class="row">
	<div class="col-md-10">
		<p><?php echo $dados['endereco']; ?></p>
		<p><?php echo $dados['provincia']; ?> - <?php echo $dados['pais']; ?></p>
		<p>Tel.: +258 <?php echo $dados['contacto']; ?></p>
		<p>Capital Social: <?php echo $dados['capital_social']; ?></p>
		<p><?php echo $dados['email']; ?></p>
	</div>
</div>
<?php
	}
?>

<?php 
	$sql = "SELECT * FROM clientes WHERE id = (SELECT cliente FROM factura WHERE id = '$id_factura')";
	$rs = mysqli_query($db, $sql);
	while ($dados = mysqli_fetch_array($rs)) {
?>
<div class="row">
	<div class="col-md-12" style="text-align: right;">
		<p>Exmo.(s) Sr.(s)</p>
		<p><?php echo $dados['nome']; ?> <?php echo $dados['apelido']; ?></p>
		<p><?php echo $dados['endereco']; ?></p>
		<p>Contribuinte: <?php echo $dados['nuit']; ?></p>
	</div>
</div>

<?php
	}
?>
<div style="display: flex;flex-direction: row;justify-content: space-between;">
	<h4>Factura #<?php echo "$serie/$id_factura"; ?></h4>

	<p style="margin-top: 8px;">Original</p>
</div>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>

<table class="table" style="margin-top: -10px;">
	<thead>
		<th>Desconto</th>
		<th>Prazo</th>
		
		<th>Condicao de Pagamento</th>
		<th>Data</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT * FROM factura WHERE id = '$id_factura'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['disconto']; ?></td>
				<td><?php echo $dados['prazo']; ?></td>
				<td><?php echo $dados['metodo']; ?></td>
				<td><?php echo $dados['data']; ?></td>
			</tr>
		<?php
			}
		?>
	</tbody>
</table>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>

<table class="table" style="margin-top: -10px;">
	<thead>
		<th>Artigo</th>
		<th>Descricao</th>
		<th>Quantidade</th>
		<th>Preco</th>
		<th>Total</th>
	</thead>
	<tbody>
		<?php 
			$sql = "SELECT artigo,qtd,preco,total,(SELECT nomeproduto FROM produto as p WHERE p.idproduto = f.artigo) as descricao FROM fa_artigos_fact as f WHERE factura = '$id_factura'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
			<tr>
				<td><?php echo $dados['artigo']; ?></td>
				<td><?php echo $dados['descricao']; ?></td>
				<td><?php echo $dados['qtd']; ?></td>
				<td><?php echo $dados['preco']; ?></td>
				<td><?php echo $dados['total']; ?></td>
			</tr>
		<?php
			}
		?>
	</tbody>
</table>
<div class="row">
	<hr class="col-md-12" style="margin-top: -5px;background-color: #000;border-style: inset;border-width: 1px;">
</div>
<div>
	<div class="col-md-12" style="text-align: right;">
		<?php 
			$sql = "SELECT * FROM factura as f WHERE id = '$id_factura'";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
			$v = $dados['valor'];
			$i = $dados['iva'];
			$d = $dados['disconto'];
			$vv = $v + $d;
			$t = $v + $i;
		?>
			<p>Total: <?php echo $v; ?></p>
			<p>IVA 16%: <?php echo $dados['iva']; ?></p>
			<p>Disconto: <?php echo $dados['disconto']; ?></p>
			<h4>Total Geral: <?php echo "$t Mt"; ?></h4>
		<?php
			}
		?>
	</div>
</div>

<div style="text-align: left;border: 1px solid #000;width: 35%;padding: 5px 10px;margin-bottom: 25px;">
		<p>Dados Bancarios</p>
		<?php 
			$sql = "SELECT * FROM empresa";
			$rs = mysqli_query($db, $sql);
			while ($dados = mysqli_fetch_array($rs)) {
		?>
		<p><b>BANCO:</b><b><?php echo $dados['banco']; ?></b></p>
		<p>CONTA: <?php echo $dados['conta']; ?></p>
		<p>NIB: <?php echo $dados['nib']; ?></p>
		<?php
			}
		?>
</div>
<div class="especial" style="display: flex;flex-direction: row;justify-content: space-between;width: 80%;margin: 5px auto;padding-top: 15px;">
	<div style="text-align: center;">
		<p>_________________________________</p>
		<p>Assinatura do Cliente</p>
	</div>
	<div style="text-align: center;">
		<p>_________________________________</p>
		<p>Assinatura do Administrativo</p>
	</div>
</div>
<script type="text/javascript">
	$(function() {
		window.print();
	});
</script>
</body>
</html>