<?php

// TODO: in $fieldsCacheConfig si $filtersCacheConfig - ar trebui sa ma oblice sa definesc toate campurile (si sa pun false acolo unde nu vreau cache)

namespace App\AutoProcessRules;

class ImportedServiceProcess_Definition extends DefinitionAbstract
{

	/**
	 * List with all available filters
	 * @var array
	 */
	protected $filtersConfig = [
		'serviceCodes',
		'orderExternalSystems',
		'supplierApplyMode',
		'offerSuppliers',
		'offerSupplierCategory',
		'customers',
		'corporates',
		'corporateGroups',
		'offerCurrencies',
		'serviceUsers', 'serviceOffices', 'serviceUsersSource',
		'orderDate',
		'serviceAddDate',
		'serviceStartDate',
		'serviceEndDate',
		'ticketDate',
		'offerExtSysOrderDate',
		'formOfPayment',
		'formOfCashing',
		'corporateRemarks',
		'serviceRemarks',
		'offerOptionDate',
		'offerCriterion',
		'externalSystemStatus',
		'reissueStatus'
	];

	/**
	 * List with filters for cache
	 * Value of array represents type of variable
	 * @var array
	 */
	protected $filtersCacheConfig = [
		'serviceCodes' =>			'serviceCode',
		'orderExternalSystems' =>	'orderExternalSystem',
		'offerSuppliers' =>			'supplier',
		'offerSupplierCategory' =>	'supplierCategory',
		'customers' =>				'customer',
		'corporates' =>				'corporate',
		'offerCurrencies' =>		'currency',
		'serviceUsers' =>			'user',
		'serviceOffices' =>			'office',
		'formOfPayment' =>			'formOfPayment',
		'formOfCashing' =>			'formOfCashing',
		'corporateRemarks' =>		'remarks',
		'serviceRemarks' =>			'remarks',
		'offerCriterion' =>			'criterion',
	];

	/**
	 * List with all available fields
	 * @var array
	 */
	protected $fieldsConfig = [
		'processGrouping',
		'groupByServiceRemarkVar',
		'attachToPreviousOrder',
		'stopOnPsgMultipleMatch',
		'updateMatchedPsgProfile',
		'forceOwnerCustomerFromImportedOrder',
		'stopOnDuplicateServices',
		'orderFlag',
		'orderIdResponsibleUser',
		'forceOrderUserFromServiceUser',
		'orderType',
		'orderServicesType',
		'idMiceProject',
		'serviceRemarks',
		'autoInsertOwnerCorporate',
		'forceOwnerCorporateId',
		'autoInsertSupplierProductOnService',
		'autoInsertSupplierProductOnOffer',
	];

	/**
	 * List with fields for cached
	 * Value of array represents type of variable
	 * @var array
	 */
	protected $fieldsCacheConfig = [
		'processGrouping' =>			'processGrouping',
		'orderFlag' =>					'orderFlag',
		'orderIdResponsibleUser' =>		'responsibleUser',
		'orderType' =>					'orderType',
		'orderServicesType' =>			'orderServicesType',
		'idMiceProject' =>				'miceProject',
		'forceOwnerCorporateId' =>		'corporate',
	];

	/**
	 * (non-PHPdoc)
	 * @see \App\AutoProcessRules\DefinitionAbstract::filterIsValid()
	 */
	protected function filterIsValid($filter)
	{
		// TODO: toate fara date, supplierApplyMode,
		$allFilters = [
			'serviceCodes',
			'orderExternalSystems',
			'supplierApplyMode',
			'offerSuppliers',
			'offerSupplierCategory',
			'customers',
			'corporates',
			'corporateGroups',
			'offerCurrencies',
			'serviceUsers',
			'serviceOffices',
			'serviceUsersSource',
			'orderDate',
			'serviceAddDate',
			'serviceStartDate',
			'serviceEndDate',
			'ticketDate',
			'offerExtSysOrderDate',
			'formOfPayment',
			'formOfCashing',
			'corporateRemarks',
			'serviceRemarks',
			'offerOptionDate',
			'offerCriterion',
			'externalSystemStatus',
			'reissueStatus'
		];
		return in_array($filter, $allFilters);
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\AutoProcessRules\DefinitionAbstract::checkFields()
	 */
	protected function checkFields(\App\MessagesCollector $msgs)
	{

		// check process grouping
		if ( !in_array($this->fields['processGrouping'], ['one', 'order', 'day', 'day/client', 'serviceRemark', 'all', 'all/client']) ) {
			$msgs->addWarning('Invalid processing grouping field.');
		}

		// if the process grouping is set as "service remark", the remark var must be specified
		if ($this->fields['processGrouping']=='serviceRemark' and !strlen($this->fields['groupByServiceRemarkVar'])) {
			$msgs->addWarning('The service remark var must be set for this type of grouping.');
		}

		// check order flag
		if ($this->fields['orderFlag'] and !\App\Config\OrderFlags::getInstance()->check($this->fields['orderFlag'])) {
			$msgs->addWarning('Invalid order flag.');
		}

		// check order responsible user
		if (!$this->fields['orderIdResponsibleUser']) {
			$msgs->addWarning('No order responsible selected.');
		} else {
			$userEnt = \Entity\User::select($this->fields['orderIdResponsibleUser']);
			if (!$userEnt->recordExists() or $userEnt->deleted) {
				$msgs->addWarning('Invalid order responsible user id.');
			}
		}

		// check order type
		if ($this->fields['orderType'] and !\App\Config\OrderTypes::getInstance()->check($this->fields['orderType'])) {
			$msgs->addWarning('Invalid order type.');
		}
		// check order services type
		if ($this->fields['orderServicesType'] and !\App\Config\OrderServicesTypes::getInstance()->check($this->fields['orderServicesType'])) {
			$msgs->addWarning('Invalid order services type.');
		}

		// check mice project
		if ($this->fields['idMiceProject']) {
			$miceProjectEnt = \Entity\MiceProject::select($this->fields['idMiceProject']);
			if (!$miceProjectEnt->recordExists()) {
				$msgs->addError("The MICE project id is not valid.");
			}
			elseif ($miceProjectEnt->deleted) {
				$msgs->addWarning("The MICE project has been deleted in the meantime and it cannot be selected.");
			}
			elseif ($miceProjectEnt->closed) {
				$msgs->addWarning("The MICE project has been closed in the meantime and it cannot be selected.");
			}
		}

		// check forceOwnerCorporateId
		if ($this->fields['forceOwnerCorporateId']) {
			$corporateEnt = \Entity\Corporate::select($this->fields['forceOwnerCorporateId']);
			if (!$corporateEnt->recordExists()) {
				$msgs->addError("The forced owner corporate id is not valid.");
			}
			elseif ($corporateEnt->deleted) {
				$msgs->addWarning("The forced owner corporate is deleted.");
			}
			if ($this->fields['attachToPreviousOrder']) {
				$msgs->addWarning("You cannot force the owner corporation when processing on previous order.");
			}
		}

	}

	/**
	 * (non-PHPdoc)
	 * @see \App\AutoProcessRules\DefinitionAbstract::checkFilters()
	 */
	protected function checkFilters(\App\MessagesCollector $msgs)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// check service codes
		if ($this->filters['serviceCodes']) {
			if (!is_array($this->filters['serviceCodes']) ) {
				$msgs->addError('The service codes must be set as a list.');
			} else {
				foreach ($this->filters['serviceCodes'] as $sc) {
					if (!\App\Config\Services_Basic::getInstance()->checkService($sc)) {
						$msgs->addError(sprintf('Invalid service code - %s.', $sc));
					}
				}
			}
		}

		// check orderExternalSystems
		if (!$this->filters['orderExternalSystems']) {
			$msgs->addWarning("Must select at least one external system.");
		}
		else {
			if (!is_array($this->filters['orderExternalSystems']) ) {
				$msgs->addError('The external systems must be set as a list.');
			}
			else {
				foreach ($this->filters['orderExternalSystems'] as $idSys) {
					$sysEnt = \Entity\ExtResSystem::select($idSys);
					if (!$sysEnt->recordExists() or $sysEnt->deleted) {
						$msgs->addError(sprintf('Invalid external system id - %s.', $idSys));
					}
				}
			}
		}

		// check suppliers
		if ($this->filters['offerSuppliers']) {
			// check supplier apply mode
			if (!in_array($this->filters['supplierApplyMode'], ['included', 'excluded'])) {
				$msgs->addWarning(sprintf("Invalid supplier apply mode - '%s'", $this->filters['supplierApplyMode']));
			}

			if (!is_array($this->filters['offerSuppliers']) ) {
				$msgs->addError('The offer suppliers must be set as a list.');
			} else {
				foreach ($this->filters['offerSuppliers'] as $idSupp) {
					$suppEnt = \Entity\Supplier::select($idSupp);
					if (!$suppEnt->recordExists() or $suppEnt->deleted) {
						$msgs->addError(sprintf('Invalid supplier id - %s.', $idSupp));
					}
				}
				// check for duplicated suppliers
				if (count($this->filters['offerSuppliers'])!=count(array_unique($this->filters['offerSuppliers']))) {
					$msgs->addWarning('Found some duplicated suppliers in the list.');
				}
			}
		}

		// check supplier category
		if ($this->filters['offerSupplierCategory']) {
			if (!is_array($this->filters['offerSupplierCategory']) ) {
				$msgs->addError('The offer supplier categories must be set as a list.');
			} else {
				foreach ($this->filters['offerSupplierCategory'] as $category) {
					if (!in_array($category, $CFG['suppliers']['possibleCategories'])) {
						$msgs->addError(sprintf('Invalid supplier category - %s.', $category));
					}
				}
				// check for duplicated suppliers
				if (count($this->filters['offerSupplierCategory'])!=count(array_unique($this->filters['offerSupplierCategory']))) {
					$msgs->addWarning('Found some duplicated supplier categories in the list.');
				}
			}
		}

		// check currencies
		if ($this->filters['offerCurrencies']) {
			if (!is_array($this->filters['offerCurrencies'])) {
				$msgs->addError('The offer currencies must be set as a list.');
			} else {
				foreach ($this->filters['offerCurrencies'] as $curr) {
					if (!\App\Config\Currencies::getInstance()->check($curr)) {
						$msgs->addWarning(sprintf('Invalid currency: %s', $curr));
					}
				}
			}
		}

		// check users/offices
		if ($this->filters['serviceUsers']) {
			if (!is_array($this->filters['serviceUsers'])) {
				$msgs->addError('The service users must be set as a list.');
			} else {
				foreach ($this->filters['serviceUsers'] as $idUser) {
					$userEnt = \Entity\User::select($idUser);
					if (!$userEnt->recordExists() or $userEnt->deleted) {
						$msgs->addError(sprintf('Invalid service user id - %s.', $idUser));
					}
				}
				// check for duplicated users
				if (count($this->filters['serviceUsers'])!=count(array_unique($this->filters['serviceUsers']))) {
					$msgs->addWarning('Found some duplicated users in the list.');
				}
			}
		}
		if ($this->filters['serviceOffices']) {
			if (!is_array($this->filters['serviceOffices'])) {
				$msgs->addError('The service offices must be set as a list.');
			} else {
				foreach ($this->filters['serviceOffices'] as $idOffice) {
					$officeEnt = \Entity\Office::select($idOffice);
					if (!$officeEnt->recordExists() or $officeEnt->deleted) {
						$msgs->addError(sprintf('Invalid office id - %s.', $idOffice));
					}
				}
				// check for duplicated offices
				if (count($this->filters['serviceOffices'])!=count(array_unique($this->filters['serviceOffices']))) {
					$msgs->addWarning('Found some duplicated offices in the list.');
				}
			}
		}
		if ($this->filters['serviceUsersSource'] and !in_array($this->filters['serviceUsersSource'], ['any', 'responsible', 'issue', 'book'])) {
			$msgs->addWarning('Invalid user/office source.');
		}

		// check order date
		if ($this->filters['orderDate']) {
			if ( !($this->filters['orderDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for order date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['orderDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Order date problem: %s');
			}
		}

		// check service add date
		if ($this->filters['serviceAddDate']) {
			if ( !($this->filters['serviceAddDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for service add date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['serviceAddDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Service add date problem: %s');
			}
		}

		// check service start date
		if ($this->filters['serviceStartDate']) {
			if ( !($this->filters['serviceStartDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for service checkin date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['serviceStartDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Service check-in date problem: %s');
			}
		}

		// check service end date
		if ($this->filters['serviceEndDate']) {
			if ( !($this->filters['serviceEndDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for service checkout date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['serviceEndDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Service check-out date problem: %s');
			}
		}

		// check ticket date
		if ($this->filters['ticketDate']) {
			if ( !($this->filters['ticketDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for ticket date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['ticketDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Ticket date problem: %s');
			}
		}

		// check extSys order date
		if ($this->filters['offerExtSysOrderDate']) {
			if ( !($this->filters['offerExtSysOrderDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for offer external system order date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['offerExtSysOrderDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Ext. system order date problem: %s');
			}
		}

		// check option date
		if ($this->filters['offerOptionDate']) {
			if ( !($this->filters['offerOptionDate'] instanceof \App\Utils\SmartDateInterval) ) {
				$msgs->addError('Invalid smart date interval for option date.');
			} else {
				$msgsTemp = new \App\MessagesCollector();
				$this->filters['offerOptionDate']->check($msgsTemp);
				$msgs->appendMessages($msgsTemp, 'Option date problem: %s');
			}
		}

		// check form of payment
		if ($this->filters['formOfPayment']) {
			if (!is_array($this->filters['formOfPayment']) ) {
				$msgs->addError('The form of payments must be set as a list.');
			} else {
				$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
				foreach ($this->filters['formOfPayment'] as $fop) {
					if ($fop != '' and !in_array($fop, $CFG['offers']['formOfPaymentList'])) {
						$msgs->addError(sprintf('Invalid form of payment - %s.', $fop));
					}
				}
			}
		}

		// check form of payment
		if ($this->filters['formOfCashing']) {
			if (!is_array($this->filters['formOfCashing']) ) {
				$msgs->addError('The form of cashings must be set as a list.');
			} else {
				$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
				foreach ($this->filters['formOfCashing'] as $foc) {
					if ($foc != '' and !in_array($foc, $CFG['offers']['formOfCashingList'])) {
						$msgs->addError(sprintf('Invalid form of payment - %s.', $foc));
					}
				}
			}
		}

		// check corporate remark(s)
		if ($this->filters['corporateRemarks']) {
			if (!is_array($this->filters['corporateRemarks'])) {
				$msgs->addError('The corporate remark(s) must be set as a list.');
			} else {
				foreach($this->filters['corporateRemarks'] as $idx=>$rmDet) {
					if (!in_array($rmDet['op'], ['anyValue', 'noValue', 'equal', 'notEqual'])) {
						$msgs->addError(sprintf('The operation for the corporate remark #%s is not valid.', $idx+1));
					}
					if (!$rmDet['var']) {
						$msgs->addWarning(sprintf('The variable name for the corporate remark #%s is not set.', $idx+1));
					}
					if (isset($rmDet['val']) and !is_array($rmDet['val'])) {
						$msgs->addError(sprintf('The variable value for the corporate remark #%s is not valid.', $idx+1));
					}
					if ( ($rmDet['op']=='equal' or $rmDet['op']=='notEqual') and !$rmDet['val']) {
						$msgs->addWarning(sprintf('The variable value for the corporate remark #%s is not set.', $idx+1));
					}
				}
			}
		}

		// check service remark(s)
		if ($this->filters['serviceRemarks']) {
			if (!is_array($this->filters['serviceRemarks'])) {
				$msgs->addError('The service remark(s) must be set as a list.');
			} else {
				foreach($this->filters['serviceRemarks'] as $idx=>$rmDet) {
					if (!in_array($rmDet['op'], ['anyValue', 'noValue', 'equal', 'notEqual'])) {
						$msgs->addError(sprintf('The operation for the service remark #%s is not valid.', $idx+1));
					}
					if (!$rmDet['var']) {
						$msgs->addWarning(sprintf('The variable name for the service remark #%s is not set.', $idx+1));
					}
					if (isset($rmDet['val']) and !is_array($rmDet['val'])) {
						$msgs->addError(sprintf('The variable value for the service remark #%s is not valid.', $idx+1));
					}
					if ( ($rmDet['op']=='equal' or $rmDet['op']=='notEqual') and !$rmDet['val']) {
						$msgs->addWarning(sprintf('The variable value for the service remark #%s is not set.', $idx+1));
					}
				}
			}
		}

		// check offer criterion
		if ($this->filters['offerCriterion']) {
			if (!is_array($this->filters['offerCriterion']) ) {
				$msgs->addError('The offer criteria must be set as a list.');
			} else {
				foreach($this->filters['offerCriterion'] as $criterion) {
					if (!in_array($criterion, $CFG['countriesCriteria'])) {
						$msgs->addError(sprintf('Invalid offer criterion - %s', $criterion));
					}
				}
			}
		}

		// external system status
		if ($this->filters['externalSystemStatus']) {
			if (!is_array($this->filters['externalSystemStatus']) ) {
				$msgs->addError('The external system status must be set as a list.');
			} else {
				foreach($this->filters['externalSystemStatus'] as $externalSystemStatus) {
					if (!in_array($externalSystemStatus, ['confirmed', 'request', 'refunded'])) {
						$msgs->addError(sprintf('Invalid external system status - %s', $externalSystemStatus));
					}
				}
			}
		}


		// check reissue status
		if ($this->filters['reissueStatus']) {
			if (!in_array($this->filters['reissueStatus'], ['nonReissue', 'reissue'])) {
				$msgs->addError(sprintf('Invalid reissue status - %s', $this->filters['reissueStatus']));
			}
		}

	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\AutoProcessRules\DefinitionAbstract::getDescriptionFilters()
	 */
	public function getDescriptionFilters()
	{
		// lang
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();


		// filters
		$filters = $this->getFilters();
		$descrFilters = [];

		// FILTERS
		foreach ($this->filtersConfig as $filterName) {
			switch ($filterName) {
				case 'serviceCodes':
					$servCodeArray = [];
					if ($filters['serviceCodes']) {
						foreach ($filters['serviceCodes'] as $code) {
							$servCodeArray[] = \App\Config\Services_Basic::getInstance()->getServiceName($code);
						}
						$descrFilters[] =  sprintf("service codes - %s", implode(', ', $servCodeArray));
					}
				break;

				case 'orderExternalSystems':
					if ($filters['orderExternalSystems']) {
						foreach ($filters['orderExternalSystems'] as $code) {
							$extSystem = [];
							if ($code) {
								$extSystem[] = \Entity\ExtResSystem::select($code)->name;
							}
							else {
								$extSystem[] = 'MANUAL';
							}
						}
						$descrFilters[] = sprintf("order external systems - %s", implode(', ', $extSystem));
					}
				break;

				case 'supplierApplyMode':
					if ($filters['supplierApplyMode']) {
						$descrFilters[] = sprintf("supplier apply mode - %s", $filters['supplierApplyMode']);
					}
				break;

				case 'offerSuppliers':
					$suppArray = [];
					if ($filters['offerSuppliers']) {
						foreach ($filters['offerSuppliers'] as $idSupp) {
							$suppArray[] = \Entity\Supplier::select($idSupp)->alias;
						}
						$descrFilters[] = sprintf("offer suppliers - %s", implode(', ', $suppArray));
					}
				break;

				case 'offerSupplierCategory':
					$categoryArray = [];
					if ($filters['offerSupplierCategory']) {
						foreach ($filters['offerSupplierCategory'] as $category) {
							$categoryArray[] = ($_LANG['generalData']['suppliers']['possibleCategories'][$category]?:$category);
						}
						$descrFilters[] = sprintf("offer supplier category - %s", implode(', ', $categoryArray));
					}
				break;

				case 'serviceUsers':
					if ($filters['serviceUsers']) {
						$list = [];
						foreach ($filters['serviceUsers'] as $idRow) {
							$list[] = \Entity\User::select($idRow)->name;
						}
						$descrFilters[] = sprintf("service users - %s", implode(',', $list));
					}
				break;

				case 'serviceOffices':
					if ($filters['serviceOffices']) {
						$list = [];
						foreach ($filters['serviceOffices'] as $idRow) {
							$list[] = \Entity\Office::select($idRow)->name;
						}
						$descrFilters[] = sprintf("service offices - %s", implode(',', $list));
					}
				break;

				case 'serviceUsersSource':
					if ($filters['serviceUsersSource']) {
						$descrFilters[] = sprintf("service user source - %s", $filters['serviceUsersSource']);
					}
				break;

				case 'offerCurrencies':
					if ($filters['offerCurrencies']) {
						$descrFilters[] = sprintf("offer currencies - %s", implode(', ', $filters['offerCurrencies']));
					}
				break;

				case 'orderDate': case 'serviceAddDate': case 'serviceStartDate': case 'serviceEndDate': case 'ticketDate': case 'offerExtSysOrderDate': case 'offerOptionDate':
					if ($filters[$filterName] ){
						if ($filters[$filterName]->getReadableDescription()) {
							$descrFilters[] = $filterName . ' - '  .$filters[$filterName]->getReadableDescription();
						}
					}
				break;

				case 'formOfPayment':
					if ($filters['formOfPayment']) {
						$fopArray = [];
						foreach ($filters['formOfPayment'] as $fop) {
							if (!$fop) {
								$fopArray[] = 'none';
							}
							else {
								$fopArray[] = $_LANG['offers']['formOfPayment'][$fop];
							}
						}
						$descrFilters[] = sprintf("forms of payment - %s", implode(', ', $fopArray));
					}
				break;

				case 'formOfCashing':
					if ($filters['formOfCashing']) {
						$focArray = [];
						foreach ($filters['formOfCashing'] as $foc) {
							if (!$foc) {
								$focArray[] = 'none';
							}
							else {
								$focArray[] = $_LANG['offers']['formOfCashing'][$foc];
							}
						}
						$descrFilters[] = sprintf("forms of cashing - %s", implode(', ', $focArray));
					}
				break;

				case 'corporateRemarks':
					if ($filters['corporateRemarks']) {
						$remark = null;
						foreach ($filters['corporateRemarks'] as $code=>$value) {
							if ($value['op'] == 'equal') {
								$remark = sprintf('%s is equal to %s', $value['var'], implode(', ', $value['val']));
							} elseif ($value['op'] == 'notEqual') {
								$remark = sprintf('%s is not equal to %s', $value['var'], implode(', ', $value['val']));
							}
							else {
								$remark = sprintf('%s %s', $value['var'], $value['op']);
							}
						}
						$descrFilters[] = sprintf("corporation remarks - %s", $remark);
					}
				break;

				case 'serviceRemarks':
					if ($filters['serviceRemarks']) {
						$remark = null;
						foreach ($filters['serviceRemarks'] as $code=>$value) {
							if ($value['op'] == 'equal') {
								$remark = sprintf('%s is equal to %s', $value['var'], implode(', ', $value['val']));
							} elseif ($value['op'] == 'notEqual') {
								$remark = sprintf('%s is not equal to %s', $value['var'], implode(', ', $value['val']));
							}
							else {
								$remark = sprintf('%s %s', $value['var'], $value['op']);
							}
						}
						$descrFilters[] = sprintf("service remarks - %s", $remark);
					}
				break;

				case 'offerCriterion':
					if ($filters['offerCriterion']) {
						$criteria = [];
						foreach ($filters['offerCriterion'] as $criterion) {
							$criteria[] = $_LANG['locations']['countriesCriteria']['long'][$criterion];
						}
						$descrFilters[] = sprintf("offer criteria - %s", implode(', ', $criteria));
					}
				break;

				case 'externalSystemStatus':
					if ($filters['externalSystemStatus']) {
						$list = [];
						foreach ($filters['externalSystemStatus'] as $externalSystemStatus) {
							$list[] = $_LANG['importedService']['systemStatusName'][$externalSystemStatus];
						}
						$descrFilters[] = sprintf("external system status - %s", implode(',', $list));
					}
				break;

				default: {
					$this->populateDescriptionCommonFilters($descrFilters, $filterName);
				}

			}
		}

		return implode('; ',$descrFilters);

	}



	/**
	 *
	 * {@inheritDoc}
	 * @see \App\AutoProcessRules\DefinitionAbstract::getDescriptionFields()
	 */
	public function getDescriptionFields()
	{
		// lang
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		// fields
		$fields = $this->getFields();
		$descrFields = [];

		// FIELDS
		foreach ($this->fieldsConfig as $fieldName) {
			switch ($fieldName) {
				case 'processGrouping': case 'orderFlag':
					if ($fields[$fieldName]) {
						$descrFields[] = $fieldName.' - '.$fields[$fieldName];
					}
				break;

				case 'attachToPreviousOrder': case 'stopOnPsgMultipleMatch': case 'updateMatchedPsgProfile': case 'forceOwnerCustomerFromImportedOrder': case 'stopOnDuplicateServices': case 'forceOrderUserFromServiceUser': case 'autoInsertOwnerCorporate':
					if ($fields[$fieldName]) {
						$descrFields[] = $fieldName.' - '.$_LANG['yes'];
					}
				break;

				case 'orderIdResponsibleUser':
					if ($fields['invoiceRespUser']) {
						$descrFields[] = sprintf('order id responsible user - %s', \Entity\User::select($fields['invoiceRespUser'])->name);
					}
				break;

				case 'orderType':
					if ($fields['orderType']) {
						$descrFields[] = sprintf('order type - %s', \App\Config\OrderTypes::getInstance()->get($fields['orderType']));
					}
				break;

				case 'orderServicesType':
					if ($fields['orderServicesType']) {
						$descrFields[] = sprintf('order services type - %s', \App\Config\OrderServicesTypes::getInstance()->get($fields['orderServicesType']));
					}
				break;

				case 'idMiceProject':
					if ($fields['idMiceProject']) {
						$descrFields[] = sprintf('MICE project - %s', \Entity\MiceProject::select($fields['idMiceProject'])->name);
					}
				break;

				case 'forceOwnerCorporateId':
					if ($fields['forceOwnerCorporateId']) {
						$descrFields[] = sprintf('Forces owner corporate - %s', \Entity\Corporate::select($fields['forceOwnerCorporateId'])->alias);
					}
				break;

			}
		}

		return implode('; ',$descrFields);

	}



}

