<?php
$res = 0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
	$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"] . "/main.inc.php";
}
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
	$i--;
	$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1)) . "/main.inc.php")) {
	$res = @include substr($tmp, 0, ($i + 1)) . "/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php")) {
	$res = @include dirname(substr($tmp, 0, ($i + 1))) . "/main.inc.php";
}
// Try main.inc.php using relative path
if (!$res && file_exists("../main.inc.php")) {
	$res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
	$res = @include "../../main.inc.php";
}
if (!$res && file_exists("../../../main.inc.php")) {
	$res = @include "../../../main.inc.php";
}
if (!$res) {
	die("Include of main fails");
}

require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT . '/user/class/usergroup.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/financialreport/class/userinvoice.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/settlements/class/groupusers.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/financialreport/class/helper.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/commissionhandler.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/bbus/class/userloginnaplo.class.php';


$helper = new HelperUserInvoice($db);
$commmissionhandler = new CommissionHandler();
$userLoginNaploObj = new UserLoginNaplo($db);


// Load translation files required by the page
$langs->loadLangs(array("financialreport@financialreport"));

$action = GETPOST('action', 'aZ09');
$cancel = GETPOST('cancel', 'int');
$id = GETPOST('id', 'int');
$idmenu = GETPOST('idmenu', 'int');
$daterange = GETPOST('daterange', 'alpha');
$datepicker = GETPOST('datepicker', 'alpha');
$interval = GETPOST('interval', 'alpha');
$array = GETPOST('array', 'array');
$options = GETPOST('options', 'array');

$excelexport = GETPOST('excelexport', 'int');

if ($daterange == '' && $datepicker == '') {
	$now = dol_now();
	$daterange = date('Y-m-01') . ' - ' . date('Y-m-t');
	$isSetDatepicker = true;
	//$datesOfRange = $helper->createArray($daterange);
} else {
	$daterange = $daterange != '' ? $daterange : $datepicker;
	$isSetDatepicker = $datepicker != '';
	//$datesOfRange = $helper->createArray($daterange);
}
$daterangeString = $helper->createDaterangeString($daterange);

//print_r($daterange);

$socid = GETPOST('socid', 'int');
if (isset($user->socid) && $user->socid > 0) {
	$action = '';
	$socid = $user->socid;
}

$form = new Form($db);
$formfile = new FormFile($db);
$groups = $helper->getAllHotelGroupsFromGroupClass($db);

if (empty($id)) {
	$keys = array_keys($groups);
	$id = $keys[0];
}

$entity = $groups[$id]['entity'];

if($excelexport){
	$helper->createExcelHotelReport($daterange, $id, $entity, $groups);
}

include DOL_DOCUMENT_ROOT . '/custom/financialreport/core/actions_addupdatedelete.inc.php';

$deletedUsersArray = $helper->getDeletedHotelUsers($id);
$usersOfGroups = $helper->getUsersByIdFromGroupUsers($id);

$title = '';
$help_url = '';
$morejs = '';
$morecss = '';
llxHeader('', $title, $help_url, '', 0, 0, $morejs, $morecss, '', 'classforhorizontalscrolloftabs');

$url = $_SERVER["PHP_SELF"] . "?id={$id}";

print '<form method="POST" id="statementlists" action="' . $url . '">' . "\n";
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
print '<input type="hidden" name="action" value="list">';
/*print '<input type="hidden" name="sortfield" value="' . $sortfield . '">';
print '<input type="hidden" name="sortorder" value="' . $sortorder . '">';
print '<input type="hidden" name="page" value="' . $page . '">';
print '<input type="hidden" name="contextpage" value="' . $contextpage . '">';*/
print '<input type="hidden" name="datepickerdata" value="' . $daterange . '">';
print '<input type="hidden" name="intervalchk" value="' . $interval . '">';
$now = date('Y.m.d', dol_now());
print load_fiche_titre($langs->trans("HotelGroupFinancialReport"), '', 'financialreport@financialreport');
include DOL_DOCUMENT_ROOT . '/custom/financialreport/core/tpl/daterangehoteldiv.tpl.php';

$hotelsArray = $helper->getAllHotels();
$usersList = $helper->getUsersList($id, $daterange, $entity);
$usersIdList = $helper->getUsersIdList($id, $daterange, $entity);
$AllAmountHUFInPeriod = $helper->getAllAmountInPeriod($id, $daterange, 'HUF', $usersIdList, $entity);
$AllAmountEURInPeriod = $helper->getAllAmountInPeriod($id, $daterange, 'EUR', $usersIdList, $entity);
$AllCommissionHUFInPeriod = $helper->getAllCommissionInPeriod($id, $daterange, 'HUF', $usersIdList, $entity);
$AllCommissionEURHUFInPeriod = $helper->getAllCommissionInPeriod($id, $daterange, 'EUR', $usersIdList, $entity);
//teszt
//teszt2
//---------------------------------------------
# 1
//---------------------------------------------
print '<div class=""><div class="" style="width:33%;float:left;">';
print '<table style="width:100%;">';
print '<tr style="width: 100%; height: 40px; text-align: center; background-color: lightgrey;"><td>' . $langs->trans("nameofgroup") . '</td></tr>';
foreach ($groups as $key => $val) {
	$coloredLine = $id == $key ? ' background-color: #b0c4de' : '';
	print '<tr style="height:30px;' . $coloredLine . '"><td><a href="settlementsstatementshotelsindex.php?id=' . $key . '&datepicker=' . $daterange . '"><span class="fa fa-group pictofixedwidth"></span> ' . $val['ref'] . '</a></td></tr>';
}
print '</table>';
print '</div>';

//---------------------------------------------
# 3
//---------------------------------------------
$colspan = 2;
print '<div class="" style="width:66%;float:left;  margin-left: 10px;">';
print '<table class="center" style="width:100%;">';
print '<tr class="statementtitles"><td colspan="3"><div class="groupTitle">' . $hotelsArray[$groups[$id]['fk_hotel_id']] . '</div></td></tr> 
<tr><td colspan="4">' . $daterangeString . '</td></tr>
<tr><td colspan="4">&nbsp;</td></tr>
<tr class="statementtitles"><td style="width:50%;">' . $langs->trans('Incomes') . '</b></td><td style="width:50%;"><b>' . $langs->trans('Commissions') . '</b></td></tr>
<tr class="redcolor"><td>' . $helper->correctNumber($AllAmountHUFInPeriod) . ' HUF</td><td>' . $helper->correctNumber($AllCommissionHUFInPeriod) . ' HUF</td></tr>
<tr class="redcolor"><td>' . $helper->correctNumber($AllAmountEURInPeriod) . ' EUR</td><td>' . $helper->correctNumber($AllCommissionEURHUFInPeriod) . ' HUF</td></tr>
<tr><td colspan="3"><div class="closer"></div></td></tr>
<tr><td>&nbsp;</td></tr>
</table>';
print '<div class="center" style="margin-bottom: 30px;"><button class="ExcelExport" type="submit" name="excelexport", value="1">' . $langs->trans('Excel export') . '</button></div>';
print '<table class="center" style="width:100%;">
<tr class="statementtitles"><td colspan="4"><div class="groupTitle">' . $hotelsArray[$groups[$id]['fk_hotel_id']] . '</div></td></tr>
<tr><td colspan="4">' . $daterangeString . '</td></tr>
<tr><td colspan="4">&nbsp;</td></tr>
<tr class="statementtitles"><td colspan="2" style="width:33%;">' . $langs->trans('User') . '</td><td style="width:33%;">' . $langs->trans('AmounOfSales') . '</td><td style="width:33%;">' . $langs->trans('AmounOfSales') . '</td></tr>';
print $usersList;
print '<tr><td colspan="4"><div class="closer"></div></td></tr>';
print '</table>';

print '</div>';
print '</form>' . "\n";

// End of page
llxFooter();
$db->close();
?>
<style>
	.ExcelExport{
		background-color: green;
	}
	.groupTitle {
		height: 30px;
		width: 100%;
		background-color: rgb(163, 0, 51);
		color: white!important;
		border-radius: 10px 10px 0 0;
		padding-top: 10px;
	}

	.closer {
		height: 20px;
		width: 100%;
		border-radius: 0 0 10px 10px;
		border-right: 2px solid lightgrey;
		border-left: 2px solid lightgrey;
		border-bottom: 2px solid lightgrey;
	}
	
	.shadow {
	}

	.statementtitles {
		font-size: 15px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.redcolor {
		color: rgb(163, 0, 51);
		font-weight: bold;
		font-size: 20px;
	}

	.smallredcolor {
		color: rgb(163, 0, 51);
		font-weight: normal;
	}

	button {
		width: 200px;
		height: 30px;
		background-color: rgb(163, 0, 51);
		color: white;
		border-radius: 20px;
		border: none;
		cursor: pointer;
	}

	button:disabled {
		background-color: #cccccc;
		color: #666666;
		cursor: not-allowed;
	}

	.container {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.content {
		margin-top: auto;
	}

	.box {
		height: 220px;
		width: 100%;
		background-image: url('img/rectangle-6.png');
		background-repeat: no-repeat;
		background-position: center;
	}
</style>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css">
<script src="https://cdn.jsdelivr.net/npm/moment"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>

<script>
	function giveMeUserData(id) {
		var divDisplay = $('#commissiondata_' + id).css("display"); // Az "myDiv" div display tulajdonságának lekérése
		if (divDisplay === "none") {
			$('#commissiondata_' + id).slideDown();
			$('#commissiondata_' + id).css('display', 'block');
			$('#span_' + id).removeClass('fa-angle-down').addClass('fa-angle-up');
		} else {
			$('#commissiondata_' + id).slideUp();
			$('#span_' + id).removeClass('fa-angle-up').addClass('fa-angle-down');
			//$('#commissiondata_' + id).css('display', 'none');
		}
	}
	var datepickerdata = document.querySelector('input[type="hidden"][name="datepickerdata"]');
	var interval = document.querySelector('input[type="hidden"][name="intervalchk"]');
	var checkbox = document.querySelector('input[type="checkbox"][name="interval"]');
	var datepickerinput = document.querySelector('input[type="text"][name="datepicker"]');
	var daterangeinput = document.querySelector('input[type="text"][name="daterange"]');
	if (interval.value == 'on') {
		checkbox.checked = true;
		checkStatus();
		daterangeinput.value = datepickerdata.value;
	}
	datepickerinput.value = datepickerdata.value;

	function checkStatus() {
		var checkbox = document.getElementById("chkbdatepicker");
		var isChecked = checkbox.checked;
		if (isChecked) {
			var datepickerdiv = document.getElementById("datepickerdiv");
			var daterangediv = document.getElementById("daterangediv");
			datepickerdiv.style.display = "none";
			daterangediv.style.display = "block";
			datepickerinput.value = '';
			daterangeinput.value = '';
		} else {
			datepickerinput.value = '';
			daterangeinput.value = '';
			var datepickerdiv = document.getElementById("datepickerdiv");
			var daterangediv = document.getElementById("daterangediv");
			daterangediv.style.display = "none";
			datepickerdiv.style.display = "block";
		}
	}
	var datepicker =
		$(function () {
			$('#daterange').daterangepicker({
				"showDropdowns": true,
				"autoUpdateInput": false,
				locale: {
					format: 'YYYY-MM-DD',
					cancelLabel: 'Mégse',
					applyLabel: 'OK',
					moLabel: 'OK',
					firstDay: 1,
					"daysOfWeek": [
						"Vas",
						"Hé",
						"Ke",
						"Sze",
						"Csü",
						"Pé",
						"Szo"
					],
					"monthNames": [
						"Január",
						"Február",
						"Március",
						"Április",
						"Május",
						"Június",
						"Július",
						"Augsztus",
						"Szeptember",
						"Október",
						"November",
						"December"
					],

				},
			});
		});
	$(function () {
		$('#datepicker').daterangepicker({
			"singleDatePicker": true,
			"showDropdowns": true,
			"autoUpdateInput": false,
			locale: {
				format: 'YYYY-MM-DD',
				cancelLabel: 'Mégse',
				applyLabel: 'OK',
				moLabel: 'OK',
				firstDay: 1,
				"daysOfWeek": [
					"Vas",
					"Hé",
					"Ke",
					"Sze",
					"Csü",
					"Pé",
					"Szo"
				],
				"monthNames": [
					"Január",
					"Február",
					"Március",
					"Április",
					"Május",
					"Június",
					"Július",
					"Augsztus",
					"Szeptember",
					"Október",
					"November",
					"December"
				],

			},
		});
	});
	$('input[name="datepicker"]').on('apply.daterangepicker', function (ev, picker) {
		$(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
	});

	$('input[name="datepicker"]').on('cancel.daterangepicker', function (ev, picker) {
		$(this).val('');
	});
	$('input[name="daterange"]').on('apply.daterangepicker', function (ev, picker) {
		$(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
	});

	$('input[name="daterange"]').on('cancel.daterangepicker', function (ev, picker) {
		$(this).val('');
	});
</script>