<?php
// CLEANUP FILTERS ////////////////////////////////////////////////////////////////////////////////
// cleanup operationType - if all 3 values are checked - ignore the filter
if ($reportFilters['operationType'] and count($reportFilters['operationType'])==3) {
	$reportFilters['operationType'] = [];
}

// cleanup operation period
$reportFilters['operationDate']['startDate'] = standard2mysqlDateConversion($reportFilters['operationDate']['startDate']);
$reportFilters['operationDate']['endDate'] = standard2mysqlDateConversion($reportFilters['operationDate']['endDate']);

// external systems
$reportFilters['impServSystem'] = array_keys((array)$reportFilters['impServSystem']);

// offer FOPS - if everything is checked, treat it as nothing is checked
$reportFilters['offerFormOfPayment'] = array_keys((array)$reportFilters['offerFormOfPayment']);
if (count($reportFilters['offerFormOfPayment'])==count($CFG['offers']['formOfPaymentList'])+1) {
	$reportFilters['offerFormOfPayment'] = [];
}

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

// cleanup service startDate
$reportFilters['offerStartDate']['startDate'] = standard2mysqlDateConversion($reportFilters['offerStartDate']['startDate']);
$reportFilters['offerStartDate']['endDate'] = standard2mysqlDateConversion($reportFilters['offerStartDate']['endDate']);

// cleanup service passenger filter
$reportFilters['servicePassenger'] = $reportFilters['servicePassenger']['id'];

// cleanup imported service issue/book office/user
list($reportFilters['impServBookUserOffice']['idOffice'], $reportFilters['impServBookUserOffice']['idUser']) = explode('|', $reportFilters['impServBookUserOffice']['id']);
list($reportFilters['impServIssueUserOffice']['idOffice'], $reportFilters['impServIssueUserOffice']['idUser']) = explode('|', $reportFilters['impServIssueUserOffice']['id']);

// cleanup service resp office/user
list($reportFilters['servUserOffice']['idOffice'], $reportFilters['servUserOffice']['idUser']) = explode('|', $reportFilters['servUserOffice']['id']);

// cleanup offer supplier filter
$reportFilters['offerSupplier'] = $reportFilters['offerSupplier']['id'];

// cleanup offer destination
list($reportFilters['offerDestination']['countryCode'], $reportFilters['offerDestination']['cityId']) = explode('|', $reportFilters['offerDestination']['id']);

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

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

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

// the available columns (in numeric indexed array)
$availableTableColumns = array_keys($tableColumns);

try {

	// IMPORT INITIAL ISSUES AND REFUNDS (from TINA services) -------------------------------------
	include(__DIR__."/reportData_initialImport.php");

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

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


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

