<?php


///////////////////////////////////////////////////////////////////////////////////////////////////
// CHECK IF REPORT SOURCE FOUND ///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

$OK=1;

// NO REPORT SOURCE ///////////////////////////////////////////////////////////////////////////////
if ( !$reportSource ) {
	$showMessage2['type']="MSG_WARNING";
	$showMessage2['message']=$_LANG['reportsManagement']['admin']['filters']['noSource'];
	$OK=0;
}

// TRY TO LOAD THE FILTERS FROM CUSTOM CONFIGS ////////////////////////////////////////////////////
if ($OK) {

	if ($reportSource) {
		// load the filters configs
		loadReportConfigs($reportSource, 'filters');
		// load the default filter names
		@include($MODULE_REL_PATH."_lang/".$S['global']['lang']."/_customLoad/reports/".$reportSource."/filters.lang.php");
	}

	// check if filters retrieved
	if (!$CFG['reportsManagement']['adminReport']['filtersList'][$reportSource]) {
		$showMessage2['type']="MSG_WARNING";
		$showMessage2['message']=$_LANG['reportsManagement']['admin']['filters']['noDefinedFilters'];
		$OK=0;
	}

}

///////////////////////////////////////////////////////////////////////////////////////////////////
// GENERATE LIST OF FILTERS ///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($OK) {

	$filtersList=$CFG['reportsManagement']['adminReport']['filtersList'][$reportSource];
	foreach($filtersList as $filter=>$filterDet) {

		// delete disabled filters
		if ($filterDet['disabled']) {
			unset($filtersList[$filter]);
			continue;
		}

		// associate filter name
		$filtersList[$filter]['originalName']=$_LANG['reports']['filterNames'][$reportSource][$filter];

		// visible flag
		$filtersList[$filter]['visible']=$S['adminReport']['filters'][$filter]['v'];

		// default value
		$filtersList[$filter]['default']=$S['adminReport']['filters'][$filter]['d'];

		// IF FILTER IS CHECKBOX - GENERATE ASSOCIATIVE LIST OF OPTIONS (CODE=>TRANSLATION) ///////
		if ($filterDet['type']=="checkbox") {

			// loop through every data types
			switch($filterDet['data']) {

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

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

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

				// clientTypeWithBlank ------------------------------------------------------------
				case 'clientTypeWithBlank': {
					$filtersList[$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': {
					$filtersList[$filter]['listValues']= ['1'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['locked'], '0'=>$_LANG['reportsManagement']['admin']['filters']['checkboxes']['notLocked']];
					break;
				}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

				// taskStatus ---------------------------------------------------------------------
				case 'taskStatus': {
					$filtersList[$filter]['listValues']['notDistributed']=$_LANG['tasks']['statusName']['notDistributed'];
					$filtersList[$filter]['listValues']['distributed']=$_LANG['tasks']['statusName']['distributed'];
					$filtersList[$filter]['listValues']['refused']=$_LANG['tasks']['statusName']['refused'];
					$filtersList[$filter]['listValues']['pending']=$_LANG['tasks']['statusName']['pending'];
					$filtersList[$filter]['listValues']['inProgress']=$_LANG['tasks']['statusName']['inProgress'];
					$filtersList[$filter]['listValues']['onHold']=$_LANG['tasks']['statusName']['onHold'];
					$filtersList[$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) {
							$filtersList[$filter]['listValues'][$docType] = $docName.' (<i>'.$_LANG['documentPivotName'][$pivot].'</i>)';
						}
					}
					break;
				}

				// imported document TINA status --------------------------------------------------
				case 'impDocStatus': {
					$filtersList[$filter]['listValues']['new']=$_LANG['importedDocuments']['statusName']['new'];
					$filtersList[$filter]['listValues']['partial']=$_LANG['importedDocuments']['statusName']['partial'];
					$filtersList[$filter]['listValues']['stopped']=$_LANG['importedDocuments']['statusName']['stopped'];
					$filtersList[$filter]['listValues']['processed']=$_LANG['importedDocuments']['statusName']['processed'];
					$filtersList[$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) {
						$filtersList[$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) {
						$filtersList[$filter]['listValues'][$rec['id']] = $rec['name'];
					}

					// add manual system
					if ($filterDet['data']=='impOrderExtSystemWithBlank') {
						$filtersList[$filter]['listValues']['0'] = sprintf('<i>%s</i>', $_LANG['reportsManagement']['admin']['filters']['checkboxes']['noExtSystem']);
					}

					break;
				}

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

					break;
				}

				// imported service external system status ----------------------------------------
				case 'impServExtStatus': {
					$filtersList[$filter]['listValues']['added']=$_LANG['importedService']['systemStatusName']['added'];
					$filtersList[$filter]['listValues']['request']=$_LANG['importedService']['systemStatusName']['request'];
					$filtersList[$filter]['listValues']['confirmed']=$_LANG['importedService']['systemStatusName']['confirmed'];
					$filtersList[$filter]['listValues']['cancelled']=$_LANG['importedService']['systemStatusName']['cancelled'];
					$filtersList[$filter]['listValues']['deleted']=$_LANG['importedService']['systemStatusName']['deleted'];
					$filtersList[$filter]['listValues']['refunded']=$_LANG['importedService']['systemStatusName']['refunded'];
					break;
				}

			}

		}

		// IF FILTER IS SELECT - GENERATE ASSOCIATIVE LIST OF OPTIONS (CODE=>TRANSLATION) /////////
		if ($filterDet['type']=="select") {

			// loop through every data types
			switch($filterDet['data']) {
				// invRowsGroupType ---------------------------------------------------------------
				case 'invRowsGroupType': {
					$filtersList[$filter]['listValues']['service']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['service'];
					$filtersList[$filter]['listValues']['issueRefund']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['issueRefund'];
					$filtersList[$filter]['listValues']['issueRefundReversed']=$_LANG['reportsManagement']['admin']['filters']['checkboxes']['invoicedServices']['issueRefundReversed'];
					break;
				}

				// docGroupType -------------------------------------------------------------------
				case 'docGroupType': {
					$filtersList[$filter]['listValues']['noGroup']=$_LANG['reportsManagement']['admin']['filters']['select']['docGroupType']['noGroup'];
					$filtersList[$filter]['listValues']['groupByPivotDoc']=$_LANG['reportsManagement']['admin']['filters']['select']['docGroupType']['groupByPivotDoc'];
					break;
				}
			}
		}


		// IF FILTER IS SELECT MULTIPLE - GENERATE ASSOCIATIVE LIST OF OPTIONS GROUP BY OPTGROUP //
		if ($filterDet['type']=="selectMultiple") {
			// loop through every data types
			switch($filterDet['data']) {
				// corporateGroups ----------------------------------------------------------------
				case 'corporateGroups': {
					foreach (\App\Config\CorporateGroups::getInstance()->getGroupsList() as $corporateGroup) {
						if ($corporateGroup['idGroupElement']) {
							$filtersList[$filter]['listValues'][$corporateGroup['groupName']][$corporateGroup['idGroupElement']] = $corporateGroup['groupElement'];
						}
					}
					break;
				}
				// supplierGroups -----------------------------------------------------------------
				case 'supplierGroups': {
					foreach (\App\Config\SupplierGroups::getInstance()->getGroupsList() as $supplierGroup) {
						if ($supplierGroup['idGroupElement']) {
							$filtersList[$filter]['listValues'][$supplierGroup['groupName']][$supplierGroup['idGroupElement']] = $supplierGroup['groupElement'];
						}
					}
					break;
				}
			}
		}

	}

}


///////////////////////////////////////////////////////////////////////////////////////////////////
// GETS A UNIDIMENSIONAL LIST OF SERVICES /////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// get srevices configs
$servCodes=servicesCfg_serviceCodes();
$servNames=servicesCfg_servicesTranslations();

// GENERATE LIST OF SERVICES //////////////////////////////////////////////////////////////////////
$servicesList= [];
if (is_array($servCodes)) {
	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]];
		}
	}
}

?>