1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
paymentcollection.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale;
4
5use Bitrix\Main;
6use Bitrix\Sale\Internals;
7use Bitrix\Main\Entity;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Sale\PaySystem\Manager;
10use Bitrix\Sale\PaySystem\Service;
11
12Loc::loadMessages(__FILE__);
13
19{
21 protected $order;
22
26 protected function getEntityParent()
27 {
28 return $this->getOrder();
29 }
30
35 public function createItem(Service $service = null)
36 {
38 $paymentClassName = static::getItemCollectionClassName();
39
40 $payment = $paymentClassName::create($this, $service);
41 $this->addItem($payment);
42
43 return $payment;
44 }
45
50 public function addItem(Internals\CollectableEntity $payment)
51 {
53 $payment = parent::addItem($payment);
54
55 $order = $this->getOrder();
56 return $order->onPaymentCollectionModify(EventActions::ADD, $payment);
57 }
58
65 public function deleteItem($index)
66 {
67 $oldItem = parent::deleteItem($index);
68
70 $order = $this->getOrder();
71 return $order->onPaymentCollectionModify(EventActions::DELETE, $oldItem);
72 }
73
84 public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
85 {
87 $order = $this->getOrder();
88 return $order->onPaymentCollectionModify(EventActions::UPDATE, $item, $name, $oldValue, $value);
89 }
90
94 public function isPaid()
95 {
96 if (!empty($this->collection) && is_array($this->collection))
97 {
99 foreach ($this->collection as $payment)
100 {
101 if (!$payment->isPaid())
102 return false;
103 }
104
105 return true;
106 }
107
108 return false;
109 }
110
117 public function onOrderModify($name, $oldValue, $value)
118 {
119 $result = new Result();
120
121 switch($name)
122 {
123 case "CANCELED":
124
125 if ($value == "Y")
126 {
127 $isPaid = false;
128
130 foreach ($this->collection as $payment)
131 {
132 if ($payment->isPaid())
133 {
134 $isPaid = true;
135 break;
136 }
137 }
138
139 if ($isPaid)
140 {
141 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'), 'SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'));
142 }
143 }
144
145 break;
146
147 case "PRICE":
148 $payment = $this->getItemForAutoEdit($oldValue);
149 if ($payment !== null)
150 {
151 $r = $payment->setField("SUM", $value);
152 if (!$r->isSuccess())
153 {
154 $result->addErrors($r->getErrors());
155 }
156
157 $service = $payment->getPaySystem();
158 if ($service)
159 {
160 $price = $service->getPaymentPrice($payment);
161 $payment->setField('PRICE_COD', $price);
162 }
163 }
164 break;
165 }
166
167 return $result;
168 }
169
170 public function onBeforeBasketItemDelete(BasketItem $basketItem) : Result
171 {
172 $result = new Result();
173
175 foreach ($this->collection as $payment)
176 {
177 $r = $payment->onBeforeBasketItemDelete($basketItem);
178 if (!$r->isSuccess())
179 {
180 $result->addErrors($r->getErrors());
181 }
182 }
183
184 return $result;
185 }
186
187 protected function isAllowAutoEdit()
188 {
189 if (
190 !$this->getOrder()->isCanceled()
191 &&
192 $this->count() === 1
193 )
194 {
196 foreach ($this as $payment)
197 {
198 $isAllowEditPayment =
199 !$payment->isPaid()
200 &&
201 !$payment->isReturn()
202 ;
203
204 if ($isAllowEditPayment)
205 {
206 if ($service = $payment->getPaySystem())
207 {
208 $isAllowEditPayment = $service->isAllowEditPayment();
209 }
210 }
211
212 return $isAllowEditPayment;
213 }
214 }
215
216 return false;
217 }
218
219 private function getItemForAutoEdit($previousOrderSum) :? Payment
220 {
221 if ($this->isAllowAutoEdit())
222 {
224 foreach ($this as $payment)
225 {
226 if ($payment->getSum() === $previousOrderSum)
227 {
228 return $payment;
229 }
230 }
231 }
232
233 return null;
234 }
235
239 public function setOrder(Order $order)
240 {
241 $this->order = $order;
242 }
243
247 public function getOrder()
248 {
249 return $this->order;
250 }
251
255 protected static function createPaymentCollectionObject()
256 {
257 $registry = Registry::getInstance(static::getRegistryType());
258 $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
259
260 return new $paymentCollectionClassName();
261 }
262
266 public static function getRegistryType()
267 {
269 }
270
275 public static function load(Order $order)
276 {
278 $paymentCollection = static::createPaymentCollectionObject();
279 $paymentCollection->setOrder($order);
280
281 if ($order->getId() > 0)
282 {
284 $paymentClassName = static::getItemCollectionClassName();
285
286 $paymentList = $paymentClassName::loadForOrder($order->getId());
288 foreach ($paymentList as $payment)
289 {
290 $payment->setCollection($paymentCollection);
291 $paymentCollection->bindItem($payment);
292 }
293 }
294
295 return $paymentCollection;
296 }
297
298
302 public function getPaidSum()
303 {
304 $sum = 0;
305 if (!empty($this->collection) && is_array($this->collection))
306 {
308 foreach ($this->collection as $payment)
309 {
310 if ($payment->getField('PAID') == "Y")
311 {
312 $sum += $payment->getSum();
313 }
314 }
315 }
316
317 return $sum;
318 }
319
323 public function getSum()
324 {
325 $sum = 0;
326 if (!empty($this->collection) && is_array($this->collection))
327 {
329 foreach ($this->collection as $payment)
330 {
331 $sum += $payment->getSum();
332 }
333 }
334
335 return $sum;
336 }
337
341 public function hasPaidPayment()
342 {
344 foreach ($this->collection as $payment)
345 {
346 if ($payment->getField('PAID') === "Y")
347 {
348 return true;
349 }
350 }
351
352 return false;
353 }
354
358 public function hasUnpaidPayment()
359 {
361 foreach ($this->collection as $payment)
362 {
363 if ($payment->getField('PAID') === "N")
364 {
365 return true;
366 }
367 }
368
369 return false;
370 }
371
376 public function save()
377 {
378 $result = new Entity\Result();
379
381 if (!$order = $this->getOrder())
382 {
383 throw new Main\ObjectNotFoundException('Entity "Order" not found');
384 }
385
386 $itemsFromDb = array();
387 if ($this->getOrder()->getId() > 0)
388 {
389 $itemsFromDbList = static::getList(
390 array(
391 "filter" => array("ORDER_ID" => $this->getOrder()->getId()),
392 "select" => array("ID", "PAY_SYSTEM_NAME", "PAY_SYSTEM_ID")
393 )
394 );
395 while ($itemsFromDbItem = $itemsFromDbList->fetch())
396 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
397 }
398
399 $changeMeaningfulFields = array(
400 "PAID",
401 "PAY_SYSTEM_ID",
402 "PAY_SYSTEM_NAME",
403 "SUM",
404 "IS_RETURN",
405 "ACCOUNT_NUMBER",
406 "EXTERNAL_PAYMENT",
407 );
408
410 foreach ($this->collection as $payment)
411 {
412 $isNew = $payment->getId() <= 0;
413 $isChanged = $payment->isChanged();
414
415 if ($order->getId() > 0 && $isChanged)
416 {
417 $logFields = array();
418
419 $fields = $payment->getFields();
420 $originalValues = $fields->getOriginalValues();
421
422 foreach($originalValues as $originalFieldName => $originalFieldValue)
423 {
424 if (in_array($originalFieldName, $changeMeaningfulFields) && $payment->getField($originalFieldName) != $originalFieldValue)
425 {
426 $logFields[$originalFieldName] = $payment->getField($originalFieldName);
427 if (!$isNew)
428 $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
429 }
430 }
431 }
432
433 $r = $payment->save();
434 if ($r->isSuccess())
435 {
436 if ($order->getId() > 0)
437 {
438 if ($isChanged)
439 {
440 $registry = Registry::getInstance(static::getRegistryType());
441
443 $orderHistory = $registry->getOrderHistoryClassName();
444 $orderHistory::addLog(
445 'PAYMENT',
446 $order->getId(),
447 $isNew ? 'PAYMENT_ADD' : 'PAYMENT_UPDATE',
448 $payment->getId(),
449 $payment,
450 $logFields,
451 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
452 );
453
454 $orderHistory::addAction(
455 'PAYMENT',
456 $order->getId(),
457 "PAYMENT_SAVED",
458 $payment->getId(),
459 $payment,
460 array(),
462 );
463 }
464
465 }
466 }
467 else
468 {
469 $result->addErrors($r->getErrors());
470 }
471
472 if (isset($itemsFromDb[$payment->getId()]))
473 {
474 unset($itemsFromDb[$payment->getId()]);
475 }
476 }
477
478 foreach ($itemsFromDb as $k => $v)
479 {
480 $v['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
481
483 $event = new Main\Event('sale', "OnBeforeSalePaymentDeleted", array(
484 'VALUES' => $v,
485 ));
486 $event->send();
487
488 static::deleteInternal($k);
489
491 $event = new Main\Event('sale', "OnSalePaymentDeleted", array(
492 'VALUES' => $v,
493 ));
494 $event->send();
495
496 if ($order->getId() > 0)
497 {
498 $registry = Registry::getInstance(static::getRegistryType());
499
501 $orderHistory = $registry->getOrderHistoryClassName();
502 $orderHistory::addAction('PAYMENT', $order->getId(), 'PAYMENT_REMOVE', $k, null, array(
503 "PAY_SYSTEM_NAME" => $v["PAY_SYSTEM_NAME"],
504 "PAY_SYSTEM_ID" => $v["PAY_SYSTEM_ID"],
505 ));
506
507 $registry = Registry::getInstance(static::getRegistryType());
508
510 $entityMarker = $registry->getEntityMarkerClassName();
511 $entityMarker::deleteByFilter([
512 '=ORDER_ID' => $order->getId(),
513 '=ENTITY_TYPE' => $entityMarker::ENTITY_TYPE_PAYMENT,
514 '=ENTITY_ID' => $k,
515 ]);
516 }
517
518 }
519
520 if ($order->getId() > 0)
521 {
522 $registry = Registry::getInstance(static::getRegistryType());
523
525 $orderHistory = $registry->getOrderHistoryClassName();
526 $orderHistory::collectEntityFields('PAYMENT', $order->getId());
527 }
528
529 return $result;
530 }
531
536 public function getInnerPayment()
537 {
539 if (!$order = $this->getOrder())
540 {
541 throw new Main\ObjectNotFoundException('Entity "Order" not found');
542 }
543
544 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
545 {
547 foreach ($this->collection as $payment)
548 {
549 if ($payment->getPaymentSystemId() == $paySystemId)
550 return $payment;
551 }
552 }
553
554 return false;
555 }
556
561 public function createInnerPayment()
562 {
563 $payment = $this->getInnerPayment();
564 if ($payment)
565 {
566 return $payment;
567 }
568
569 $paySystemId = PaySystem\Manager::getInnerPaySystemId();
570 if (!empty($paySystemId))
571 {
573 $paySystem = Manager::getObjectById($paySystemId);
574 if ($paySystem)
575 {
576 return $this->createItem($paySystem);
577 }
578 }
579
580 return false;
581 }
582
586 public function isExistsInnerPayment()
587 {
588 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
589 {
591 foreach ($this->collection as $payment)
592 {
593 if ($payment->getPaymentSystemId() == $paySystemId)
594 return true;
595 }
596 }
597
598 return false;
599 }
600
605 public function verify()
606 {
607 $result = new Result();
608
610 foreach ($this->collection as $payment)
611 {
612 $r = $payment->verify();
613 if (!$r->isSuccess())
614 {
615 $result->addErrors($r->getErrors());
616
618 if (!$order = $this->getOrder())
619 {
620 throw new Main\ObjectNotFoundException('Entity "Order" not found');
621 }
622
623 $registry = Registry::getInstance(static::getRegistryType());
624
626 $entityMarker = $registry->getEntityMarkerClassName();
627 $entityMarker::addMarker($order, $payment, $r);
628 $order->setField('MARKED', 'Y');
629 }
630 }
631 return $result;
632 }
633
634 public function getBasketItemQuantity(BasketItem $basketItem) : float
635 {
636 $quantity = 0;
637
639 foreach ($this->collection as $payment)
640 {
641 $quantity += $payment->getBasketItemQuantity($basketItem);
642 }
643
644 return $quantity;
645 }
646
653 public function createClone(\SplObjectStorage $cloneEntity)
654 {
655 if ($this->isClone() && $cloneEntity->contains($this))
656 {
657 return $cloneEntity[$this];
658 }
659
661 $paymentCollectionClone = parent::createClone($cloneEntity);
662
663 if ($this->order)
664 {
665 if ($cloneEntity->contains($this->order))
666 {
667 $paymentCollectionClone->order = $cloneEntity[$this->order];
668 }
669 }
670
671 return $paymentCollectionClone;
672 }
673
679 public function isMarked()
680 {
681 if (!empty($this->collection) && is_array($this->collection))
682 {
684 foreach ($this->collection as $payment)
685 {
686 if ($payment->isMarked())
687 return true;
688 }
689 }
690
691 return false;
692 }
693
698 protected function deleteInternal($primary)
699 {
701 }
702
706 private static function getItemCollectionClassName()
707 {
708 $registry = Registry::getInstance(static::getRegistryType());
709 return $registry->getPaymentClassName();
710 }
711
716 public static function getList(array $parameters = array())
717 {
718 return Internals\PaymentTable::getList($parameters);
719 }
720
726 public static function getInnerPaySystemId()
727 {
729 }
730}
$sum
Определения checkout.php:6
const ADD
Определения eventactions.php:8
const DELETE
Определения eventactions.php:10
const UPDATE
Определения eventactions.php:9
static deleteWithItems(int $id)
Определения payment.php:466
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
Определения orderhistory.php:29
static getInnerPaySystemId()
Определения manager.php:642
deleteInternal($primary)
Определения paymentcollection.php:698
static getInnerPaySystemId()
Определения paymentcollection.php:726
setOrder(Order $order)
Определения paymentcollection.php:239
static getList(array $parameters=array())
Определения paymentcollection.php:716
static createPaymentCollectionObject()
Определения paymentcollection.php:255
static getRegistryType()
Определения paymentcollection.php:266
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
$payment
Определения payment.php:14
$order
Определения payment.php:8
$paymentCollection
Определения payment.php:11
$service
Определения payment.php:18
$event
Определения prolog_after.php:141
$k
Определения template_pdf.php:567
$fields
Определения yandex_run.php:501