<?php

namespace Modules\Invoices\InvoicesList\Cancel;

/**
 * This section cancels one or more invoices.
 */
class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\WindowAbstract::getDefaultFormData()
	 */
	protected function getDefaultFormData($R)
	{
		return $R;
	}

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

		// check that at least one cashing document id was sent
		if (!is_array($R['id']) or !$R['id']) {
			$msgs->addError($this->lang['index']['msgs']['noInvIds']);
			return false;
		}

		// check that the void reason is filled in if the field is mandatory
		$invoiceCashingEntCfg = \Entity\InvoiceCashing::loadCFG();
		if ($invoiceCashingEntCfg['_special']['voidReasonMandatory'] and !$R['invoiceVoidReason']) {
			foreach ($R['id'] as $idInvoice) {
				if (\Entity\Invoice::select($idInvoice)->issueTime) {
					$msgs->addWarning($this->lang['index']['msgs']['noVoidReason']);
					return false;
				}
			}

		}

		// returns the listing selection for the invoices list
		$invoicesListingSelection = new \App\Mvc\View\Listing\Members\Selection('Modules/Invoices/InvoicesList');

		// VOIDS ALL SELECTED INVOICES ------------------------------------------------------------
		$OK = true;
		foreach ($R['id'] as $idInvoice) {

			try {

				\DB::startTransaction();

				$invoiceEnt = \Entity\Invoice::selectForUpdate($idInvoice);

				// VOID CASHING DOCUMENTS .........................................................
				if ($R['voidCashingDocs']) {
					foreach ($invoiceEnt->cashingDocumentsList as $idDocumentCashing => $documentCashingDetails) {
						if (!$documentCashingDetails['voidTime']) {
							$invoiceCashingEntity = \Entity\InvoiceCashing::cancel($idDocumentCashing);
							$invoiceCashingEntity->voidReason = $R['cashingVoidReason'];
							$invoiceCashingEntity->save();
						}
					}
				}

				// VOIDS THE CURRENT INVOICE ......................................................
				$invoiceVoidingEnt = \Entity\Invoice::cancel($idInvoice);
				$invoiceVoidingEnt->voidReason = $R['invoiceVoidReason'];
				$invoiceVoidingEnt->save();

				\DB::commitTransaction();

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

				// clear selection for the current invoice that was just cancelled
				$invoicesListingSelection->delete([$idInvoice]);

				// KEEP SELECTION OF TEMPORARY INVOICES ...........................................
				if ($R['selectTempInvoices']) {
					if ($invoiceEnt->cumulatedInvoicesIdsList) {
						$invoicesListingSelection->add($invoiceEnt->cumulatedInvoicesIdsList);
					}
				}

			} catch (\App\Entity\Exception $e) {
				\DB::rollbackTransaction();
				$msgs->appendMessages($e->getMessagesCollector(), sprintf($this->lang['index']['msgs']['error'], $idInvoice).' %s');
				$OK = false;
				break ;
			}

		}

		return $OK;

	}

}