1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
order.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale;
4
5use Bitrix\Main\Entity;
6use Bitrix\Main;
7use Bitrix\Main\Type;
8use Bitrix\Sale\Cashbox;
9use Bitrix\Sale\Internals;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Sale\Reservation\Configuration\ReserveCondition;
12
13Loc::loadMessages(__FILE__);
14
20{
23
26
29
31 protected $printedChecks = array();
32
33
37
41 public static function getRegistryType()
42 {
44 }
45
46
50 protected static function getFieldsMap()
51 {
53 }
54
58 public static function getUfId()
59 {
61 }
62
69 public function getPrintedChecks()
70 {
71 if (!$this->printedChecks
72 && !$this->isNew()
73 )
74 {
75 $this->printedChecks = $this->loadPrintedChecks();
76 }
77
78 return $this->printedChecks;
79 }
80
85 protected function loadPrintedChecks()
86 {
87 $result = [];
88
90 'filter' => [
91 '=ORDER_ID' => $this->getId()
92 ]
93 ]);
94
95 while ($data = $dbRes->fetch())
96 {
98 }
99
100 return $result;
101 }
102
103
109 public function addPrintedCheck($check)
110 {
111 $this->printedChecks[] = $check;
112 }
113
131 public function onShipmentCollectionModify($action, Shipment $shipment, $name = null, $oldValue = null, $value = null)
132 {
133 $result = new Result();
134
135 $registry = Registry::getInstance(static::getRegistryType());
136
138 $entityMarker = $registry->getEntityMarkerClassName();
139
141 {
142 if ((int)$this->getField('DELIVERY_ID') === $shipment->getDeliveryId())
143 {
144 $foundShipment = false;
145
147 foreach ($this->getShipmentCollection()->getNotSystemItems() as $entityShipment)
148 {
149 if ((int)$entityShipment->getField('DELIVERY_ID') > 0)
150 {
151 $foundShipment = true;
152 $this->setFieldNoDemand('DELIVERY_ID', $entityShipment->getField('DELIVERY_ID'));
153 break;
154 }
155 }
156
157 if (!$foundShipment && !$shipment->isSystem())
158 {
159 $systemShipment = $this->getShipmentCollection()->getSystemShipment();
160
161 if ((int)$systemShipment->getField('DELIVERY_ID') > 0)
162 {
163 $this->setFieldNoDemand('DELIVERY_ID', $systemShipment->getField('DELIVERY_ID'));
164 }
165 }
166 }
167 }
169 {
170 if ($shipment->getId() === 0)
171 {
172 $this->getPropertyCollection()->refreshRelated();
173 }
174
175 if (!$shipment->isSystem())
176 {
177 $this->setFieldNoDemand('DELIVERY_ID', $shipment->getDeliveryId());
178 }
179 }
180
182 {
183 return $result;
184 }
185
186 if ($name === "ALLOW_DELIVERY")
187 {
188 if ($this->isCanceled())
189 {
190 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'));
191 return $result;
192 }
193
194 $r = $shipment->deliver();
195 if ($r->isSuccess())
196 {
197 $eventManager = Main\EventManager::getInstance();
198 if ($eventManager->findEventHandlers('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER))
199 {
200 $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER, array(
201 'ENTITY' =>$shipment
202 ));
203 $event->send();
204 }
205 }
206 else
207 {
208 $result->addErrors($r->getErrors());
209 }
210
211 if (
213 && Configuration::getProductReservationCondition() === ReserveCondition::ON_ALLOW_DELIVERY
214 )
215 {
216 if ($value === "Y")
217 {
218 $r = $shipment->tryReserve();
219 if (!$r->isSuccess())
220 {
221 $result->addErrors($r->getErrors());
222 }
223
224 if ($r->hasWarnings())
225 {
226 $result->addWarnings($r->getWarnings());
227 $entityMarker::addMarker($this, $shipment, $r);
228 if (!$shipment->isSystem())
229 {
230 $shipment->setField('MARKED', 'Y');
231 }
232 }
233 }
234 elseif (!$shipment->isShipped())
235 {
236 $r = $shipment->tryUnreserve();
237 if (!$r->isSuccess())
238 {
239 $result->addErrors($r->getErrors());
240 }
241
242 if ($r->hasWarnings())
243 {
244 $result->addWarnings($r->getWarnings());
245 $entityMarker::addMarker($this, $shipment, $r);
246 if (!$shipment->isSystem())
247 {
248 $shipment->setField('MARKED', 'Y');
249 }
250 }
251 }
252
253 if (!$result->isSuccess())
254 {
255 return $result;
256 }
257 }
258
259 $orderStatus = null;
260
261 if ($oldValue === "N")
262 {
264 {
265 $orderStatus = Main\Config\Option::get('sale', 'status_on_allow_delivery', '');
266 }
267 elseif ($this->getShipmentCollection()->hasAllowDelivery())
268 {
269 $orderStatus = Main\Config\Option::get('sale', 'status_on_allow_delivery_one_of', '');
270 }
271 }
272
273 if ($orderStatus !== null && $this->getField('STATUS_ID') != static::getFinalStatus())
274 {
275 if ((string)$orderStatus != '')
276 {
277 $r = $this->setField('STATUS_ID', $orderStatus);
278 if (!$r->isSuccess())
279 {
280 $result->addErrors($r->getErrors());
281 }
282
283 if ($r->hasWarnings())
284 {
285 $result->addWarnings($r->getWarnings());
286 $entityMarker::addMarker($this, $this, $r);
287 $this->setField('MARKED', 'Y');
288 }
289 }
290 }
291
292 if (Configuration::needShipOnAllowDelivery() && $value === "Y")
293 {
294 if (!$shipment->isEmpty())
295 {
296 $r = $shipment->setField("DEDUCTED", "Y");
297 if (!$r->isSuccess())
298 {
299 $result->addErrors($r->getErrors());
300 }
301
302 if ($r->hasWarnings())
303 {
304 $result->addWarnings($r->getWarnings());
305 $entityMarker::addMarker($this, $shipment, $r);
306 if (!$shipment->isSystem())
307 {
308 $shipment->setField('MARKED', 'Y');
309 }
310 }
311 }
312 }
313
314 $isAllowDelivery = $this->getShipmentCollection()->isAllowDelivery();
315 if (
316 $isAllowDelivery
317 && $this->getField('ALLOW_DELIVERY') === 'N'
318 )
319 {
320 $this->setFieldNoDemand('DATE_ALLOW_DELIVERY', new Type\DateTime());
321 }
322
323 $this->setFieldNoDemand('ALLOW_DELIVERY', $isAllowDelivery ? "Y" : "N");
324 }
325 elseif ($name === "DEDUCTED")
326 {
327 if ($this->isCanceled())
328 {
329 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_SHIPMENT_ORDER_CANCELED'), 'SALE_ORDER_SHIPMENT_ORDER_CANCELED'));
330 return $result;
331 }
332
333 if (
335 && Configuration::getProductReservationCondition() == ReserveCondition::ON_SHIP
336 )
337 {
338 if ($value === "Y")
339 {
341 $r = $shipment->tryReserve();
342 if (!$r->isSuccess())
343 {
344 $result->addErrors($r->getErrors());
345 }
346
347 if ($r->hasWarnings())
348 {
349 $result->addWarnings($r->getWarnings());
350 $entityMarker::addMarker($this, $shipment, $r);
351 if (!$shipment->isSystem())
352 {
353 $shipment->setField('MARKED', 'Y');
354 }
355 }
356 }
357 else
358 {
359 $r = $shipment->tryUnreserve();
360 if (!$r->isSuccess())
361 {
362 $result->addErrors($r->getErrors());
363 }
364
365 if ($r->hasWarnings())
366 {
367 $result->addWarnings($r->getWarnings());
368 $entityMarker::addMarker($this, $shipment, $r);
369 if (!$shipment->isSystem())
370 {
371 $shipment->setField('MARKED', 'Y');
372 }
373 }
374 }
375 }
376
377 if ($value === "Y")
378 {
380 $r = $shipment->tryShip();
381 if (!$r->isSuccess())
382 {
383 $result->addErrors($r->getErrors());
384 }
385
386 if ($r->hasWarnings())
387 {
388 $result->addWarnings($r->getWarnings());
389 $entityMarker::addMarker($this, $shipment, $r);
390 if (!$shipment->isSystem())
391 {
392 $shipment->setField('MARKED', 'Y');
393 }
394 }
395
396 }
397 elseif ($oldValue === 'Y')
398 {
399 $r = $shipment->tryUnship();
400 if (!$r->isSuccess())
401 {
402 $result->addErrors($r->getErrors());
403 }
404
405 if ($r->hasWarnings())
406 {
407 $result->addWarnings($r->getWarnings());
408 $entityMarker::addMarker($this, $shipment, $r);
409 if (!$shipment->isSystem())
410 {
411 $shipment->setField('MARKED', 'Y');
412 }
413 }
414
415 if (
417 && $shipment->needReservation()
418 )
419 {
420 $r = $shipment->tryReserve();
421 if (!$r->isSuccess())
422 {
423 $result->addErrors($r->getErrors());
424 }
425
426 if ($r->hasWarnings())
427 {
428 $result->addWarnings($r->getWarnings());
429 $entityMarker::addMarker($this, $shipment, $r);
430 if (!$shipment->isSystem())
431 {
432 $shipment->setField('MARKED', 'Y');
433 }
434 }
435 }
436 }
437
438 if (!$result->isSuccess())
439 {
440 return $result;
441 }
442
443 $orderStatus = null;
444
445 $allowSetStatus = false;
446
447 if ($oldValue === "N")
448 {
449 if ($this->getShipmentCollection()->isShipped())
450 {
451 $orderStatus = Main\Config\Option::get('sale', 'status_on_shipped_shipment', '');
452 }
453 elseif ($this->getShipmentCollection()->hasShipped())
454 {
455 $orderStatus = Main\Config\Option::get('sale', 'status_on_shipped_shipment_one_of', '');
456 }
457 $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
458 }
459 else
460 {
461 $fields = $this->getFields();
462 $originalValues = $fields->getOriginalValues();
463 if (!empty($originalValues['STATUS_ID']))
464 {
465 $orderStatus = $originalValues['STATUS_ID'];
466 $allowSetStatus = true;
467 }
468 }
469
470 if (
471 (string)$orderStatus !== ''
472 && $allowSetStatus
473 )
474 {
475 $r = $this->setField('STATUS_ID', $orderStatus);
476 if (!$r->isSuccess())
477 {
478 $result->addErrors($r->getErrors());
479 }
480 elseif ($r->hasWarnings())
481 {
482 $result->addWarnings($r->getWarnings());
483 $entityMarker::addMarker($this, $this, $r);
484 $this->setField('MARKED', 'Y');
485 }
486 }
487
488 $isShipped = $this->getShipmentCollection()->isShipped();
489 $this->setFieldNoDemand('DEDUCTED', $isShipped ? "Y" : "N");
490
491 if ($isShipped)
492 {
493 if (strval($shipment->getField('DATE_DEDUCTED')) != '')
494 {
495 $this->setFieldNoDemand('DATE_DEDUCTED', $shipment->getField('DATE_DEDUCTED'));
496 }
497 if (strval($shipment->getField('EMP_DEDUCTED_ID')) != '')
498 {
499 $this->setFieldNoDemand('EMP_DEDUCTED_ID', $shipment->getField('EMP_DEDUCTED_ID'));
500 }
501 }
502 }
503 elseif ($name == "MARKED")
504 {
505 if ($value == "Y")
506 {
508 $r = $this->setField('MARKED', 'Y');
509 if (!$r->isSuccess())
510 {
511 $result->addErrors($r->getErrors());
512 }
513 }
514 }
515 elseif ($name == "REASON_MARKED")
516 {
517 $r = $this->setReasonMarked($value);
518 if (!$r->isSuccess())
519 {
520 $result->addErrors($r->getErrors());
521 }
522 }
523 elseif ($name == "BASE_PRICE_DELIVERY")
524 {
525 if ($this->isCanceled())
526 {
527 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
528 return $result;
529 }
530
531 $discount = $this->getDiscount();
532 $discount->setCalculateShipments($shipment);
533 }
534 elseif ($name == "PRICE_DELIVERY")
535 {
536 if ($this->isCanceled())
537 {
538 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
539 return $result;
540 }
541
542 $deliveryPrice = ($this->isNew()) ? $value : $this->getField("PRICE_DELIVERY") - $oldValue + $value;
543 $this->setFieldNoDemand(
544 "PRICE_DELIVERY",
545 $deliveryPrice
546 );
547
549 $r = $this->setField(
550 "PRICE",
551 $this->getField("PRICE") - $oldValue + $value
552 );
553
554 if (!$r->isSuccess())
555 {
556 $result->addErrors($r->getErrors());
557 }
558
559 }
560 elseif ($name == "DELIVERY_ID")
561 {
562 if ($shipment->isSystem() || intval($shipment->getField('DELIVERY_ID')) <= 0 )
563 {
564 return $result;
565 }
566
567 $this->setFieldNoDemand('DELIVERY_ID', $shipment->getField('DELIVERY_ID'));
568
569 $this->getPropertyCollection()->refreshRelated();
570
571 }
572 elseif ($name == "TRACKING_NUMBER")
573 {
574 if ($shipment->isSystem())
575 {
576 return $result;
577 }
578
579 $this->setFieldNoDemand('TRACKING_NUMBER', $shipment->getField('TRACKING_NUMBER'));
580 }
581
582 if ($value != $oldValue)
583 {
584 $fields = $this->fields->getChangedValues();
585 if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields))
586 {
587 parent::setField("UPDATED_1C", "N");
588 }
589 }
590
591 return $result;
592 }
593
594 public function onTradeBindingCollectionModify($action, TradeBindingEntity $entity, $name = null, $oldValue = null, $value = null)
595 {
597 {
598 if (
599 $entity->getId() === 0
600 && $entity->getField('TRADING_PLATFORM_ID') > 0
601 )
602 {
603 $this->propertyCollection = $this->loadPropertyCollection();
604 }
605 }
607 {
608 if ($name === 'TRADING_PLATFORM_ID')
609 {
610 $this->propertyCollection = $this->loadPropertyCollection();
611 }
612 }
613
614 return new Result();
615 }
616
623 public function resetData($select = array('PRICE'))
624 {
625 $this->getShipmentCollection()->resetData();
626
627 parent::resetData($select);
628 }
629
635 public function getBasePrice(): float
636 {
637 $price = parent::getBasePrice();
638
639 return $price + $this->getShipmentCollection()->getBasePriceDelivery();
640 }
641
651 public function setBasket(BasketBase $basket)
652 {
653 $result = new Result();
654
655 $isStartField = $this->isStartField();
656
657 $r = parent::setBasket($basket);
658 if (!$r->isSuccess())
659 {
660 $result->addErrors($r->getErrors());
661 return $result;
662 }
663
664 $result = $this->getShipmentCollection()->resetCollection();
665 if (!$r->isSuccess())
666 {
667 $result->addErrors($r->getErrors());
668 return $result;
669 }
670
671 if (!$this->isMathActionOnly())
672 {
673 $r = $this->refreshData();
674 if (!$r->isSuccess())
675 {
676 $result->addErrors($r->getErrors());
677 }
678 }
679
680 if ($isStartField)
681 {
682 $hasMeaningfulFields = $this->hasMeaningfulField();
683
684 $r = $this->doFinalAction($hasMeaningfulFields);
685 if (!$r->isSuccess())
686 {
687 $result->addErrors($r->getErrors());
688 }
689 }
690
691 return $result;
692 }
693
703 public function appendBasket(BasketBase $basket)
704 {
705 $result = new Result();
706
707 $isStartField = $this->isStartField();
708
709 $r = parent::appendBasket($basket);
710 if (!$r->isSuccess())
711 {
712 $result->addErrors($r->getErrors());
713 return $result;
714 }
715
717 $result = $this->getShipmentCollection()->resetCollection();
718 if (!$r->isSuccess())
719 {
720 $result->addErrors($r->getErrors());
721 return $result;
722 }
723
724 if (!$this->isMathActionOnly())
725 {
727 $r = $this->refreshData();
728 if (!$r->isSuccess())
729 {
730 $result->addErrors($r->getErrors());
731 }
732 }
733
734 if ($isStartField)
735 {
736 $hasMeaningfulFields = $this->hasMeaningfulField();
737
739 $r = $this->doFinalAction($hasMeaningfulFields);
740 if (!$r->isSuccess())
741 {
742 $result->addErrors($r->getErrors());
743 }
744 }
745
746 return $result;
747 }
748
756 public function getShipmentCollection()
757 {
758 if (empty($this->shipmentCollection))
759 {
760 $this->shipmentCollection = $this->loadShipmentCollection();
761 }
762
763 return $this->shipmentCollection;
764 }
765
775 {
776 if (empty($this->tradeBindingCollection))
777 {
778 $this->tradeBindingCollection = $this->loadTradeBindingCollection();
779 }
780
781 return $this->tradeBindingCollection;
782 }
783
789 public function getPaymentCollection()
790 {
791 if (empty($this->paymentCollection))
792 {
793 $this->paymentCollection = $this->loadPaymentCollection();
794 }
795
797 }
798
806 public function loadShipmentCollection()
807 {
808 $registry = Registry::getInstance(static::getRegistryType());
809
811 $shipmentCollectionClassName = $registry->getShipmentCollectionClassName();
812 return $shipmentCollectionClassName::load($this);
813 }
814
821 public function loadPaymentCollection()
822 {
823 $registry = Registry::getInstance(static::getRegistryType());
824
826 $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
827 return $paymentCollectionClassName::load($this);
828 }
829
836 protected function loadTradeBindingCollection()
837 {
838 $registry = Registry::getInstance(static::getRegistryType());
839
841 $tradeBindingCollection = $registry->get(Registry::ENTITY_TRADE_BINDING_COLLECTION);
842
843 return $tradeBindingCollection::load($this);
844 }
845
852 protected static function deleteEntitiesNoDemand($orderId)
853 {
854 $r = parent::deleteEntitiesNoDemand($orderId);
855 if (!$r->isSuccess())
856 return $r;
857
858 $registry = Registry::getInstance(static::getRegistryType());
859
861 $shipmentClassName = $registry->getShipmentClassName();
862 $shipmentClassName::deleteNoDemand($orderId);
863 if (!$r->isSuccess())
864 return $r;
865
867 $paymentClassName = $registry->getPaymentClassName();
868 $paymentClassName::deleteNoDemand($orderId);
869 if (!$r->isSuccess())
870 return $r;
871
872 return new Result();
873 }
874
884 protected static function deleteEntities(OrderBase $order)
885 {
886 parent::deleteEntities($order);
887
889 foreach ($order->getShipmentCollection() as $shipment)
890 {
891 $shipment->delete();
892 }
893
895 foreach ($order->getPaymentCollection() as $payment)
896 {
897 $payment->delete();
898 }
899 }
900
904 public function isShipped()
905 {
906 return $this->getShipmentCollection()->isShipped();
907 }
908
923 public function onPaymentCollectionModify($action, Payment $payment, $name = null, $oldValue = null, $value = null)
924 {
925 $result = new Result();
926
928 {
929 if ($this->getField('PAY_SYSTEM_ID') == $payment->getPaymentSystemId())
930 {
932 foreach ($payment->getCollection() as $entityPayment)
933 {
934 if (intval($entityPayment->getField('PAY_SYSTEM_ID')) > 0
935 && intval($entityPayment->getField('PAY_SYSTEM_ID')) != $payment->getPaymentSystemId())
936 {
937 $this->setFieldNoDemand('PAY_SYSTEM_ID', $entityPayment->getField('PAY_SYSTEM_ID'));
938 break;
939 }
940 }
941 }
942 }
944 {
945 if ($payment->getId() === 0)
946 {
947 $this->getPropertyCollection()->refreshRelated();
948 }
949 }
950
952 {
953 return $result;
954 }
955
956 if (($name == "CURRENCY") && ($value != $this->getField("CURRENCY")))
957 {
958 throw new Main\NotImplementedException();
959 }
960
961 if ($name == "SUM" || $name == "PAID")
962 {
963 if ($this->isCanceled())
964 {
965 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PAID_ORDER_CANCELED'), 'SALE_ORDER_PAID_ORDER_CANCELED'));
966 return $result;
967 }
968
969 if ($name == "SUM"
970 && !$payment->isPaid()
971 )
972 {
973 return $result;
974 }
975
976 $r = $this->syncOrderAndPayments($payment);
977 if (!$r->isSuccess())
978 {
979 $result->addErrors($r->getErrors());
980 }
981 elseif ($r->hasWarnings())
982 {
983 $result->addWarnings($r->getWarnings());
984 }
985 }
986 elseif ($name == "PAY_SYSTEM_ID")
987 {
988 $this->setFieldNoDemand('PAY_SYSTEM_ID', $payment->getField('PAY_SYSTEM_ID'));
989
990 $this->getPropertyCollection()->refreshRelated();
991 }
992 elseif ($name == "DATE_PAID")
993 {
994 $this->setFieldNoDemand('DATE_PAYED', $payment->getField('DATE_PAID'));
995 }
996 elseif ($name == "PAY_VOUCHER_NUM")
997 {
998 $this->setFieldNoDemand('PAY_VOUCHER_NUM', $payment->getField('PAY_VOUCHER_NUM'));
999 }
1000 elseif ($name == "PAY_VOUCHER_DATE")
1001 {
1002 $this->setFieldNoDemand('PAY_VOUCHER_DATE', $payment->getField('PAY_VOUCHER_DATE'));
1003 }
1004 elseif ($name == "EMP_PAID_ID")
1005 {
1006 $this->setFieldNoDemand('EMP_PAYED_ID', $payment->getField('EMP_PAID_ID'));
1007 }
1008 elseif ($name == "MARKED")
1009 {
1010 if ($value == "Y")
1011 {
1013 $r = $this->setField('MARKED', 'Y');
1014 if (!$r->isSuccess())
1015 {
1016 $result->addErrors($r->getErrors());
1017 }
1018 }
1019 }
1020 elseif ($name == "REASON_MARKED")
1021 {
1022 $r = $this->setReasonMarked($value);
1023 if (!$r->isSuccess())
1024 {
1025 $result->addErrors($r->getErrors());
1026 }
1027 }
1028
1029 if ($value != $oldValue)
1030 {
1031 $fields = $this->fields->getChangedValues();
1032 if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields) && $name != 'UPDATED_1C')
1033 {
1034 parent::setField("UPDATED_1C", "N");
1035 }
1036 }
1037
1038 return $result;
1039 }
1040
1053 protected function onFieldModify($name, $oldValue, $value)
1054 {
1055 $result = parent::onFieldModify($name, $oldValue, $value);
1056
1057 if ($name == "PRICE")
1058 {
1059 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1060 if (!$r->isSuccess())
1061 {
1062 return $result->addErrors($r->getErrors());
1063 }
1064
1065 $r = $this->getPaymentCollection()->onOrderModify($name, $oldValue, $value);
1066 if (!$r->isSuccess())
1067 {
1068 return $result->addErrors($r->getErrors());
1069 }
1070
1071 $r = $this->syncOrderAndPayments();
1072 if (!$r->isSuccess())
1073 {
1074 $result->addErrors($r->getErrors());
1075 }
1076 }
1077 elseif($name == "MARKED")
1078 {
1079 global $USER;
1080
1081 if (is_object($USER))
1082 {
1083 $this->setField('EMP_MARKED_ID', $USER->GetID());
1084 }
1085
1086 $this->setField('DATE_MARKED', new Main\Type\DateTime());
1087
1088 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1089 if (!$r->isSuccess())
1090 {
1091 return $result->addErrors($r->getErrors());
1092 }
1093 }
1094
1095 return $result;
1096 }
1097
1106 protected function onOrderModify($name, $oldValue, $value)
1107 {
1108 $result = new Result();
1109
1110 $r = $this->getPaymentCollection()->onOrderModify($name, $oldValue, $value);
1111 if (!$r->isSuccess())
1112 {
1113 return $result->addErrors($r->getErrors());
1114 }
1115
1116 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1117 if (!$r->isSuccess())
1118 {
1119 return $result->addErrors($r->getErrors());
1120 }
1121
1122 return $result;
1123 }
1124
1137 public function onBeforeBasketItemDelete(BasketItem $basketItem)
1138 {
1139 $result = new Result();
1140
1141 $r = $this->getShipmentCollection()->onBeforeBasketItemDelete($basketItem);
1142 if (!$r->isSuccess())
1143 {
1144 return $result->addErrors($r->getErrors());
1145 }
1146
1147 $r = $this->getPaymentCollection()->onBeforeBasketItemDelete($basketItem);
1148 if (!$r->isSuccess())
1149 {
1150 return $result->addErrors($r->getErrors());
1151 }
1152
1153
1154 return $result;
1155 }
1156
1174 public function onBasketModify($action, BasketItemBase $basketItem, $name = null, $oldValue = null, $value = null)
1175 {
1176 $result = new Result();
1177
1179 {
1180 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1181 if (!$r->isSuccess())
1182 {
1183 $result->addErrors($r->getErrors());
1184 return $result;
1185 }
1186
1187 return $result;
1188 }
1190 {
1191 if ($basketItem->getField('ORDER_ID'))
1192 {
1193 return $result;
1194 }
1195
1196 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1197 }
1199 {
1200 return $result;
1201 }
1202
1203 if ($name === "QUANTITY")
1204 {
1205 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1206 if (!$r->isSuccess())
1207 {
1208 $result->addErrors($r->getErrors());
1209 return $result;
1210 }
1211
1212 $r = $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1213 if (!$r->isSuccess())
1214 {
1215 $result->addErrors($r->getErrors());
1216 return $result;
1217 }
1218 elseif ($r->hasWarnings())
1219 {
1220 $result->addWarnings($r->getWarnings());
1221 }
1222
1223 return $result;
1224 }
1225 elseif ($name === "PRICE")
1226 {
1227 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1228 if (!$r->isSuccess())
1229 {
1230 $result->addErrors($r->getErrors());
1231 return $result;
1232 }
1233
1234 if ($this->getId() === 0 && !$this->isMathActionOnly())
1235 {
1236 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1237 }
1238 }
1239 elseif ($name === 'WEIGHT')
1240 {
1241 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1242 }
1243 else
1244 {
1245 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1246 if (!$r->isSuccess())
1247 {
1248 $result->addErrors($r->getErrors());
1249 return $result;
1250 }
1251 }
1252
1253 return $result;
1254 }
1255
1259 public function onBeforeBasketRefresh()
1260 {
1261 $result = new Result();
1262
1263 $r = $this->getShipmentCollection()->tryUnreserve();
1264 if (!$r->isSuccess())
1265 {
1266 $result->addErrors($r->getErrors());
1267 }
1268
1269 return $result;
1270 }
1271
1275 public function onAfterBasketRefresh()
1276 {
1277 $result = new Result();
1278
1280 foreach ($this->getShipmentCollection() as $shipment)
1281 {
1282 if (
1283 $shipment->isShipped()
1284 || !$shipment->needReservation()
1286 )
1287 {
1288 continue;
1289 }
1290
1291 $r = $shipment->tryReserve();
1292 if (!$r->isSuccess())
1293 {
1294 $result->addErrors($r->getErrors());
1295 }
1296 }
1297
1298 return $result;
1299 }
1300
1313 private function syncOrderAndPayments(Payment $payment = null)
1314 {
1315 $result = new Result();
1316
1317 $oldPaid = $this->getField('PAYED');
1319 $sumPaid = $paymentCollection->getPaidSum();
1320
1321 if ($payment)
1322 {
1323 $finalSumPaid = $sumPaid;
1324
1325 if ($payment->isPaid())
1326 {
1327 if ($sumPaid > $this->getPrice())
1328 {
1329 $finalSumPaid = $this->getSumPaid() + $payment->getSum();
1330 }
1331 }
1332 else
1333 {
1334 $r = $this->syncOrderPaymentPaid($payment);
1335 if ($r->isSuccess())
1336 {
1337 $paidResult = $r->getData();
1338 if (isset($paidResult['SUM_PAID']))
1339 {
1340 $finalSumPaid = $paidResult['SUM_PAID'];
1341 }
1342 }
1343 else
1344 {
1345 return $result->addErrors($r->getErrors());
1346 }
1347 }
1348 }
1349 else
1350 {
1351 $finalSumPaid = $this->getSumPaid();
1352
1353 $r = $this->syncOrderPaid();
1354 if (!$r->isSuccess())
1355 {
1356 return $result->addErrors($r->getErrors());
1357 }
1358
1359 $paidResult = $r->getData();
1360 if (isset($paidResult['SUM_PAID']))
1361 {
1362 $finalSumPaid = $paidResult['SUM_PAID'];
1363 }
1364 }
1365
1366 $paid = false;
1367
1368 if ($finalSumPaid >= 0 && $paymentCollection->hasPaidPayment()
1369 && PriceMaths::roundPrecision($this->getPrice()) <= PriceMaths::roundPrecision($finalSumPaid))
1370 {
1371 $paid = true;
1372 }
1373
1374 $this->setFieldNoDemand('PAYED', $paid ? "Y" : "N");
1375
1376 if ($this->getFields()->isChanged('PAYED'))
1377 {
1378 Internals\EventsPool::addEvent(
1379 $this->getInternalId(),
1381 [
1382 'ENTITY' => $this,
1383 ]
1384 );
1385
1386 Internals\EventsPool::addEvent(
1387 $this->getInternalId(),
1389 [
1390 'ENTITY' => $this,
1391 ]
1392 );
1393 }
1394
1395 if ($finalSumPaid > 0 && $finalSumPaid > $this->getPrice())
1396 {
1397 if (!$payment || $payment->isPaid())
1398 {
1399 Internals\UserBudgetPool::addPoolItem($this, $finalSumPaid - $this->getPrice(), Internals\UserBudgetPool::BUDGET_TYPE_EXCESS_SUM_PAID, $payment);
1400 }
1401
1402 $finalSumPaid = $this->getPrice();
1403 }
1404
1405 $this->setFieldNoDemand('SUM_PAID', $finalSumPaid);
1406
1407 $r = $this->onAfterSyncPaid($oldPaid);
1408 if (!$r->isSuccess())
1409 {
1410 $result->addErrors($r->getErrors());
1411 }
1412 elseif ($r->hasWarnings())
1413 {
1414 $result->addWarnings($r->getWarnings());
1415 }
1416
1417 return $result;
1418 }
1419
1424 private function syncOrderPaymentPaid(Payment $payment)
1425 {
1426 $result = new Result();
1427
1428 if ($payment->isPaid())
1429 {
1430 return $result;
1431 }
1432
1433 $sumPaid = $this->getPaymentCollection()->getPaidSum();
1434
1435 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($this);
1436
1437 $debitSum = $payment->getSum();
1438
1439 $maxPaid = $payment->getSum() + $sumPaid - $this->getSumPaid();
1440
1441 if ($maxPaid >= $payment->getSum())
1442 {
1443 $finalSumPaid = $this->getSumPaid();
1444 }
1445 else
1446 {
1447 $debitSum = $maxPaid;
1448 $finalSumPaid = $sumPaid;
1449 }
1450
1451 if ($debitSum > 0 && $payment->isInner())
1452 {
1453 if (PriceMaths::roundPrecision($debitSum) > PriceMaths::roundPrecision($userBudget))
1454 {
1455 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_PAYMENT_CANCELLED_PAID'), 'SALE_ORDER_PAYMENT_NOT_ENOUGH_USER_BUDGET_SYNCPAID') );
1456 return $result;
1457 }
1458
1459 Internals\UserBudgetPool::addPoolItem($this, ($debitSum * -1), Internals\UserBudgetPool::BUDGET_TYPE_ORDER_CANCEL_PART, $payment);
1460 }
1461
1462 $result->setData(array('SUM_PAID' => $finalSumPaid));
1463
1464 return $result;
1465 }
1466
1470 private function syncOrderPaid()
1471 {
1472 $result = new Result();
1473
1474 if ($this->getSumPaid() == $this->getPrice())
1475 return $result;
1476
1477 $debitSum = $this->getPrice() - $this->getSumPaid();
1478
1479 $sumPaid = $this->getPaymentCollection()->getPaidSum();
1480 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($this);
1481
1482 $bePaid = $sumPaid - $this->getSumPaid();
1483
1484 if ($bePaid > 0)
1485 {
1486 if ($debitSum > $bePaid)
1487 {
1488 $debitSum = $bePaid;
1489 }
1490
1491 if ($debitSum >= $userBudget)
1492 {
1493 $debitSum = $userBudget;
1494 }
1495
1496 if ($userBudget >= $debitSum && $debitSum > 0)
1497 {
1498 Internals\UserBudgetPool::addPoolItem($this, ($debitSum * -1), Internals\UserBudgetPool::BUDGET_TYPE_ORDER_PAY);
1499
1500 $finalSumPaid = $this->getSumPaid() + $debitSum;
1501 $result->setData(['SUM_PAID' => $finalSumPaid]);
1502 }
1503 }
1504
1505 return $result;
1506 }
1507
1513 protected function getStatusOnPaid()
1514 {
1515 return Main\Config\Option::get('sale', 'status_on_paid', '');
1516 }
1517
1523 protected function getStatusOnPartialPaid()
1524 {
1525 return Main\Config\Option::get('sale', 'status_on_half_paid', '');
1526 }
1527
1539 private function onAfterSyncPaid($oldPaid = null)
1540 {
1541 $result = new Result();
1542
1543 $paymentCollection = $this->getPaymentCollection();
1544
1545 $shipmentCollection = $this->getShipmentCollection();
1546
1547 $orderStatus = null;
1548
1549 $allowSetStatus = false;
1550
1551 if ($oldPaid == "N")
1552 {
1553 if ($this->isPaid())
1554 {
1555 $orderStatus = $this->getStatusOnPaid();
1556 }
1557 elseif ($paymentCollection->hasPaidPayment())
1558 {
1559 $orderStatus = $this->getStatusOnPartialPaid();
1560 }
1561
1562 $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
1563 }
1564
1565 if ($orderStatus !== null && $allowSetStatus)
1566 {
1567 if (strval($orderStatus) != '')
1568 {
1569 $r = $this->setField('STATUS_ID', $orderStatus);
1570 if (!$r->isSuccess())
1571 {
1572 $result->addErrors($r->getErrors());
1573 }
1574 elseif ($r->hasWarnings())
1575 {
1576 $result->addWarnings($r->getWarnings());
1577
1578 $registry = Registry::getInstance(static::getRegistryType());
1579
1581 $entityMarker = $registry->getEntityMarkerClassName();
1582 $entityMarker::addMarker($this, $this, $r);
1583 $this->setField('MARKED', 'Y');
1584 }
1585 }
1586 }
1587
1589 {
1590 $r = null;
1591
1592 if (Configuration::getProductReservationCondition() == ReserveCondition::ON_PAY)
1593 {
1594 if ($paymentCollection->hasPaidPayment())
1595 {
1596 $r = $shipmentCollection->tryReserve();
1597 }
1598 else
1599 {
1600 $r = $shipmentCollection->tryUnreserve();
1601 }
1602 }
1603 elseif (Configuration::getProductReservationCondition() == ReserveCondition::ON_FULL_PAY)
1604 {
1605 if ($oldPaid == "N" && $this->isPaid())
1606 {
1607 $r = $shipmentCollection->tryReserve();
1608 }
1609 elseif ($oldPaid == "Y" && !$this->isPaid())
1610 {
1611 $r = $shipmentCollection->tryUnreserve();
1612 }
1613 }
1614
1615 if ($r !== null)
1616 {
1617 if (!$r->isSuccess())
1618 {
1619 $result->addErrors($r->getErrors());
1620 }
1621 elseif ($r->hasWarnings())
1622 {
1623 $result->addWarnings($r->getWarnings());
1624 }
1625 }
1626 }
1627
1628 $allowDelivery = null;
1629
1631 {
1632 if ($oldPaid == "N" && $paymentCollection->hasPaidPayment())
1633 {
1634 $allowDelivery = true;
1635 }
1636 elseif ($oldPaid == "Y" && !$paymentCollection->hasPaidPayment())
1637 {
1638 $allowDelivery = false;
1639 }
1640 }
1642 {
1643 if ($oldPaid == "N" && $this->isPaid())
1644 {
1645 $allowDelivery = true;
1646 }
1647 elseif ($oldPaid == "Y" && !$this->isPaid())
1648 {
1649 $allowDelivery = false;
1650 }
1651 }
1652
1653 if ($allowDelivery !== null)
1654 {
1655 if ($allowDelivery)
1656 {
1657 $r = $shipmentCollection->allowDelivery();
1658 }
1659 else
1660 {
1661 $r = $shipmentCollection->disallowDelivery();
1662 }
1663
1664 if (!$r->isSuccess())
1665 {
1666 $result->addErrors($r->getErrors());
1667 }
1668 }
1669
1670 return $result;
1671 }
1672
1681 protected function refreshInternal()
1682 {
1683 $result = parent::refreshInternal();
1684 if (!$result->isSuccess())
1685 {
1686 return $result;
1687 }
1688
1689 return $this->getShipmentCollection()->refreshData();
1690 }
1691
1701 public function applyDiscount(array $data)
1702 {
1703 $r = parent::applyDiscount($data);
1704 if (!$r->isSuccess())
1705 {
1706 return $r;
1707 }
1708
1709 if (isset($data['SHIPMENT']) && intval($data['SHIPMENT']) > 0
1710 && (isset($data['PRICE_DELIVERY']) && floatval($data['PRICE_DELIVERY']) >= 0
1711 || isset($data['DISCOUNT_PRICE']) && floatval($data['DISCOUNT_PRICE']) >= 0))
1712 {
1713 if ($shipment = $this->getShipmentCollection()->getItemByShipmentCode($data['SHIPMENT']))
1714 {
1715 if (!$shipment->isCustomPrice())
1716 {
1717 $data['PRICE_DELIVERY'] = (float)$data['PRICE_DELIVERY'];
1718 $data['DISCOUNT_PRICE'] = (float)$data['DISCOUNT_PRICE'];
1719 if ($data['PRICE_DELIVERY'] >= 0 && $data['PRICE_DELIVERY'] != $shipment->getPrice())
1720 {
1721 $data['PRICE_DELIVERY'] = PriceMaths::roundPrecision(floatval($data['PRICE_DELIVERY']));
1722 $shipment->setField('PRICE_DELIVERY', $data['PRICE_DELIVERY']);
1723 }
1724
1725 if ($data['DISCOUNT_PRICE'] != $shipment->getField('DISCOUNT_PRICE'))
1726 {
1727 $data['DISCOUNT_PRICE'] = PriceMaths::roundPrecision(floatval($data['DISCOUNT_PRICE']));
1728 $shipment->setField('DISCOUNT_PRICE', $data['DISCOUNT_PRICE']);
1729 }
1730 }
1731
1732 }
1733 }
1734
1735 return new Result();
1736 }
1737
1745 public static function lock($id)
1746 {
1747 global $USER;
1748
1749 $result = new Result();
1750 $id = (int)$id;
1751 if ($id <= 0)
1752 {
1753 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1754 return $result;
1755 }
1756
1757 return static::updateInternal($id, array(
1758 'DATE_LOCK' => new Main\Type\DateTime(),
1759 'LOCKED_BY' => (is_object($USER) ? $USER->GetID(): false)
1760 ));
1761 }
1762
1771 public static function unlock($id)
1772 {
1773 global $USER;
1774
1775 $result = new Result();
1776 $id = (int)$id;
1777 if ($id <= 0)
1778 {
1779 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1780 return $result;
1781 }
1782
1783 if(!$order = static::load($id))
1784 {
1785 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_ENTITY_NOT_FOUND'), 'SALE_ORDER_ENTITY_NOT_FOUND') );
1786 return $result;
1787 }
1788
1789 $userRights = \CMain::getUserRight("sale", $USER->getUserGroupArray(), "Y", "Y");
1790
1791 if (($userRights >= "W") || ($order->getField("LOCKED_BY") == $USER->getID()))
1792 {
1793 return static::updateInternal($id, array(
1794 'DATE_LOCK' => null,
1795 'LOCKED_BY' => null
1796 ));
1797 }
1798
1799 return $result;
1800 }
1801
1808 public static function isLocked($id)
1809 {
1811 $r = static::getLockedStatus($id);
1812 if ($r->isSuccess())
1813 {
1814 $lockResultData = $r->getData();
1815
1816 if (array_key_exists('LOCK_STATUS', $lockResultData)
1817 && $lockResultData['LOCK_STATUS'] == static::SALE_ORDER_LOCK_STATUS_RED)
1818 {
1819 return true;
1820 }
1821 }
1822
1823 return false;
1824 }
1825
1833 public static function getLockedStatus($id)
1834 {
1835 $result = new Result();
1836
1837 $res = static::getList(array(
1838 'filter' => array('=ID' => $id),
1839 'select' => array(
1840 'LOCKED_BY',
1841 'LOCK_STATUS',
1842 'DATE_LOCK'
1843 )
1844 ));
1845
1846 if ($data = $res->fetch())
1847 {
1848 $result->addData(array(
1849 'LOCKED_BY' => $data['LOCKED_BY'],
1850 'LOCK_STATUS' => $data['LOCK_STATUS'],
1851 'DATE_LOCK' => $data['DATE_LOCK'],
1852 ));
1853 }
1854
1855 return $result;
1856 }
1857
1866 public function verify()
1867 {
1868 $result = parent::verify();
1869
1870 $r = $this->getPaymentCollection()->verify();
1871 if (!$r->isSuccess())
1872 {
1873 $result->addErrors($r->getErrors());
1874 }
1875
1876 $r = $this->getShipmentCollection()->verify();
1877 if (!$r->isSuccess())
1878 {
1879 $result->addErrors($r->getErrors());
1880 }
1881
1882 return $result;
1883 }
1884
1889 public function getBusinessValueProviderInstance($mapping)
1890 {
1891 $providerInstance = null;
1892
1893 if (is_array($mapping) && isset($mapping['PROVIDER_KEY']))
1894 {
1895 switch ($mapping['PROVIDER_KEY'])
1896 {
1897 case 'ORDER':
1898 case 'PROPERTY':
1899 $providerInstance = $this;
1900 break;
1901 case 'USER':
1902 $providerInstance = $this->getField('USER_ID');
1903 break;
1904 case 'COMPANY':
1905 $providerInstance = $this->getField('COMPANY_ID');
1906 break;
1907 }
1908 }
1909
1910 return $providerInstance;
1911 }
1912
1919 public static function getList(array $parameters = array())
1920 {
1921 return Internals\OrderTable::getList($parameters);
1922 }
1923
1930 protected function cloneEntities(\SplObjectStorage $cloneEntity)
1931 {
1933 parent::cloneEntities($cloneEntity);
1934
1935 $orderClone = $cloneEntity[$this];
1936
1937 $orderClone->shipmentCollection = $this->getShipmentCollection()->createClone($cloneEntity);
1938
1939 $orderClone->paymentCollection = $this->getPaymentCollection()->createClone($cloneEntity);
1940
1941 $orderClone->tradeBindingCollection = $this->getTradeBindingCollection()->createClone($cloneEntity);
1942 }
1943
1947 public function isChanged()
1948 {
1949 if (parent::isChanged())
1950 {
1951 return true;
1952 }
1953
1954 if ($this->getPaymentCollection()->isChanged())
1955 {
1956 return true;
1957 }
1958
1959 if ($this->getShipmentCollection()->isChanged())
1960 {
1961 return true;
1962 }
1963
1964 return false;
1965 }
1966
1971 public function clearChanged()
1972 {
1973 parent::clearChanged();
1974
1975 $this->getPaymentCollection()->clearChanged();
1976
1977 $this->getShipmentCollection()->clearChanged();
1978
1979 $this->getTradeBindingCollection()->clearChanged();
1980 }
1981
1987 public function getDeliveryIdList() : array
1988 {
1989 $result = [];
1990
1992 foreach ($this->getShipmentCollection()->getNotSystemItems() as $shipment)
1993 {
1994 if ($shipment->getDeliveryId() > 0)
1995 {
1996 $result[] = $shipment->getDeliveryId();
1997 }
1998 }
1999
2000 return $result;
2001 }
2002
2006 public function getPaySystemIdList() : array
2007 {
2008 $result = [];
2009
2011 foreach ($this->getPaymentCollection() as $payment)
2012 {
2013 if ($payment->getPaymentSystemId() > 0)
2014 {
2015 $result[] = $payment->getPaymentSystemId();
2016 }
2017 }
2018
2019 return $result;
2020 }
2021
2025 protected function calculateVat()
2026 {
2027 $vatInfo = parent::calculateVat();
2028
2030 foreach ($this->getShipmentCollection() as $shipment)
2031 {
2032 $rate = $shipment->getVatRate();
2033 if ($rate)
2034 {
2035 $vatInfo['VAT_SUM'] += $shipment->getVatSum();
2036 $vatInfo['VAT_RATE'] = max($vatInfo['VAT_RATE'], $rate);
2037 }
2038 }
2039
2040 return $vatInfo;
2041 }
2042
2046 protected function saveEntities()
2047 {
2048 $result = parent::saveEntities();
2049
2050 $r = $this->getShipmentCollection()->save();
2051 if (!$r->isSuccess())
2052 {
2053 $result->addWarnings($r->getErrors());
2054 }
2055
2056 $r = $this->getPaymentCollection()->save();
2057 if (!$r->isSuccess())
2058 {
2059 $result->addWarnings($r->getErrors());
2060 }
2061
2062 // user budget
2063 Internals\UserBudgetPool::onUserBudgetSave($this->getUserId());
2064
2065 $r = $this->getTradeBindingCollection()->save();
2066 if (!$r->isSuccess())
2067 {
2068 $result->addWarnings($r->getErrors());
2069 }
2070
2071 $res = Cashbox\Internals\Pool::generateChecks($this->getInternalId());
2072 if (!$res->isSuccess())
2073 {
2074 $result->addWarnings($res->getErrors());
2075
2076 $warningResult = new Result();
2077 $warningResult->addWarnings($res->getErrors());
2078
2079 $registry = Registry::getInstance(static::getRegistryType());
2081 $entityMarker = $registry->getEntityMarkerClassName();
2082 $entityMarker::addMarker($this, $this, $warningResult);
2083 static::updateInternal($this->getId(), array('MARKED' => 'Y'));
2084 }
2085
2086 return $result;
2087 }
2088
2092 protected function calculatePrice()
2093 {
2094 $price = parent::calculatePrice();
2096
2097 return $price + $shipmentCollection->getPriceDelivery();
2098 }
2099
2107 protected function onBeforeSave()
2108 {
2109 $registry = Registry::getInstance(static::getRegistryType());
2110
2112 $entityMarker = $registry->getEntityMarkerClassName();
2113
2115 if ($result->hasWarnings())
2116 {
2117 $entityMarker::addMarker($this, $this, $result);
2118 if ($this->getId() > 0)
2119 {
2120 static::updateInternal($this->getId(), ['MARKED' => 'Y']);
2121 }
2122 }
2123
2124 $entityMarker::refreshMarkers($this);
2125
2126 return $result;
2127 }
2128
2132 protected function onAfterSave()
2133 {
2134 $result = parent::onAfterSave();
2135 if (!$result->isSuccess())
2136 {
2137 return $result;
2138 }
2139
2140 global $CACHE_MANAGER;
2141
2142 if (defined("CACHED_b_sale_order")
2143 && (
2144 $this->isNew
2145 || (
2146 $this->isChanged()
2147 && $this->getField("UPDATED_1C") != "Y"
2148 )
2149 )
2150 )
2151 {
2152 $CACHE_MANAGER->Read(CACHED_b_sale_order, "sale_orders");
2153 $CACHE_MANAGER->SetImmediate("sale_orders", true);
2154 }
2155
2156 return $result;
2157 }
2158
2166 public function save()
2167 {
2168 $result = parent::save();
2169
2170 $registry = Registry::getInstance(static::getRegistryType());
2171
2173 $orderHistory = $registry->getOrderHistoryClassName();
2174 $orderHistory::collectEntityFields('ORDER', $this->getId(), $this->getId());
2175
2177 $entityMarker = $registry->getEntityMarkerClassName();
2178 if ($entityMarker::hasErrors($this))
2179 {
2180 $entityMarker::saveMarkers($this);
2181 static::updateInternal(
2182 $this->getId(),
2183 [
2184 'MARKED' => 'Y',
2185 'REASON_MARKED' => $this->getField('REASON_MARKED')
2186 ]
2187 );
2188 }
2189
2190 return $result;
2191 }
2192
2201 protected function add()
2202 {
2203 $result = parent::add();
2204
2205 $registry = Registry::getInstance(static::getRegistryType());
2206
2208 $orderHistory = $registry->getOrderHistoryClassName();
2209 $orderHistory::addAction('ORDER', $result->getId(), 'ORDER_ADDED', $result->getId(), $this);
2210
2211 return $result;
2212 }
2213
2218 protected function update()
2219 {
2220 $result = parent::update();
2221
2222 $registry = Registry::getInstance(static::getRegistryType());
2224 $orderHistory = $registry->getOrderHistoryClassName();
2225
2226 if (!$result->isSuccess())
2227 {
2228 $orderHistory::addAction(
2229 'ORDER',
2230 $this->getId(),
2231 'ORDER_UPDATE_ERROR',
2232 $this->getId(),
2233 $this,
2234 array("ERROR" => $result->getErrorMessages())
2235 );
2236 }
2237 else
2238 {
2239 $orderHistory::addAction(
2240 'ORDER',
2241 $this->getId(),
2242 'ORDER_UPDATED',
2243 $this->getId(),
2244 $this,
2245 array(),
2247 );
2248 }
2249
2250 return $result;
2251 }
2252
2257 protected function callEventOnSaleOrderEntitySaved()
2258 {
2259 parent::callEventOnSaleOrderEntitySaved();
2260
2261 $changeMeaningfulFields = array(
2262 "PERSON_TYPE_ID",
2263 "CANCELED",
2264 "STATUS_ID",
2265 "MARKED",
2266 "PRICE",
2267 "SUM_PAID",
2268 "USER_ID",
2269 "EXTERNAL_ORDER",
2270 );
2271
2272 if ($this->isChanged())
2273 {
2274 $logFields = array();
2275
2276 if (!$this->isNew)
2277 {
2278 $fields = $this->getFields();
2279 $originalValues = $fields->getOriginalValues();
2280
2281 foreach($originalValues as $originalFieldName => $originalFieldValue)
2282 {
2283 if (in_array($originalFieldName, $changeMeaningfulFields) && $this->getField($originalFieldName) != $originalFieldValue)
2284 {
2285 $logFields[$originalFieldName] = $this->getField($originalFieldName);
2286 $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
2287 }
2288 }
2289
2290 $registry = Registry::getInstance(static::getRegistryType());
2291
2293 $orderHistory = $registry->getOrderHistoryClassName();
2294 $orderHistory::addLog(
2295 'ORDER',
2296 $this->getId(),
2297 "ORDER_UPDATE",
2298 $this->getId(),
2299 $this,
2300 $logFields,
2301 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2302 );
2303 }
2304 }
2305 }
2306
2311 protected function callEventOnSaleOrderSaved()
2312 {
2313 $registry = Registry::getInstance(static::getRegistryType());
2314
2316 $orderHistory = $registry->getOrderHistoryClassName();
2317 $orderHistory::addLog(
2318 'ORDER',
2319 $this->getId(),
2320 'ORDER_EVENT_ON_ORDER_SAVED',
2321 null,
2322 null,
2323 array(),
2324 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2325 );
2326
2327 parent::callEventOnSaleOrderSaved();
2328 }
2329
2335 protected function addInternal(array $data)
2336 {
2337 return Internals\OrderTable::add($data);
2338 }
2339
2346 protected static function updateInternal($primary, array $data)
2347 {
2348 return Internals\OrderTable::update($primary, $data);
2349 }
2350
2356 protected static function deleteInternal($primary)
2357 {
2358 return Internals\OrderTable::delete($primary);
2359 }
2360
2365 protected static function deleteExternalEntities($orderId)
2366 {
2367 parent::deleteExternalEntities($orderId);
2368
2369 $registry = Registry::getInstance(static::getRegistryType());
2370
2373
2375 $entityMarker = $registry->getEntityMarkerClassName();
2376 $entityMarker::deleteByOrderId($orderId);
2377
2379 $orderHistory = $registry->getOrderHistoryClassName();
2380 $orderHistory::deleteByOrderId($orderId);
2381 }
2382
2390 protected function addChangesToHistory($name, $oldValue = null, $value = null)
2391 {
2392 if ($this->getId() > 0)
2393 {
2394 $historyFields = array();
2395 if ($name == "PRICE")
2396 {
2397 $historyFields['CURRENCY'] = $this->getCurrency();
2398 }
2399 if ($name == "RESPONSIBLE_ID")
2400 {
2401 $historyFields[$name] = $value;
2402 }
2403
2404 $historyFields['OLD_'.$name] = $oldValue;
2405
2406 $registry = Registry::getInstance(static::getRegistryType());
2407
2409 $orderHistory = $registry->getOrderHistoryClassName();
2410 $orderHistory::addField(
2411 'ORDER',
2412 $this->getId(),
2413 $name,
2414 $oldValue,
2415 $value,
2416 $this->getId(),
2417 $this,
2418 $historyFields
2419 );
2420 }
2421 }
2422
2429 public function getDeliverySystemId()
2430 {
2431 return $this->getDeliveryIdList();
2432 }
2433
2439 public function getPaymentSystemId()
2440 {
2441 return $this->getPaySystemIdList();
2442 }
2443
2449 public function toArray() : array
2450 {
2451 $result = parent::toArray();
2452
2453 $result['PAYMENTS'] = $this->getPaymentCollection()->toArray();
2454 $result['SHIPMENTS'] = $this->getShipmentCollection()->toArray();
2455 $result['TRADE_BINDINGS'] = $this->getTradeBindingCollection()->toArray();
2456
2457 return $result;
2458 }
2459}
static update($id, array $data)
Определения entity.php:229
static get($moduleId, $name, $default="", $siteId=false)
Определения option.php:30
getField($name)
Определения basketitembase.php:478
static getList(array $parameters=array())
Определения checkmanager.php:1722
static create(array $settings)
Определения checkmanager.php:1547
const ALLOW_DELIVERY_ON_FULL_PAY
Определения configuration.php:23
const ALLOW_DELIVERY_ON_PAY
Определения configuration.php:22
static isEnableAutomaticReservation()
Определения configuration.php:86
static getAllowDeliveryOnPayCondition()
Определения configuration.php:214
static needShipOnAllowDelivery()
Определения configuration.php:193
static getProductReservationCondition()
Определения configuration.php:78
const ADD
Определения eventactions.php:8
const DELETE
Определения eventactions.php:10
const EVENT_ON_ORDER_PAID_SEND_MAIL
Определения eventactions.php:23
const EVENT_ON_ORDER_PAID
Определения eventactions.php:13
const UPDATE
Определения eventactions.php:9
const EVENT_ON_SHIPMENT_DELIVER
Определения eventactions.php:18
static save(Sale\Order $order, array $context=array())
Определения provider.php:1299
static deleteByOrderId($orderId)
Определения orderprocessing.php:182
static getMap()
Определения order.php:48
static getUfId()
Определения order.php:549
const BUDGET_TYPE_ORDER_CANCEL_PART
Определения userbudgetpool.php:21
isPaid()
Определения orderbase.php:1938
getUserId()
Определения orderbase.php:845
getPrice()
Определения orderbase.php:722
hasMeaningfulField()
Определения orderbase.php:2081
saveEntities()
Определения orderbase.php:1462
$discount
Определения orderbase.php:35
isMathActionOnly()
Определения orderbase.php:2073
getPropertyCollection()
Определения orderbase.php:589
callEventOnSaleOrderSaved()
Определения orderbase.php:1392
isStartField($isMeaningfulField=false)
Определения orderbase.php:2101
calculateVat()
Определения orderbase.php:964
isCanceled()
Определения orderbase.php:1968
getInternalId()
Определения orderbase.php:86
$isStartField
Определения orderbase.php:59
setFieldNoDemand($name, $value)
Определения orderbase.php:517
getSumPaid()
Определения orderbase.php:745
setField($name, $value)
Определения orderbase.php:456
isAllowDelivery()
Определения orderbase.php:1948
getDiscount()
Определения orderbase.php:2370
isNew()
Определения orderbase.php:1852
getField($name)
Определения orderbase.php:547
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
Определения orderhistory.php:29
onFieldModify($name, $oldValue, $value)
Определения order.php:1053
loadPrintedChecks()
Определения order.php:85
refreshInternal()
Определения order.php:1681
onBeforeBasketRefresh()
Определения order.php:1259
setBasket(BasketBase $basket)
Определения order.php:651
onOrderModify($name, $oldValue, $value)
Определения order.php:1106
applyDiscount(array $data)
Определения order.php:1701
$tradeBindingCollection
Определения order.php:28
static getUfId()
Определения order.php:58
getBusinessValueProviderInstance($mapping)
Определения order.php:1889
isChanged()
Определения order.php:1947
onBasketModify($action, BasketItemBase $basketItem, $name=null, $oldValue=null, $value=null)
Определения order.php:1174
const SALE_ORDER_LOCK_STATUS_YELLOW
Определения order.php:36
static getList(array $parameters=array())
Определения order.php:1919
static getFieldsMap()
Определения order.php:50
static updateInternal($primary, array $data)
Определения order.php:2346
const SALE_ORDER_LOCK_STATUS_GREEN
Определения order.php:35
toArray()
Определения order.php:2449
const SALE_ORDER_LOCK_STATUS_RED
Определения order.php:34
$printedChecks
Определения order.php:31
calculatePrice()
Определения order.php:2092
getPrintedChecks()
Определения order.php:69
onBeforeBasketItemDelete(BasketItem $basketItem)
Определения order.php:1137
resetData($select=array('PRICE'))
Определения order.php:623
$shipmentCollection
Определения order.php:22
onTradeBindingCollectionModify($action, TradeBindingEntity $entity, $name=null, $oldValue=null, $value=null)
Определения order.php:594
static getLockedStatus($id)
Определения order.php:1833
onAfterSave()
Определения order.php:2132
getStatusOnPartialPaid()
Определения order.php:1523
verify()
Определения order.php:1866
getPaymentSystemId()
Определения order.php:2439
addInternal(array $data)
Определения order.php:2335
getShipmentCollection()
Определения order.php:756
addPrintedCheck($check)
Определения order.php:109
$paymentCollection
Определения order.php:25
static deleteInternal($primary)
Определения order.php:2356
getStatusOnPaid()
Определения order.php:1513
getPaymentCollection()
Определения order.php:789
isShipped()
Определения order.php:904
static unlock($id)
Определения order.php:1771
static getRegistryType()
Определения order.php:41
static lock($id)
Определения order.php:1745
getTradeBindingCollection()
Определения order.php:774
getBasePrice()
Определения order.php:635
getDeliverySystemId()
Определения order.php:2429
clearChanged()
Определения order.php:1971
Определения payment.php:19
static roundPrecision($value)
Определения pricemaths.php:16
const ENTITY_TRADE_BINDING_COLLECTION
Определения registry.php:55
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
tryReserve()
Определения shipment.php:1487
tryUnship()
Определения shipment.php:1546
tryUnreserve()
Определения shipment.php:1497
needReservation()
Определения shipment.php:263
deliver()
Определения shipment.php:1575
isSystem()
Определения shipment.php:1370
isShipped()
Определения shipment.php:1384
setField($name, $value)
Определения shipment.php:792
isEmpty()
Определения shipment.php:1452
getDeliveryId()
Определения shipment.php:1315
static deleteByOrderId($orderId)
Определения order.php:102
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$data['IS_AVAILABLE']
Определения .description.php:13
$orderId
Определения payment.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$select
Определения iblock_catalog_list.php:194
global $USER
Определения csv_new_run.php:40
$name
Определения menu_edit.php:35
$value
Определения Param.php:39
Определения collection.php:2
$payment
Определения payment.php:14
$order
Определения payment.php:8
$paymentCollection
Определения payment.php:11
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
const CACHED_b_sale_order
Определения include.php:33
$eventManager
Определения include.php:412
$action
Определения file_dialog.php:21
$dbRes
Определения yandex_detail.php:168