• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/order.php
  • Класс: BitrixSaleOrder
  • Вызов: Order::syncOrderAndPayments
private function syncOrderAndPayments(Payment $payment = null)
{
	$result = new Result();

	$oldPaid = $this->getField('PAYED');
	$paymentCollection = $this->getPaymentCollection();
	$sumPaid = $paymentCollection->getPaidSum();

	if ($payment)
	{
		$finalSumPaid = $sumPaid;

		if ($payment->isPaid())
		{
			if ($sumPaid > $this->getPrice())
			{
				$finalSumPaid = $this->getSumPaid() + $payment->getSum();
			}
		}
		else
		{
			$r = $this->syncOrderPaymentPaid($payment);
			if ($r->isSuccess())
			{
				$paidResult = $r->getData();
				if (isset($paidResult['SUM_PAID']))
				{
					$finalSumPaid = $paidResult['SUM_PAID'];
				}
			}
			else
			{
				return $result->addErrors($r->getErrors());
			}
		}
	}
	else
	{
		$finalSumPaid = $this->getSumPaid();

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

		$paidResult = $r->getData();
		if (isset($paidResult['SUM_PAID']))
		{
			$finalSumPaid = $paidResult['SUM_PAID'];
		}
	}

	$paid = false;

	if ($finalSumPaid >= 0 && $paymentCollection->hasPaidPayment()
		&& PriceMaths::roundPrecision($this->getPrice()) <= PriceMaths::roundPrecision($finalSumPaid))
	{
		$paid = true;
	}

	$this->setFieldNoDemand('PAYED', $paid ? "Y" : "N");

	if ($this->getFields()->isChanged('PAYED'))
	{
		InternalsEventsPool::addEvent(
			$this->getInternalId(),
			EventActions::EVENT_ON_ORDER_PAID,
			[
				'ENTITY' => $this,
			]
		);

		InternalsEventsPool::addEvent(
			$this->getInternalId(),
			EventActions::EVENT_ON_ORDER_PAID_SEND_MAIL,
			[
				'ENTITY' => $this,
			]
		);
	}

	if ($finalSumPaid > 0 && $finalSumPaid > $this->getPrice())
	{
		if (!$payment || $payment->isPaid())
		{
			InternalsUserBudgetPool::addPoolItem($this, $finalSumPaid - $this->getPrice(), InternalsUserBudgetPool::BUDGET_TYPE_EXCESS_SUM_PAID, $payment);
		}

		$finalSumPaid = $this->getPrice();
	}

	$this->setFieldNoDemand('SUM_PAID', $finalSumPaid);

	$r = $this->onAfterSyncPaid($oldPaid);
	if (!$r->isSuccess())
	{
		$result->addErrors($r->getErrors());
	}
	elseif ($r->hasWarnings())
	{
		$result->addWarnings($r->getWarnings());
	}

	return $result;
}