Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
order.php
1<?php
2
3namespace Bitrix\Sale;
4
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 {
52 return Internals\OrderTable::getMap();
53 }
54
58 public static function getUfId()
59 {
60 return Internals\OrderTable::getUfId();
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
89 $dbRes = Cashbox\CheckManager::getList([
90 'filter' => [
91 '=ORDER_ID' => $this->getId()
92 ]
93 ]);
94
95 while ($data = $dbRes->fetch())
96 {
97 $result[] = Cashbox\CheckManager::create($data);
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
137 $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
138
140 $entityMarker = $registry->getEntityMarkerClassName();
141
142 if ($action === EventActions::DELETE)
143 {
144 if ((int)$this->getField('DELIVERY_ID') === $shipment->getDeliveryId())
145 {
146 $foundShipment = false;
147
149 foreach ($this->getShipmentCollection()->getNotSystemItems() as $entityShipment)
150 {
151 if ((int)$entityShipment->getField('DELIVERY_ID') > 0)
152 {
153 $foundShipment = true;
154 $this->setFieldNoDemand('DELIVERY_ID', $entityShipment->getField('DELIVERY_ID'));
155 break;
156 }
157 }
158
159 if (!$foundShipment && !$shipment->isSystem())
160 {
161 $systemShipment = $this->getShipmentCollection()->getSystemShipment();
162
163 if ((int)$systemShipment->getField('DELIVERY_ID') > 0)
164 {
165 $this->setFieldNoDemand('DELIVERY_ID', $systemShipment->getField('DELIVERY_ID'));
166 }
167 }
168 }
169 }
170 elseif ($action === EventActions::ADD)
171 {
172 if ($shipment->getId() === 0)
173 {
174 $this->getPropertyCollection()->refreshRelated();
175 }
176
177 if (!$shipment->isSystem())
178 {
179 $this->setFieldNoDemand('DELIVERY_ID', $shipment->getDeliveryId());
180 }
181 }
182
183 if ($action !== EventActions::UPDATE)
184 {
185 return $result;
186 }
187
188 if ($name === "ALLOW_DELIVERY")
189 {
190 if ($this->isCanceled())
191 {
192 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'));
193 return $result;
194 }
195
196 $r = $shipment->deliver();
197 if ($r->isSuccess())
198 {
199 $eventManager = Main\EventManager::getInstance();
200 if ($eventManager->findEventHandlers('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER))
201 {
202 $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER, array(
203 'ENTITY' =>$shipment
204 ));
205 $event->send();
206 }
207 }
208 else
209 {
210 $result->addErrors($r->getErrors());
211 }
212
213 if (
215 && Configuration::getProductReservationCondition() === ReserveCondition::ON_ALLOW_DELIVERY
216 )
217 {
218 if ($value === "Y")
219 {
220 $r = $shipment->tryReserve();
221 if (!$r->isSuccess())
222 {
223 $result->addErrors($r->getErrors());
224 }
225
226 if ($r->hasWarnings())
227 {
228 $result->addWarnings($r->getWarnings());
229 $entityMarker::addMarker($this, $shipment, $r);
230 if (!$shipment->isSystem())
231 {
232 $shipment->setField('MARKED', 'Y');
233 }
234 }
235 }
236 elseif (!$shipment->isShipped())
237 {
238 $r = $shipment->tryUnreserve();
239 if (!$r->isSuccess())
240 {
241 $result->addErrors($r->getErrors());
242 }
243
244 if ($r->hasWarnings())
245 {
246 $result->addWarnings($r->getWarnings());
247 $entityMarker::addMarker($this, $shipment, $r);
248 if (!$shipment->isSystem())
249 {
250 $shipment->setField('MARKED', 'Y');
251 }
252 }
253 }
254
255 if (!$result->isSuccess())
256 {
257 return $result;
258 }
259 }
260
261 $orderStatus = null;
262
263 if ($oldValue === "N")
264 {
266 {
267 $orderStatus = $optionClassName::get('sale', 'status_on_allow_delivery', '');
268 }
269 elseif ($this->getShipmentCollection()->hasAllowDelivery())
270 {
271 $orderStatus = $optionClassName::get('sale', 'status_on_allow_delivery_one_of', '');
272 }
273 }
274
275 if ($orderStatus !== null && $this->getField('STATUS_ID') != static::getFinalStatus())
276 {
277 if ((string)$orderStatus != '')
278 {
279 $r = $this->setField('STATUS_ID', $orderStatus);
280 if (!$r->isSuccess())
281 {
282 $result->addErrors($r->getErrors());
283 }
284
285 if ($r->hasWarnings())
286 {
287 $result->addWarnings($r->getWarnings());
288 $entityMarker::addMarker($this, $this, $r);
289 $this->setField('MARKED', 'Y');
290 }
291 }
292 }
293
294 if (Configuration::needShipOnAllowDelivery() && $value === "Y")
295 {
296 if (!$shipment->isEmpty())
297 {
298 $r = $shipment->setField("DEDUCTED", "Y");
299 if (!$r->isSuccess())
300 {
301 $result->addErrors($r->getErrors());
302 }
303
304 if ($r->hasWarnings())
305 {
306 $result->addWarnings($r->getWarnings());
307 $entityMarker::addMarker($this, $shipment, $r);
308 if (!$shipment->isSystem())
309 {
310 $shipment->setField('MARKED', 'Y');
311 }
312 }
313 }
314 }
315
316 $isAllowDelivery = $this->getShipmentCollection()->isAllowDelivery();
317 if (
318 $isAllowDelivery
319 && $this->getField('ALLOW_DELIVERY') === 'N'
320 )
321 {
322 $this->setFieldNoDemand('DATE_ALLOW_DELIVERY', new Type\DateTime());
323 }
324
325 $this->setFieldNoDemand('ALLOW_DELIVERY', $isAllowDelivery ? "Y" : "N");
326 }
327 elseif ($name === "DEDUCTED")
328 {
329 if ($this->isCanceled())
330 {
331 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_SHIPMENT_ORDER_CANCELED'), 'SALE_ORDER_SHIPMENT_ORDER_CANCELED'));
332 return $result;
333 }
334
335 if (
337 && Configuration::getProductReservationCondition() == ReserveCondition::ON_SHIP
338 )
339 {
340 if ($value === "Y")
341 {
343 $r = $shipment->tryReserve();
344 if (!$r->isSuccess())
345 {
346 $result->addErrors($r->getErrors());
347 }
348
349 if ($r->hasWarnings())
350 {
351 $result->addWarnings($r->getWarnings());
352 $entityMarker::addMarker($this, $shipment, $r);
353 if (!$shipment->isSystem())
354 {
355 $shipment->setField('MARKED', 'Y');
356 }
357 }
358 }
359 else
360 {
361 $r = $shipment->tryUnreserve();
362 if (!$r->isSuccess())
363 {
364 $result->addErrors($r->getErrors());
365 }
366
367 if ($r->hasWarnings())
368 {
369 $result->addWarnings($r->getWarnings());
370 $entityMarker::addMarker($this, $shipment, $r);
371 if (!$shipment->isSystem())
372 {
373 $shipment->setField('MARKED', 'Y');
374 }
375 }
376 }
377 }
378
379 if ($value === "Y")
380 {
382 $r = $shipment->tryShip();
383 if (!$r->isSuccess())
384 {
385 $result->addErrors($r->getErrors());
386 }
387
388 if ($r->hasWarnings())
389 {
390 $result->addWarnings($r->getWarnings());
391 $entityMarker::addMarker($this, $shipment, $r);
392 if (!$shipment->isSystem())
393 {
394 $shipment->setField('MARKED', 'Y');
395 }
396 }
397
398 }
399 elseif ($oldValue === 'Y')
400 {
401 $r = $shipment->tryUnship();
402 if (!$r->isSuccess())
403 {
404 $result->addErrors($r->getErrors());
405 }
406
407 if ($r->hasWarnings())
408 {
409 $result->addWarnings($r->getWarnings());
410 $entityMarker::addMarker($this, $shipment, $r);
411 if (!$shipment->isSystem())
412 {
413 $shipment->setField('MARKED', 'Y');
414 }
415 }
416
417 if (
419 && $shipment->needReservation()
420 )
421 {
422 $r = $shipment->tryReserve();
423 if (!$r->isSuccess())
424 {
425 $result->addErrors($r->getErrors());
426 }
427
428 if ($r->hasWarnings())
429 {
430 $result->addWarnings($r->getWarnings());
431 $entityMarker::addMarker($this, $shipment, $r);
432 if (!$shipment->isSystem())
433 {
434 $shipment->setField('MARKED', 'Y');
435 }
436 }
437 }
438 }
439
440 if (!$result->isSuccess())
441 {
442 return $result;
443 }
444
445 $orderStatus = null;
446
447 $allowSetStatus = false;
448
449 if ($oldValue === "N")
450 {
451 if ($this->getShipmentCollection()->isShipped())
452 {
453 $orderStatus = $optionClassName::get('sale', 'status_on_shipped_shipment', '');
454 }
455 elseif ($this->getShipmentCollection()->hasShipped())
456 {
457 $orderStatus = $optionClassName::get('sale', 'status_on_shipped_shipment_one_of', '');
458 }
459 $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
460 }
461 else
462 {
463 $fields = $this->getFields();
464 $originalValues = $fields->getOriginalValues();
465 if (!empty($originalValues['STATUS_ID']))
466 {
467 $orderStatus = $originalValues['STATUS_ID'];
468 $allowSetStatus = true;
469 }
470 }
471
472 if (
473 (string)$orderStatus !== ''
474 && $allowSetStatus
475 )
476 {
477 $r = $this->setField('STATUS_ID', $orderStatus);
478 if (!$r->isSuccess())
479 {
480 $result->addErrors($r->getErrors());
481 }
482 elseif ($r->hasWarnings())
483 {
484 $result->addWarnings($r->getWarnings());
485 $entityMarker::addMarker($this, $this, $r);
486 $this->setField('MARKED', 'Y');
487 }
488 }
489
490 $isShipped = $this->getShipmentCollection()->isShipped();
491 $this->setFieldNoDemand('DEDUCTED', $isShipped ? "Y" : "N");
492
493 if ($isShipped)
494 {
495 if (strval($shipment->getField('DATE_DEDUCTED')) != '')
496 {
497 $this->setFieldNoDemand('DATE_DEDUCTED', $shipment->getField('DATE_DEDUCTED'));
498 }
499 if (strval($shipment->getField('EMP_DEDUCTED_ID')) != '')
500 {
501 $this->setFieldNoDemand('EMP_DEDUCTED_ID', $shipment->getField('EMP_DEDUCTED_ID'));
502 }
503 }
504 }
505 elseif ($name == "MARKED")
506 {
507 if ($value == "Y")
508 {
510 $r = $this->setField('MARKED', 'Y');
511 if (!$r->isSuccess())
512 {
513 $result->addErrors($r->getErrors());
514 }
515 }
516 }
517 elseif ($name == "REASON_MARKED")
518 {
519 $r = $this->setReasonMarked($value);
520 if (!$r->isSuccess())
521 {
522 $result->addErrors($r->getErrors());
523 }
524 }
525 elseif ($name == "BASE_PRICE_DELIVERY")
526 {
527 if ($this->isCanceled())
528 {
529 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
530 return $result;
531 }
532
533 $discount = $this->getDiscount();
534 $discount->setCalculateShipments($shipment);
535 }
536 elseif ($name == "PRICE_DELIVERY")
537 {
538 if ($this->isCanceled())
539 {
540 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
541 return $result;
542 }
543
544 $deliveryPrice = ($this->isNew()) ? $value : $this->getField("PRICE_DELIVERY") - $oldValue + $value;
545 $this->setFieldNoDemand(
546 "PRICE_DELIVERY",
547 $deliveryPrice
548 );
549
551 $r = $this->setField(
552 "PRICE",
553 $this->getField("PRICE") - $oldValue + $value
554 );
555
556 if (!$r->isSuccess())
557 {
558 $result->addErrors($r->getErrors());
559 }
560
561 }
562 elseif ($name == "DELIVERY_ID")
563 {
564 if ($shipment->isSystem() || intval($shipment->getField('DELIVERY_ID')) <= 0 )
565 {
566 return $result;
567 }
568
569 $this->setFieldNoDemand('DELIVERY_ID', $shipment->getField('DELIVERY_ID'));
570
571 $this->getPropertyCollection()->refreshRelated();
572
573 }
574 elseif ($name == "TRACKING_NUMBER")
575 {
576 if ($shipment->isSystem())
577 {
578 return $result;
579 }
580
581 $this->setFieldNoDemand('TRACKING_NUMBER', $shipment->getField('TRACKING_NUMBER'));
582 }
583
584 if ($value != $oldValue)
585 {
586 $fields = $this->fields->getChangedValues();
587 if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields))
588 {
589 parent::setField("UPDATED_1C", "N");
590 }
591 }
592
593 return $result;
594 }
595
596 public function onTradeBindingCollectionModify($action, TradeBindingEntity $entity, $name = null, $oldValue = null, $value = null)
597 {
598 if ($action === EventActions::ADD)
599 {
600 if (
601 $entity->getId() === 0
602 && $entity->getField('TRADING_PLATFORM_ID') > 0
603 )
604 {
605 $this->propertyCollection = $this->loadPropertyCollection();
606 }
607 }
608 elseif ($action === EventActions::UPDATE)
609 {
610 if ($name === 'TRADING_PLATFORM_ID')
611 {
612 $this->propertyCollection = $this->loadPropertyCollection();
613 }
614 }
615
616 return new Result();
617 }
618
625 public function resetData($select = array('PRICE'))
626 {
627 $this->getShipmentCollection()->resetData();
628
629 parent::resetData($select);
630 }
631
637 public function getBasePrice(): float
638 {
639 $price = parent::getBasePrice();
640
641 return $price + $this->getShipmentCollection()->getBasePriceDelivery();
642 }
643
653 public function setBasket(BasketBase $basket)
654 {
655 $result = new Result();
656
657 $isStartField = $this->isStartField();
658
659 $r = parent::setBasket($basket);
660 if (!$r->isSuccess())
661 {
662 $result->addErrors($r->getErrors());
663 return $result;
664 }
665
666 $result = $this->getShipmentCollection()->resetCollection();
667 if (!$r->isSuccess())
668 {
669 $result->addErrors($r->getErrors());
670 return $result;
671 }
672
673 if (!$this->isMathActionOnly())
674 {
675 $r = $this->refreshData();
676 if (!$r->isSuccess())
677 {
678 $result->addErrors($r->getErrors());
679 }
680 }
681
682 if ($isStartField)
683 {
684 $hasMeaningfulFields = $this->hasMeaningfulField();
685
686 $r = $this->doFinalAction($hasMeaningfulFields);
687 if (!$r->isSuccess())
688 {
689 $result->addErrors($r->getErrors());
690 }
691 }
692
693 return $result;
694 }
695
705 public function appendBasket(BasketBase $basket)
706 {
707 $result = new Result();
708
709 $isStartField = $this->isStartField();
710
711 $r = parent::appendBasket($basket);
712 if (!$r->isSuccess())
713 {
714 $result->addErrors($r->getErrors());
715 return $result;
716 }
717
719 $result = $this->getShipmentCollection()->resetCollection();
720 if (!$r->isSuccess())
721 {
722 $result->addErrors($r->getErrors());
723 return $result;
724 }
725
726 if (!$this->isMathActionOnly())
727 {
729 $r = $this->refreshData();
730 if (!$r->isSuccess())
731 {
732 $result->addErrors($r->getErrors());
733 }
734 }
735
736 if ($isStartField)
737 {
738 $hasMeaningfulFields = $this->hasMeaningfulField();
739
741 $r = $this->doFinalAction($hasMeaningfulFields);
742 if (!$r->isSuccess())
743 {
744 $result->addErrors($r->getErrors());
745 }
746 }
747
748 return $result;
749 }
750
758 public function getShipmentCollection()
759 {
760 if (empty($this->shipmentCollection))
761 {
762 $this->shipmentCollection = $this->loadShipmentCollection();
763 }
764
765 return $this->shipmentCollection;
766 }
767
777 {
778 if (empty($this->tradeBindingCollection))
779 {
780 $this->tradeBindingCollection = $this->loadTradeBindingCollection();
781 }
782
783 return $this->tradeBindingCollection;
784 }
785
791 public function getPaymentCollection()
792 {
793 if (empty($this->paymentCollection))
794 {
795 $this->paymentCollection = $this->loadPaymentCollection();
796 }
797
798 return $this->paymentCollection;
799 }
800
808 public function loadShipmentCollection()
809 {
810 $registry = Registry::getInstance(static::getRegistryType());
811
813 $shipmentCollectionClassName = $registry->getShipmentCollectionClassName();
814 return $shipmentCollectionClassName::load($this);
815 }
816
823 public function loadPaymentCollection()
824 {
825 $registry = Registry::getInstance(static::getRegistryType());
826
828 $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
829 return $paymentCollectionClassName::load($this);
830 }
831
838 protected function loadTradeBindingCollection()
839 {
840 $registry = Registry::getInstance(static::getRegistryType());
841
843 $tradeBindingCollection = $registry->get(Registry::ENTITY_TRADE_BINDING_COLLECTION);
844
845 return $tradeBindingCollection::load($this);
846 }
847
854 protected static function deleteEntitiesNoDemand($orderId)
855 {
856 $r = parent::deleteEntitiesNoDemand($orderId);
857 if (!$r->isSuccess())
858 return $r;
859
860 $registry = Registry::getInstance(static::getRegistryType());
861
863 $shipmentClassName = $registry->getShipmentClassName();
864 $shipmentClassName::deleteNoDemand($orderId);
865 if (!$r->isSuccess())
866 return $r;
867
869 $paymentClassName = $registry->getPaymentClassName();
870 $paymentClassName::deleteNoDemand($orderId);
871 if (!$r->isSuccess())
872 return $r;
873
874 return new Result();
875 }
876
886 protected static function deleteEntities(OrderBase $order)
887 {
888 parent::deleteEntities($order);
889
891 foreach ($order->getShipmentCollection() as $shipment)
892 {
893 $shipment->delete();
894 }
895
897 foreach ($order->getPaymentCollection() as $payment)
898 {
899 $payment->delete();
900 }
901 }
902
906 public function isShipped()
907 {
908 return $this->getShipmentCollection()->isShipped();
909 }
910
925 public function onPaymentCollectionModify($action, Payment $payment, $name = null, $oldValue = null, $value = null)
926 {
927 $result = new Result();
928
929 if ($action == EventActions::DELETE)
930 {
931 if ($this->getField('PAY_SYSTEM_ID') == $payment->getPaymentSystemId())
932 {
934 foreach ($payment->getCollection() as $entityPayment)
935 {
936 if (intval($entityPayment->getField('PAY_SYSTEM_ID')) > 0
937 && intval($entityPayment->getField('PAY_SYSTEM_ID')) != $payment->getPaymentSystemId())
938 {
939 $this->setFieldNoDemand('PAY_SYSTEM_ID', $entityPayment->getField('PAY_SYSTEM_ID'));
940 break;
941 }
942 }
943 }
944 }
945 elseif ($action === EventActions::ADD)
946 {
947 if ($payment->getId() === 0)
948 {
949 $this->getPropertyCollection()->refreshRelated();
950 }
951 }
952
953 if ($action != EventActions::UPDATE)
954 {
955 return $result;
956 }
957
958 if (($name == "CURRENCY") && ($value != $this->getField("CURRENCY")))
959 {
960 throw new Main\NotImplementedException();
961 }
962
963 if ($name == "SUM" || $name == "PAID")
964 {
965 if ($this->isCanceled())
966 {
967 $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PAID_ORDER_CANCELED'), 'SALE_ORDER_PAID_ORDER_CANCELED'));
968 return $result;
969 }
970
971 if ($name == "SUM"
972 && !$payment->isPaid()
973 )
974 {
975 return $result;
976 }
977
978 $r = $this->syncOrderAndPayments($payment);
979 if (!$r->isSuccess())
980 {
981 $result->addErrors($r->getErrors());
982 }
983 elseif ($r->hasWarnings())
984 {
985 $result->addWarnings($r->getWarnings());
986 }
987 }
988 elseif ($name == "PAY_SYSTEM_ID")
989 {
990 $this->setFieldNoDemand('PAY_SYSTEM_ID', $payment->getField('PAY_SYSTEM_ID'));
991
992 $this->getPropertyCollection()->refreshRelated();
993 }
994 elseif ($name == "DATE_PAID")
995 {
996 $this->setFieldNoDemand('DATE_PAYED', $payment->getField('DATE_PAID'));
997 }
998 elseif ($name == "PAY_VOUCHER_NUM")
999 {
1000 $this->setFieldNoDemand('PAY_VOUCHER_NUM', $payment->getField('PAY_VOUCHER_NUM'));
1001 }
1002 elseif ($name == "PAY_VOUCHER_DATE")
1003 {
1004 $this->setFieldNoDemand('PAY_VOUCHER_DATE', $payment->getField('PAY_VOUCHER_DATE'));
1005 }
1006 elseif ($name == "EMP_PAID_ID")
1007 {
1008 $this->setFieldNoDemand('EMP_PAYED_ID', $payment->getField('EMP_PAID_ID'));
1009 }
1010 elseif ($name == "MARKED")
1011 {
1012 if ($value == "Y")
1013 {
1015 $r = $this->setField('MARKED', 'Y');
1016 if (!$r->isSuccess())
1017 {
1018 $result->addErrors($r->getErrors());
1019 }
1020 }
1021 }
1022 elseif ($name == "REASON_MARKED")
1023 {
1024 $r = $this->setReasonMarked($value);
1025 if (!$r->isSuccess())
1026 {
1027 $result->addErrors($r->getErrors());
1028 }
1029 }
1030
1031 if ($value != $oldValue)
1032 {
1033 $fields = $this->fields->getChangedValues();
1034 if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields) && $name != 'UPDATED_1C')
1035 {
1036 parent::setField("UPDATED_1C", "N");
1037 }
1038 }
1039
1040 return $result;
1041 }
1042
1055 protected function onFieldModify($name, $oldValue, $value)
1056 {
1057 $result = parent::onFieldModify($name, $oldValue, $value);
1058
1059 if ($name == "PRICE")
1060 {
1061 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1062 if (!$r->isSuccess())
1063 {
1064 return $result->addErrors($r->getErrors());
1065 }
1066
1067 $r = $this->getPaymentCollection()->onOrderModify($name, $oldValue, $value);
1068 if (!$r->isSuccess())
1069 {
1070 return $result->addErrors($r->getErrors());
1071 }
1072
1073 $r = $this->syncOrderAndPayments();
1074 if (!$r->isSuccess())
1075 {
1076 $result->addErrors($r->getErrors());
1077 }
1078 }
1079 elseif($name == "MARKED")
1080 {
1081 global $USER;
1082
1083 if (is_object($USER))
1084 {
1085 $this->setField('EMP_MARKED_ID', $USER->GetID());
1086 }
1087
1088 $this->setField('DATE_MARKED', new Main\Type\DateTime());
1089
1090 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1091 if (!$r->isSuccess())
1092 {
1093 return $result->addErrors($r->getErrors());
1094 }
1095 }
1096
1097 return $result;
1098 }
1099
1108 protected function onOrderModify($name, $oldValue, $value)
1109 {
1110 $result = new Result();
1111
1112 $r = $this->getPaymentCollection()->onOrderModify($name, $oldValue, $value);
1113 if (!$r->isSuccess())
1114 {
1115 return $result->addErrors($r->getErrors());
1116 }
1117
1118 $r = $this->getShipmentCollection()->onOrderModify($name, $oldValue, $value);
1119 if (!$r->isSuccess())
1120 {
1121 return $result->addErrors($r->getErrors());
1122 }
1123
1124 return $result;
1125 }
1126
1139 public function onBeforeBasketItemDelete(BasketItem $basketItem)
1140 {
1141 $result = new Result();
1142
1143 $r = $this->getShipmentCollection()->onBeforeBasketItemDelete($basketItem);
1144 if (!$r->isSuccess())
1145 {
1146 return $result->addErrors($r->getErrors());
1147 }
1148
1149 $r = $this->getPaymentCollection()->onBeforeBasketItemDelete($basketItem);
1150 if (!$r->isSuccess())
1151 {
1152 return $result->addErrors($r->getErrors());
1153 }
1154
1155
1156 return $result;
1157 }
1158
1176 public function onBasketModify($action, BasketItemBase $basketItem, $name = null, $oldValue = null, $value = null)
1177 {
1178 $result = new Result();
1179
1180 if ($action === EventActions::DELETE)
1181 {
1182 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1183 if (!$r->isSuccess())
1184 {
1185 $result->addErrors($r->getErrors());
1186 return $result;
1187 }
1188
1189 return $result;
1190 }
1191 elseif ($action === EventActions::ADD)
1192 {
1193 if ($basketItem->getField('ORDER_ID'))
1194 {
1195 return $result;
1196 }
1197
1198 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1199 }
1200 elseif ($action !== EventActions::UPDATE)
1201 {
1202 return $result;
1203 }
1204
1205 if ($name === "QUANTITY")
1206 {
1207 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1208 if (!$r->isSuccess())
1209 {
1210 $result->addErrors($r->getErrors());
1211 return $result;
1212 }
1213
1214 $r = $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1215 if (!$r->isSuccess())
1216 {
1217 $result->addErrors($r->getErrors());
1218 return $result;
1219 }
1220 elseif ($r->hasWarnings())
1221 {
1222 $result->addWarnings($r->getWarnings());
1223 }
1224
1225 return $result;
1226 }
1227 elseif ($name === "PRICE")
1228 {
1229 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1230 if (!$r->isSuccess())
1231 {
1232 $result->addErrors($r->getErrors());
1233 return $result;
1234 }
1235
1236 if ($this->getId() === 0 && !$this->isMathActionOnly())
1237 {
1238 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1239 }
1240 }
1241 elseif ($name === 'WEIGHT')
1242 {
1243 return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1244 }
1245 else
1246 {
1247 $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1248 if (!$r->isSuccess())
1249 {
1250 $result->addErrors($r->getErrors());
1251 return $result;
1252 }
1253 }
1254
1255 return $result;
1256 }
1257
1261 public function onBeforeBasketRefresh()
1262 {
1263 $result = new Result();
1264
1265 $r = $this->getShipmentCollection()->tryUnreserve();
1266 if (!$r->isSuccess())
1267 {
1268 $result->addErrors($r->getErrors());
1269 }
1270
1271 return $result;
1272 }
1273
1277 public function onAfterBasketRefresh()
1278 {
1279 $result = new Result();
1280
1282 foreach ($this->getShipmentCollection() as $shipment)
1283 {
1284 if (
1285 $shipment->isShipped()
1286 || !$shipment->needReservation()
1288 )
1289 {
1290 continue;
1291 }
1292
1293 $r = $shipment->tryReserve();
1294 if (!$r->isSuccess())
1295 {
1296 $result->addErrors($r->getErrors());
1297 }
1298 }
1299
1300 return $result;
1301 }
1302
1315 private function syncOrderAndPayments(Payment $payment = null)
1316 {
1317 $result = new Result();
1318
1319 $oldPaid = $this->getField('PAYED');
1321 $sumPaid = $paymentCollection->getPaidSum();
1322
1323 if ($payment)
1324 {
1325 $finalSumPaid = $sumPaid;
1326
1327 if ($payment->isPaid())
1328 {
1329 if ($sumPaid > $this->getPrice())
1330 {
1331 $finalSumPaid = $this->getSumPaid() + $payment->getSum();
1332 }
1333 }
1334 else
1335 {
1336 $r = $this->syncOrderPaymentPaid($payment);
1337 if ($r->isSuccess())
1338 {
1339 $paidResult = $r->getData();
1340 if (isset($paidResult['SUM_PAID']))
1341 {
1342 $finalSumPaid = $paidResult['SUM_PAID'];
1343 }
1344 }
1345 else
1346 {
1347 return $result->addErrors($r->getErrors());
1348 }
1349 }
1350 }
1351 else
1352 {
1353 $finalSumPaid = $this->getSumPaid();
1354
1355 $r = $this->syncOrderPaid();
1356 if (!$r->isSuccess())
1357 {
1358 return $result->addErrors($r->getErrors());
1359 }
1360
1361 $paidResult = $r->getData();
1362 if (isset($paidResult['SUM_PAID']))
1363 {
1364 $finalSumPaid = $paidResult['SUM_PAID'];
1365 }
1366 }
1367
1368 $paid = false;
1369
1370 if ($finalSumPaid >= 0 && $paymentCollection->hasPaidPayment()
1371 && PriceMaths::roundPrecision($this->getPrice()) <= PriceMaths::roundPrecision($finalSumPaid))
1372 {
1373 $paid = true;
1374 }
1375
1376 $this->setFieldNoDemand('PAYED', $paid ? "Y" : "N");
1377
1378 if ($this->getFields()->isChanged('PAYED'))
1379 {
1380 Internals\EventsPool::addEvent(
1381 $this->getInternalId(),
1383 [
1384 'ENTITY' => $this,
1385 ]
1386 );
1387
1388 Internals\EventsPool::addEvent(
1389 $this->getInternalId(),
1391 [
1392 'ENTITY' => $this,
1393 ]
1394 );
1395 }
1396
1397 if ($finalSumPaid > 0 && $finalSumPaid > $this->getPrice())
1398 {
1399 if (!$payment || $payment->isPaid())
1400 {
1401 Internals\UserBudgetPool::addPoolItem($this, $finalSumPaid - $this->getPrice(), Internals\UserBudgetPool::BUDGET_TYPE_EXCESS_SUM_PAID, $payment);
1402 }
1403
1404 $finalSumPaid = $this->getPrice();
1405 }
1406
1407 $this->setFieldNoDemand('SUM_PAID', $finalSumPaid);
1408
1409 $r = $this->onAfterSyncPaid($oldPaid);
1410 if (!$r->isSuccess())
1411 {
1412 $result->addErrors($r->getErrors());
1413 }
1414 elseif ($r->hasWarnings())
1415 {
1416 $result->addWarnings($r->getWarnings());
1417 }
1418
1419 return $result;
1420 }
1421
1426 private function syncOrderPaymentPaid(Payment $payment)
1427 {
1428 $result = new Result();
1429
1430 if ($payment->isPaid())
1431 {
1432 return $result;
1433 }
1434
1435 $sumPaid = $this->getPaymentCollection()->getPaidSum();
1436
1437 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($this);
1438
1439 $debitSum = $payment->getSum();
1440
1441 $maxPaid = $payment->getSum() + $sumPaid - $this->getSumPaid();
1442
1443 if ($maxPaid >= $payment->getSum())
1444 {
1445 $finalSumPaid = $this->getSumPaid();
1446 }
1447 else
1448 {
1449 $debitSum = $maxPaid;
1450 $finalSumPaid = $sumPaid;
1451 }
1452
1453 if ($debitSum > 0 && $payment->isInner())
1454 {
1455 if (PriceMaths::roundPrecision($debitSum) > PriceMaths::roundPrecision($userBudget))
1456 {
1457 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_PAYMENT_CANCELLED_PAID'), 'SALE_ORDER_PAYMENT_NOT_ENOUGH_USER_BUDGET_SYNCPAID') );
1458 return $result;
1459 }
1460
1461 Internals\UserBudgetPool::addPoolItem($this, ($debitSum * -1), Internals\UserBudgetPool::BUDGET_TYPE_ORDER_CANCEL_PART, $payment);
1462 }
1463
1464 $result->setData(array('SUM_PAID' => $finalSumPaid));
1465
1466 return $result;
1467 }
1468
1472 private function syncOrderPaid()
1473 {
1474 $result = new Result();
1475
1476 if ($this->getSumPaid() == $this->getPrice())
1477 return $result;
1478
1479 $debitSum = $this->getPrice() - $this->getSumPaid();
1480
1481 $sumPaid = $this->getPaymentCollection()->getPaidSum();
1482 $userBudget = Internals\UserBudgetPool::getUserBudgetByOrder($this);
1483
1484 $bePaid = $sumPaid - $this->getSumPaid();
1485
1486 if ($bePaid > 0)
1487 {
1488 if ($debitSum > $bePaid)
1489 {
1490 $debitSum = $bePaid;
1491 }
1492
1493 if ($debitSum >= $userBudget)
1494 {
1495 $debitSum = $userBudget;
1496 }
1497
1498 if ($userBudget >= $debitSum && $debitSum > 0)
1499 {
1500 Internals\UserBudgetPool::addPoolItem($this, ($debitSum * -1), Internals\UserBudgetPool::BUDGET_TYPE_ORDER_PAY);
1501
1502 $finalSumPaid = $this->getSumPaid() + $debitSum;
1503 $result->setData(['SUM_PAID' => $finalSumPaid]);
1504 }
1505 }
1506
1507 return $result;
1508 }
1509
1515 protected function getStatusOnPaid()
1516 {
1517 $registry = Registry::getInstance(static::getRegistryType());
1518
1519 $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
1520 return $optionClassName::get('sale', 'status_on_paid', '');
1521 }
1522
1528 protected function getStatusOnPartialPaid()
1529 {
1530 $registry = Registry::getInstance(static::getRegistryType());
1531
1532 $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
1533 return $optionClassName::get('sale', 'status_on_half_paid', '');
1534 }
1535
1547 private function onAfterSyncPaid($oldPaid = null)
1548 {
1549 $result = new Result();
1550
1551 $paymentCollection = $this->getPaymentCollection();
1552
1553 $shipmentCollection = $this->getShipmentCollection();
1554
1555 $orderStatus = null;
1556
1557 $allowSetStatus = false;
1558
1559 if ($oldPaid == "N")
1560 {
1561 if ($this->isPaid())
1562 {
1563 $orderStatus = $this->getStatusOnPaid();
1564 }
1565 elseif ($paymentCollection->hasPaidPayment())
1566 {
1567 $orderStatus = $this->getStatusOnPartialPaid();
1568 }
1569
1570 $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
1571 }
1572
1573 if ($orderStatus !== null && $allowSetStatus)
1574 {
1575 if (strval($orderStatus) != '')
1576 {
1577 $r = $this->setField('STATUS_ID', $orderStatus);
1578 if (!$r->isSuccess())
1579 {
1580 $result->addErrors($r->getErrors());
1581 }
1582 elseif ($r->hasWarnings())
1583 {
1584 $result->addWarnings($r->getWarnings());
1585
1586 $registry = Registry::getInstance(static::getRegistryType());
1587
1589 $entityMarker = $registry->getEntityMarkerClassName();
1590 $entityMarker::addMarker($this, $this, $r);
1591 $this->setField('MARKED', 'Y');
1592 }
1593 }
1594 }
1595
1597 {
1598 $r = null;
1599
1600 if (Configuration::getProductReservationCondition() == ReserveCondition::ON_PAY)
1601 {
1602 if ($paymentCollection->hasPaidPayment())
1603 {
1604 $r = $shipmentCollection->tryReserve();
1605 }
1606 else
1607 {
1608 $r = $shipmentCollection->tryUnreserve();
1609 }
1610 }
1611 elseif (Configuration::getProductReservationCondition() == ReserveCondition::ON_FULL_PAY)
1612 {
1613 if ($oldPaid == "N" && $this->isPaid())
1614 {
1615 $r = $shipmentCollection->tryReserve();
1616 }
1617 elseif ($oldPaid == "Y" && !$this->isPaid())
1618 {
1619 $r = $shipmentCollection->tryUnreserve();
1620 }
1621 }
1622
1623 if ($r !== null)
1624 {
1625 if (!$r->isSuccess())
1626 {
1627 $result->addErrors($r->getErrors());
1628 }
1629 elseif ($r->hasWarnings())
1630 {
1631 $result->addWarnings($r->getWarnings());
1632 }
1633 }
1634 }
1635
1636 $allowDelivery = null;
1637
1639 {
1640 if ($oldPaid == "N" && $paymentCollection->hasPaidPayment())
1641 {
1642 $allowDelivery = true;
1643 }
1644 elseif ($oldPaid == "Y" && !$paymentCollection->hasPaidPayment())
1645 {
1646 $allowDelivery = false;
1647 }
1648 }
1650 {
1651 if ($oldPaid == "N" && $this->isPaid())
1652 {
1653 $allowDelivery = true;
1654 }
1655 elseif ($oldPaid == "Y" && !$this->isPaid())
1656 {
1657 $allowDelivery = false;
1658 }
1659 }
1660
1661 if ($allowDelivery !== null)
1662 {
1663 if ($allowDelivery)
1664 {
1665 $r = $shipmentCollection->allowDelivery();
1666 }
1667 else
1668 {
1669 $r = $shipmentCollection->disallowDelivery();
1670 }
1671
1672 if (!$r->isSuccess())
1673 {
1674 $result->addErrors($r->getErrors());
1675 }
1676 }
1677
1678 return $result;
1679 }
1680
1689 protected function refreshInternal()
1690 {
1691 $result = parent::refreshInternal();
1692 if (!$result->isSuccess())
1693 {
1694 return $result;
1695 }
1696
1697 return $this->getShipmentCollection()->refreshData();
1698 }
1699
1709 public function applyDiscount(array $data)
1710 {
1711 $r = parent::applyDiscount($data);
1712 if (!$r->isSuccess())
1713 {
1714 return $r;
1715 }
1716
1717 if (isset($data['SHIPMENT']) && intval($data['SHIPMENT']) > 0
1718 && (isset($data['PRICE_DELIVERY']) && floatval($data['PRICE_DELIVERY']) >= 0
1719 || isset($data['DISCOUNT_PRICE']) && floatval($data['DISCOUNT_PRICE']) >= 0))
1720 {
1721 if ($shipment = $this->getShipmentCollection()->getItemByShipmentCode($data['SHIPMENT']))
1722 {
1723 if (!$shipment->isCustomPrice())
1724 {
1725 $data['PRICE_DELIVERY'] = (float)$data['PRICE_DELIVERY'];
1726 $data['DISCOUNT_PRICE'] = (float)$data['DISCOUNT_PRICE'];
1727 if ($data['PRICE_DELIVERY'] >= 0 && $data['PRICE_DELIVERY'] != $shipment->getPrice())
1728 {
1729 $data['PRICE_DELIVERY'] = PriceMaths::roundPrecision(floatval($data['PRICE_DELIVERY']));
1730 $shipment->setField('PRICE_DELIVERY', $data['PRICE_DELIVERY']);
1731 }
1732
1733 if ($data['DISCOUNT_PRICE'] != $shipment->getField('DISCOUNT_PRICE'))
1734 {
1735 $data['DISCOUNT_PRICE'] = PriceMaths::roundPrecision(floatval($data['DISCOUNT_PRICE']));
1736 $shipment->setField('DISCOUNT_PRICE', $data['DISCOUNT_PRICE']);
1737 }
1738 }
1739
1740 }
1741 }
1742
1743 return new Result();
1744 }
1745
1753 public static function lock($id)
1754 {
1755 global $USER;
1756
1757 $result = new Result();
1758 $id = (int)$id;
1759 if ($id <= 0)
1760 {
1761 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1762 return $result;
1763 }
1764
1765 return static::updateInternal($id, array(
1766 'DATE_LOCK' => new Main\Type\DateTime(),
1767 'LOCKED_BY' => (is_object($USER) ? $USER->GetID(): false)
1768 ));
1769 }
1770
1779 public static function unlock($id)
1780 {
1781 global $USER;
1782
1783 $result = new Result();
1784 $id = (int)$id;
1785 if ($id <= 0)
1786 {
1787 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1788 return $result;
1789 }
1790
1791 if(!$order = static::load($id))
1792 {
1793 $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_ENTITY_NOT_FOUND'), 'SALE_ORDER_ENTITY_NOT_FOUND') );
1794 return $result;
1795 }
1796
1797 $userRights = \CMain::getUserRight("sale", $USER->getUserGroupArray(), "Y", "Y");
1798
1799 if (($userRights >= "W") || ($order->getField("LOCKED_BY") == $USER->getID()))
1800 {
1801 return static::updateInternal($id, array(
1802 'DATE_LOCK' => null,
1803 'LOCKED_BY' => null
1804 ));
1805 }
1806
1807 return $result;
1808 }
1809
1816 public static function isLocked($id)
1817 {
1819 $r = static::getLockedStatus($id);
1820 if ($r->isSuccess())
1821 {
1822 $lockResultData = $r->getData();
1823
1824 if (array_key_exists('LOCK_STATUS', $lockResultData)
1825 && $lockResultData['LOCK_STATUS'] == static::SALE_ORDER_LOCK_STATUS_RED)
1826 {
1827 return true;
1828 }
1829 }
1830
1831 return false;
1832 }
1833
1841 public static function getLockedStatus($id)
1842 {
1843 $result = new Result();
1844
1845 $res = static::getList(array(
1846 'filter' => array('=ID' => $id),
1847 'select' => array(
1848 'LOCKED_BY',
1849 'LOCK_STATUS',
1850 'DATE_LOCK'
1851 )
1852 ));
1853
1854 if ($data = $res->fetch())
1855 {
1856 $result->addData(array(
1857 'LOCKED_BY' => $data['LOCKED_BY'],
1858 'LOCK_STATUS' => $data['LOCK_STATUS'],
1859 'DATE_LOCK' => $data['DATE_LOCK'],
1860 ));
1861 }
1862
1863 return $result;
1864 }
1865
1874 public function verify()
1875 {
1876 $result = parent::verify();
1877
1878 $r = $this->getPaymentCollection()->verify();
1879 if (!$r->isSuccess())
1880 {
1881 $result->addErrors($r->getErrors());
1882 }
1883
1884 $r = $this->getShipmentCollection()->verify();
1885 if (!$r->isSuccess())
1886 {
1887 $result->addErrors($r->getErrors());
1888 }
1889
1890 return $result;
1891 }
1892
1897 public function getBusinessValueProviderInstance($mapping)
1898 {
1899 $providerInstance = null;
1900
1901 if (is_array($mapping) && isset($mapping['PROVIDER_KEY']))
1902 {
1903 switch ($mapping['PROVIDER_KEY'])
1904 {
1905 case 'ORDER':
1906 case 'PROPERTY':
1907 $providerInstance = $this;
1908 break;
1909 case 'USER':
1910 $providerInstance = $this->getField('USER_ID');
1911 break;
1912 case 'COMPANY':
1913 $providerInstance = $this->getField('COMPANY_ID');
1914 break;
1915 }
1916 }
1917
1918 return $providerInstance;
1919 }
1920
1927 public static function getList(array $parameters = array())
1928 {
1929 return Internals\OrderTable::getList($parameters);
1930 }
1931
1938 protected function cloneEntities(\SplObjectStorage $cloneEntity)
1939 {
1941 parent::cloneEntities($cloneEntity);
1942
1943 $orderClone = $cloneEntity[$this];
1944
1945 $orderClone->shipmentCollection = $this->getShipmentCollection()->createClone($cloneEntity);
1946
1947 $orderClone->paymentCollection = $this->getPaymentCollection()->createClone($cloneEntity);
1948
1949 $orderClone->tradeBindingCollection = $this->getTradeBindingCollection()->createClone($cloneEntity);
1950 }
1951
1955 public function isChanged()
1956 {
1957 if (parent::isChanged())
1958 {
1959 return true;
1960 }
1961
1962 if ($this->getPaymentCollection()->isChanged())
1963 {
1964 return true;
1965 }
1966
1967 if ($this->getShipmentCollection()->isChanged())
1968 {
1969 return true;
1970 }
1971
1972 return false;
1973 }
1974
1979 public function clearChanged()
1980 {
1981 parent::clearChanged();
1982
1983 $this->getPaymentCollection()->clearChanged();
1984
1985 $this->getShipmentCollection()->clearChanged();
1986
1987 $this->getTradeBindingCollection()->clearChanged();
1988 }
1989
1995 public function getDeliveryIdList() : array
1996 {
1997 $result = [];
1998
2000 foreach ($this->getShipmentCollection()->getNotSystemItems() as $shipment)
2001 {
2002 if ($shipment->getDeliveryId() > 0)
2003 {
2004 $result[] = $shipment->getDeliveryId();
2005 }
2006 }
2007
2008 return $result;
2009 }
2010
2014 public function getPaySystemIdList() : array
2015 {
2016 $result = [];
2017
2019 foreach ($this->getPaymentCollection() as $payment)
2020 {
2021 if ($payment->getPaymentSystemId() > 0)
2022 {
2023 $result[] = $payment->getPaymentSystemId();
2024 }
2025 }
2026
2027 return $result;
2028 }
2029
2033 protected function calculateVat()
2034 {
2035 $vatInfo = parent::calculateVat();
2036
2038 foreach ($this->getShipmentCollection() as $shipment)
2039 {
2040 $rate = $shipment->getVatRate();
2041 if ($rate)
2042 {
2043 $vatInfo['VAT_SUM'] += $shipment->getVatSum();
2044 $vatInfo['VAT_RATE'] = max($vatInfo['VAT_RATE'], $rate);
2045 }
2046 }
2047
2048 return $vatInfo;
2049 }
2050
2054 protected function saveEntities()
2055 {
2056 $result = parent::saveEntities();
2057
2058 $r = $this->getShipmentCollection()->save();
2059 if (!$r->isSuccess())
2060 {
2061 $result->addWarnings($r->getErrors());
2062 }
2063
2064 $r = $this->getPaymentCollection()->save();
2065 if (!$r->isSuccess())
2066 {
2067 $result->addWarnings($r->getErrors());
2068 }
2069
2070 // user budget
2071 Internals\UserBudgetPool::onUserBudgetSave($this->getUserId());
2072
2073 $r = $this->getTradeBindingCollection()->save();
2074 if (!$r->isSuccess())
2075 {
2076 $result->addWarnings($r->getErrors());
2077 }
2078
2079 $res = Cashbox\Internals\Pool::generateChecks($this->getInternalId());
2080 if (!$res->isSuccess())
2081 {
2082 $result->addWarnings($res->getErrors());
2083
2084 $warningResult = new Result();
2085 $warningResult->addWarnings($res->getErrors());
2086
2087 $registry = Registry::getInstance(static::getRegistryType());
2089 $entityMarker = $registry->getEntityMarkerClassName();
2090 $entityMarker::addMarker($this, $this, $warningResult);
2091 static::updateInternal($this->getId(), array('MARKED' => 'Y'));
2092 }
2093
2094 return $result;
2095 }
2096
2100 protected function calculatePrice()
2101 {
2102 $price = parent::calculatePrice();
2104
2105 return $price + $shipmentCollection->getPriceDelivery();
2106 }
2107
2115 protected function onBeforeSave()
2116 {
2117 $registry = Registry::getInstance(static::getRegistryType());
2118
2120 $entityMarker = $registry->getEntityMarkerClassName();
2121
2122 $result = Internals\Catalog\Provider::save($this);
2123 if ($result->hasWarnings())
2124 {
2125 $entityMarker::addMarker($this, $this, $result);
2126 if ($this->getId() > 0)
2127 {
2128 static::updateInternal($this->getId(), ['MARKED' => 'Y']);
2129 }
2130 }
2131
2132 $entityMarker::refreshMarkers($this);
2133
2134 return $result;
2135 }
2136
2140 protected function onAfterSave()
2141 {
2142 $result = parent::onAfterSave();
2143 if (!$result->isSuccess())
2144 {
2145 return $result;
2146 }
2147
2148 global $CACHE_MANAGER;
2149
2150 if (defined("CACHED_b_sale_order")
2151 && (
2152 $this->isNew
2153 || (
2154 $this->isChanged()
2155 && $this->getField("UPDATED_1C") != "Y"
2156 )
2157 )
2158 )
2159 {
2160 $CACHE_MANAGER->Read(CACHED_b_sale_order, "sale_orders");
2161 $CACHE_MANAGER->SetImmediate("sale_orders", true);
2162 }
2163
2164 return $result;
2165 }
2166
2174 public function save()
2175 {
2176 $result = parent::save();
2177
2178 $registry = Registry::getInstance(static::getRegistryType());
2179
2181 $orderHistory = $registry->getOrderHistoryClassName();
2182 $orderHistory::collectEntityFields('ORDER', $this->getId(), $this->getId());
2183
2185 $entityMarker = $registry->getEntityMarkerClassName();
2186 if ($entityMarker::hasErrors($this))
2187 {
2188 $entityMarker::saveMarkers($this);
2189 static::updateInternal(
2190 $this->getId(),
2191 [
2192 'MARKED' => 'Y',
2193 'REASON_MARKED' => $this->getField('REASON_MARKED')
2194 ]
2195 );
2196 }
2197
2198 return $result;
2199 }
2200
2209 protected function add()
2210 {
2211 $result = parent::add();
2212
2213 $registry = Registry::getInstance(static::getRegistryType());
2214
2216 $orderHistory = $registry->getOrderHistoryClassName();
2217 $orderHistory::addAction('ORDER', $result->getId(), 'ORDER_ADDED', $result->getId(), $this);
2218
2219 return $result;
2220 }
2221
2226 protected function update()
2227 {
2228 $result = parent::update();
2229
2230 $registry = Registry::getInstance(static::getRegistryType());
2232 $orderHistory = $registry->getOrderHistoryClassName();
2233
2234 if (!$result->isSuccess())
2235 {
2236 $orderHistory::addAction(
2237 'ORDER',
2238 $this->getId(),
2239 'ORDER_UPDATE_ERROR',
2240 $this->getId(),
2241 $this,
2242 array("ERROR" => $result->getErrorMessages())
2243 );
2244 }
2245 else
2246 {
2247 $orderHistory::addAction(
2248 'ORDER',
2249 $this->getId(),
2250 'ORDER_UPDATED',
2251 $this->getId(),
2252 $this,
2253 array(),
2255 );
2256 }
2257
2258 return $result;
2259 }
2260
2265 protected function callEventOnSaleOrderEntitySaved()
2266 {
2267 parent::callEventOnSaleOrderEntitySaved();
2268
2269 $changeMeaningfulFields = array(
2270 "PERSON_TYPE_ID",
2271 "CANCELED",
2272 "STATUS_ID",
2273 "MARKED",
2274 "PRICE",
2275 "SUM_PAID",
2276 "USER_ID",
2277 "EXTERNAL_ORDER",
2278 );
2279
2280 if ($this->isChanged())
2281 {
2282 $logFields = array();
2283
2284 if (!$this->isNew)
2285 {
2286 $fields = $this->getFields();
2287 $originalValues = $fields->getOriginalValues();
2288
2289 foreach($originalValues as $originalFieldName => $originalFieldValue)
2290 {
2291 if (in_array($originalFieldName, $changeMeaningfulFields) && $this->getField($originalFieldName) != $originalFieldValue)
2292 {
2293 $logFields[$originalFieldName] = $this->getField($originalFieldName);
2294 $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
2295 }
2296 }
2297
2298 $registry = Registry::getInstance(static::getRegistryType());
2299
2301 $orderHistory = $registry->getOrderHistoryClassName();
2302 $orderHistory::addLog(
2303 'ORDER',
2304 $this->getId(),
2305 "ORDER_UPDATE",
2306 $this->getId(),
2307 $this,
2308 $logFields,
2309 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2310 );
2311 }
2312 }
2313 }
2314
2319 protected function callEventOnSaleOrderSaved()
2320 {
2321 $registry = Registry::getInstance(static::getRegistryType());
2322
2324 $orderHistory = $registry->getOrderHistoryClassName();
2325 $orderHistory::addLog(
2326 'ORDER',
2327 $this->getId(),
2328 'ORDER_EVENT_ON_ORDER_SAVED',
2329 null,
2330 null,
2331 array(),
2332 $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2333 );
2334
2335 parent::callEventOnSaleOrderSaved();
2336 }
2337
2343 protected function addInternal(array $data)
2344 {
2345 return Internals\OrderTable::add($data);
2346 }
2347
2354 protected static function updateInternal($primary, array $data)
2355 {
2356 return Internals\OrderTable::update($primary, $data);
2357 }
2358
2364 protected static function deleteInternal($primary)
2365 {
2366 return Internals\OrderTable::delete($primary);
2367 }
2368
2373 protected static function deleteExternalEntities($orderId)
2374 {
2375 parent::deleteExternalEntities($orderId);
2376
2377 $registry = Registry::getInstance(static::getRegistryType());
2378
2379 TradingPlatform\OrderTable::deleteByOrderId($orderId);
2380 Internals\OrderProcessingTable::deleteByOrderId($orderId);
2381
2383 $entityMarker = $registry->getEntityMarkerClassName();
2384 $entityMarker::deleteByOrderId($orderId);
2385
2387 $orderHistory = $registry->getOrderHistoryClassName();
2388 $orderHistory::deleteByOrderId($orderId);
2389 }
2390
2398 protected function addChangesToHistory($name, $oldValue = null, $value = null)
2399 {
2400 if ($this->getId() > 0)
2401 {
2402 $historyFields = array();
2403 if ($name == "PRICE")
2404 {
2405 $historyFields['CURRENCY'] = $this->getCurrency();
2406 }
2407 if ($name == "RESPONSIBLE_ID")
2408 {
2409 $historyFields[$name] = $value;
2410 }
2411
2412 $historyFields['OLD_'.$name] = $oldValue;
2413
2414 $registry = Registry::getInstance(static::getRegistryType());
2415
2417 $orderHistory = $registry->getOrderHistoryClassName();
2418 $orderHistory::addField(
2419 'ORDER',
2420 $this->getId(),
2421 $name,
2422 $oldValue,
2423 $value,
2424 $this->getId(),
2425 $this,
2426 $historyFields
2427 );
2428 }
2429 }
2430
2437 public function getDeliverySystemId()
2438 {
2439 return $this->getDeliveryIdList();
2440 }
2441
2447 public function getPaymentSystemId()
2448 {
2449 return $this->getPaySystemIdList();
2450 }
2451
2457 public function toArray() : array
2458 {
2459 $result = parent::toArray();
2460
2461 $result['PAYMENTS'] = $this->getPaymentCollection()->toArray();
2462 $result['SHIPMENTS'] = $this->getShipmentCollection()->toArray();
2463 $result['TRADE_BINDINGS'] = $this->getTradeBindingCollection()->toArray();
2464
2465 return $result;
2466 }
2467}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
isStartField($isMeaningfulField=false)
setFieldNoDemand($name, $value)
setField($name, $value)
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
onFieldModify($name, $oldValue, $value)
Definition order.php:1055
setBasket(BasketBase $basket)
Definition order.php:653
onOrderModify($name, $oldValue, $value)
Definition order.php:1108
applyDiscount(array $data)
Definition order.php:1709
static getUfId()
Definition order.php:58
getBusinessValueProviderInstance($mapping)
Definition order.php:1897
onBasketModify($action, BasketItemBase $basketItem, $name=null, $oldValue=null, $value=null)
Definition order.php:1176
const SALE_ORDER_LOCK_STATUS_YELLOW
Definition order.php:36
static getList(array $parameters=array())
Definition order.php:1927
static getFieldsMap()
Definition order.php:50
static updateInternal($primary, array $data)
Definition order.php:2354
const SALE_ORDER_LOCK_STATUS_GREEN
Definition order.php:35
const SALE_ORDER_LOCK_STATUS_RED
Definition order.php:34
onBeforeBasketItemDelete(BasketItem $basketItem)
Definition order.php:1139
resetData($select=array('PRICE'))
Definition order.php:625
onTradeBindingCollectionModify($action, TradeBindingEntity $entity, $name=null, $oldValue=null, $value=null)
Definition order.php:596
static getLockedStatus($id)
Definition order.php:1841
addInternal(array $data)
Definition order.php:2343
addPrintedCheck($check)
Definition order.php:109
static deleteInternal($primary)
Definition order.php:2364
static unlock($id)
Definition order.php:1779
static getRegistryType()
Definition order.php:41
static lock($id)
Definition order.php:1753
getTradeBindingCollection()
Definition order.php:776
static roundPrecision($value)
const ENTITY_TRADE_BINDING_COLLECTION
Definition registry.php:55
static getInstance($type)
Definition registry.php:183
setField($name, $value)
Definition shipment.php:789