Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
paymentcollection.php
1<?php
2namespace Bitrix\Sale;
3
10
11Loc::loadMessages(__FILE__);
12
18{
20 protected $order;
21
25 protected function getEntityParent()
26 {
27 return $this->getOrder();
28 }
29
34 public function createItem(Service $service = null)
35 {
37 $paymentClassName = static::getItemCollectionClassName();
38
39 $payment = $paymentClassName::create($this, $service);
40 $this->addItem($payment);
41
42 return $payment;
43 }
44
49 public function addItem(Internals\CollectableEntity $payment)
50 {
52 $payment = parent::addItem($payment);
53
54 $order = $this->getOrder();
55 return $order->onPaymentCollectionModify(EventActions::ADD, $payment);
56 }
57
64 public function deleteItem($index)
65 {
66 $oldItem = parent::deleteItem($index);
67
69 $order = $this->getOrder();
70 return $order->onPaymentCollectionModify(EventActions::DELETE, $oldItem);
71 }
72
83 public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
84 {
86 $order = $this->getOrder();
87 return $order->onPaymentCollectionModify(EventActions::UPDATE, $item, $name, $oldValue, $value);
88 }
89
93 public function isPaid()
94 {
95 if (!empty($this->collection) && is_array($this->collection))
96 {
98 foreach ($this->collection as $payment)
99 {
100 if (!$payment->isPaid())
101 return false;
102 }
103
104 return true;
105 }
106
107 return false;
108 }
109
116 public function onOrderModify($name, $oldValue, $value)
117 {
118 $result = new Result();
119
120 switch($name)
121 {
122 case "CANCELED":
123
124 if ($value == "Y")
125 {
126 $isPaid = false;
127
129 foreach ($this->collection as $payment)
130 {
131 if ($payment->isPaid())
132 {
133 $isPaid = true;
134 break;
135 }
136 }
137
138 if ($isPaid)
139 {
140 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'), 'SALE_ORDER_CANCEL_PAYMENT_EXIST_ACTIVE'));
141 }
142 }
143
144 break;
145
146 case "PRICE":
147 $payment = $this->getItemForAutoEdit($oldValue);
148 if ($payment !== null)
149 {
150 $r = $payment->setField("SUM", $value);
151 if (!$r->isSuccess())
152 {
153 $result->addErrors($r->getErrors());
154 }
155
156 $service = $payment->getPaySystem();
157 if ($service)
158 {
159 $price = $service->getPaymentPrice($payment);
160 $payment->setField('PRICE_COD', $price);
161 }
162 }
163 break;
164 }
165
166 return $result;
167 }
168
169 public function onBeforeBasketItemDelete(BasketItem $basketItem) : Result
170 {
171 $result = new Result();
172
174 foreach ($this->collection as $payment)
175 {
176 $r = $payment->onBeforeBasketItemDelete($basketItem);
177 if (!$r->isSuccess())
178 {
179 $result->addErrors($r->getErrors());
180 }
181 }
182
183 return $result;
184 }
185
186 protected function isAllowAutoEdit()
187 {
188 if (
189 !$this->getOrder()->isCanceled()
190 &&
191 $this->count() === 1
192 )
193 {
195 foreach ($this as $payment)
196 {
197 $isAllowEditPayment =
198 !$payment->isPaid()
199 &&
200 !$payment->isReturn()
201 ;
202
203 if ($isAllowEditPayment)
204 {
205 if ($service = $payment->getPaySystem())
206 {
207 $isAllowEditPayment = $service->isAllowEditPayment();
208 }
209 }
210
211 return $isAllowEditPayment;
212 }
213 }
214
215 return false;
216 }
217
218 private function getItemForAutoEdit($previousOrderSum) :? Payment
219 {
220 if ($this->isAllowAutoEdit())
221 {
223 foreach ($this as $payment)
224 {
225 if ($payment->getSum() === $previousOrderSum)
226 {
227 return $payment;
228 }
229 }
230 }
231
232 return null;
233 }
234
238 public function setOrder(Order $order)
239 {
240 $this->order = $order;
241 }
242
246 public function getOrder()
247 {
248 return $this->order;
249 }
250
254 protected static function createPaymentCollectionObject()
255 {
256 $registry = Registry::getInstance(static::getRegistryType());
257 $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
258
259 return new $paymentCollectionClassName();
260 }
261
265 public static function getRegistryType()
266 {
268 }
269
276 public static function load(Order $order)
277 {
279 $paymentCollection = static::createPaymentCollectionObject();
280 $paymentCollection->setOrder($order);
281
282 if ($order->getId() > 0)
283 {
285 $paymentClassName = static::getItemCollectionClassName();
286
287 $paymentList = $paymentClassName::loadForOrder($order->getId());
289 foreach ($paymentList as $payment)
290 {
291 $payment->setCollection($paymentCollection);
292 $paymentCollection->addItem($payment);
293 }
294 }
295
296 return $paymentCollection;
297 }
298
299
303 public function getPaidSum()
304 {
305 $sum = 0;
306 if (!empty($this->collection) && is_array($this->collection))
307 {
309 foreach ($this->collection as $payment)
310 {
311 if ($payment->getField('PAID') == "Y")
312 {
313 $sum += $payment->getSum();
314 }
315 }
316 }
317
318 return $sum;
319 }
320
324 public function getSum()
325 {
326 $sum = 0;
327 if (!empty($this->collection) && is_array($this->collection))
328 {
330 foreach ($this->collection as $payment)
331 {
332 $sum += $payment->getSum();
333 }
334 }
335
336 return $sum;
337 }
338
342 public function hasPaidPayment()
343 {
345 foreach ($this->collection as $payment)
346 {
347 if ($payment->getField('PAID') === "Y")
348 {
349 return true;
350 }
351 }
352
353 return false;
354 }
355
359 public function hasUnpaidPayment()
360 {
362 foreach ($this->collection as $payment)
363 {
364 if ($payment->getField('PAID') === "N")
365 {
366 return true;
367 }
368 }
369
370 return false;
371 }
372
377 public function save()
378 {
379 $result = new Entity\Result();
380
382 if (!$order = $this->getOrder())
383 {
384 throw new Main\ObjectNotFoundException('Entity "Order" not found');
385 }
386
387 $itemsFromDb = array();
388 if ($this->getOrder()->getId() > 0)
389 {
390 $itemsFromDbList = static::getList(
391 array(
392 "filter" => array("ORDER_ID" => $this->getOrder()->getId()),
393 "select" => array("ID", "PAY_SYSTEM_NAME", "PAY_SYSTEM_ID")
394 )
395 );
396 while ($itemsFromDbItem = $itemsFromDbList->fetch())
397 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
398 }
399
400 $changeMeaningfulFields = array(
401 "PAID",
402 "PAY_SYSTEM_ID",
403 "PAY_SYSTEM_NAME",
404 "SUM",
405 "IS_RETURN",
406 "ACCOUNT_NUMBER",
407 "EXTERNAL_PAYMENT",
408 );
409
411 foreach ($this->collection as $payment)
412 {
413 $isNew = $payment->getId() <= 0;
414 $isChanged = $payment->isChanged();
415
416 if ($order->getId() > 0 && $isChanged)
417 {
418 $logFields = array();
419
420 $fields = $payment->getFields();
421 $originalValues = $fields->getOriginalValues();
422
423 foreach($originalValues as $originalFieldName => $originalFieldValue)
424 {
425 if (in_array($originalFieldName, $changeMeaningfulFields) && $payment->getField($originalFieldName) != $originalFieldValue)
426 {
427 $logFields[$originalFieldName] = $payment->getField($originalFieldName);
428 if (!$isNew)
429 $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
430 }
431 }
432 }
433
434 $r = $payment->save();
435 if ($r->isSuccess())
436 {
437 if ($order->getId() > 0)
438 {
439 if ($isChanged)
440 {
441 $registry = Registry::getInstance(static::getRegistryType());
442
444 $orderHistory = $registry->getOrderHistoryClassName();
445 $orderHistory::addLog(
446 'PAYMENT',
447 $order->getId(),
448 $isNew ? 'PAYMENT_ADD' : 'PAYMENT_UPDATE',
449 $payment->getId(),
450 $payment,
451 $logFields,
452 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
453 );
454
455 $orderHistory::addAction(
456 'PAYMENT',
457 $order->getId(),
458 "PAYMENT_SAVED",
459 $payment->getId(),
460 $payment,
461 array(),
463 );
464 }
465
466 }
467 }
468 else
469 {
470 $result->addErrors($r->getErrors());
471 }
472
473 if (isset($itemsFromDb[$payment->getId()]))
474 {
475 unset($itemsFromDb[$payment->getId()]);
476 }
477 }
478
479 foreach ($itemsFromDb as $k => $v)
480 {
481 $v['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
482
484 $event = new Main\Event('sale', "OnBeforeSalePaymentDeleted", array(
485 'VALUES' => $v,
486 ));
487 $event->send();
488
489 static::deleteInternal($k);
490
492 $event = new Main\Event('sale', "OnSalePaymentDeleted", array(
493 'VALUES' => $v,
494 ));
495 $event->send();
496
497 if ($order->getId() > 0)
498 {
499 $registry = Registry::getInstance(static::getRegistryType());
500
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"],
506 ));
507
508 $registry = Registry::getInstance(static::getRegistryType());
509
511 $entityMarker = $registry->getEntityMarkerClassName();
512 $entityMarker::deleteByFilter(array(
513 '=ORDER_ID' => $order->getId(),
514 '=ENTITY_TYPE' => $entityMarker::ENTITY_TYPE_PAYMENT,
515 '=ENTITY_ID' => $k,
516 ));
517 }
518
519 }
520
521 if ($order->getId() > 0)
522 {
523 $registry = Registry::getInstance(static::getRegistryType());
524
526 $orderHistory = $registry->getOrderHistoryClassName();
527 $orderHistory::collectEntityFields('PAYMENT', $order->getId());
528 }
529
530 return $result;
531 }
532
537 public function getInnerPayment()
538 {
540 if (!$order = $this->getOrder())
541 {
542 throw new Main\ObjectNotFoundException('Entity "Order" not found');
543 }
544
545 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
546 {
548 foreach ($this->collection as $payment)
549 {
550 if ($payment->getPaymentSystemId() == $paySystemId)
551 return $payment;
552 }
553 }
554
555 return false;
556 }
557
562 public function createInnerPayment()
563 {
564 $payment = $this->getInnerPayment();
565 if ($payment)
566 {
567 return $payment;
568 }
569
570 $paySystemId = PaySystem\Manager::getInnerPaySystemId();
571 if (!empty($paySystemId))
572 {
574 $paySystem = Manager::getObjectById($paySystemId);
575 if ($paySystem)
576 {
577 return $this->createItem($paySystem);
578 }
579 }
580
581 return false;
582 }
583
587 public function isExistsInnerPayment()
588 {
589 if ($paySystemId = PaySystem\Manager::getInnerPaySystemId())
590 {
592 foreach ($this->collection as $payment)
593 {
594 if ($payment->getPaymentSystemId() == $paySystemId)
595 return true;
596 }
597 }
598
599 return false;
600 }
601
606 public function verify()
607 {
608 $result = new Result();
609
611 foreach ($this->collection as $payment)
612 {
613 $r = $payment->verify();
614 if (!$r->isSuccess())
615 {
616 $result->addErrors($r->getErrors());
617
619 if (!$order = $this->getOrder())
620 {
621 throw new Main\ObjectNotFoundException('Entity "Order" not found');
622 }
623
624 $registry = Registry::getInstance(static::getRegistryType());
625
627 $entityMarker = $registry->getEntityMarkerClassName();
628 $entityMarker::addMarker($order, $payment, $r);
629 $order->setField('MARKED', 'Y');
630 }
631 }
632 return $result;
633 }
634
635 public function getBasketItemQuantity(BasketItem $basketItem) : float
636 {
637 $quantity = 0;
638
640 foreach ($this->collection as $payment)
641 {
642 $quantity += $payment->getBasketItemQuantity($basketItem);
643 }
644
645 return $quantity;
646 }
647
654 public function createClone(\SplObjectStorage $cloneEntity)
655 {
656 if ($this->isClone() && $cloneEntity->contains($this))
657 {
658 return $cloneEntity[$this];
659 }
660
662 $paymentCollectionClone = parent::createClone($cloneEntity);
663
664 if ($this->order)
665 {
666 if ($cloneEntity->contains($this->order))
667 {
668 $paymentCollectionClone->order = $cloneEntity[$this->order];
669 }
670 }
671
672 return $paymentCollectionClone;
673 }
674
680 public function isMarked()
681 {
682 if (!empty($this->collection) && is_array($this->collection))
683 {
685 foreach ($this->collection as $payment)
686 {
687 if ($payment->isMarked())
688 return true;
689 }
690 }
691
692 return false;
693 }
694
699 protected function deleteInternal($primary)
700 {
701 return Internals\PaymentTable::deleteWithItems($primary);
702 }
703
707 private static function getItemCollectionClassName()
708 {
709 $registry = Registry::getInstance(static::getRegistryType());
710 return $registry->getPaymentClassName();
711 }
712
717 public static function getList(array $parameters = array())
718 {
719 return Internals\PaymentTable::getList($parameters);
720 }
721
727 public static function getInnerPaySystemId()
728 {
729 return PaySystem\Manager::getInnerPaySystemId();
730 }
731}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
onItemModify(CollectableEntity $item, $name=null, $oldValue=null, $value=null)
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
static getList(array $parameters=array())
static getInstance($type)
Definition registry.php:183