<?php
$availableTableColumns = array_keys($tableColumns);
// PREPROCESS FILTERS /////////////////////////////////////////////////////////////////////////////
// cleanup dates
$reportFilters['invoiceDate']['startDate'] = standard2mysqlDateConversion($reportFilters['invoiceDate']['startDate']);
$reportFilters['invoiceDate']['endDate'] = standard2mysqlDateConversion($reportFilters['invoiceDate']['endDate']);

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

// cleanup responsible user
list($reportFilters['responsibleUser']['idOffice'], $reportFilters['responsibleUser']['idUser']) = explode("|", $reportFilters['responsibleUser']['id']);

// cleanup corporate key account
list($reportFilters['clientCorporateKeyAccount']['idOffice'], $reportFilters['clientCorporateKeyAccount']['idUser']) = explode("|", $reportFilters['clientCorporateKeyAccount']['id']);

// cleanup cashed status
$reportFilters['invoiceCashedStatus'] = array_keys((array)$reportFilters['invoiceCashedStatus']);

// cleanup invoice clients filters
$reportFilters['invoiceCorporate'] = $reportFilters['corporate']['id'];
$reportFilters['invoiceCustomer'] = $reportFilters['customer']['id'];

$reportFilters['clientType'] = (array)$reportFilters['clientType'];
$reportFilters['issued'] = (array)$reportFilters['issued'];
$reportFilters['voided'] = (array)$reportFilters['voided'];


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

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