<?php

namespace Modules\Orders\ServiceOffers\RedistributeCommission;

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

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		$formData = $R;
		$formData['calculationMode'] = 'commissionPercent';

		$formData['supplier']['id'] = \Entity\Service::select($R['idService'])->chosenOffer->idSupplier;

		return $formData;
	}


	protected function processFormData(array $R, \App\MessagesCollector $msgs)
	{

		$serviceEnt = \Entity\Service::select($R['idService']);

		// determine $redistributeCommissionPercent based on calculation mode /////////////////////
		$redistributeCommissionPercent = null;
		switch ($R['calculationMode']) {

			case 'commissionPercent':
				if (strlen($R['commissionPercent'])) {
					if (!\Engine\Validator::set($R['commissionPercent'])->isFloat()) {
						$msgs->addWarning($this->lang['index']['msgs']['warn_invalidValue']);
						return false;
					} else {
						$redistributeCommissionPercent = $R['commissionPercent']/100;
					}
				}
			break;
			case 'commissionValue':
				if (strlen($R['commissionValue'])) {
					if (!\Engine\Validator::set($R['commissionValue'])->isFloat()) {
						$msgs->addWarning($this->lang['index']['msgs']['warn_invalidValue']);
						return false;
					} else {
						$originalServicePrice = $serviceEnt->chosenOffer->commissionWorkValue + $serviceEnt->chosenOffer->commissionWorkVat + $serviceEnt->chosenOffer->supplierWorkValue + $serviceEnt->chosenOffer->supplierWorkVat;
						$redistributeCommissionPercent = $R['commissionValue'] / $originalServicePrice;
					}
				}
			break;
			case 'supplierPriceValue':
				if (strlen($R['supplierPriceValue'])) {
					if (!\Engine\Validator::set($R['supplierPriceValue'])->isFloat()) {
						$msgs->addWarning($this->lang['index']['msgs']['warn_invalidValue']);
						return false;
					} else {
						$originalServicePrice = $serviceEnt->chosenOffer->commissionWorkValue + $serviceEnt->chosenOffer->commissionWorkVat + $serviceEnt->chosenOffer->supplierWorkValue + $serviceEnt->chosenOffer->supplierWorkVat;
						$redistributeCommissionPercent = ($originalServicePrice - $R['supplierPriceValue']) / $originalServicePrice;
					}
				}
			break;
			default:
				$msgs->addWarning($this->lang['index']['msgs']['warn_noCalcMode']);
				return false;
		}

		try {

			\DB::startTransaction();

			$offerUpd = \Entity\Offer::update($serviceEnt->chosenOfferId);
			if ($redistributeCommissionPercent!==null) {
				$offerUpd->redistributeCommissionPercent = $redistributeCommissionPercent;
			}
			$offerUpd->idSupplier = $R['supplier']['id'];
			$offerUpd->save();

			\DB::commitTransaction();

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

		} catch (\App\Entity\Exception $e) {
			\DB::rollbackTransaction();
			$msgs->appendMessages( $e->getMessagesCollector() );
			return false;
		}

		return true; // closes the popup
	}

}
