<?php

namespace Modules\Admin\Config\ClientInvoices\Compensation;

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


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$clientInvCompensationCfg = \App\Config\ClientInvoicesCompensate::getInstance();

		$formData = [];
		$formData['cashingDocType'] = $clientInvCompensationCfg->get('cashingDocType');
		$formData['cashingIdUser'] = $clientInvCompensationCfg->get('cashingIdUser');

		return $formData;

	}


	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		try {

			$clientInvCompensationCfg = \App\Config\ClientInvoicesCompensate::getAdmin();
			$clientInvCompensationCfg->set('cashingDocType', $R['cashingDocType']);
			$clientInvCompensationCfg->set('cashingIdUser', $R['cashingIdUser']['id']);
			$clientInvCompensationCfg->save();

			$msgs->addSuccess($this->lang['process']['messages']['success']);
		}

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

}