<style type="text/css">
	.tk_container {
		font-family: sans-serif;
	}

	.enterprise_title {
		font-size: 20px;
		font-weight: bold;
		text-transform: uppercase;
		text-align: center;
	}

	.tx_center {
		text-align: center;
	}

	.tx_left {
		text-align: left;
	}

	.tx_right {
		text-align: right;
	}

	.description {
		font-size: 15px;
	}

	.tx_uppercase td {
		text-transform: uppercase;
	}

	.italic {
		font-style: italic;
	}
</style>

<?php
session_start();
require_once "../models/Operations.php";
$pdo = Databases::connect();

$user_ent_id = isset($_POST["ent_id"]) ? $_POST["ent_id"] : "";
$sale_pos_id = isset($_POST["pos_id"]) ? $_POST["pos_id"] : "";
$sale_created_by = isset($_POST["emp_id"]) ? $_POST["emp_id"] : "";
$from_date = isset($_POST["from_date"]) ? $_POST["from_date"] : "";
$to_date = isset($_POST["to_date"]) ? $_POST["to_date"] : "";

if (empty(intval($user_ent_id)) || empty(intval($sale_pos_id))) exit();

$entInfo = getEnterpriseData($user_ent_id);
$posInfo = getBranchData($sale_pos_id);


// USER INTERFACE TRANSLATE//POS TRANSLATE
$posLang = $posInfo['pos_lang'];

if ($posLang == "es") {
	$getPosLanguage = file_get_contents("../lang/pos_es.json");
} else if ($posLang == "fr") {
	$getPosLanguage = file_get_contents("../lang/pos_fr.json");
} else {
	$getPosLanguage = file_get_contents("../lang/pos_en.json");
}
$posTranslate = json_decode($getPosLanguage, true);

if (empty(trim((string) $from_date)) || empty(trim((string) $to_date))) exit();

$from_date = "{$from_date} {$posInfo['pos_day_start_time']}";
$to_date_end = date("Y-m-d", strtotime("+1 day", strtotime($to_date))) . " {$posInfo['pos_day_end_time']}";

//Establecemos la fecha actual de la impresion
$timeZone = $posInfo['pos_time_zone'] ?? "America/New_York";
date_default_timezone_set($timeZone);

$currentDateTimeByZone = mdlGetDateTimeByZone($timeZone);

$start_date_formated = date("d-M-Y", strtotime($from_date));
$end_date_formated = date("d-M-Y", strtotime($to_date));

?>

<div class="tk_container">

	<table width="100%">
		<tr>
			<td style="text-align: center">
				<div class="enterprise_title"><?= $entInfo['ent_name']; ?></div>
				<div class="description"><?= $posInfo['pos_address']; ?>.</div>
				<div class="description"><?= "{$posInfo['pos_city']}, {$posInfo['pos_country']}"; ?>.</div>
			</td>
		</tr>
	</table>

	<table width="100%" style="margin-top: 5px;">
		<tr>
			<td style="text-align: center">
				<div class="description" style="text-transform: uppercase; font-weight: bold;"><?= $posTranslate['cash_summary'] ?></div>
				<div><?= "{$posTranslate['from_date']}: {$start_date_formated} - {$posTranslate['to_date']}:{$end_date_formated}"; ?></div>
			</td>
		</tr>
	</table>

	<div style="border-bottom: 1.5px dashed; margin-top: 10px; margin-bottom: 10px;"></div>

	<?php

	$user_fullName = "";
	$userQuery = "";
	if (!empty($sale_created_by)) {
		$userInfo = getUserData($sale_created_by);
		$user_fullName = $userInfo['user_firstname'] . ' ' . $userInfo['user_lastname'];
		$userQuery = "AND pay_det_created_by = {$sale_created_by}";
	}

	$sql = "SELECT
	pay_det_sale_type
	,pay_det_payment_type
	,IFNULL(SUM(pay_det_income),0) AS pay_det_income
	FROM tb_payments_details 
	WHERE pay_det_pos_id = {$sale_pos_id}
	AND pay_det_date BETWEEN '$from_date' AND '$to_date_end'
	$userQuery
	AND pay_det_status = 'Enabled'
	GROUP BY pay_det_payment_type,pay_det_sale_type
	ORDER BY pay_det_id ASC;";

	// exit($sql);

	$answerPaymentDetail = executePDOFetchAll($sql, "No se pudo listar el reporte de ventas por empleado");

	?>

	<table width="100%" style="text-transform: uppercase;">
		<tr>
			<td colspan="2" class="description tx_center" style="font-weight: bold;"><?= "{$posTranslate['branch']}: {$posInfo['pos_name']}"; ?></td>
		</tr>
		<tr>
			<td colspan="2" class="description tx_center"><?= $user_fullName; ?></td>
		</tr>
	</table>

	<div style="border-bottom: 1.5px dashed; margin-top: 5px; margin-bottom: 10px;"></div>

	<div style="display: flex; justify-content: center; margin-top: 10px;">

		<table width="94%">

			<?php

			$TotalCash = 0;
			$TotalCreditCard = 0;
			$TotalUber = 0;
			$TotalPedidosYa = 0;
			$TotalDidiFood = 0;
			$TotalCashApp = 0;
			$TotalZelle = 0;
			$TotalBankDeposit = 0;
			$TotalMonCash = 0;
			$Totalfinal = 0;

			foreach ($answerPaymentDetail as $key => $value) {

				$sale_pay_type = $value['pay_det_payment_type'];

				if ($value['pay_det_sale_type'] == "Uber Eats") $sale_pay_type = "Uber Eats";
				if ($value['pay_det_sale_type'] == "Pedidos Ya") $sale_pay_type = "Pedidos Ya";
				if ($value['pay_det_sale_type'] == "Didi Food") $sale_pay_type = "Didi Food";

				if ($sale_pay_type == "Uber Eats") $TotalUber += $value['pay_det_income'];
				if ($sale_pay_type == "Pedidos Ya") $TotalPedidosYa += $value['pay_det_income'];
				if ($sale_pay_type == "Didi Food") $TotalDidiFood += $value['pay_det_income'];
				if ($sale_pay_type == "CASH") $TotalCash += $value['pay_det_income'];
				if ($sale_pay_type == "CREDIT CARD") $TotalCreditCard += $value['pay_det_income'];
				if ($sale_pay_type == "CASHAPP") $TotalCashApp += $value['pay_det_income'];
				if ($sale_pay_type == "ZELLE") $TotalZelle += $value['pay_det_income'];
				if ($sale_pay_type == "BANK DEPOSIT") $TotalBankDeposit += $value['pay_det_income'];
				if ($sale_pay_type == "MONCASH") $TotalMonCash += $value['pay_det_income'];

				$Totalfinal += $value['pay_det_income'];
			}

			$posCurrency = $posInfo['pos_currency'];

			if ($posCurrency == "USD") $posCurrency = "$";
			if ($posCurrency == "EUR") $posCurrency = "€";
			if ($posCurrency == "HTG") $posCurrency = "G";
			if ($posCurrency == "DOP") $posCurrency = "RD$";
			if ($posCurrency == "AUD") $posCurrency = "A$";

			if ($TotalCash > 0) { ?>

				<tr>
					<td class="description" style="text-align: left;" width="70%"><?= mb_strtoupper($posTranslate['cash']); ?></td>
					<td class="description" style="text-align: right;" width="50%"><?= $posCurrency . ' ' . number_format($TotalCash, 2); ?></td>
				</tr>

			<?php }

			if ($TotalCreditCard > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%"><?= mb_strtoupper($posTranslate['credit_card']); ?></td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalCreditCard, 2); ?></td>
				</tr>
			<?php }

			if ($TotalUber > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">UBER</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalUber, 2); ?></td>
				</tr>
			<?php }

			if ($TotalPedidosYa > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">PEDIDOS YA</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalPedidosYa, 2); ?></td>
				</tr>
			<?php }

			if ($TotalDidiFood > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">DIDI FOOD</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalDidiFood, 2); ?></td>
				</tr>
			<?php }

			if ($TotalCashApp > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">CASHAPP</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalCashApp, 2); ?></td>
				</tr>
			<?php }

			if ($TotalZelle > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">ZELLE</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalZelle, 2); ?></td>
				</tr>
			<?php }

			if ($TotalBankDeposit > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%"><?= mb_strtoupper($posTranslate['bank_deposit']); ?></td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalBankDeposit, 2); ?></td>
				</tr>
			<?php }

			if ($TotalMonCash > 0) { ?>
				<tr>
					<td class="description" style="text-align: left;" width="70%">MONCASH</td>
					<td class="description" style="text-align: right;" width="50%"> <?= $posCurrency . ' ' .  number_format($TotalMonCash, 2); ?></td>
				</tr>
			<?php } ?>

		</table>

	</div>

	<div style="border-bottom: 1.5px dashed; margin-top: 10px; margin-bottom: 10px;"></div>

	<div style="display: flex; justify-content: center; margin-top: 10px;">
		<table width="95%" style="text-transform: uppercase; font-weight: bold;">
			<tr>
				<td class="description" style="text-align: left;" width="50%"><?= mb_strtoupper($posTranslate['grand_total']); ?></td>
				<td class="description" style="text-align: right;" width="50%"><?= $posCurrency . ' ' .  number_format($Totalfinal, 2); ?></td>
			</tr>
		</table>
	</div>

</div>