<?php

namespace Modules\Admin\Config\Services\Advanced\Invoice;

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


	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\PopupAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] = [ 'checkSection' => 'Modules/Admin/Config/Services/Advanced', 'checkOption' => false ];
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{

		$formData = \App\Config\Services_Calculations_InvoiceVat::getAdmin()->getCalculationDetails($R['serviceCode'], $R['location'], $R['calculation']);

		$formData['calculation'] 	= $R['calculation'];
		$formData['serviceCode']	= $R['serviceCode'];
		$formData['location'] 		= $R['location'];

		return $formData;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{

		foreach(['price', 'commission', 'discount', 'serviceFee', 'tax', 'penalty', 'advance'] as $row ) {
			$setInvoiceDetails[$row] = $R[$row];
		}

		try {
			\App\Config\Services_Calculations_InvoiceVat::getAdmin()->setCalculationDetails($R['serviceCode'], $R['location'], $R['calculation'], $setInvoiceDetails);
			$msgs->addSuccess($this->lang['index']['messages']['updatedOK']);
			return true;
		}
		catch (\App\Config\MessageException $e) {
			$msgs->addError($e->getMessage());
		}
	}

	protected function preRenderOperations(\App\Mvc\ViewAbstract $view, array $formData)
	{
		$_LANG = \App\Loader_Lang::getInstance()->getAll();

		$servName 	= \App\Config\Services_Basic::getInstance()->getServiceName($formData['serviceCode']);
		$servLoc 	= $_LANG['locations']['countriesCriteria']['long'][$formData['location']];
		$servCalc 	= \App\Config\Services_Calculations::getAdmin()->getServiceLocationCalculationName($formData['serviceCode'], $formData['location'], $formData['calculation']);

		$view->setPageTitle(sprintf($this->lang['index']['pageTitle'], $servName, $servLoc, $servCalc));
	}
}
