<?php
$availableTableColumns = array_keys($tableColumns);
// TODO include serviciile neprocesate in filtre

// PREPROCESS FILTERS /////////////////////////////////////////////////////////////////////////////

// cleanup order date
$reportFilters['orderDate']['startDate'] = standard2mysqlDateConversion($reportFilters['orderDate']['startDate']);
$reportFilters['orderDate']['endDate'] = standard2mysqlDateConversion($reportFilters['orderDate']['endDate']);

// cleanup offer start date
if ($reportFilters['offerStartDate']['startDate']) {
	$reportFilters['offerStartDate']['startDate'] = standard2mysqlDateConversion($reportFilters['offerStartDate']['startDate'])." 00:00:00";
}
if ($reportFilters['offerStartDate']['endDate']) {
	$reportFilters['offerStartDate']['endDate'] = standard2mysqlDateConversion($reportFilters['offerStartDate']['endDate'])." 23:59:59";
}
// cleanup offer end date
if ($reportFilters['offerEndDate']['startDate']) {
	$reportFilters['offerEndDate']['startDate'] = standard2mysqlDateConversion($reportFilters['offerEndDate']['startDate'])." 00:00:00";
}
if ($reportFilters['offerEndDate']['endDate']) {
	$reportFilters['offerEndDate']['endDate'] = standard2mysqlDateConversion($reportFilters['offerEndDate']['endDate'])." 23:59:59";
}
// cleanup service add time
if ($reportFilters['addTime']['startDate']) {
	$reportFilters['addTime']['startDate'] = standard2mysqlDateConversion($reportFilters['addTime']['startDate'])." 00:00:00";
}
if ($reportFilters['addTime']['endDate']) {
	$reportFilters['addTime']['endDate'] = standard2mysqlDateConversion($reportFilters['addTime']['endDate'])." 23:59:59";
}

// cleanup invoice date
$reportFilters['invoiceDate']['startDate'] = standard2mysqlDateConversion($reportFilters['invoiceDate']['startDate']);
$reportFilters['invoiceDate']['endDate'] = standard2mysqlDateConversion($reportFilters['invoiceDate']['endDate']);

// cleeanup order / service responsible user
list($reportFilters['serviceResponsibleOfficeUser']['idOffice'], $reportFilters['serviceResponsibleOfficeUser']['idUser']) = explode("|", $reportFilters['serviceResponsibleOfficeUser']['id']);
list($reportFilters['orderResponsibleOfficeUser']['idOffice'], $reportFilters['orderResponsibleOfficeUser']['idUser']) = explode("|", $reportFilters['orderResponsibleOfficeUser']['id']);

// cleanup invoice client filters
$reportFilters['invoiceCorporate'] = $reportFilters['invoiceClientCorporate']['id'];
$reportFilters['invoiceCustomer'] = $reportFilters['invoiceClientCustomer']['id'];
$reportFilters['offerSupplier'] = $reportFilters['offerSupplier']['id'];

$reportFilters['serviceStatusOp'] = (array)$reportFilters['serviceStatusOp'];
$reportFilters['serviceStatusFin'] = (array)$reportFilters['serviceStatusFin'];
$reportFilters['serviceBalanced'] = (array)$reportFilters['serviceBalanced'];

// PREPARE EXTRA FILTERS
$reportExtraFilters = [];
if ($reportFilters['invoiceCorporate']) {
	$reportExtraFilters['invoiceCorporate'] = \Entity\Corporate::select($reportFilters['invoiceCorporate'])->fullDuplicatedCorporatesGroup;
}
if ($reportFilters['invoiceCustomer']) {
	$reportExtraFilters['invoiceCustomer'] = \Entity\Customer::select($reportFilters['invoiceCustomer'])->fullDuplicatedCustomersGroup;
}

// cleanup corporate groups
if ($reportFilters['corporateGroups']) {
	$reportFilters['corporateGroups'] = $reportFilters['corporateGroups']['d'];
}

// determine corporations list that are associated to selected groups
if ($reportFilters['corporateGroups']) {
	$corporateGroupsHelper = new \App\ReportsData\Helpers\PartnerGroups\Corporate();
	$corporateGroupsHelper->setGroupElementsLinear($reportFilters['corporateGroups']);
	$reportExtraFilters['corporatesInGroups'] = $corporateGroupsHelper->getPartnerIdsList();
}

// cleanup supplier groups
if ($reportFilters['supplierGroups']) {
	$reportFilters['supplierGroups'] = $reportFilters['supplierGroups']['d'];
}

// determine suppliers list that are associated to selected groups
if ($reportFilters['supplierGroups']) {
	$supplierGroupsHelper = new \App\ReportsData\Helpers\PartnerGroups\Supplier();
	$supplierGroupsHelper->setGroupElementsLinear($reportFilters['supplierGroups']);
	$reportExtraFilters['suppliersInGroups'] = $supplierGroupsHelper->getPartnerIdsList();
}

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());
}
