<?php
namespace App\Config;

class ImportedDocsCompensate extends \App\Config\SystemVars
{
	protected $dbVarsPrefix = 'ImportedDocuments_Compensate_';

	protected $dbVarsList = [
		// Service details
		'service_code',
		'offer_idSupplier',
		'invoiceInsert_idSeries',
		'invoiceInsert_formOfPayment',
		'invoiceInsert_idBankAccount',
		'cancel_active',
		'cancel_serviceReason',
		'invoiceIssue_active',
		'invoiceIssue_docType',
		'invoiceIssue_idTemplate',
		'invoiceIssue_idSeries',
		'creditNoteIssue_active',
		'creditNoteIssue_docType',
		'creditNoteIssue_idTemplate',
		'creditNoteIssue_idSeries',
	];

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Config\PluginVarsAbstract::save_check()
	 */
	protected function save_check()
	{
		parent::save_check();

		if ($this->itemsList) {
			foreach($this->itemsList as $var=>$value) {
				switch ($var) {

					// Service details
					case 'invoiceInsert_formOfPayment':
						if ($value && !\App\Config\FormsOfPayment::getInstance()->check($value)) {
							$this->addErrorMessage("The form of payment must be valid");
						}
					break;

					case 'service_code':
						if (!\App\Config\Services_Basic::getInstance()->checkService($value)) {
							$this->addErrorMessage("The service code must be valid");
						}
					break;

					case 'offer_idSupplier':
						$supplierEnt = \Entity\Supplier::select($value);
						if (!$supplierEnt->recordExists() || $supplierEnt->deleted) {
							$this->addErrorMessage("The supplier must be valid");
						}
					break;

					case 'invoiceInsert_idSeries':
						$docSeriesEnt = \Entity\DocumentSeries::select($value);
						if (!$docSeriesEnt->recordExists() || $docSeriesEnt->deleted) {
							$this->addErrorMessage("The document serie must be valid");
						}
					break;

					case 'invoiceInsert_idBankAccount':
						if ($value) {
							$bankAccoutEnt = \Entity\AgencyBankAccount::select($value);
							if (!$bankAccoutEnt->recordExists() || $bankAccoutEnt->deleted) {
								$this->addErrorMessage("The agency bank account must be valid");
							}
						}
					break;

					case 'invoiceIssue_docType':
						if ($value && !array_key_exists($value, \App\Config\IssueDocuments::getInstance()->getInvoiceDocsList())) {
							$this->addErrorMessage("The document type must be valid");
						}
					break;

					case 'creditNoteIssue_docType':
						if ($value && !array_key_exists($value, \App\Config\IssueDocuments::getInstance()->getInvoiceDocsList())) {
							$this->addErrorMessage("The document type must be valid");
						}
					break;

					case 'invoiceIssue_idTemplate':
						if ($value) {
							$docTemplate = \Entity\DocumentTemplate::select($value);
							if (!$docTemplate->recordExists() || $docTemplate->deleted || $docTemplate->docType != $this->itemsList['invoiceIssue_docType']) {
								$this->addErrorMessage("The invoice issue document type must be valid");
							}
						}
					break;

					case 'creditNoteIssue_idTemplate':
						if ($value) {
							$docTemplate = \Entity\DocumentTemplate::select($value);
							if (!$docTemplate->recordExists() || $docTemplate->deleted || $docTemplate->docType != $this->itemsList['creditNoteIssue_docType']) {
								$this->addErrorMessage("The credit note issue document type must be valid");
							}
						}
					break;

					case 'invoiceIssue_idSeries':
						if ($value) {
							$docSeries = \Entity\DocumentSeries::select($value);
							if (!$docSeries->recordExists() || $docSeries->deleted || !in_array($this->itemsList['invoiceIssue_docType'], $docSeries->documentTypes)) {
								$this->addErrorMessage("The invoice document serie must be valid");
							}
						}
					break;

					case 'creditNoteIssue_idSeries':
						if ($value) {
							$docSeries = \Entity\DocumentSeries::select($value);
							if (!$docSeries->recordExists() || $docSeries->deleted || !in_array($this->itemsList['creditNoteIssue_docType'], $docSeries->documentTypes)) {
								$this->addErrorMessage("The credit note document serie must be valid");
							}
						}
					break;

					case 'invoiceIssue_active': case 'cancel_active': case 'creditNoteIssue_active':
					break;

					case 'cancel_serviceReason':
						if ($value) {
							$entityCfg = \Entity\Service::loadCFG();
							if (!in_array($value, $entityCfg['_special']['serviceCancelReasons'])) {
								$this->addErrorMessage("The service cancellation reason must be valid");
							}
						}
					break;

					default:
						$this->addErrorMessage("The var `{$var}` is invalid");
				}
			}

		}

	}
}
