<?php
session_start();

require_once 'script_php/ConexaoSQL.php';

require_once 'gerador/Sqlserver.php';

$dataInicio = $_POST['dataInicio'];
$data = "2025-08-28";
$novaDataInicio = DateTime::createFromFormat("Y-m-d", $dataInicio)->format("d/m/Y");

$dataFinal = $_POST['dataFinal'];

$data = "2025-08-28";
$novaDataFinal = DateTime::createFromFormat("Y-m-d", $dataFinal)->format("d/m/Y");


$loja = $_POST['loja'];

$valorTotal = (float) 0;

$sql = new SqlServer();

$dados = $sql->vendasProPagamento($loja, $novaDataInicio,$novaDataFinal);

//formas de pagamento
$pix = 0;
$cartaoCredito = 0;
$cartaoDebito = 0;
$especie = 0;
$rotativo = 0;
$crecritoMaisQueSeisVezes = 0;

?>

<!DOCTYPE html>
<html>
<head>

<?php include_once 'head.html' ?>

<title></title>
</head>

<body>

<?php include_once 'header.php'; ?>

<h4> 
<svg xmlns="http://www.w3.org/2000/svg" width="26" height="26" fill="currentColor" class="bi bi-app-indicator" viewBox="0 0 16 16">
  <path d="M5.5 2A3.5 3.5 0 0 0 2 5.5v5A3.5 3.5 0 0 0 5.5 14h5a3.5 3.5 0 0 0 3.5-3.5V8a.5.5 0 0 1 1 0v2.5a4.5 4.5 0 0 1-4.5 4.5h-5A4.5 4.5 0 0 1 1 10.5v-5A4.5 4.5 0 0 1 5.5 1H8a.5.5 0 0 1 0 1z"/>
  <path d="M16 3a3 3 0 1 1-6 0 3 3 0 0 1 6 0"/>
</svg>
Pagamentos por forma de pagamento
</h4>

<p>
<p>

<section class=''>	
	<table class="table tabelaindex">

		<thead class="table-active ">

			<th>TIPO</th>
			<th>FORMA PAGAMENTO</th>
			<th>VALOR</th>
		</thead>

<?php foreach ($dados as $value) {

	//AGRUPANDO FORMAS DE PAGAMENTO

if($value[2] == "PIX" | $value[2] == "DEPOSITO" | $value[1] == "PIX" ){

		$pix = $pix + $value[3];

	}elseif($value[2] == 'ESPÉCIE'){

		$especie = $especie + $value[3];

	}elseif($value[1] == "ELO CREDITO 6X" | $value[1] == "MASTER 6X" | $value[1] == "VISA 6X" | $value[1] == "HIPERCARD 6X" | $value[1] == "AMERICAN EXPRESS 6X" ){
		
		$cartaoCredito = $cartaoCredito + $value[3];

	}elseif($value[1] == "ELO DEBITO 1X" | $value[1] == "MASTER DEBT" | $value[1] == "VISA DEBITO" | $value[1] == "American Express" | $value[1] == "MAESTRO" | $value[1] == "MASTERCARD" ){
		
		$cartaoDebito = $cartaoDebito + $value[3];

	}elseif($value[1] == "ELO ROTATIVO 1X" | $value[1] == "MASTER ROTATIVO" | $value[1] == "VISA ROTATIVO" | $value[1] == "AMERICAN EXPRESS ROTATIVO" | $value[1] == "HIPERCARD ROTATIVO"){
		
		$rotativo = $rotativo + $value[3];
	}elseif($value[1] == "MASTER 10X" | $value[1] == "VISA 10X" ){
		$crecritoMaisQueSeisVezes = $crecritoMaisQueSeisVezes + $value[3];
	}



	$valor01 = (float) $value[3];
	$valor = number_format($valor01, 2, ',', '.');
	$valorTotal = $valor01 + $valorTotal;
?>
	
		<tr>
			<td><?= $value[1]?></td>
			<td><?= $value[2]?></td>
			<td> <?="R$" . " " . $valor; ?> </td>
		</tr>

<?php } ?>

		<tr>
			<td colspan="2" class="table-active">TOTAL </td>
			<td> <b> R$ <?=  number_format($valorTotal, 2, ',', '.');?> </b></td>
		</tr>
	</table>

</section>

<h4> 

<svg xmlns="http://www.w3.org/2000/svg" width="26" height="26" fill="currentColor" class="bi bi-card-heading" viewBox="0 0 16 16">
  <path d="M14.5 3a.5.5 0 0 1 .5.5v9a.5.5 0 0 1-.5.5h-13a.5.5 0 0 1-.5-.5v-9a.5.5 0 0 1 .5-.5zm-13-1A1.5 1.5 0 0 0 0 3.5v9A1.5 1.5 0 0 0 1.5 14h13a1.5 1.5 0 0 0 1.5-1.5v-9A1.5 1.5 0 0 0 14.5 2z"/>
  <path d="M3 8.5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 0 1h-9a.5.5 0 0 1-.5-.5m0 2a.5.5 0 0 1 .5-.5h6a.5.5 0 0 1 0 1h-6a.5.5 0 0 1-.5-.5m0-5a.5.5 0 0 1 .5-.5h9a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-9a.5.5 0 0 1-.5-.5z"/>
</svg>

Pagamentos por forma de pagamento
</h4>

<p>
<p>

<section class='tabelaindex'>

<?php $totalFormaPagamento = $cartaoCredito + $pix + $cartaoDebito + $especie + $rotativo + $crecritoMaisQueSeisVezes; ?>

<table class="table tabelaindex">

		<thead class="table-active ">

			<th>TIPO</th>
			<th>FORMA PAGAMENTO</th>
			<th>VALOR</th>
			<th>%</th>
		</thead>

		<tr>
			<td>ESPÉCIE</td>
			<td>ESPÉCIE</td>
			<td>R$ <?=number_format($especie, 2 , ',' , '.');?></td>
			<td><?=ROUND($especie / $totalFormaPagamento * 100 , 2);?></td>
		</tr>

		<tr>
			<td>PIX</td>
			<td>PIX</td>
			<td>R$ <?= number_format($pix, 2 , ',' , '.');?></td>
			<td><?=ROUND(($pix /$totalFormaPagamento) * 100, 2); ?></td>
		</tr>

		<tr>
			<td>CARTÃO</td>
			<td>CRÉDITO ATÉ 6X</td>
			<td>R$ <?= number_format($cartaoCredito, 2, ',', '.');?></td>
			<td><?=ROUND($cartaoCredito / $totalFormaPagamento* 100, 2); ?></td>
		</tr>
		
		<tr>
			<td>CARTÃO</td>
			<td>ROTATIVO</td>
			<td>R$ <?=number_format($rotativo, 2, ',', '.'); ?></td>
			<td><?=ROUND($rotativo / $totalFormaPagamento * 100, 2 ); ?></td>
		</tr>

		<tr>
			<td>CARTÃO</td>
			<td>DÉBITO</td>
			<td>R$ <?=number_format($cartaoDebito, 2, ',', '.');?></td>
			<td><?=ROUND($cartaoDebito / $totalFormaPagamento * 100, 2); ?></td>
		</tr>

		<tr>
			<td>CARTÃO</td>
			<td>CRÉDITO MAIS DE 6X</td>
			<td>R$ <?=number_format($crecritoMaisQueSeisVezes,2, ',', '.')?></td>
			<td><?= ROUND($crecritoMaisQueSeisVezes / $totalFormaPagamento * 100 , 2);?></td>
		</tr>

		<tr>
			<td colspan="2" class="table-active"> <b> TOTAL </b></td>

			<td colspan = "2" align="center"><b>
				<?php
								
				echo 'R$' . ' ' . number_format($totalFormaPagamento, 2, ',', '.'); ?>		
			</b></td>
		</tr>
</table>

</section>

<?php include_once 'footer.php' ?>