<?php

namespace App\AutoProcessRules;

class InvoiceInsert_Definition extends DefinitionAbstract
{

	/**
	 * List with all available filters
	 * @var array
	 */
	protected $filtersConfig = [
		'serviceCodes',
		'orderExternalSystems',
		'supplierApplyMode',
		'offerSuppliers',
		'offerSupplierCategory',
		'offerCurrencies',
		'orderDate',
		'serviceStartDate',
		'serviceEndDate',
		'ticketDate',
		'offerExtSysOrderDate',
		'orderFlags',
		'serviceRespUser',
		'serviceRespOffice',
		'offerLocation',
		'serviceFormOfPayment',
		'serviceFormOfCashing',
		'miceProjectType',
		'miceProject',
		'serviceRemarks',
		'customers',
		'corporates',
		'corporateGroups',
		'offerOptionDate',
	];

	/**
	 * List with filters for cache
	 * @var array
	 */
	protected $filtersCacheConfig = [
		'serviceCodes' =>				'serviceCode',
		'orderExternalSystems' =>		'orderExternalSystem',
		'offerSuppliers' =>				'supplier',
		'offerSupplierCategory' =>		'supplierCategory',
		'offerCurrencies' =>			'currency',
		'serviceRespUser' =>			'responsibleUser',
		'serviceRespOffice' =>			'responsibleOffice',
		'offerLocation' =>				'location',
		'serviceFormOfPayment' =>		'formOfPayment',
		'serviceFormOfCashing' =>		'formOfCashing',
		'miceProject' =>				'miceProject',
		'serviceRemarks' =>				'remarks',
		'customers' =>					'customer',
		'corporates' =>					'corporate',
	];

	/**
	 * List with all available fields
	 * @var array
	 */
	protected $fieldsConfig = [
		'processGrouping',
		'groupByServiceRemarkVar',
		'advanceComponent',
		'advancePercent',
		'rowsGrouping',
		'invoiceDateMode',
		'forceInvoiceResponsibleFromOrder',
		'invoiceIdResponsibleUser',
		'invoiceCurrency',
		'dueDays',
		'idInvoiceSeries',
		'defaultClientType',
		'forceClientCorporateId',
		'temporaryInvoice',
		'formOfPayment',
		'roundTotal',
		'idAgencyBankAccount',
		'invoiceRowsCurrencyType',
		'invoiceRowsCurrencyDate',
		'invoiceRowsDescriptionScheme',
		'remarks'
	];

	/**
	 * List with fields for cached
	 * Value of array represents type of variable
	 * @var array
	 */
	protected $fieldsCacheConfig = [
		'processGrouping' =>			'processGrouping',
		'advanceComponent' =>			'advanceComponent',
		'rowsGrouping' =>				'rowsGrouping',
		'invoiceIdResponsibleUser' =>	'responsibleUser',
		'invoiceCurrency' =>			'currency',
		'idInvoiceSeries' =>			'docSeries',
		'forceClientCorporateId' =>		'corporate',
		'idAgencyBankAccount' =>		'agencyBankAccount',
	];

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

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

		// check service remark for grouping
		if ($this->fields['processGrouping']=='serviceRemark' and strlen(!$this->fields['groupByServiceRemarkVar'])) {
			$msgs->addWarning('Service remark var must be set for this type of grouping.');
		}
		// check advance details
		if ($this->fields['advanceComponent'] and !in_array($this->fields['advanceComponent'], ['advance', 'finalization'])) {
			$msgs->addWarning('Advance component can only be set as `advance` or `finalization`.');
		}
		if ($this->fields['advanceComponent']=='advance') {
			if (!is_numeric($this->fields['advancePercent']) or $this->fields['advancePercent']<=0 or $this->fields['advancePercent']>100) {
				$msgs->addWarning('Advance percent must be a positive value between 0 and 100.');
			}
		} else {
			if ($this->fields['advancePercent']) {
				$msgs->addWarning('Advance percent must be set only on advance invoicing.');
			}
		}

		// check process grouping
		if ( $this->fields['rowsGrouping'] and !in_array($this->fields['rowsGrouping'], ['vatPercentAndVatQuota', 'serviceType', 'serviceId', 'serviceIdAndRowType']) ) {
			$msgs->addWarning('Invalid rows grouping field.');
		}

		// check invoice date
		if (!$this->fields['invoiceDateMode']) {
			$msgs->addWarning('Invoice date mode not set.');
		} elseif (!in_array($this->fields['invoiceDateMode'], ['currentDate', 'servicesMinStartDate', 'servicesMaxEndDate'])) {
			$msgs->addWarning('Invalid invoice date mode set.');
		}

		// check order responsible user
		if ($this->fields['forceInvoiceResponsibleFromOrder'] and $this->fields['invoiceIdResponsibleUser']) {
			$msgs->addWarning('You cannot set the invoice responsible user and force it from the order at the same time.');
		}
		if (!$this->fields['forceInvoiceResponsibleFromOrder'] and !$this->fields['invoiceIdResponsibleUser']) {
			$msgs->addWarning('No invoice responsible selected.');
		}
		if ($this->fields['invoiceIdResponsibleUser']) {
			$userEnt = \Entity\User::select($this->fields['invoiceIdResponsibleUser']);
			if (!$userEnt->recordExists() or $userEnt->deleted) {
				$msgs->addWarning('Invalid invoice responsible user id.');
			}
		}

		// check invoice currency
		if (!$this->fields['invoiceCurrency']) {
			$msgs->addWarning('No invoice currency selected.');
		}
		elseif ($this->fields['invoiceCurrency'] != 'offerCurrency' and !\App\Config\Currencies::getInstance()->check($this->fields['invoiceCurrency'])) {
			$msgs->addWarning('Invalid invoice currency.');
		}

		// check the dueDays
		if (strlen($this->fields['dueDays']) and (!is_numeric($this->fields['dueDays']) or $this->fields['dueDays']<0)) {
			$msgs->addWarning('The due days is not valid - it should be a positive integer.');
		}

		// id invoice series
		if ($this->fields['idInvoiceSeries']) {
			$ser = \Entity\DocumentSeries::select($this->fields['idInvoiceSeries']);
			if (!$ser->recordExists() or $ser->deleted) {
				$msgs->addWarning('Invalid invoice series.');
			}
		}

		// defaultClientType
		if (!$this->fields['defaultClientType']) {
			$msgs->addWarning('No default client type selected');
		} elseif (!in_array($this->fields['defaultClientType'], ['corporate', 'customer'])) {
			$msgs->addError('The default client type is not correct.');
		}

		// forceClientCorporateId
		if ($this->fields['forceClientCorporateId']) {
			$corpEnt = \Entity\Corporate::select($this->fields['forceClientCorporateId']);
			if (!$corpEnt->recordExists()) {
				$msgs->addWarning('Invalid corporate selected.');
			} elseif ($corpEnt->deleted) {
				$msgs->addWarning('The corporate is deleted and cannot be used.');
			}
		}

		// check form of payment
		if ($this->fields['formOfPayment'] and !\App\Config\FormsOfPayment::getInstance()->check($this->fields['formOfPayment'])) {
			$msgs->addWarning('Invalid form of payment.');
		}

		// check agency bank account
		if ($this->fields['idAgencyBankAccount']) {
			$abaEnt = \Entity\AgencyBankAccount::select($this->fields['idAgencyBankAccount']);
			if (!$abaEnt->recordExists() or $abaEnt->deleted) {
				$msgs->addWarning('Invalid agency bank account.');
			}
		}

		// check currency type
		if ($this->fields['invoiceRowsCurrencyType']) {
			if (!\App\Config\CurrencyTypes::getInstance()->checkActive($this->fields['invoiceRowsCurrencyType'])) {
				$msgs->addError('Invalid currency type selected.');
			}
		}
		// check currency date
		if ($this->fields['invoiceRowsCurrencyDate'] and $this->fields['invoiceRowsCurrencyDate']!='currentDate') {
			$msgs->addError('Invalid currency date selected; it must be either blank (offer currency date) or `currentDate`.');
		}

		// check rows description scheme
		if ($this->fields['invoiceRowsDescriptionScheme']) {
			if (!\App\Config\InvoiceRowsDescription::getInstance()->check($this->fields['invoiceRowsDescriptionScheme'])) {
				$msgs->addError('Invalid rows description scheme selected.');
			}
		}

		// check if temporary is set and rowsGrouping
		if ($this->fields['temporaryInvoice'] and $this->fields['rowsGrouping']) {
			$msgs->addError("You cannot set to group invoice rows and mark invoice as temporary.");
		}

	}


	/**
	 * (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("You 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) {
					if ($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 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 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 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, 'Service ticket date problem: %s');
			}
		}

		// offer 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, 'Offer external system order date problem: %s');
			}
		}

		// check order flags
		if ($this->filters['orderFlags']) {
			if (!is_array($this->filters['orderFlags'])) {
				$msgs->addError('The order flags must be set as a list.');
			} else {
				foreach ($this->filters['orderFlags'] as $of) {
					if (!\App\Config\OrderFlags::getInstance()->check($of)) {
						$msgs->addWarning(sprintf('Invalid order flag: %s', $curr));
					}
				}
			}
		}

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

		// check responsible users
		if ($this->filters['serviceRespOffice']) {
			if (!is_array($this->filters['serviceRespOffice']) ) {
				$msgs->addError('The service responsible offices must be set as a list.');
			} else {
				foreach ($this->filters['serviceRespOffice'] as $idOffice) {
					$officeEntity = \Entity\Office::select($idOffice);
					if (!$officeEntity->recordExists() or $officeEntity->deleted) {
						$msgs->addError(sprintf('Invalid office id - %s.', $idOffice));
					}
				}
				// check for duplicated users
				if (count($this->filters['serviceRespOffice']) != count(array_unique($this->filters['serviceRespOffice']))) {
					$msgs->addWarning('Found some duplicated offices in the list.');
				}
			}
		}

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

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

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

		// check MICE type
		if ($this->filters['miceProjectType']) {
			switch ($this->filters['miceProjectType']) {
				case 'all': case 'none': {
				break;
			}
				case 'custom': {
					if (!$this->filters['miceProject']) {
						$msgs->addWarning('You must select at least one MICE project.');
					}
					break;
				}
				default : {
					$msgs->addError('Invalid MICE project type.');
				}
			}
		}

		// check mice project
		if ($this->filters['miceProject']) {
			if (!is_array($this->filters['miceProject']) ) {
				$msgs->addError('The mice project must be set as a list.');
			} else {
				foreach ($this->filters['miceProject'] as $idMiceProject) {
					$miceProjectEnt = \Entity\MiceProject::select($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 for duplicated projects
				if (count($this->filters['miceProject'])!=count(array_unique($this->filters['miceProject']))) {
					$msgs->addWarning('Found some duplicated MICE projects in the list.');
				}
			}
		}

		// 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 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, 'Service option date problem: %s');
			}
		}
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\AutoProcessRules\DefinitionAbstract::getDescriptionFilters()
	 */
	public function getDescriptionFilters()
	{

		// load langs
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		$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']) {
						$extSystem = [];
						foreach ($filters['orderExternalSystems'] as $code) {
							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':
					if ($filters['offerSuppliers']) {
						$suppArr = [];
						foreach ($filters['offerSuppliers'] as $idSupp) {
							$suppArr[] = \Entity\Supplier::select($idSupp)->alias;
						}
						$descrFilters[] = sprintf("offer suppliers - %s", implode(', ', $suppArr));
					}
				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 'offerCurrencies':
					if ($filters['offerCurrencies']) {
						$descrFilters[] = sprintf("offer currencies - %s", implode(', ', $filters['offerCurrencies']));
					}
				break;

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

				case 'orderFlags':
					if ($filters['orderFlags']) {
						$flags = [];
						foreach ($filters['orderFlags'] as $of) {
							$flags[] = \App\Config\OrderFlags::getInstance()->get($of);
						}
						$descrFilters[] = sprintf( "order flags - %s", implode(', ', $flags) );
					}
				break;

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

				case 'serviceRespOffice':
					if ($filters['serviceRespOffice']) {
						$descrFilters[] = sprintf("service responsible office - %s", \Entity\Office::select($filters['serviceRespOffice'])->name);
					}
				break;

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

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

				case 'serviceFormOfCashing':
					if ($filters['serviceFormOfCashing']) {
						$focList = [];
						foreach ($filters['serviceFormOfCashing'] as $fop) {
							if (!$fop) {
								$focList[] = 'none';
							}
							else {
								$focList[] = $_LANG['offers']['formOfCashing'][$fop];
							}
						}
						$descrFilters[] = sprintf("service form of cashing - %s", $focList);
					}
				break;

				case 'miceProjectType':
					if ($filters['miceProjectType']) {
						$descrFilters[] = sprintf("MICE project type - %s", $filters['miceProjectType']);
					}
				break;

				case 'miceProject':
					if ($filters['miceProject']) {
						$micePrjList = [];
						foreach ($filters['miceProject'] as $idMiceProject) {
							$micePrjList[] = \Entity\MiceProject::select($idMiceProject)->name;
						}
						$descrFilters[] = sprintf("MICE project - %s", implode(', ', $micePrjList));
					}
				break;

				case 'serviceRemarks':
					if ($filters['serviceRemarks']) {
						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;

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

			}
		}

		return implode('; ',$descrFilters);

	}


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

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

		// FIELDS
		foreach ($this->fieldsConfig as $fieldName) {
			switch ($fieldName) {

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

				case 'processGrouping': case 'advanceComponent': case 'advancePercent': case 'invoiceCurrency': case 'dueDays':  case 'invoiceRowsCurrencyType': case 'invoiceRowsDescriptionScheme': case 'remarks':
					if ($fields[$fieldName]) {
						$descrFields[] = $fieldName.' - '.$fields[$fieldName];
					}
				break;

				case 'invoiceRowsCurrencyDate':
					if ($fields[$fieldName]=='currentDate') {
						$descrFields[] = 'invoice rows currency date - current date';
					} else {
						$descrFields[] = 'invoice rows currency date - offer currency date';
					}
				break;

				case 'idInvoiceSeries':
					if ($fields['idInvoiceSeries']) {
						$descrFields[] = sprintf("invoice series - %s", \Entity\DocumentSeries::select($fields['idInvoiceSeries'])->code);
					}
				break;

				case 'defaultClientType':
					if ($fields['defaultClientType']) {
						$descrFields[] = sprintf("default client type - %s", $fields['defaultClientType']);
					}
				break;

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

				case 'formOfPayment':
					if ($fields['formOfPayment']) {
						$descrFields[] = sprintf("form of payment - %s", \App\Config\FormsOfPayment::getInstance()->get($fields['formOfPayment']));
					}
				break;

				case 'roundTotal': case 'temporaryInvoice': case 'forceInvoiceResponsibleFromOrder':
					if ($fields[$fieldName]) {
						$descrFields[] = $fieldName.' - '.$_LANG['yes'];
					}
				break;

				case 'idAgencyBankAccount':
					if ($fields['idAgencyBankAccount']) {
						$descrFields[] = sprintf("agency bank account - %s", \Entity\AgencyBankAccount::select($fields['idAgencyBankAccount'])->account);
					}
				break;
			}
		}

		return implode('; ',$descrFields);

	}

}
