<?php
namespace Modules\Admin\ExportData\ExportConfig;

class Controller extends \App\Mvc\Controller\Form\WindowAbstract
{


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();
		$activeEntities = \App\Config\ExportData_ActiveEntities::getInstance();

		$formData = [];

		$formData['currencyExchange'] = $activeEntities->get('currencyExchange');

		$formData['legalEntity'] = $activeEntities->get('legalEntity');

		$formData['supplier'] = $activeEntities->get('supplier');

		$formData['supplierInvoice'] = $activeEntities->get('supplierInvoice');

		$formData['balance'] = $activeEntities->get('balance');

		$formData['clientInvoices'] = [];
		foreach (\App\Config\IssueDocuments::getInstance()->getInvoiceDocsList() as $docType=>$docName) {
			if ($activeEntities->get($docType)) { // de ce nu e doar ceva returnat de $activeEntities?
				$formData['clientInvoices'][] = $docType;
			}
		}

		$formData['cashings'] = [];
		foreach(\App\Config\CashingDocuments::getInstance()->getList() as $docType => $docName) {
			$cashingEntity = $activeEntities->get(sprintf('cashing|%s', $docType));
			if ($cashingEntity and $cashingEntity['issue']) {
				$formData['issueCashings'][] = sprintf('cashing|%s', $docType);
			}
			if ($cashingEntity and $cashingEntity['block']) {
				$formData['blockCashings'][] = sprintf('cashing|%s', $docType);
			}

		}

		$formData['payments'] = [];
		foreach(\App\Config\PaymentDocuments::getInstance()->getList() as $docType => $docName) {
			$paymentEntity = $activeEntities->get(sprintf('payment|%s', $docType));
			if ($paymentEntity and $paymentEntity['issue']) {
				$formData['issuePayments'][] =  sprintf('payment|%s', $docType);
			}
			if ($paymentEntity and $paymentEntity['block']) {
				$formData['blockPayments'][] =  sprintf('payment|%s', $docType);
			}
		}

		$formData['cashbookDocuments'] = [];
		foreach ($CFG['cashBook']['documentTypes'] as $cashBookType => $documentsList) {
			foreach ($documentsList as $documentType => $documentDetails) {
				if ($documentDetails['active']) {
					$cashBookEntity = $activeEntities->get(sprintf('cashbookDoc|%s|%s', $cashBookType, $documentType));

					if ($cashBookEntity and $cashBookEntity['issue']) {
						$formData['issueCashbookDocuments'][$cashBookType][] = sprintf('cashbookDoc|%s|%s', $cashBookType, $documentType);
					}
					if ($cashBookEntity and $cashBookEntity['block']) {
						$formData['blockCashbookDocuments'][$cashBookType][] = sprintf('cashbookDoc|%s|%s', $cashBookType, $documentType);
					}
				}
			}
		}

		$formData['issueImportedDocument'] = [];
		foreach(\App\Config\CashingDocuments::getInstance()->getList() as $docType => $docName) {
			$cashingEntity = $activeEntities->get(sprintf('importedDocument|cashing|%s', $docType));
			if ($cashingEntity and $cashingEntity['issue']) {
				$formData['issueImportedDocument']['cashing'][] = sprintf('importedDocument|cashing|%s', $docType);
			}
		}

		foreach(\App\Config\PaymentDocuments::getInstance()->getList() as $docType => $docName) {
			$paymentEntity = $activeEntities->get(sprintf('importedDocument|payment|%s', $docType));
			if ($paymentEntity and $paymentEntity['issue']) {
				$formData['issueImportedDocument']['payment'][] = sprintf('importedDocument|payment|%s', $docType);
			}
		}

		return $formData;
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(array $R, \App\MessagesCollector $msgs)
	{
		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		try {
			$sysVarsObj = \App\Config\ExportData_ActiveEntities::getAdmin();

			$sysVarsObj->set('currencyExchange', $R['currencyExchange'] ? 1 : 0);

			$sysVarsObj->set('legalEntity', $R['legalEntity'] ? 1 : 0);

			$sysVarsObj->set('supplier', $R['supplier'] ? 1 : 0);

			$sysVarsObj->set('supplierInvoice', $R['supplierInvoice'] ? 1 : 0);

			$sysVarsObj->set('balance', $R['balance'] ? 1 : 0);


			// client invoices
			foreach (\App\Config\IssueDocuments::getInstance()->getInvoiceDocsList() as $docType => $docName) {
				$sysVarsObj->set($docType, $R['clientInvoices'][$docType] ? 1 : 0);
			}

			// new cashings serializable
			foreach(\App\Config\CashingDocuments::getInstance()->getList() as $docType => $docName) {
				$var = 'cashing|'.$docType;
				$docOptions = ['block' => 0, 'issue' => 0];
				if ($R['blockCashings'][$var]) {
					$docOptions['block'] = 1;
				}
				if ($R['issueCashings'][$var]) {
					$docOptions['issue'] = 1;
				}
				$sysVarsObj->set($var, $docOptions);
			}

			// new payments serializable
			foreach(\App\Config\PaymentDocuments::getInstance()->getList() as $docType => $docName) {
				$var = 'payment|'.$docType;
				$docOptions = ['block' => 0, 'issue' => 0];
				if ($R['issuePayments'][$var]) {
					$docOptions['issue'] = 1;
				}
				if ($R['blockPayments'][$var]) {
					$docOptions['block'] = 1;
				}
				$sysVarsObj->set($var, $docOptions);
			}


 			// cashbook cashing & payments
			foreach ($CFG['cashBook']['documentTypes'] as $cashBookType => $documentsList) {
				foreach ($documentsList as $documentType => $documentDetails) {
					$var = sprintf('cashbookDoc|%s|%s', $cashBookType, $documentType);
					if ($documentDetails['active']) {

						$docOptions = ['block' => 0, 'issue' => 0];

						if ($R['issueCashbookDocuments'][$cashBookType][$var]) {
							$docOptions['issue'] = 1;
						}

						if ($R['blockCashbookDocuments'][$cashBookType][$var]) {
							$docOptions['block'] = 1;
						}

						$sysVarsObj->set($var, $docOptions);
					}
				}
			}

			// imported documents cashings
			foreach (\App\Config\CashingDocuments::getInstance()->getList() as $docType => $docName) {
				$var = 'importedDocument|cashing|'.$docType;
				$docOptions = ['issue' => 0];
				if ($R['issueImportedDocument']['cashing'][$var]) {
					$docOptions['issue'] = 1;
				}
				$sysVarsObj->set($var, $docOptions);
			}

			// imported documents payments
			foreach (\App\Config\PaymentDocuments::getInstance()->getList() as $docType => $docName) {
				$var = 'importedDocument|payment|'.$docType;
				$docOptions = ['issue' => 0];
				if ($R['issueImportedDocument']['payment'][$var]) {
					$docOptions['issue'] = 1;
				}
				$sysVarsObj->set($var, $docOptions);
			}


			$sysVarsObj->save();
			$msgs->addSuccess($this->lang['index']['messages']['success']);

		} catch (\App\Config\MessageException $e) {
			$msgs->addError(sprintf($this->lang['index']['messages']['error'], $e->getMessage()));
		}
	}

}