46 if ($this->payableItemCollection ===
null)
51 $itemCollectionClassName = $registry->getPayableItemCollectionClassName();
52 $this->payableItemCollection = $itemCollectionClassName::load($this);
58 public function getBasketItemQuantity(BasketItem $basketItem) : float
63 foreach ($this->getPayableItemCollection()->getBasketItems() as $payableBasketItem)
65 if ($payableBasketItem->getEntityObject()->getBasketCode() === $basketItem->getBasketCode())
67 $quantity += $payableBasketItem->getQuantity();
80 if (isset($values[
'PAID']))
82 if ($this->getField(
'PAID') ===
'Y')
84 if ($values[
'PAID'] ===
'N')
86 $values = [
'PAID' => $values[
'PAID']] + $values;
91 if ($values[
'PAID'] ===
'Y')
94 unset($values[
'PAID']);
95 $values[
'PAID'] =
'Y';
115 'PS_STATUS_DESCRIPTION',
120 'PS_RECURRING_TOKEN',
135 'PAY_RETURN_COMMENT',
137 'EMP_RESPONSIBLE_ID',
138 'DATE_RESPONSIBLE_ID',
159 return [
'PAY_SYSTEM_ID'];
170 $paymentClassName = $registry->getPaymentClassName();
172 return new $paymentClassName($fields);
198 'XML_ID' => static::generateXmlId(),
199 'IS_RETURN' => static::RETURN_NONE,
200 'CURRENCY' =>
$collection->getOrder()->getCurrency(),
204 $payment = static::createPaymentObject();
205 $payment->setFieldsNoDemand($fields);
208 if ($paySystem !==
null)
210 $payment->setPaySystemService($paySystem);
224 $result = $this->setField(
"PAY_SYSTEM_ID",
$service->getField(
'ID'));
225 if ($result->isSuccess())
227 $this->setField(
"PAY_SYSTEM_NAME",
$service->getField(
'NAME'));
236 return uniqid(
'bx_');
249 if (intval($id) <= 0)
256 $paymentDataList = static::getList([
'filter' => [
'=ORDER_ID' => $id]]);
257 while ($paymentData = $paymentDataList->fetch())
259 $payments[] = static::createPaymentObject($paymentData);
277 $dbRes = static::getList([
279 "filter" => [
"=ORDER_ID" => $orderId]
282 while ($payment = $dbRes->fetch())
284 $r = static::deleteInternal($payment[
'ID']);
285 if (!$r->isSuccess())
287 $result->addErrors($r->getErrors());
299 public function delete()
309 $r = $this->callEventOnBeforeEntityDeleted();
310 if (!$r->isSuccess())
312 return $result->addErrors($r->getErrors());
315 $r = parent::delete();
316 if (!$r->isSuccess())
318 $result->addErrors($r->getErrors());
321 $r = $this->callEventOnEntityDeleted();
322 if (!$r->isSuccess())
324 $result->addErrors($r->getErrors());
333 private function callEventOnBeforeEntityDeleted()
338 $event =
new Main\Event(
'sale',
"OnBeforeSalePaymentEntityDeleted", [
340 'VALUES' => $this->fields->getOriginalValues(),
344 if ($event->getResults())
347 foreach($event->getResults() as $eventResult)
349 if ($eventResult->getType() == Main\EventResult::ERROR)
352 Loc::getMessage(
'SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'),
353 'SALE_EVENT_ON_BEFORE_SALEPAYMENT_ENTITY_DELETED_ERROR'
355 if ($eventResultData = $eventResult->getParameters())
357 if (isset($eventResultData) && $eventResultData instanceof
ResultError)
360 $errorMsg = $eventResultData;
364 $result->addError($errorMsg);
375 private function callEventOnEntityDeleted()
377 $result =
new Result();
380 $event =
new Main\Event(
'sale',
"OnSalePaymentEntityDeleted", [
382 'VALUES' => $this->fields->getOriginalValues(),
386 if ($event->getResults())
389 foreach($event->getResults() as $eventResult)
391 if($eventResult->getType() == Main\EventResult::ERROR)
393 $errorMsg =
new ResultError(
395 'SALE_EVENT_ON_SALEPAYMENT_ENTITY_DELETED_ERROR'
397 if ($eventResultData = $eventResult->getParameters())
399 if (isset($eventResultData) && $eventResultData instanceof ResultError)
402 $errorMsg = $eventResultData;
406 $result->addError($errorMsg);
429 $result =
new Result();
431 if ($name ===
"PAID")
437 $this->setField(
'DATE_PAID',
new Main\
Type\DateTime());
440 $this->setField(
'EMP_PAID_ID', $USER->GetID());
442 if ($this->getField(
'IS_RETURN') === self::RETURN_INNER)
444 $paySystemId = Sale\PaySystem\Manager::getInnerPaySystemId();
451 $service = Sale\PaySystem\Manager::getObjectById($paySystemId);
454 $operationResult =
$service->creditNoDemand($this);
455 if (!$operationResult->isSuccess())
457 return $result->addErrors($operationResult->getErrors());
461 $this->setField(
'IS_RETURN', static::RETURN_NONE);
463 Internals\EventsPool::addEvent(
468 'VALUES' => $this->fields->getOriginalValues(),
475 elseif ($name ===
"IS_RETURN")
477 if ($value === static::RETURN_NONE)
482 if ($oldValue === static::RETURN_NONE)
484 $this->setField(
'EMP_RETURN_ID', $USER->GetID());
495 $creditSum = $this->
getSum();
498 elseif ($this->
getSum() - $overPaid > 0)
500 $creditSum = $this->
getSum() - $overPaid;
503 if ($value == static::RETURN_PS)
509 $psId = Sale\PaySystem\Manager::getInnerPaySystemId();
512 $service = Sale\PaySystem\Manager::getObjectById($psId);
518 if ($value == static::RETURN_PS)
522 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder(
$collection->getOrder());
525 return $result->addError(
526 new Entity\EntityError(
528 'SALE_ORDER_PAYMENT_RETURN_PAID'
535 $refResult =
$service->refund($this);
536 if (!$refResult->isSuccess())
538 return $result->addErrors($refResult->getErrors());
541 $refResultOperation = $refResult->getOperationType();
544 $setUnpaidResult = $this->setField(
'PAID',
'N');
545 if (!$setUnpaidResult->isSuccess())
547 return $result->addErrors($setUnpaidResult->getErrors());
554 return $result->addError(
555 new Entity\EntityError(
557 'SALE_ORDER_PAYMENT_RETURN_NO_SUPPORTED'
562 elseif($name ===
"SUM")
566 $result =
new Result();
568 return $result->addError(
571 'SALE_PAYMENT_NOT_ALLOWED_CHANGE_SUM'
576 elseif ($name ===
"MARKED")
578 if ($oldValue !==
"Y")
580 $this->setField(
'DATE_MARKED',
new Main\
Type\DateTime());
582 if (is_object($USER))
584 $this->setField(
'EMP_MARKED_ID', $USER->GetID());
587 elseif ($value ===
"N")
589 $r = $this->setField(
'REASON_MARKED',
'');
590 if (!$r->isSuccess())
592 return $result->addErrors($r->getErrors());
596 elseif ($name ===
'RESPONSIBLE_ID')
598 $this->setField(
'DATE_RESPONSIBLE_ID',
new Main\
Type\DateTime());
601 return parent::onFieldModify($name, $oldValue, $value);
608 $r = $this->getPayableItemCollection()->onBeforeBasketItemDelete($basketItem);
609 if (!$r->isSuccess())
611 $result->addErrors($r->getErrors());
628 $this->checkCallingContext();
632 $id = $this->getId();
635 $this->callEventOnBeforeEntitySaved();
655 if (!$r->isSuccess())
657 $result->addErrors($r->getErrors());
666 if ($this->fields->isChanged(
'PAID'))
668 $this->calculateStatistic();
671 $this->callEventOnEntitySaved();
673 $this->callDelayedEvents();
677 if (!$r->isSuccess())
679 return $result->addErrors($r->getErrors());
689 $isChanged = parent::isChanged();
695 return $this->getPayableItemCollection()->isChanged();
701 private function checkCallingContext()
705 if (!$order->isSaveRunning())
707 trigger_error(
"Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
727 Cashbox\Internals\Pool::addDoc($this->
getOrder()->getInternalId(), $this);
734 protected function calculateStatistic()
748 private function add()
750 $result =
new Result();
754 $orderHistory = $registry->getOrderHistoryClassName();
758 $this->setFieldNoDemand(
'ORDER_ID', $this->
getOrder()->getId());
761 $r = $this->
addInternal($this->getFields()->getValues());
762 if (!$r->isSuccess())
764 $orderHistory::addAction(
770 [
"ERROR" => $r->getErrorMessages()]
773 $result->addErrors($r->getErrors());
778 $this->setFieldNoDemand(
'ID', $id);
783 $orderHistory::addAction(
802 $result =
new Result();
804 $r = static::updateInternal($this->getId(), $this->
getFields()->getChangedValues());
805 if (!$r->isSuccess())
810 $orderHistory = $registry->getOrderHistoryClassName();
812 $orderHistory::addAction(
815 'PAYMENT_UPDATE_ERROR',
818 [
"ERROR" => $r->getErrorMessages()]
821 $result->addErrors($r->getErrors());
830 private function callEventOnBeforeEntitySaved()
833 $event =
new Main\Event(
'sale',
'OnBeforeSalePaymentEntitySaved', [
835 'VALUES' => $this->fields->getOriginalValues()
844 private function callEventOnEntitySaved()
847 $event =
new Main\Event(
'sale',
'OnSalePaymentEntitySaved', [
849 'VALUES' => $this->fields->getOriginalValues(),
858 private function callDelayedEvents()
860 $eventList = Internals\EventsPool::getEvents(
'p'.$this->
getInternalIndex());
863 foreach ($eventList as $eventName => $eventData)
865 $event =
new Main\Event(
'sale', $eventName, $eventData);
871 $notifyClassName = $registry->getNotifyClassName();
872 $notifyClassName::callNotify($this, $eventName);
892 return floatval($this->getField(
'SUM'));
900 return $this->getField(
'PS_SUM');
908 return $this->getField(
'PAID') ===
'Y';
917 $this->getField(
'IS_RETURN') === static::RETURN_INNER
919 $this->getField(
'IS_RETURN') === static::RETURN_PS
928 return (
int)$this->getField(
'ORDER_ID');
936 if ($this->service ===
null)
951 return Sale\PaySystem\Manager::getObjectById($paySystemId);
962 return (
int)$this->getField(
'PAY_SYSTEM_ID');
970 return $this->getField(
'PAY_SYSTEM_NAME');
980 public function setPaid($value)
985 $r = $this->setField(
'PAID', $value);
986 if (!$r->isSuccess())
988 $result->addErrors($r->getErrors());
990 elseif($r->hasWarnings())
992 $result->addWarnings($r->getWarnings());
1005 public function setReturn($value)
1007 $result =
new Result();
1009 if ($value === static::RETURN_INNER || $value === static::RETURN_PS)
1013 return new Result();
1016 elseif($value === static::RETURN_NONE)
1020 return new Result();
1025 throw new Main\ArgumentOutOfRangeException(
'value');
1029 $r = $this->setField(
'IS_RETURN', $value);
1030 if (!$r->isSuccess())
1032 $result->addErrors($r->getErrors());
1043 return $this->
getPaymentSystemId() === Sale\PaySystem\Manager::getInnerPaySystemId();
1060 elseif ($name ===
'REASON_MARKED')
1062 $value = (string)$value;
1063 if (mb_strlen($value) > 255)
1065 $value = mb_substr($value, 0, 255);
1069 return parent::normalizeValue($name, $value);
1080 $result = parent::checkValueBeforeSet($name, $value);
1082 if ($name ==
"PAY_SYSTEM_ID")
1084 if (intval($value) > 0 && !
Sale\PaySystem\Manager::isExist($value))
1089 'SALE_PAYMENT_WRONG_PAYMENT_SERVICE'
1094 elseif ($name ===
'ACCOUNT_NUMBER')
1096 $dbRes = static::getList([
1098 'filter' => [
'=ACCOUNT_NUMBER' => $value]
1101 if ($dbRes->fetch())
1121 if ($this->getId() > 0)
1125 if ($order && $order->getId() > 0)
1148 $result->addError(
new ResultError(
Loc::getMessage(
"SALE_PAYMENT_PAYMENT_SERVICE_EMPTY"),
'SALE_PAYMENT_PAYMENT_SERVICE_EMPTY'));
1164 $value = Internals\AccountNumberGenerator::generateForPayment($this);
1168 $r = static::updateInternal($id, [
"ACCOUNT_NUMBER" => $value]);
1169 $res = $r->isSuccess(
true);
1178 $this->setFieldNoDemand(
'ACCOUNT_NUMBER', $value);
1190 $providerInstance =
null;
1192 if (is_array($mapping) && isset($mapping[
'PROVIDER_KEY']))
1194 switch ($mapping[
'PROVIDER_KEY'])
1197 $providerInstance = $this;
1200 $providerInstance = $this->getField(
'COMPANY_ID');
1206 $providerInstance = $order->getBusinessValueProviderInstance($mapping);
1211 return $providerInstance;
1222 return $order->getPersonTypeId();
1235 public static function getList(array $parameters = [])
1237 return Internals\PaymentTable::getList($parameters);
1246 public function createClone(\SplObjectStorage $cloneEntity)
1248 if ($this->
isClone() && $cloneEntity->contains($this))
1250 return $cloneEntity[$this];
1254 $paymentClone = parent::createClone($cloneEntity);
1259 if (!$cloneEntity->contains($paySystem))
1261 $cloneEntity[$paySystem] = $paySystem->createClone($cloneEntity);
1264 if ($cloneEntity->contains($paySystem))
1266 $paymentClone->service = $cloneEntity[$paySystem];
1270 return $paymentClone;
1297 public function isAllowPay()
1302 return $order->isAllowPay();
1310 return $this->getField(
'MARKED') ==
"Y";
1320 static $className =
null;
1321 $errorsList = static::getAutoFixErrorsList();
1322 if (is_array($errorsList) && in_array($value, $errorsList))
1324 if ($className ===
null)
1325 $className = static::getClassName();
1339 $errorsList = static::getAutoFixErrorsList();
1340 if (is_array($errorsList) && in_array($value, $errorsList))
1384 $name ===
'PRICE_COD'
1397 return Internals\PaymentTable::add($data);
1408 return Internals\PaymentTable::update($primary, $data);
1418 return Internals\PaymentTable::delete($primary);
1426 return Internals\PaymentTable::getMap();
1434 return Internals\PaymentTable::getUfId();
1444 return 'SalePayment';
static addField($entityName, $orderId, $field, $oldValue=null, $value=null, $id=null, $entity=null, array $fields=array())