<?php


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// MANAGE REPORT NORMAL FILTERS ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

if ($OK) {
	$reportFilters=unserialize($reportDetails['filters']);
	unset($reportDetails['filters']);
	if (!is_array($reportFilters)) $reportFilters= [];

	// loop through every filter //////////////////////////////////////////////////////////////////
	foreach($reportFilters as $filter=>$filterDet) {

		///////////////////////////////////////////////////////////////////////////////////////////
		// IF FILTER IS CHECKBOX - GET THE LIST OF OPTIONS ////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////
		if ($filtersConfig[$filter]['type']=="checkbox") {

			switch($filtersConfig[$filter]['data']) {

				// corporateType ------------------------------------------------------------------
				case 'corporateType': {
					$reportFilters[$filter]['listValues']= ['CC'=>$_LANG['generalData']['companyTypes']['CC'], 'SA'=>$_LANG['generalData']['companyTypes']['SA']];
					break;
				}

				// corporateStatus ------------------------------------------------------------------
				case 'corporateStatus': {
					$reportFilters[$filter]['listValues']= [];
					foreach ($CFG['generalData']['corporateStatuses'] as $sts) {
						$reportFilters[$filter]['listValues'][$sts] = $_LANG['generalData']['corporateStatus'][$sts];
					}
					break;
				}

				// clientType ---------------------------------------------------------------------
				case 'clientType': {
					$reportFilters[$filter]['listValues']= ['DC'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['directClients'], 'CC'=>$_LANG['generalData']['companyTypes']['CC'], 'SA'=>$_LANG['generalData']['companyTypes']['SA']];
					break;
				}

				// clientTypeWithBlank ------------------------------------------------------------
				case 'clientTypeWithBlank': {
					$reportFilters[$filter]['listValues']= ['DC'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['directClients'], 'CC'=>$_LANG['generalData']['companyTypes']['CC'], 'SA'=>$_LANG['generalData']['companyTypes']['SA'], '0'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['clientType']['0']];
					break;
				}

				// locked -------------------------------------------------------------------------
				case 'locked': {
					$reportFilters[$filter]['listValues']= ['1'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['locked'], '0'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['notLocked']];
					break;
				}

				// personTitle --------------------------------------------------------------------
				case 'personTitle': {
					$reportFilters[$filter]['listValues']= [];
					foreach($CFG['generalData']['personTitles'] as $code) {
						$reportFilters[$filter]['listValues'][$code]=$_LANG['generalData']['personTitles'][$code];
					}
					break;
				}

				// personType ---------------------------------------------------------------------
				case 'personType': {
					$reportFilters[$filter]['listValues']= [];
					foreach($CFG['generalData']['personTypes'] as $code) {
						$reportFilters[$filter]['listValues'][$code]=$_LANG['generalData']['personTypes'][$code];
					}
					break;
				}

				// formOfPayment --------------------------------------------------------------
				case 'formOfPayment': { // TODO: not used
					$reportFilters[$filter]['listValues']= [];
					$fops=\App\Config\FormsOfPayment::getInstance()->getAll();
					if ($fops) {
						foreach($fops as $code=>$name) {
							$reportFilters[$filter]['listValues'][$code]=$name;
						}
					}
					break;
				}

				// offerFormOfPayment -------------------------------------------------------------
				case 'offerFormOfPayment': {
					$reportFilters[$filter]['listValues'] = [];
					foreach ($CFG['offers']['formOfPaymentList'] as $fop) {
						$reportFilters[$filter]['listValues'][$fop] = $_LANG['offers']['formOfPayment'][$fop];
					}
					$reportFilters[$filter]['listValues']['0'] = $_LANG['reportsManagement']['admin']['filters']['checkboxes']['noOfferFop'];
					break;
				}

				// cashedStatus -------------------------------------------------------------------
				case 'cashedStatus': {
					$reportFilters[$filter]['listValues']['n']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['notCashed'];
					$reportFilters[$filter]['listValues']['p']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['partialCashed'];
					$reportFilters[$filter]['listValues']['t']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['totalCashed'];
					$reportFilters[$filter]['listValues']['o']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['overCashed'];
					break;
				}

				// supplierInvoiceFinStatus ------------------------------------------------------
				case 'supplierInvoiceFinStatus': {
					$reportFilters[$filter]['listValues']['n']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['notCashed'];
					$reportFilters[$filter]['listValues']['p']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['partialCashed'];
					$reportFilters[$filter]['listValues']['t']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['totalCashed'];
					break;
				}

				// paymentDocTypes ----------------------------------------------------------------
				case 'paymentDocTypes' : {
					$reportFilters[$filter]['listValues'] = \App\Config\PaymentDocuments::getInstance()->getList();
					break;
				}

				// boolean ------------------------------------------------------------------------
				case 'boolean': {
					$reportFilters[$filter]['listValues']['0']=$_LANG['no'];
					$reportFilters[$filter]['listValues']['1']=$_LANG['yes'];
					break;
				}

				// balancedSimple -----------------------------------------------------------------
				case 'balancedSimple': {
					$reportFilters[$filter]['listValues']['n']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['notBalanced'];
					$reportFilters[$filter]['listValues']['t']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['totalBalanced'];
					break;
				}

				// serviceStatusOp ----------------------------------------------------------------
				case 'serviceStatusOp': {
					$reportFilters[$filter]['listValues']['NP']=$_LANG['reservationServices']['statusOpName']['NP'];
					$reportFilters[$filter]['listValues']['PE']=$_LANG['reservationServices']['statusOpName']['PE'];
					$reportFilters[$filter]['listValues']['OF']=$_LANG['reservationServices']['statusOpName']['OF'];
					$reportFilters[$filter]['listValues']['OK']=$_LANG['reservationServices']['statusOpName']['OK'];
					$reportFilters[$filter]['listValues']['XX']=$_LANG['reservationServices']['statusOpName']['XX'];
					break;
				}

				// serviceStatusFin ---------------------------------------------------------------
				case 'serviceStatusFin': {
					$reportFilters[$filter]['listValues']['NI']=$_LANG['reservationServices']['statusFinName']['NI'];
					$reportFilters[$filter]['listValues']['PI']=$_LANG['reservationServices']['statusFinName']['PI'];
					$reportFilters[$filter]['listValues']['TI']=$_LANG['reservationServices']['statusFinName']['TI'];
					break;
				}

				// serviceStatusFinWithBlank ------------------------------------------------------
				case 'serviceStatusFinWithBlank': {
					$reportFilters[$filter]['listValues']['NI']=$_LANG['reservationServices']['statusFinName']['NI'];
					$reportFilters[$filter]['listValues']['PI']=$_LANG['reservationServices']['statusFinName']['PI'];
					$reportFilters[$filter]['listValues']['TI']=$_LANG['reservationServices']['statusFinName']['TI'];
					$reportFilters[$filter]['listValues']['00']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['noFinStatus'];
					break;
				}

				// orderStatusOp ----------------------------------------------------------------
				case 'orderStatusOp': {
					$reportFilters[$filter]['listValues']['RQ']=$_LANG['reservations']['statusOpName']['RQ'];
					$reportFilters[$filter]['listValues']['WL']=$_LANG['reservations']['statusOpName']['WL'];
					$reportFilters[$filter]['listValues']['OK']=$_LANG['reservations']['statusOpName']['OK'];
					$reportFilters[$filter]['listValues']['XX']=$_LANG['reservations']['statusOpName']['XX'];
					break;
				}

				// orderStatusFin ---------------------------------------------------------------
				case 'orderStatusFin': {
					$reportFilters[$filter]['listValues']['NI']=$_LANG['reservations']['statusFinName']['NI'];
					$reportFilters[$filter]['listValues']['PI']=$_LANG['reservations']['statusFinName']['PI'];
					$reportFilters[$filter]['listValues']['TI']=$_LANG['reservations']['statusFinName']['TI'];
					break;
				}

				// cashingDocTypes ----------------------------------------------------------------
				case 'cashingDocTypes' : {
					$reportFilters[$filter]['listValues'] = \App\Config\CashingDocuments::getInstance()->getList();
					break;
				}

				// ticket issue and refund operations ---------------------------------------------
				case 'issueAndRefundOps': {
					$reportFilters[$filter]['listValues']['ISSUE'] = "ISSUE";
					$reportFilters[$filter]['listValues']['REFUND'] = "REFUND";
					$reportFilters[$filter]['listValues']['REFUNDED'] = "REFUNDED";
					break;
				}

				// taskStatus ---------------------------------------------------------------------
				case 'taskStatus': {
					$reportFilters[$filter]['listValues']['notDistributed']=$_LANG['tasks']['statusName']['notDistributed'];
					$reportFilters[$filter]['listValues']['distributed']=$_LANG['tasks']['statusName']['distributed'];
					$reportFilters[$filter]['listValues']['refused']=$_LANG['tasks']['statusName']['refused'];
					$reportFilters[$filter]['listValues']['pending']=$_LANG['tasks']['statusName']['pending'];
					$reportFilters[$filter]['listValues']['inProgress']=$_LANG['tasks']['statusName']['inProgress'];
					$reportFilters[$filter]['listValues']['onHold']=$_LANG['tasks']['statusName']['onHold'];
					$reportFilters[$filter]['listValues']['finished']=$_LANG['tasks']['statusName']['finished'];
					break;
				}

				// documentType -------------------------------------------------------------------
				case 'documentType' : {
					foreach (\App\Config\IssueDocuments::getInstance()->getDocsListByPivot() as $pivot=>$docsList) {
						foreach ($docsList as $docType=>$docName) {
							$reportFilters[$filter]['listValues'][$docType] = $docName.' (<i>'.$_LANG['documentPivotName'][$pivot].'</i>)';
						}
					}
					break;
				}

				// imported document TINA status --------------------------------------------------
				case 'impDocStatus': {
					$reportFilters[$filter]['listValues']['new']=$_LANG['importedDocuments']['statusName']['new'];
					$reportFilters[$filter]['listValues']['partial']=$_LANG['importedDocuments']['statusName']['partial'];
					$reportFilters[$filter]['listValues']['stopped']=$_LANG['importedDocuments']['statusName']['stopped'];
					$reportFilters[$filter]['listValues']['processed']=$_LANG['importedDocuments']['statusName']['processed'];
					$reportFilters[$filter]['listValues']['blocked']=$_LANG['importedDocuments']['statusName']['blocked'];
					break;
				}

				// imported document external systems ---------------------------------------------
				case 'impDocExtSystem': {
					$sysListObj = new \App\EntityList\ImportedDocumentsSystems();
					$sysListObj->setColumns(['name']);
					$sysListObj->setOrderBy('impDocSystem.name', 'ASC');

					foreach($sysListObj->exportRecords() as $rec) {
						$reportFilters[$filter]['listValues'][$rec['id']] = $rec['name'];
					}
					break;
				}

				// imported order external systems ------------------------------------------------
				case 'impOrderExtSystem': case 'impOrderExtSystemWithBlank': {
					$sysListObj = new \App\EntityList\ImportedOrdersSystems();
					$sysListObj->setFilter('active', '=', 1);
					$sysListObj->setColumns(['name']);
					$sysListObj->setOrderBy('extResSys.name', 'ASC');

					foreach($sysListObj->exportRecords() as $rec) {
						$reportFilters[$filter]['listValues'][$rec['id']] = $rec['name'];
					}
					// add manual system
					if ($filtersConfig[$filter]['data']=='impOrderExtSystemWithBlank') {
						$reportFilters[$filter]['listValues']['0'] = sprintf('<i>%s</i>', $_LANG['reportsManagement']['admin']['filters']['checkboxes']['noExtSystem']);
					}

					break;
				}

				// imported service TINA status ---------------------------------------------------
				case 'impServTinaStatus': case 'impServTinaStatusWithBlank': {
					$reportFilters[$filter]['listValues']['new']=$_LANG['importedService']['statusName']['new'];
					$reportFilters[$filter]['listValues']['processed']=$_LANG['importedService']['statusName']['processed'];
					$reportFilters[$filter]['listValues']['blocked']=$_LANG['importedService']['statusName']['blocked'];
					// add undetermined value
					if ($filtersConfig[$filter]['data']=='impServTinaStatusWithBlank') {
						$reportFilters[$filter]['listValues']['0'] = sprintf('<i>%s</i>', $_LANG['reportsManagement']['admin']['filters']['checkboxes']['blank']);
					}
					break;
				}

				// imported service external system status ----------------------------------------
				case 'impServExtStatus': case 'impServExtStatusWithBlank': {
					$reportFilters[$filter]['listValues']['added']=$_LANG['importedService']['systemStatusName']['added'];
					$reportFilters[$filter]['listValues']['request']=$_LANG['importedService']['systemStatusName']['request'];
					$reportFilters[$filter]['listValues']['confirmed']=$_LANG['importedService']['systemStatusName']['confirmed'];
					$reportFilters[$filter]['listValues']['cancelled']=$_LANG['importedService']['systemStatusName']['cancelled'];
					$reportFilters[$filter]['listValues']['deleted']=$_LANG['importedService']['systemStatusName']['deleted'];
					$reportFilters[$filter]['listValues']['refunded']=$_LANG['importedService']['systemStatusName']['refunded'];
					if ($filtersConfig[$filter]['data']=='impServExtStatusWithBlank') {
						$reportFilters[$filter]['listValues']['0'] = sprintf('<i>%s</i>', $_LANG['reportsManagement']['admin']['filters']['checkboxes']['blank']);
					}
					break;
				}
			}

		}

		if ($filtersConfig[$filter]['type']=="select") {

			switch($filtersConfig[$filter]['data']) {
				// invRowsGroupType ---------------------------------------------------------------
				case 'invRowsGroupType': {
					$reportFilters[$filter]['listValues']['service']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['service'];
					$reportFilters[$filter]['listValues']['issueRefund']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['issueRefund'];
					$reportFilters[$filter]['listValues']['issueRefundReversed']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['issueRefundReversed'];
					break;
				}
				// docGroupType -------------------------------------------------------------------
				case 'docGroupType': {
					$reportFilters[$filter]['listValues']['noGroup']=$_LANG['reportsManagement']['admin']['filters']['select']['docGroupType']['noGroup'];
					$reportFilters[$filter]['listValues']['groupByPivotDoc']=$_LANG['reportsManagement']['admin']['filters']['select']['docGroupType']['groupByPivotDoc'];
					break;
				}
			}
		}

		if ($filtersConfig[$filter]['type']=="selectMultiple") {
			// loop through every data types
			switch($filtersConfig[$filter]['data']) {
				// corporateGroups ----------------------------------------------------------------
				case 'corporateGroups': {
					foreach (\App\Config\CorporateGroups::getInstance()->getGroupsList() as $corporateGroup) {
						if ($corporateGroup['idGroupElement']) {
							$reportFilters[$filter]['listValues'][$corporateGroup['groupName']][$corporateGroup['idGroupElement']] = $corporateGroup['groupElement'];
						}
					}
					break;
				}

				// supplierGroups -----------------------------------------------------------------
				case 'supplierGroups': {
					foreach (\App\Config\SupplierGroups::getInstance()->getGroupsList() as $supplierGroup) {
						if ($supplierGroup['idGroupElement']) {
							$reportFilters[$filter]['listValues'][$supplierGroup['groupName']][$supplierGroup['idGroupElement']] = $supplierGroup['groupElement'];
						}
					}
					break;
				}
			}
		}

		///////////////////////////////////////////////////////////////////////////////////////////
		// GET THE VALUES OF THE FILTERS //////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////

 		// FROM REQUEST ---------------------------------------------------------------------------
		if ($R['generateReport'] and $filterDet['v']) {
			$reportFilters[$filter]['value']=$R['filters'][$filter];
		}
		// FROM DEFAULTS --------------------------------------------------------------------------
		else {

			switch ($filtersConfig[$filter]['type']) {
				case 'text': case 'checkbox': case 'servicesSimple': case 'select':{
					$reportFilters[$filter]['value']=$reportFilters[$filter]['d'];
					break;
				}
				case 'dateInterval': {
					if ( is_numeric($reportFilters[$filter]['d']['startDate']['duration']) ) {
						if ($reportFilters[$filter]['d']['startDate']['duration']>=0) {
							$strtotime = "-".$reportFilters[$filter]['d']['startDate']['duration'].' '.$reportFilters[$filter]['d']['startDate']['timeUnit'];
						} else {
							$strtotime = "+".abs($reportFilters[$filter]['d']['startDate']['duration']).' '.$reportFilters[$filter]['d']['startDate']['timeUnit'];
						}
						$reportFilters[$filter]['value']['startDate']=\Engine\DateTime::create($strtotime)->formatDateStd();
					}
					if ( is_numeric($reportFilters[$filter]['d']['endDate']['duration']) ) {
						if ($reportFilters[$filter]['d']['endDate']['duration']>=0) {
							$strtotime = "-".$reportFilters[$filter]['d']['endDate']['duration'].' '.$reportFilters[$filter]['d']['endDate']['timeUnit'];
						} else {
							$strtotime = "+".abs($reportFilters[$filter]['d']['endDate']['duration']).' '.$reportFilters[$filter]['d']['endDate']['timeUnit'];
						}
						$reportFilters[$filter]['value']['endDate']=\Engine\DateTime::create($strtotime)->formatDateStd();
					}
					break;
				}
				case 'autocomplete': {
					// get actual data
					$reportFilters[$filter]['value']['value']=$reportFilters[$filter]['d']['value'];
					$reportFilters[$filter]['value']['id']=$reportFilters[$filter]['d']['id'];

					// rewrite with the values chosen from selector
					if ( $filtersConfig[$filter]['data']=='users' or $filtersConfig[$filter]['data']=='offices' or $filtersConfig[$filter]['data']=='officesAndUsers' ) {

						// reset the value
						$reportFilters[$filter]['value']['value']=$reportFilters[$filter]['value']['id']='';

						// currentUser
						if ($reportFilters[$filter]['d']['type']=="currentUser") {
							$getUserDet=getUserDetails($S['auth']['idUser']);
							$reportFilters[$filter]['value']['value']=$getUserDet['lastName'].' '.$getUserDet['firstName'];
							if ($filtersConfig[$filter]['data']=="officesAndUsers") $reportFilters[$filter]['value']['id']=$getUserDet['idOffice'].'|'.$S['auth']['idUser'];
							if ($filtersConfig[$filter]['data']=="users") $reportFilters[$filter]['value']['id']=$S['auth']['idUser'];
						}

						// currentOffice
						if ($reportFilters[$filter]['d']['type']=="currentOffice") {
							$getOfficeDet=getOfficeDetails($S['auth']['idOffice']);
							$reportFilters[$filter]['value']['value']=$getOfficeDet['name'];
							if ($filtersConfig[$filter]['data']=="officesAndUsers") $reportFilters[$filter]['value']['id']=$S['auth']['idOffice'].'|0';
							if ($filtersConfig[$filter]['data']=="offices") $reportFilters[$filter]['value']['id']=$S['auth']['idOffice'];
						}

						// predefined
						if ($reportFilters[$filter]['d']['type']=="custom" and $reportFilters[$filter]['d']['id']) {
							$reportFilters[$filter]['value']['value']=$reportFilters[$filter]['d']['value'];
							$reportFilters[$filter]['value']['id']=$reportFilters[$filter]['d']['id'];
						}
					}
					break;
				}
				case 'selectMultiple': {
					if ($reportFilters[$filter]['d']) {
						foreach ($reportFilters[$filter]['d'] as $option) {
							$reportFilters[$filter]['value']['d'][] = $option;
						}
					}
					break;
				}
			}


		}

	}


}

// GETS A UNIDIMENSIONAL LIST OF SERVICES (FOR servicesSimple FILTERS) ////////////////////////////
if ($OK) {

	// set the flag - to indicate that services must pe processed
	$processServices=false;
	if (is_array($filtersConfig)) {
		foreach($filtersConfig as $filDet) {
			if ($filDet['type']=="servicesSimple") {
				$processServices=true;
				break;
			}
		}
	}

	// if there is a filter that needs the list of services
	if ($processServices) {

		// get srevices configs
		$servCodes=servicesCfg_serviceCodes();
		$servNames=servicesCfg_servicesTranslations();
		// GENERATE LIST OF SERVICES ------------------------------------------------------------------
		$servicesList= [];
		foreach($servCodes as $servCode1=>$servDet) {
			if (is_array($servDet)) {
				$servicesList[$servCode1]= ['name'=>$servNames[$servCode1], 'group'=>true];
				foreach($servDet as $servCode2) {
					$servicesList[$servCode2]= ['name'=>$servNames[$servCode2], 'grouped'=>true];
				}
			} else {
				$servicesList[$servCode1]= ['name'=>$servNames[$servCode1]];
			}
		}
	}

}



///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// MANAGE REPORT CUSTOM FILTERS ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($OK) {

	$reportCustomFilters=unserialize($reportDetails['customFilters']);
	if (!is_array($reportCustomFilters)) $reportCustomFilters= [];

	///////////////////////////////////////////////////////////////////////////////////////////////
	// LOOP THROUGH EVERY FILTER //////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	foreach($reportCustomFilters as $col=>$filterDet) {

		// associate column type for each filter
		if ($columnsConfig[$col]) {
			$reportCustomFilters[$col]['type']=$columnsConfig[$col]['type'];
		} else {
			$reportCustomFilters[$col]['type']='string';
		}
		// associate column name
		if ($reportColumns and $reportColumns[$col] and $reportColumns[$col]['n']) {
			$reportCustomFilters[$col]['columnName']=$reportColumns[$col]['n'];
		} else {
			$reportCustomFilters[$col]['columnName']=$_LANG['reports']['columnNames'][$reportDetails['source']][$col];
		}

		///////////////////////////////////////////////////////////////////////////////////////////
		// GET THE VALUES OF THE FILTERS //////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////

		// FROM REQUEST ///////////////////////////////////////////////////////////////////////////
		if ($R['generateReport'] and $filterDet['v']) {

			// numeric / price / percent ----------------------------------------------------------
			if ($columnsConfig[$col]['type']=='numeric' or $columnsConfig[$col]['type']=='price' or $columnsConfig[$col]['type']=='percent') {
				$reportCustomFilters[$col]['minValue']=$R['customFilters'][$col]['minValue'];
				$reportCustomFilters[$col]['maxValue']=$R['customFilters'][$col]['maxValue'];
				$reportCustomFilters[$col]['value']=$R['customFilters'][$col]['value'];
				if ($R['customFilters'][$col]['operation']) $reportCustomFilters[$col]['operation']=$R['customFilters'][$col]['operation'];
				// set values
				$listOfValues=explodeClean(',', $filterDet['values']);
				$reportCustomFilters[$col]['valuesList']= [];
				foreach($listOfValues as $idx=>$elem) {
					$reportCustomFilters[$col]['valuesList'][]= ['element'=>$elem, 'checked'=>$R['customFilters'][$col]['values'][$idx]];
				}
			}
			// boolean ----------------------------------------------------------------------------
			if ($columnsConfig[$col]['type']=='boolean') {
				$reportCustomFilters[$col]['boolean']=$R['customFilters'][$col]['boolean'];
			}

			// year/yearMonth/date/time -----------------------------------------------------------
			if ($columnsConfig[$col]['type']=='year' or $columnsConfig[$col]['type']=='yearMonth' or $columnsConfig[$col]['type']=='date' or $columnsConfig[$col]['type']=='datetime') {
				$reportCustomFilters[$col]['startDate']=$R['customFilters'][$col]['startDate'];
				$reportCustomFilters[$col]['endDate']=$R['customFilters'][$col]['endDate'];
				$reportCustomFilters[$col]['minAnniv_day']=$R['customFilters'][$col]['minAnniv_day'];
				$reportCustomFilters[$col]['minAnniv_month']=$R['customFilters'][$col]['minAnniv_month'];
				$reportCustomFilters[$col]['maxAnniv_day']=$R['customFilters'][$col]['maxAnniv_day'];
				$reportCustomFilters[$col]['maxAnniv_month']=$R['customFilters'][$col]['maxAnniv_month'];
			}

			// string -----------------------------------------------------------------------------
			if ($columnsConfig[$col]['type'] == 'string' or is_numeric($col)) {
				if ($R['customFilters'][$col]['operation']) $reportCustomFilters[$col]['operation']=$R['customFilters'][$col]['operation'];
				$reportCustomFilters[$col]['text'] = $R['customFilters'][$col]['text'];
				// set values
				$listOfValues = explodeClean(',', $filterDet['values']);
				$reportCustomFilters[$col]['valuesList'] = [];
				foreach($listOfValues as $idx => $elem) {
					$reportCustomFilters[$col]['valuesList'][] = ['element'=>trim($elem), 'checked'=>$R['customFilters'][$col]['values'][$idx]];
				}
			}

		}
		// FROM DEFAULTS //////////////////////////////////////////////////////////////////////////
		else {

			// 'numeric', 'price', 'percent', 'boolean', 'string' - are processed correctly -------

			// 'date', 'yearMonth', 'year', -------------------------------------------------------
			if ($columnsConfig[$col]['type']=='date' or $columnsConfig[$col]['type']=='yearMonth' or $columnsConfig[$col]['type']=='year' or $columnsConfig[$col]['type']=='datetime') {
				if ( is_numeric($reportCustomFilters[$col]['minValue']) ) {
					if ($reportCustomFilters[$col]['minValue']>=0) {
						$strtotime = "-".$reportCustomFilters[$col]['minValue'].' '.$reportCustomFilters[$col]['minMu'];
					} else {
						$strtotime = "+".abs($reportCustomFilters[$col]['minValue']).' '.$reportCustomFilters[$col]['minMu'];
					}
					$reportCustomFilters[$col]['startDate'] = \Engine\DateTime::create($strtotime)->formatDateStd();
				}
				if ( is_numeric($reportCustomFilters[$col]['maxValue']) ) {
					if ($reportCustomFilters[$col]['maxValue']>=0) {
						$strtotime = "-".$reportCustomFilters[$col]['maxValue'].' '.$reportCustomFilters[$col]['maxMu'];
					} else {
						$strtotime = "+".abs($reportCustomFilters[$col]['maxValue']).' '.$reportCustomFilters[$col]['maxMu'];
					}
					$reportCustomFilters[$col]['endDate'] = \Engine\DateTime::create($strtotime)->formatDateStd();
				}
				// if anniversary - calculate start/end day
				if ($reportCustomFilters[$col]['searchMode']=="anniversary") {
					// start date (DD-MM)
					if (is_numeric($reportCustomFilters[$col]['annivStart'])) {
						if ($reportCustomFilters[$col]['annivStart']>0)
							$annivStartTime=strtotime("+ ".abs($reportCustomFilters[$col]['annivStart'])." days");
						else
							$annivStartTime=strtotime($reportCustomFilters[$col]['annivStart']." days");
						$reportCustomFilters[$col]['minAnniv_day']=date("d", $annivStartTime);
						$reportCustomFilters[$col]['minAnniv_month']=date("m", $annivStartTime);
					}
					// end date (DD-MM)
					if (is_numeric($reportCustomFilters[$col]['annivEnd'])) {
						if ($reportCustomFilters[$col]['annivEnd']>0)
							$annivEndTime=strtotime("+ ".abs($reportCustomFilters[$col]['annivEnd'])." days");
						else
							$annivEndTime=strtotime($reportCustomFilters[$col]['annivEnd']." days");
						$reportCustomFilters[$col]['maxAnniv_day']=date("d", $annivEndTime);
						$reportCustomFilters[$col]['maxAnniv_month']=date("m", $annivEndTime);
					}
				}
			}

			// if numeric/price/string - check if 'values' set (for distinct values) --------------
			if ($columnsConfig[$col]['type']=='string' or $columnsConfig[$col]['type']=='price' or $columnsConfig[$col]['type']=='numeric' or is_numeric($col)) {
				$listOfValues = explodeClean(',', $filterDet['values']);
				foreach ($listOfValues as $elem) {
					$reportCustomFilters[$col]['valuesList'][]=['element'=>trim($elem), 'checked'=>1];
				}
				if ( $columnsConfig[$col]['type']=='price' or $columnsConfig[$col]['type']=='numeric' ) {
					if ($filterDet['values']) {
						$reportCustomFilters[$col]['operation']='values';
					}
					elseif ($filterDet['differentFromValues']) {
						$reportCustomFilters[$col]['operation']='differentFromValues';
					}
					elseif ($filterDet['operation']) {
						$reportCustomFilters[$col]['operation']=$filterDet['operation'];
					}
					else {
						$reportCustomFilters[$col]['operation']='inclClosedInterval';
					}
				}

			}


		}

	}

}

