<?php
namespace Modules\Orders\Details\GeneralDetails;

/**
 * This section is used for opening a popup that will add/update order details.
 */
class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Window\PaginatedListing\PaginatedListing::initialize() 
	 */
	protected function initialize()
	{
		parent::initialize();
		$this->actionsCfg['index'] =	['checkSection' => false, 'checkOption' => false,];
	}

	/**
	 *
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()  
	 */
	protected function getDefaultFormData($R)
	{
		$formData = [];
		$ordEnt = \Entity\Order::select($R['idOrder']);

		$formData['reservationCode'] = 			$ordEnt->reservationCode;
		$formData['resType'] = 					$ordEnt->resType;
		$formData['servicesType'] = 			$ordEnt->servicesType;
		$formData['flag'] = 					$ordEnt->flag;
		$formData['idMiceProject']['id'] = 		$ordEnt->idMiceProject;
		$formData['description'] =	 			$ordEnt->description;
		$formData['comments'] = 				$ordEnt->comments;
		$formData['idContactPerson']['id'] = 	$ordEnt->idContactPerson;
		$formData['idUser']['id'] = 			$ordEnt->idUser;
		$formData['idOffice']['id'] = 			$ordEnt->idOffice;
		$formData['idDepartment']['id'] = 		$ordEnt->idDepartment;

		return $formData;
	}

	/**
	 * (non-PHPdoc) 
	 * @see \App\Mvc\Controller\Form\WindowAbstract::processFormData()  
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		try {
			\DB::startTransaction();
			$orderEnt = \Entity\Order::update($R['idOrder']);

			$orderEnt->reservationCode = 	$R['reservationCode'];
			$orderEnt->resType = 			$R['resType'];
			$orderEnt->servicesType = 		$R['servicesType'];

			if (\App\UserAccess\CurrentUserAccessFast::getInstance()->getAccessOptionValue('Modules/Orders/Details/OrderFlag')) {
				$orderEnt->flag = 			$R['flag'];
			}

			$orderEnt->idMiceProject = 		$R['idMiceProject']['id'];
			$orderEnt->description = 		$R['description'];
			$orderEnt->comments = 			$R['comments'];
			$orderEnt->idContactPerson = 	$R['idContactPerson']['id'];
			$orderEnt->idDepartment = 		$R['idDepartment']['id'];

			if (\App\UserAccess\CurrentUserAccessFast::getInstance()->getAccessOptionValue('reservationChangeResponsibleUser')) {
				$orderEnt->idUser = 		$R['idUser']['id'];
				$orderEnt->idOffice = 		$R['idOffice']['id'];
			}

			$orderEnt->save();
			\DB::commitTransaction();
			$msgs->addSuccess($this->lang['index']['saveOk']);
		}
		catch (\App\Entity\Exception $e) {
			\DB::rollbackTransaction();
			$msgs->appendMessages($e->getMessagesCollector());
			return false;
		}

		return true;
	}
}