34 public function createItem(
Service $service =
null)
37 $paymentClassName = static::getItemCollectionClassName();
39 $payment = $paymentClassName::create($this, $service);
40 $this->addItem($payment);
49 public function addItem(Internals\CollectableEntity $payment)
52 $payment = parent::addItem($payment);
64 public function deleteItem($index)
66 $oldItem = parent::deleteItem($index);
83 public function onItemModify(Internals\CollectableEntity $item, $name =
null, $oldValue =
null, $value =
null)
93 public function isPaid()
95 if (!empty($this->collection) && is_array($this->collection))
98 foreach ($this->collection as $payment)
100 if (!$payment->isPaid())
116 public function onOrderModify($name, $oldValue, $value)
118 $result =
new Result();
129 foreach ($this->collection as $payment)
131 if ($payment->isPaid())
140 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'),
'SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'));
147 $payment = $this->getItemForAutoEdit($oldValue);
148 if ($payment !==
null)
150 $r = $payment->setField(
"SUM", $value);
151 if (!$r->isSuccess())
153 $result->addErrors($r->getErrors());
156 $service = $payment->getPaySystem();
159 $price = $service->getPaymentPrice($payment);
160 $payment->setField(
'PRICE_COD', $price);
169 public function onBeforeBasketItemDelete(BasketItem $basketItem) : Result
171 $result =
new Result();
174 foreach ($this->collection as $payment)
176 $r = $payment->onBeforeBasketItemDelete($basketItem);
177 if (!$r->isSuccess())
179 $result->addErrors($r->getErrors());
186 protected function isAllowAutoEdit()
195 foreach ($this as $payment)
197 $isAllowEditPayment =
200 !$payment->isReturn()
203 if ($isAllowEditPayment)
205 if ($service = $payment->getPaySystem())
207 $isAllowEditPayment = $service->isAllowEditPayment();
211 return $isAllowEditPayment;
218 private function getItemForAutoEdit($previousOrderSum) :? Payment
220 if ($this->isAllowAutoEdit())
223 foreach ($this as $payment)
225 if ($payment->getSum() === $previousOrderSum)
257 $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
259 return new $paymentCollectionClassName();
279 $paymentCollection = static::createPaymentCollectionObject();
280 $paymentCollection->setOrder(
$order);
285 $paymentClassName = static::getItemCollectionClassName();
287 $paymentList = $paymentClassName::loadForOrder(
$order->getId());
289 foreach ($paymentList as $payment)
291 $payment->setCollection($paymentCollection);
292 $paymentCollection->addItem($payment);
296 return $paymentCollection;
303 public function getPaidSum()
306 if (!empty($this->collection) && is_array($this->collection))
309 foreach ($this->collection as $payment)
311 if ($payment->getField(
'PAID') ==
"Y")
313 $sum += $payment->getSum();
324 public function getSum()
327 if (!empty($this->collection) && is_array($this->collection))
330 foreach ($this->collection as $payment)
332 $sum += $payment->getSum();
342 public function hasPaidPayment()
345 foreach ($this->collection as $payment)
347 if ($payment->getField(
'PAID') ===
"Y")
359 public function hasUnpaidPayment()
362 foreach ($this->collection as $payment)
364 if ($payment->getField(
'PAID') ===
"N")
377 public function save()
379 $result =
new Entity\Result();
384 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
387 $itemsFromDb = array();
390 $itemsFromDbList = static::getList(
392 "filter" => array(
"ORDER_ID" => $this->
getOrder()->getId()),
393 "select" => array(
"ID",
"PAY_SYSTEM_NAME",
"PAY_SYSTEM_ID")
396 while ($itemsFromDbItem = $itemsFromDbList->fetch())
397 $itemsFromDb[$itemsFromDbItem[
"ID"]] = $itemsFromDbItem;
400 $changeMeaningfulFields = array(
411 foreach ($this->collection as $payment)
413 $isNew = $payment->getId() <= 0;
416 if (
$order->getId() > 0 && $isChanged)
418 $logFields = array();
420 $fields = $payment->getFields();
421 $originalValues = $fields->getOriginalValues();
423 foreach($originalValues as $originalFieldName => $originalFieldValue)
425 if (in_array($originalFieldName, $changeMeaningfulFields) && $payment->getField($originalFieldName) != $originalFieldValue)
427 $logFields[$originalFieldName] = $payment->getField($originalFieldName);
429 $logFields[
'OLD_'.$originalFieldName] = $originalFieldValue;
434 $r = $payment->save();
444 $orderHistory = $registry->getOrderHistoryClassName();
445 $orderHistory::addLog(
448 $isNew ?
'PAYMENT_ADD' :
'PAYMENT_UPDATE',
452 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
455 $orderHistory::addAction(
470 $result->addErrors($r->getErrors());
473 if (isset($itemsFromDb[$payment->getId()]))
475 unset($itemsFromDb[$payment->getId()]);
479 foreach ($itemsFromDb as $k => $v)
481 $v[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
484 $event =
new Main\Event(
'sale',
"OnBeforeSalePaymentDeleted", array(
489 static::deleteInternal($k);
492 $event =
new Main\Event(
'sale',
"OnSalePaymentDeleted", array(
502 $orderHistory = $registry->getOrderHistoryClassName();
503 $orderHistory::addAction(
'PAYMENT',
$order->getId(),
'PAYMENT_REMOVE', $k,
null, array(
504 "PAY_SYSTEM_NAME" => $v[
"PAY_SYSTEM_NAME"],
505 "PAY_SYSTEM_ID" => $v[
"PAY_SYSTEM_ID"],
511 $entityMarker = $registry->getEntityMarkerClassName();
512 $entityMarker::deleteByFilter(array(
513 '=ORDER_ID' =>
$order->getId(),
514 '=ENTITY_TYPE' => $entityMarker::ENTITY_TYPE_PAYMENT,
526 $orderHistory = $registry->getOrderHistoryClassName();
527 $orderHistory::collectEntityFields(
'PAYMENT',
$order->getId());
537 public function getInnerPayment()
542 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
545 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
548 foreach ($this->collection as $payment)
550 if ($payment->getPaymentSystemId() == $paySystemId)
562 public function createInnerPayment()
564 $payment = $this->getInnerPayment();
570 $paySystemId = PaySystem\Manager::getInnerPaySystemId();
571 if (!empty($paySystemId))
574 $paySystem = Manager::getObjectById($paySystemId);
577 return $this->createItem($paySystem);
587 public function isExistsInnerPayment()
589 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
592 foreach ($this->collection as $payment)
594 if ($payment->getPaymentSystemId() == $paySystemId)
608 $result =
new Result();
611 foreach ($this->collection as $payment)
613 $r = $payment->verify();
614 if (!$r->isSuccess())
616 $result->addErrors($r->getErrors());
621 throw new Main\ObjectNotFoundException(
'Entity "Order" not found');
627 $entityMarker = $registry->getEntityMarkerClassName();
628 $entityMarker::addMarker(
$order, $payment, $r);
629 $order->setField(
'MARKED',
'Y');
635 public function getBasketItemQuantity(BasketItem $basketItem) : float
640 foreach ($this->collection as $payment)
642 $quantity += $payment->getBasketItemQuantity($basketItem);
654 public function createClone(\SplObjectStorage $cloneEntity)
656 if ($this->
isClone() && $cloneEntity->contains($this))
658 return $cloneEntity[$this];
662 $paymentCollectionClone = parent::createClone($cloneEntity);
666 if ($cloneEntity->contains($this->order))
668 $paymentCollectionClone->order = $cloneEntity[
$this->order];
672 return $paymentCollectionClone;
680 public function isMarked()
682 if (!empty($this->collection) && is_array($this->collection))
685 foreach ($this->collection as $payment)
687 if ($payment->isMarked())
701 return Internals\PaymentTable::deleteWithItems($primary);
707 private static function getItemCollectionClassName()
710 return $registry->getPaymentClassName();
717 public static function getList(array $parameters = array())
719 return Internals\PaymentTable::getList($parameters);
729 return PaySystem\Manager::getInnerPaySystemId();