• Модуль: sale
  • Путь к файлу: ~/bitrix/modules/sale/lib/payment.php
  • Класс: BitrixSalePayment
  • Вызов: Payment::onFieldModify
protected function onFieldModify($name, $oldValue, $value)
{
	global $USER;

	$result = new Result();

	if ($name === "PAID")
	{
		if ($value === "Y")
		{
			if (!$this->getFields()->isChanged('DATE_PAID'))
			{
				$this->setField('DATE_PAID', new MainTypeDateTime());
			}

			$this->setField('EMP_PAID_ID', $USER->GetID());

			if ($this->getField('IS_RETURN') === self::RETURN_INNER)
			{
				$paySystemId = SalePaySystemManager::getInnerPaySystemId();
			}
			else
			{
				$paySystemId = $this->getPaymentSystemId();
			}

			$service = SalePaySystemManager::getObjectById($paySystemId);
			if ($service)
			{
				$operationResult = $service->creditNoDemand($this);
				if (!$operationResult->isSuccess())
				{
					return $result->addErrors($operationResult->getErrors());
				}
			}

			$this->setField('IS_RETURN', static::RETURN_NONE);

			InternalsEventsPool::addEvent(
				'p'.$this->getInternalIndex(),
				EventActions::EVENT_ON_PAYMENT_PAID,
				[
					'ENTITY' => $this,
					'VALUES' => $this->fields->getOriginalValues(),
				]
			);
		}

		$this->addCashboxChecks();
	}
	elseif ($name === "IS_RETURN")
	{
		if ($value === static::RETURN_NONE)
		{
			return $result;
		}

		if ($oldValue === static::RETURN_NONE)
		{
			$this->setField('EMP_RETURN_ID', $USER->GetID());
		}

		/** @var PaymentCollection $collection */
		$collection = $this->getCollection();

		$creditSum = 0;
		$overPaid = $collection->getPaidSum() - $collection->getOrder()->getPrice();

		if ($overPaid <= 0)
		{
			$creditSum = $this->getSum();
			$overPaid = 0;
		}
		elseif ($this->getSum() - $overPaid > 0)
		{
			$creditSum = $this->getSum() - $overPaid;
		}

		if ($value == static::RETURN_PS)
		{
			$psId = $this->getPaymentSystemId();
		}
		else
		{
			$psId = SalePaySystemManager::getInnerPaySystemId();
		}

		$service = SalePaySystemManager::getObjectById($psId);

		if ($service && $service->isRefundable())
		{
			if ($creditSum)
			{
				if ($value == static::RETURN_PS)
				{
					if ($overPaid > 0)
					{
						$userBudget = InternalsUserBudgetPool::getUserBudgetByOrder($collection->getOrder());
						if (PriceMaths::roundPrecision($overPaid) > PriceMaths::roundPrecision($userBudget))
						{
							return $result->addError(
								new EntityEntityError(
									Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_PAID'),
									'SALE_ORDER_PAYMENT_RETURN_PAID'
								)
							);
						}
					}
				}

				$refResult = $service->refund($this);
				if (!$refResult->isSuccess())
				{
					return $result->addErrors($refResult->getErrors());
				}

				$refResultOperation = $refResult->getOperationType();
				if ($refResultOperation === ServiceResult::MONEY_LEAVING)
				{
					$setUnpaidResult = $this->setField('PAID', 'N');
					if (!$setUnpaidResult->isSuccess())
					{
						return $result->addErrors($setUnpaidResult->getErrors());
					}
				}
			}
		}
		else
		{
			return $result->addError(
				new EntityEntityError(
					Loc::getMessage('SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'),
					'SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'
				)
			);
		}
	}
	elseif($name === "SUM")
	{
		if($this->isPaid())
		{
			$result = new Result();

			return $result->addError(
				new ResultError(
					Loc::getMessage('SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'),
					'SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'
				)
			);
		}
	}
	elseif ($name === "MARKED")
	{
		if ($oldValue !== "Y")
		{
			$this->setField('DATE_MARKED', new MainTypeDateTime());

			if (is_object($USER))
			{
				$this->setField('EMP_MARKED_ID', $USER->GetID());
			}
		}
		elseif ($value === "N")
		{
			$r = $this->setField('REASON_MARKED', '');
			if (!$r->isSuccess())
			{
				return $result->addErrors($r->getErrors());
			}
		}
	}
	elseif ($name === 'RESPONSIBLE_ID')
	{
		$this->setField('DATE_RESPONSIBLE_ID', new MainTypeDateTime());
	}

	return parent::onFieldModify($name, $oldValue, $value);
}