• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/compatible/ordercompatibility.php
  • Класс: BitrixSaleCompatibleOrderCompatibility
  • Вызов: OrderCompatibility::cancel
static function cancel($orderId, $value, $comment = false)
{
	$result = new SaleResult();

	if (intval($orderId) <= 0)
	{
		$result->addError( new SaleResultError(MainLocalizationLoc::getMessage('SALE_COMPATIBLE_ORDER_ID_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_ID_NOT_FOUND') );
		return $result;
	}

	$registry = SaleRegistry::getInstance(static::getRegistryType());
	/** @var SaleOrder $orderClassName */
	$orderClassName = $registry->getOrderClassName();
	if (!$order = $orderClassName::load($orderId))
	{
		$result->addError( new SaleResultError(MainLocalizationLoc::getMessage('SALE_COMPATIBLE_ORDER_NOT_FOUND'), 'SALE_COMPATIBLE_ORDER_NOT_FOUND') );
		return $result;
	}

	if ($value === 'N')
	{
		if ($order->isCanceled())
		{
			$r = $order->setField('CANCELED', 'N');
			if (!$r->isSuccess())
			{
				return $result->addErrors($r->getErrors());
			}

			$r = $order->save();
			if (!$r->isSuccess())
			{
				return $result->addErrors($r->getErrors());
			}
		}

		return $result;
	}

	if ($order->isCanceled())
	{
		return $result;
	}

	$paymentCollection = $order->getPaymentCollection();
	/** @var SalePayment $payment */
	foreach ($paymentCollection as $payment)
	{
		if ($payment->isPaid())
			$payment->setReturn('Y');
	}

	$shipmentCollection = $order->getShipmentCollection();
	/** @var SaleShipment $shipment */
	foreach ($shipmentCollection as $shipment)
	{
		if ($shipment->isSystem())
			continue;

		if ($shipment->isShipped())
			$shipment->setField('DEDUCTED', 'N');

		if ($shipment->isAllowDelivery())
			$shipment->disallowDelivery();
	}

	$r = $order->setField('CANCELED', 'Y');
	if (!$r->isSuccess())
	{
		return $result->addErrors($r->getErrors());
	}

	if (!empty($comment) && strval($comment) != '')
	{
		$r = $order->setField('REASON_CANCELED', $comment);
		if (!$r->isSuccess())
		{
			return $result->addErrors($r->getErrors());
		}
	}

	return $order->save();
}