<?php if (Session::get("tipo_usuario") != "1"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Relatório Financeiro de Empresa", "active_page" => "financeiro"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<?php 

	if (isset($_GET["mes"]) and isset($_GET["ano"])) {
		$mes = filter_input(INPUT_GET, "mes", FILTER_SANITIZE_NUMBER_INT);
		$ano = filter_input(INPUT_GET, "ano", FILTER_SANITIZE_NUMBER_INT);

		if (empty($mes) and empty($ano)) {
			$data1 = "1970-01-01 00:00:00";
			$data2 = date("Y-m-d 23:59:59");
		}
		else if (empty($mes)) {
			$data1 = "{$ano}-01-01 00:00:00";
			$data2 = "{$ano}-12-31 23:59:59";
		}
		else if (empty($ano)) {
			$mes = date("m");
			$ano = date("Y");

			$data1 = date("Y-m-01 00:00:00");
			$data2 = date("Y-m-t 23:59:59");
		}
		else{
			$data1 = date("{$ano}-{$mes}-01 00:00:00");
			$data2 = date("Y-m-t 23:59:59", strtotime($data1));
		}
	}
	else{
		$mes = date("m");
		$ano = date("Y");

		$data1 = date("Y-m-01 00:00:00");
		$data2 = date("Y-m-t 23:59:59");
	}

	$model = new Model();

	$empresa = $model->query("
		SELECT 
			SUM(lucro_empresa) AS lucro_empresa 
		FROM distribuicao 
		INNER JOIN compra ON (distribuicao.id_compra = compra.id_compra) 
		WHERE 
			status_compra = 'S' AND 
			datahora_compra BETWEEN :data1 AND :data2
	", [
		":data1" => $data1, 
		":data2" => $data2
	])->fetch();

	$entrada = $model->query("
		SELECT 
			SUM(valor_entrada) AS valor_total 
		FROM entrada 
		WHERE 
			status_entrada = 'S' AND 
			id_usuario = 1 AND 
			datahora_entrada 
			BETWEEN 
				:data1 AND :data2
	", [
		":data1" => $data1, 
		":data2" => $data2
	])->fetch();

	$saida = $model->query("
		SELECT 
			SUM(valor_saida) AS valor_total 
		FROM saida 
		WHERE 
			status_saida = 'S' AND 
			id_usuario = 1 AND 
			datahora_saida BETWEEN :data1 AND :data2
	", [
		":data1" => $data1, 
		":data2" => $data2
	])->fetch();

?>


<h4 class="mb-3">
	<i class="fa-regular fa-file-invoice-dollar"></i> Relatório Financeiro de Empresa
</h4>

<div class="content-box">
	<div class="form-row justify-content-center">
		<div class="col-xl-6 col-lg-8 col-md-10 col-sm-12">
			<form action="" method="get">
				<div class="form-group">
					<label for="mes">Filtros</label>
					<div class="input-group">
						<select name="mes" id="mes" class="form-control">
							<option value="">Todos</option>
							<option value="01">Janeiro</option>
							<option value="02">Fevereiro</option>
							<option value="03">Março</option>
							<option value="04">Abril</option>
							<option value="05">Maio</option>
							<option value="06">Junho</option>
							<option value="07">Julho</option>
							<option value="08">Agosto</option>
							<option value="09">Setembro</option>
							<option value="10">Outubro</option>
							<option value="11">Novembro</option>
							<option value="12">Dezembro</option>
						</select>
						<select name="ano" id="ano" class="form-control">
							<option value="">Todos</option>
							<?php for ($i = 2020; $i <= date("Y"); $i++): ?>
								<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
							<?php endfor; ?>
						</select>
						<div class="input-group-append">
							<button type="submit" class="btn btn-primary">
								<i class="fa-regular fa-search"></i>
							</button>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>
	<script>
		$(function(){
			$("#mes").val("<?php echo $mes; ?>");
			$("#ano").val("<?php echo $ano; ?>");
		});
	</script>

	<div class="form-row">
		<div class="col-xl-3 col-lg-3 col-md-6 col-sm-12 my-2">
			<div class="card bg-success text-white o-hidden h-100">
				<div class="card-body">
					<div class="card-body-icon">
						<i class="fad fa-money-check-alt"></i>
						</div>
					<div class="h4 mr-5">Lucro da Empresa</div>
					<div class="h5 my-0 text-right">
						<?php echo money_mask_BR($empresa->lucro_empresa); ?>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-lg-3 col-md-6 col-sm-12 my-2">
			<div class="card bg-success text-white o-hidden h-100">
				<div class="card-body">
					<div class="card-body-icon">
						<i class="fad fa-money-check-alt"></i>
						</div>
					<div class="h4 mr-5">Lucro do Admin</div>
					<div class="h5 my-0 text-right">
						<?php echo money_mask_BR($entrada->valor_total - $saida->valor_total); ?>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-lg-3 col-md-6 col-sm-12 my-2">
			<div class="card bg-info text-white o-hidden h-100">
				<div class="card-body">
					<div class="card-body-icon">
						<i class="fad fa-file-invoice-dollar"></i>
						</div>
					<div class="h4 mr-5">Entradas do Admin</div>
					<div class="h5 my-0 text-right">
						<?php echo money_mask_BR($entrada->valor_total); ?>
					</div>
				</div>
			</div>
		</div>
		<div class="col-xl-3 col-lg-3 col-md-6 col-sm-12 my-2">
			<div class="card bg-danger text-white o-hidden h-100">
				<div class="card-body">
					<div class="card-body-icon">
						<i class="fad fa-file-invoice-dollar"></i>
						</div>
					<div class="h4 mr-5">Saídas do Admin</div>
					<div class="h5 my-0 text-right">
						<?php echo money_mask_BR($saida->valor_total); ?>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<?php import_file(DIR_TEMPLATES."footer.php"); ?>