• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/orderbase.php
  • Класс: BitrixSaleOrderBase
  • Вызов: OrderBase::doFinalAction
public function doFinalAction($hasMeaningfulField = false)
{
	$result = new Result();

	$orderInternalId = $this->getInternalId();

	$r = InternalsActionEntity::runActions($orderInternalId);
	if (!$r->isSuccess())
	{
		$result->addErrors($r->getErrors());
	}

	if (!$hasMeaningfulField)
	{
		$this->clearStartField();
		return $result;
	}


	if ($r->hasWarnings())
	{
		$result->addWarnings($r->getWarnings());
	}

	$currentIsMathActionOnly = $this->isMathActionOnly();

	$basket = $this->getBasket();
	if ($basket)
	{
		$this->setMathActionOnly(true);

		$eventManager = MainEventManager::getInstance();
		$eventsList = $eventManager->findEventHandlers('sale', 'OnBeforeSaleOrderFinalAction');
		if (!empty($eventsList))
		{
			$event = new MainEvent('sale', 'OnBeforeSaleOrderFinalAction', array(
				'ENTITY' => $this,
				'HAS_MEANINGFUL_FIELD' => $hasMeaningfulField,
				'BASKET' => $basket,
			));
			$event->send();

			if ($event->getResults())
			{
				/** @var MainEventResult $eventResult */
				foreach($event->getResults() as $eventResult)
				{
					if($eventResult->getType() == MainEventResult::ERROR)
					{
						$errorMsg = new ResultError(
							MainLocalizationLoc::getMessage(
								'SALE_EVENT_ON_BEFORE_SALEORDER_FINAL_ACTION_ERROR'
							),
							'SALE_EVENT_ON_BEFORE_SALEORDER_FINAL_ACTION_ERROR'
						);

						$eventResultData = $eventResult->getParameters();
						if ($eventResultData)
						{
							if (isset($eventResultData) && $eventResultData instanceof ResultError)
							{
								/** @var ResultError $errorMsg */
								$errorMsg = $eventResultData;
							}
						}

						$result->addError($errorMsg);
					}
				}
			}
		}

		if (!$result->isSuccess())
		{
			return $result;
		}

		// discount
		$discount = $this->getDiscount();
		$r = $discount->calculate();
		if (!$r->isSuccess())
		{
//				$this->clearStartField();
//				$result->addErrors($r->getErrors());
//				return $result;
		}

		if ($r->isSuccess() && ($discountData = $r->getData()) && !empty($discountData) && is_array($discountData))
		{
			/** @var Result $r */
			$r = $this->applyDiscount($discountData);
			if (!$r->isSuccess())
			{
				$result->addErrors($r->getErrors());
				return $result;
			}
		}

		/** @var Tax $tax */
		$tax = $this->getTax();
		/** @var Result $r */
		$r = $tax->refreshData();
		if (!$r->isSuccess())
		{
			$result->addErrors($r->getErrors());
			return $result;
		}

		$taxResult = $r->getData();

		$taxChanged = false;
		if (isset($taxResult['TAX_PRICE']) && floatval($taxResult['TAX_PRICE']) >= 0)
		{
			if (!$this->isUsedVat())
			{
				$taxChanged = $this->getField('TAX_PRICE') !== $taxResult['TAX_PRICE'];
				if ($taxChanged)
				{
					$this->setField('TAX_PRICE', $taxResult['TAX_PRICE']);
					$this->refreshOrderPrice();
				}
			}

		}

		if (array_key_exists('VAT_SUM', $taxResult))
		{
			if ($this->isUsedVat())
			{
				$this->setField('VAT_SUM', $taxResult['VAT_SUM']);
			}
		}

		if ($taxChanged || $this->isUsedVat())
		{
			$taxValue = $this->isUsedVat()? $this->getVatSum() : $this->getField('TAX_PRICE');
			if (floatval($taxValue) != floatval($this->getField('TAX_VALUE')))
			{
				$this->setField('TAX_VALUE', floatval($taxValue));
			}
		}

	}

	if (!$currentIsMathActionOnly)
		$this->setMathActionOnly(false);

	$this->clearStartField();

	$eventManager = MainEventManager::getInstance();
	if ($eventsList = $eventManager->findEventHandlers('sale', 'OnAfterSaleOrderFinalAction'))
	{
		$event = new MainEvent(
			'sale',
			'OnAfterSaleOrderFinalAction',
			array('ENTITY' => $this)
		);
		$event->send();
	}

	return $result;
}