<?php
$availableTableColumns = array_keys($tableColumns);

// PREPROCESS FILTERS /////////////////////////////////////////////////////////////////////////////
// cleanup dates
$reportFilters['docDate']['startDate'] = standard2mysqlDateConversion($reportFilters['docDate']['startDate']);
$reportFilters['docDate']['endDate'] = standard2mysqlDateConversion($reportFilters['docDate']['endDate']);

$reportFilters['docAddDate']['startDate'] = standard2mysqlDateConversion($reportFilters['docAddDate']['startDate']);
$reportFilters['docAddDate']['endDate'] = standard2mysqlDateConversion($reportFilters['docAddDate']['endDate']);

$reportFilters['processStatus'] = array_keys((array)$reportFilters['processStatus']);
$reportFilters['extSystem'] = array_keys((array)$reportFilters['extSystem']);

// cleanup invoice clients filters
$reportFilters['idSupplier'] = $reportFilters['supplier']['id'];
$reportFilters['idCorporate'] = $reportFilters['corporate']['id'];
$reportFilters['idCustomer'] = $reportFilters['customer']['id'];

// PREPARE EXTRA FILTERS
$reportExtraFilters = [];
if ($reportFilters['idSupplier']) {
	$reportExtraFilters['idSupplier'] = \Entity\Supplier::select($reportFilters['idSupplier'])->fullDuplicatedSuppliersGroup;
}
if ($reportFilters['idCorporate']) {
	$reportExtraFilters['idCorporate'] = \Entity\Corporate::select($reportFilters['idCorporate'])->fullDuplicatedCorporatesGroup;
}
if ($reportFilters['idCustomer']) {
	$reportExtraFilters['idCustomer'] = \Entity\Customer::select($reportFilters['idCustomer'])->fullDuplicatedCustomersGroup;
}

try {
	// EXCUTE MAIN SQLs ---------------------------------------------------------------------------
	include(__DIR__."/reportData_executeSQL.php");

	// FILL IN OTHER FIELDS -----------------------------------------------------------------------
	include(__DIR__."/reportData_otherFields.php");

	// LOOP RECORDS -------------------------------------------------------------------------------
	include(__DIR__."/reportData_loopRecords.php");

} catch (\DBException $e) {
	killReport("Could not run SQL in report. SQL error: ".$e->getMessage());
} catch (\Exception $e) {
	killReport("Could not process report. Error: ".$e->getMessage());
}
