Bitrix-D7  20.5.0
sale/lib/order.php
См. документацию.
1 <?php
2 
3 namespace Bitrix\Sale;
4 
6 use Bitrix\Main;
11 
12 Loc::loadMessages(__FILE__);
13 
14 /**
15  * Class Order
16  * @package Bitrix\Sale
17  */
19 {
20  /** @var ShipmentCollection */
22 
23  /** @var PaymentCollection */
24  protected $paymentCollection;
25 
26  /** @var TradeBindingCollection */
28 
29  /** @var array $printedChecks */
30  protected $printedChecks = array();
31 
32 
36 
37  /**
38  * @return string
39  */
40  public static function getRegistryType()
41  {
43  }
44 
45 
46  /**
47  * @return array
48  */
49  protected static function getFieldsMap()
50  {
52  }
53 
54  /**
55  * @return null|string
56  */
57  public static function getUfId()
58  {
60  }
61 
62  /**
63  * Return printed check list
64  *
65  * @return array
66  * @throws Main\ArgumentException
67  */
68  public function getPrintedChecks()
69  {
70  if (!$this->printedChecks
71  && !$this->isNew()
72  )
73  {
74  $this->printedChecks = $this->loadPrintedChecks();
75  }
76 
77  return $this->printedChecks;
78  }
79 
80  /**
81  * @return array
82  * @throws Main\ArgumentException
83  */
84  protected function loadPrintedChecks()
85  {
86  $result = [];
87 
89  'filter' => [
90  '=ORDER_ID' => $this->getId()
91  ]
92  ]);
93 
94  while ($data = $dbRes->fetch())
95  {
96  $result[] = Cashbox\CheckManager::create($data);
97  }
98 
99  return $result;
100  }
101 
102 
103  /**
104  * Add printed check to order
105  *
106  * @param $check
107  */
108  public function addPrintedCheck($check)
109  {
110  $this->printedChecks[] = $check;
111  }
112 
113  /**
114  * Modify shipment collection.
115  *
116  * @param $action
117  * @param Shipment $shipment
118  * @param null $name
119  * @param null $oldValue
120  * @param null $value
121  * @return Result
122  * @throws Main\ArgumentException
123  * @throws Main\ArgumentNullException
124  * @throws Main\ArgumentOutOfRangeException
125  * @throws Main\NotImplementedException
126  * @throws Main\NotSupportedException
127  * @throws Main\ObjectException
128  * @throws Main\ObjectNotFoundException
129  */
130  public function onShipmentCollectionModify($action, Shipment $shipment, $name = null, $oldValue = null, $value = null)
131  {
132  $result = new Result();
133 
134  $registry = Registry::getInstance(static::getRegistryType());
135 
136  $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
137 
138  /** @var EntityMarker $entityMarker */
139  $entityMarker = $registry->getEntityMarkerClassName();
140 
141  if ($action == EventActions::DELETE)
142  {
143  if ($this->getField('DELIVERY_ID') == $shipment->getDeliveryId())
144  {
145  /** @var ShipmentCollection $shipmentCollection */
146  $shipmentCollection = $shipment->getCollection();
147 
148  $foundShipment = false;
149 
150  /** @var Shipment $entityShipment */
151  foreach ($shipmentCollection->getNotSystemItems() as $entityShipment)
152  {
153  if (intval($entityShipment->getField('DELIVERY_ID')) > 0)
154  {
155  $foundShipment = true;
156  $this->setFieldNoDemand('DELIVERY_ID', $entityShipment->getField('DELIVERY_ID'));
157  break;
158  }
159  }
160 
161  if (!$foundShipment && !$shipment->isSystem())
162  {
163  /** @var Shipment $systemShipment */
164  if (($systemShipment = $shipmentCollection->getSystemShipment()) && intval($systemShipment->getField('DELIVERY_ID')) > 0)
165  {
166  $this->setFieldNoDemand('DELIVERY_ID', $systemShipment->getField('DELIVERY_ID'));
167  }
168  }
169  }
170  }
171  elseif ($action === EventActions::ADD)
172  {
173  if ($shipment->getId() === 0)
174  {
175  $this->getPropertyCollection()->refreshRelated();
176  }
177 
178  if (!$shipment->isSystem())
179  {
180  $this->setFieldNoDemand('DELIVERY_ID', $shipment->getDeliveryId());
181  }
182  }
183 
184  if ($action !== EventActions::UPDATE)
185  {
186  return $result;
187  }
188 
189  if ($name == "ALLOW_DELIVERY")
190  {
191  if ($this->isCanceled())
192  {
193  $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_ALLOW_DELIVERY_ORDER_CANCELED'));
194  return $result;
195  }
196 
197  $r = $shipment->deliver();
198  if ($r->isSuccess())
199  {
200  $eventManager = Main\EventManager::getInstance();
201  if ($eventsList = $eventManager->findEventHandlers('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER))
202  {
203  $event = new Main\Event('sale', EventActions::EVENT_ON_SHIPMENT_DELIVER, array(
204  'ENTITY' =>$shipment
205  ));
206  $event->send();
207  }
208  }
209  else
210  {
211  $result->addErrors($r->getErrors());
212  }
213 
215  {
216  if ($value == "Y")
217  {
218  /** @var Result $r */
219  $r = $shipment->tryReserve();
220  if (!$r->isSuccess())
221  {
222  $result->addErrors($r->getErrors());
223  }
224 
225  if ($r->hasWarnings())
226  {
227  $result->addWarnings($r->getWarnings());
228  $entityMarker::addMarker($this, $shipment, $r);
229  if (!$shipment->isSystem())
230  {
231  $shipment->setField('MARKED', 'Y');
232  }
233  }
234  }
235  else
236  {
237  if (!$shipment->isShipped())
238  {
239  /** @var Result $r */
240  $r = $shipment->tryUnreserve();
241  if (!$r->isSuccess())
242  {
243  $result->addErrors($r->getErrors());
244  }
245 
246  if ($r->hasWarnings())
247  {
248  $result->addWarnings($r->getWarnings());
249  $entityMarker::addMarker($this, $shipment, $r);
250  if (!$shipment->isSystem())
251  {
252  $shipment->setField('MARKED', 'Y');
253  }
254  }
255  }
256  }
257 
258  if (!$result->isSuccess())
259  {
260  return $result;
261  }
262  }
263 
264  /** @var ShipmentCollection $shipmentCollection */
266  {
267  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
268  }
269 
270  $orderStatus = null;
271 
272  if ($oldValue == "N")
273  {
274  if ($shipmentCollection->isAllowDelivery())
275  {
276  $orderStatus = $optionClassName::get('sale', 'status_on_allow_delivery', '');
277  }
278  elseif ($shipmentCollection->hasAllowDelivery())
279  {
280  $orderStatus = $optionClassName::get('sale', 'status_on_allow_delivery_one_of', '');
281  }
282  }
283 
284  if ($orderStatus !== null && $this->getField('STATUS_ID') != static::getFinalStatus())
285  {
286  if (strval($orderStatus) != '')
287  {
288  $r = $this->setField('STATUS_ID', $orderStatus);
289  if (!$r->isSuccess())
290  {
291  $result->addErrors($r->getErrors());
292  }
293 
294  if ($r->hasWarnings())
295  {
296  $result->addWarnings($r->getWarnings());
297  $entityMarker::addMarker($this, $this, $r);
298  $this->setField('MARKED', 'Y');
299  }
300  }
301  }
302 
303  if (Configuration::needShipOnAllowDelivery() && $value == "Y")
304  {
305  if (!$shipment->isEmpty())
306  {
307  $r = $shipment->setField("DEDUCTED", "Y");
308  if (!$r->isSuccess())
309  {
310  $result->addErrors($r->getErrors());
311  }
312 
313  if ($r->hasWarnings())
314  {
315  $result->addWarnings($r->getWarnings());
316  $entityMarker::addMarker($this, $shipment, $r);
317  if (!$shipment->isSystem())
318  {
319  $shipment->setField('MARKED', 'Y');
320  }
321  }
322  }
323  }
324 
325  if ($shipmentCollection->isAllowDelivery() && $this->getField('ALLOW_DELIVERY') == 'N')
326  $this->setFieldNoDemand('DATE_ALLOW_DELIVERY', new Type\DateTime());
327 
328  $this->setFieldNoDemand('ALLOW_DELIVERY', $shipmentCollection->isAllowDelivery() ? "Y" : "N");
329  }
330  elseif ($name == "DEDUCTED")
331  {
332  if ($this->isCanceled())
333  {
334  $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_SHIPMENT_ORDER_CANCELED'), 'SALE_ORDER_SHIPMENT_ORDER_CANCELED'));
335  return $result;
336  }
337 
339  {
340  if ($value == "Y")
341  {
342  /** @var Result $r */
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  {
381  /** @var Result $r */
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  /** @var Result $r */
402  $r = $shipment->tryUnship();
403  if (!$r->isSuccess())
404  {
405  $result->addErrors($r->getErrors());
406  }
407 
408  if ($r->hasWarnings())
409  {
410  $result->addWarnings($r->getWarnings());
411  $entityMarker::addMarker($this, $shipment, $r);
412  if (!$shipment->isSystem())
413  {
414  $shipment->setField('MARKED', 'Y');
415  }
416  }
417  if ($shipment->needReservation())
418  {
419  $r = $shipment->tryReserve();
420  if (!$r->isSuccess())
421  {
422  $result->addErrors($r->getErrors());
423  }
424 
425  if ($r->hasWarnings())
426  {
427  $result->addWarnings($r->getWarnings());
428  $entityMarker::addMarker($this, $shipment, $r);
429  if (!$shipment->isSystem())
430  {
431  $shipment->setField('MARKED', 'Y');
432  }
433  }
434  }
435  }
436 
437  if (!$result->isSuccess())
438  {
439  return $result;
440  }
441 
442  /** @var ShipmentCollection $shipmentCollection */
443  if (!$shipmentCollection = $shipment->getCollection())
444  {
445  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
446  }
447 
448  $orderStatus = null;
449 
450  $allowSetStatus = false;
451 
452  if ($oldValue == "N")
453  {
454  if ($shipmentCollection->isShipped())
455  {
456  $orderStatus = $optionClassName::get('sale', 'status_on_shipped_shipment', '');
457  }
458  elseif ($shipmentCollection->hasShipped())
459  {
460  $orderStatus = $optionClassName::get('sale', 'status_on_shipped_shipment_one_of', '');
461  }
462  $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
463  }
464  else
465  {
466  $fields = $this->getFields();
467  $originalValues = $fields->getOriginalValues();
468  if (!empty($originalValues['STATUS_ID']))
469  {
470  $orderStatus = $originalValues['STATUS_ID'];
471  $allowSetStatus = true;
472  }
473  }
474 
475  if (strval($orderStatus) != '' && $allowSetStatus)
476  {
477  if (strval($orderStatus) != '')
478  {
479  $r = $this->setField('STATUS_ID', $orderStatus);
480  if (!$r->isSuccess())
481  {
482  $result->addErrors($r->getErrors());
483  }
484  elseif ($r->hasWarnings())
485  {
486  $result->addWarnings($r->getWarnings());
487  $entityMarker::addMarker($this, $this, $r);
488  $this->setField('MARKED', 'Y');
489  }
490  }
491  }
492 
493  $this->setFieldNoDemand('DEDUCTED', $shipmentCollection->isShipped() ? "Y" : "N");
494 
495  if ($shipmentCollection->isShipped())
496  {
497  if (strval($shipment->getField('DATE_DEDUCTED')) != '')
498  {
499  $this->setFieldNoDemand('DATE_DEDUCTED', $shipment->getField('DATE_DEDUCTED'));
500  }
501  if (strval($shipment->getField('EMP_DEDUCTED_ID')) != '')
502  {
503  $this->setFieldNoDemand('EMP_DEDUCTED_ID', $shipment->getField('EMP_DEDUCTED_ID'));
504  }
505  }
506  }
507  elseif ($name == "MARKED")
508  {
509  if ($value == "Y")
510  {
511  /** @var Result $r */
512  $r = $this->setField('MARKED', 'Y');
513  if (!$r->isSuccess())
514  {
515  $result->addErrors($r->getErrors());
516  }
517  }
518  }
519  elseif ($name == "REASON_MARKED")
520  {
521  $r = $this->setReasonMarked($value);
522  if (!$r->isSuccess())
523  {
524  $result->addErrors($r->getErrors());
525  }
526  }
527  elseif ($name == "BASE_PRICE_DELIVERY")
528  {
529  if ($this->isCanceled())
530  {
531  $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
532  return $result;
533  }
534 
535  $discount = $this->getDiscount();
536  $discount->setCalculateShipments($shipment);
537 
538  $r = $shipment->setField('PRICE_DELIVERY', $value);
539  if (!$r->isSuccess())
540  {
541  $result->addErrors($r->getErrors());
542  }
543  }
544  elseif ($name == "PRICE_DELIVERY")
545  {
546  if ($this->isCanceled())
547  {
548  $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'), 'SALE_ORDER_PRICE_DELIVERY_ORDER_CANCELED'));
549  return $result;
550  }
551 
552  $deliveryPrice = ($this->isNew()) ? $value : $this->getField("PRICE_DELIVERY") - $oldValue + $value;
553  $this->setFieldNoDemand(
554  "PRICE_DELIVERY",
555  $deliveryPrice
556  );
557 
558  /** @var Result $r */
559  $r = $this->setField(
560  "PRICE",
561  $this->getField("PRICE") - $oldValue + $value
562  );
563 
564  if (!$r->isSuccess())
565  {
566  $result->addErrors($r->getErrors());
567  }
568 
569  }
570  elseif ($name == "DELIVERY_ID")
571  {
572  if ($shipment->isSystem() || intval($shipment->getField('DELIVERY_ID')) <= 0 )
573  {
574  return $result;
575  }
576 
577  $this->setFieldNoDemand('DELIVERY_ID', $shipment->getField('DELIVERY_ID'));
578 
579  $this->getPropertyCollection()->refreshRelated();
580 
581  }
582  elseif ($name == "TRACKING_NUMBER")
583  {
584  if ($shipment->isSystem())
585  {
586  return $result;
587  }
588 
589  $this->setFieldNoDemand('TRACKING_NUMBER', $shipment->getField('TRACKING_NUMBER'));
590  }
591 
592  if ($value != $oldValue)
593  {
594  $fields = $this->fields->getChangedValues();
595  if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields))
596  {
597  parent::setField("UPDATED_1C", "N");
598  }
599  }
600 
601  return $result;
602  }
603 
604  /**
605  * @param array $select
606  * @throws Main\ArgumentException
607  * @throws Main\ArgumentNullException
608  * @throws Main\ArgumentOutOfRangeException
609  */
610  public function resetData($select = array('PRICE'))
611  {
612  $this->getShipmentCollection()->resetData();
613 
614  parent::resetData($select);
615  }
616 
617  /**
618  * @param BasketBase $basket
619  * @return Result
620  * @throws Main\ArgumentException
621  * @throws Main\ArgumentNullException
622  * @throws Main\ArgumentOutOfRangeException
623  * @throws Main\NotSupportedException
624  * @throws Main\ObjectNotFoundException
625  */
626  public function setBasket(BasketBase $basket)
627  {
628  $result = new Result();
629 
630  $isStartField = $this->isStartField();
631 
632  $r = parent::setBasket($basket);
633  if (!$r->isSuccess())
634  {
635  $result->addErrors($r->getErrors());
636  return $result;
637  }
638 
639  /** @var ShipmentCollection $shipmentCollection */
641  {
642  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
643  }
644 
645  /** @var Result $r */
646  $result = $shipmentCollection->resetCollection();
647  if (!$r->isSuccess())
648  {
649  $result->addErrors($r->getErrors());
650  return $result;
651  }
652 
653  if (!$this->isMathActionOnly())
654  {
655  /** @var Result $r */
656  $r = $this->refreshData();
657  if (!$r->isSuccess())
658  {
659  $result->addErrors($r->getErrors());
660  }
661  }
662 
663  if ($isStartField)
664  {
665  $hasMeaningfulFields = $this->hasMeaningfulField();
666 
667  /** @var Result $r */
668  $r = $this->doFinalAction($hasMeaningfulFields);
669  if (!$r->isSuccess())
670  {
671  $result->addErrors($r->getErrors());
672  }
673  }
674 
675  return $result;
676  }
677 
678  /**
679  * @param BasketBase $basket
680  * @return Result
681  * @throws Main\ArgumentException
682  * @throws Main\ArgumentNullException
683  * @throws Main\ArgumentOutOfRangeException
684  * @throws Main\NotSupportedException
685  * @throws Main\ObjectNotFoundException
686  */
687  public function appendBasket(BasketBase $basket)
688  {
689  $result = new Result();
690 
691  $isStartField = $this->isStartField();
692 
693  $r = parent::appendBasket($basket);
694  if (!$r->isSuccess())
695  {
696  $result->addErrors($r->getErrors());
697  return $result;
698  }
699 
700  /** @var ShipmentCollection $shipmentCollection */
702  {
703  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
704  }
705 
706  /** @var Result $r */
707  $result = $shipmentCollection->resetCollection();
708  if (!$r->isSuccess())
709  {
710  $result->addErrors($r->getErrors());
711  return $result;
712  }
713 
714  if (!$this->isMathActionOnly())
715  {
716  /** @var Result $r */
717  $r = $this->refreshData();
718  if (!$r->isSuccess())
719  {
720  $result->addErrors($r->getErrors());
721  }
722  }
723 
724  if ($isStartField)
725  {
726  $hasMeaningfulFields = $this->hasMeaningfulField();
727 
728  /** @var Result $r */
729  $r = $this->doFinalAction($hasMeaningfulFields);
730  if (!$r->isSuccess())
731  {
732  $result->addErrors($r->getErrors());
733  }
734  }
735 
736  return $result;
737  }
738 
739  /**
740  * Return shipment collection
741  *
742  * @return ShipmentCollection
743  * @throws Main\ArgumentException
744  * @throws Main\ArgumentNullException
745  */
746  public function getShipmentCollection()
747  {
748  if (empty($this->shipmentCollection))
749  {
750  $this->shipmentCollection = $this->loadShipmentCollection();
751  }
752 
753  return $this->shipmentCollection;
754  }
755 
756  /**
757  * Return trade binding collection
758  *
759  * @return TradeBindingCollection
760  * @throws Main\ArgumentException
761  * @throws Main\ArgumentTypeException
762  * @throws Main\SystemException
763  */
764  public function getTradeBindingCollection()
765  {
766  if (empty($this->tradeBindingCollection))
767  {
768  $this->tradeBindingCollection = $this->loadTradeBindingCollection();
769  }
770 
771  return $this->tradeBindingCollection;
772  }
773 
774  /**
775  * Return payment collection
776  *
777  * @return PaymentCollection
778  */
779  public function getPaymentCollection()
780  {
781  if (empty($this->paymentCollection))
782  {
783  $this->paymentCollection = $this->loadPaymentCollection();
784  }
785 
786  return $this->paymentCollection;
787  }
788 
789  /**
790  * Load shipment collection
791  *
792  * @return ShipmentCollection
793  * @throws Main\ArgumentException
794  * @throws Main\ArgumentNullException
795  */
796  public function loadShipmentCollection()
797  {
798  $registry = Registry::getInstance(static::getRegistryType());
799 
800  /** @var ShipmentCollection $shipmentCollectionClassName */
801  $shipmentCollectionClassName = $registry->getShipmentCollectionClassName();
802  return $shipmentCollectionClassName::load($this);
803  }
804 
805  /**
806  * Load payment collection
807  *
808  * @return PaymentCollection
809  * @throws Main\ArgumentException
810  */
811  public function loadPaymentCollection()
812  {
813  $registry = Registry::getInstance(static::getRegistryType());
814 
815  /** @var PaymentCollection $paymentCollectionClassName */
816  $paymentCollectionClassName = $registry->getPaymentCollectionClassName();
817  return $paymentCollectionClassName::load($this);
818  }
819 
820  /**
821  * @return TradeBindingCollection
822  * @throws Main\ArgumentException
823  * @throws Main\ArgumentTypeException
824  * @throws Main\SystemException
825  */
826  protected function loadTradeBindingCollection()
827  {
828  $registry = Registry::getInstance(static::getRegistryType());
829 
830  /** @var TradeBindingCollection $tradeBindingCollection */
832 
833  return $tradeBindingCollection::load($this);
834  }
835 
836  /**
837  * @param $orderId
838  * @return Result
839  * @throws Main\ArgumentException
840  * @throws Main\ObjectNotFoundException
841  */
842  protected static function deleteEntitiesNoDemand($orderId)
843  {
844  $r = parent::deleteEntitiesNoDemand($orderId);
845  if (!$r->isSuccess())
846  return $r;
847 
848  $registry = Registry::getInstance(static::getRegistryType());
849 
850  /** @var Shipment $shipmentClassName */
851  $shipmentClassName = $registry->getShipmentClassName();
852  $shipmentClassName::deleteNoDemand($orderId);
853  if (!$r->isSuccess())
854  return $r;
855 
856  /** @var Payment $paymentClassName */
857  $paymentClassName = $registry->getPaymentClassName();
858  $paymentClassName::deleteNoDemand($orderId);
859  if (!$r->isSuccess())
860  return $r;
861 
862  return new Result();
863  }
864 
865  /**
866  * @param OrderBase $order
867  * @throws Main\ArgumentException
868  * @throws Main\ArgumentNullException
869  * @throws Main\ArgumentOutOfRangeException
870  * @throws Main\ArgumentTypeException
871  * @throws Main\NotSupportedException
872  * @throws Main\ObjectNotFoundException
873  */
874  protected static function deleteEntities(OrderBase $order)
875  {
876  parent::deleteEntities($order);
877 
878  if (!($order instanceof Order))
879  throw new Main\ArgumentTypeException($order);
880 
881  /** @var ShipmentCollection $shipmentCollection */
882  if ($shipmentCollection = $order->getShipmentCollection())
883  {
884  /** @var Shipment $shipment */
885  foreach ($shipmentCollection as $shipment)
886  {
887  $shipment->delete();
888  }
889  }
890 
891  /** @var PaymentCollection $paymentCollection */
892  if ($paymentCollection = $order->getPaymentCollection())
893  {
894  /** @var Payment $payment */
895  foreach ($paymentCollection as $payment)
896  {
897  $payment->delete();
898  }
899  }
900  }
901 
902  /**
903  * @return bool
904  */
905  public function isShipped()
906  {
908  return $shipmentCollection->isShipped();
909  }
910 
911  /**
912  * @param $action
913  * @param Payment $payment
914  * @param null $name
915  * @param null $oldValue
916  * @param null $value
917  * @return Result
918  * @throws Main\ArgumentException
919  * @throws Main\ArgumentNullException
920  * @throws Main\ArgumentOutOfRangeException
921  * @throws Main\NotImplementedException
922  * @throws Main\ObjectNotFoundException
923  * @throws Main\SystemException
924  */
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  {
933  /** @var PaymentCollection $paymentCollection */
934  if (!$paymentCollection = $payment->getCollection())
935  {
936  throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
937  }
938 
939  /** @var Payment $entityPayment */
940  foreach ($paymentCollection as $entityPayment)
941  {
942  if (intval($entityPayment->getField('PAY_SYSTEM_ID')) > 0
943  && intval($entityPayment->getField('PAY_SYSTEM_ID')) != $payment->getPaymentSystemId())
944  {
945  $this->setFieldNoDemand('PAY_SYSTEM_ID', $entityPayment->getField('PAY_SYSTEM_ID'));
946  break;
947  }
948  }
949  }
950  }
951  elseif ($action === EventActions::ADD)
952  {
953  if ((int)$payment->getId() === 0)
954  {
955  $this->getPropertyCollection()->refreshRelated();
956  }
957  }
958 
959  if ($action != EventActions::UPDATE)
960  {
961  return $result;
962  }
963 
964  if (($name == "CURRENCY") && ($value != $this->getField("CURRENCY")))
965  {
966  throw new Main\NotImplementedException();
967  }
968 
969  if ($name == "SUM" || $name == "PAID")
970  {
971  if ($this->isCanceled())
972  {
973  $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_PAID_ORDER_CANCELED'), 'SALE_ORDER_PAID_ORDER_CANCELED'));
974  return $result;
975  }
976 
977  if ($name == "SUM"
978  && !$payment->isPaid()
979  )
980  {
981  return $result;
982  }
983 
984  $r = $this->syncOrderAndPayments($payment);
985  if (!$r->isSuccess())
986  {
987  $result->addErrors($r->getErrors());
988  }
989  elseif ($r->hasWarnings())
990  {
991  $result->addWarnings($r->getWarnings());
992  }
993  }
994  elseif ($name == "PAY_SYSTEM_ID")
995  {
996  $this->setFieldNoDemand('PAY_SYSTEM_ID', $payment->getField('PAY_SYSTEM_ID'));
997 
998  $this->getPropertyCollection()->refreshRelated();
999  }
1000  elseif ($name == "DATE_PAID")
1001  {
1002  $this->setFieldNoDemand('DATE_PAYED', $payment->getField('DATE_PAID'));
1003  }
1004  elseif ($name == "PAY_VOUCHER_NUM")
1005  {
1006  $this->setFieldNoDemand('PAY_VOUCHER_NUM', $payment->getField('PAY_VOUCHER_NUM'));
1007  }
1008  elseif ($name == "PAY_VOUCHER_DATE")
1009  {
1010  $this->setFieldNoDemand('PAY_VOUCHER_DATE', $payment->getField('PAY_VOUCHER_DATE'));
1011  }
1012  elseif ($name == "EMP_PAID_ID")
1013  {
1014  $this->setFieldNoDemand('EMP_PAYED_ID', $payment->getField('EMP_PAID_ID'));
1015  }
1016  elseif ($name == "MARKED")
1017  {
1018  if ($value == "Y")
1019  {
1020  /** @var Result $r */
1021  $r = $this->setField('MARKED', 'Y');
1022  if (!$r->isSuccess())
1023  {
1024  $result->addErrors($r->getErrors());
1025  }
1026  }
1027  }
1028  elseif ($name == "REASON_MARKED")
1029  {
1030  $r = $this->setReasonMarked($value);
1031  if (!$r->isSuccess())
1032  {
1033  $result->addErrors($r->getErrors());
1034  }
1035  }
1036 
1037  if ($value != $oldValue)
1038  {
1039  $fields = $this->fields->getChangedValues();
1040  if (!empty($fields) && !array_key_exists("UPDATED_1C", $fields) && $name != 'UPDATED_1C')
1041  {
1042  parent::setField("UPDATED_1C", "N");
1043  }
1044  }
1045 
1046  return $result;
1047  }
1048 
1049  /**
1050  * @param string $name
1051  * @param float|int|mixed|string $oldValue
1052  * @param float|int|mixed|string $value
1053  * @return Result
1054  * @throws Main\ArgumentException
1055  * @throws Main\ArgumentNullException
1056  * @throws Main\ArgumentOutOfRangeException
1057  * @throws Main\NotImplementedException
1058  * @throws Main\NotSupportedException
1059  * @throws Main\ObjectNotFoundException
1060  */
1061  protected function onFieldModify($name, $oldValue, $value)
1062  {
1063  $result = parent::onFieldModify($name, $oldValue, $value);
1064 
1065  if ($name == "PRICE")
1066  {
1067  /** @var ShipmentCollection $shipmentCollection */
1069  {
1070  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1071  }
1072 
1073  $r = $shipmentCollection->onOrderModify($name, $oldValue, $value);
1074  if (!$r->isSuccess())
1075  {
1076  $result->addErrors($r->getErrors());
1077  return $result;
1078  }
1079 
1080  /** @var PaymentCollection $paymentCollection */
1081  if (!$paymentCollection = $this->getPaymentCollection())
1082  {
1083  throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
1084  }
1085 
1086  $r = $paymentCollection->onOrderModify($name, $oldValue, $value);
1087  if (!$r->isSuccess())
1088  {
1089  $result->addErrors($r->getErrors());
1090  return $result;
1091  }
1092 
1093  /** @var Result $r */
1094  $r = $this->syncOrderAndPayments();
1095  if (!$r->isSuccess())
1096  {
1097  $result->addErrors($r->getErrors());
1098  }
1099  }
1100  elseif($name == "MARKED")
1101  {
1102  /** @var ShipmentCollection $shipmentCollection */
1104  {
1105  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1106  }
1107 
1108  $r = $shipmentCollection->onOrderModify($name, $oldValue, $value);
1109  if (!$r->isSuccess())
1110  {
1111  $result->addErrors($r->getErrors());
1112  return $result;
1113  }
1114  }
1115 
1116  return $result;
1117  }
1118 
1119  /**
1120  * @param $name
1121  * @param $oldValue
1122  * @param $value
1123  * @return Result
1124  * @throws Main\NotSupportedException
1125  * @throws Main\ObjectNotFoundException
1126  */
1127  protected function onOrderModify($name, $oldValue, $value)
1128  {
1129  $result = new Result();
1130 
1131  /** @var PaymentCollection $paymentCollection */
1132  if (!$paymentCollection = $this->getPaymentCollection())
1133  {
1134  throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
1135  }
1136 
1137  $r = $paymentCollection->onOrderModify($name, $oldValue, $value);
1138  if (!$r->isSuccess())
1139  {
1140  $result->addErrors($r->getErrors());
1141  return $result;
1142  }
1143 
1144  /** @var ShipmentCollection $shipmentCollection */
1146  {
1147  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1148  }
1149 
1150  $r = $shipmentCollection->onOrderModify($name, $oldValue, $value);
1151  if (!$r->isSuccess())
1152  {
1153  $result->addErrors($r->getErrors());
1154  return $result;
1155  }
1156 
1157  return $result;
1158  }
1159 
1160  /**
1161  * @internal
1162  *
1163  * @param BasketItem $basketItem
1164  * @return Result
1165  * @throws Main\ArgumentException
1166  * @throws Main\ArgumentNullException
1167  * @throws Main\ArgumentOutOfRangeException
1168  * @throws Main\NotSupportedException
1169  * @throws Main\ObjectNotFoundException
1170  * @throws Main\SystemException
1171  */
1172  public function onBeforeBasketItemDelete(BasketItem $basketItem)
1173  {
1174  $result = new Result();
1175 
1176  /** @var ShipmentCollection $shipmentCollection */
1178  {
1179  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1180  }
1181 
1182  $r = $shipmentCollection->onBeforeBasketItemDelete($basketItem);
1183  if (!$r->isSuccess())
1184  {
1185  $result->addErrors($r->getErrors());
1186  return $result;
1187  }
1188 
1189 
1190  return $result;
1191  }
1192 
1193  /**
1194  * Modify basket.
1195  *
1196  * @param string $action
1197  * @param BasketItemBase $basketItem
1198  * @param null $name
1199  * @param null $oldValue
1200  * @param null $value
1201  * @return Result
1202  * @throws Main\ArgumentException
1203  * @throws Main\ArgumentNullException
1204  * @throws Main\ArgumentOutOfRangeException
1205  * @throws Main\NotImplementedException
1206  * @throws Main\NotSupportedException
1207  * @throws Main\ObjectNotFoundException
1208  * @throws Main\SystemException
1209  */
1210  public function onBasketModify($action, BasketItemBase $basketItem, $name = null, $oldValue = null, $value = null)
1211  {
1212  $result = new Result();
1213 
1214  if ($action === EventActions::DELETE)
1215  {
1216  $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1217  if (!$r->isSuccess())
1218  {
1219  $result->addErrors($r->getErrors());
1220  return $result;
1221  }
1222 
1223  return $result;
1224  }
1225  elseif ($action === EventActions::ADD)
1226  {
1227  if ($basketItem->getField('ORDER_ID'))
1228  {
1229  return $result;
1230  }
1231 
1232  return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1233  }
1234  elseif ($action !== EventActions::UPDATE)
1235  {
1236  return $result;
1237  }
1238 
1239  if ($name === "QUANTITY")
1240  {
1241  $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1242  if (!$r->isSuccess())
1243  {
1244  $result->addErrors($r->getErrors());
1245  return $result;
1246  }
1247 
1249 
1250  $r = $shipmentCollection->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1251  if (!$r->isSuccess())
1252  {
1253  $result->addErrors($r->getErrors());
1254  return $result;
1255  }
1256  elseif ($r->hasWarnings())
1257  {
1258  $result->addWarnings($r->getWarnings());
1259  }
1260 
1261  return $result;
1262  }
1263  elseif ($name === "PRICE")
1264  {
1265  $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1266  if (!$r->isSuccess())
1267  {
1268  $result->addErrors($r->getErrors());
1269  return $result;
1270  }
1271 
1272  if ($this->getId() === 0 && !$this->isMathActionOnly())
1273  {
1274  return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1275  }
1276  }
1277  elseif ($name === 'WEIGHT')
1278  {
1279  return $this->getShipmentCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1280  }
1281  else
1282  {
1283  $r = parent::onBasketModify($action, $basketItem, $name, $oldValue, $value);
1284  if (!$r->isSuccess())
1285  {
1286  $result->addErrors($r->getErrors());
1287  return $result;
1288  }
1289  }
1290 
1291  return $result;
1292  }
1293 
1294  /**
1295  * @return Result
1296  */
1297  public function onBeforeBasketRefresh()
1298  {
1299  $result = new Result();
1300 
1302  if ($shipmentCollection)
1303  {
1304  $r = $shipmentCollection->tryUnreserve();
1305  if(!$r->isSuccess())
1306  {
1307  $result->addErrors($r->getErrors());
1308  }
1309  }
1310 
1311  return $result;
1312  }
1313 
1314  /**
1315  * @return Result
1316  */
1317  public function onAfterBasketRefresh()
1318  {
1319  $result = new Result();
1320 
1322  if ($shipmentCollection)
1323  {
1324  /** @var \Bitrix\Sale\Shipment $shipment */
1325  foreach ($shipmentCollection as $shipment)
1326  {
1327  if ($shipment->isShipped() || !$shipment->needReservation())
1328  continue;
1329 
1330  $r = $shipment->tryReserve();
1331  if(!$r->isSuccess())
1332  {
1333  $result->addErrors($r->getErrors());
1334  }
1335  }
1336  }
1337 
1338  return $result;
1339  }
1340 
1341  /**
1342  * Sync.
1343  *
1344  * @param Payment|null $payment
1345  * @return Result
1346  * @throws Main\ArgumentException
1347  * @throws Main\ArgumentNullException
1348  * @throws Main\ArgumentOutOfRangeException
1349  * @throws Main\NotImplementedException
1350  * @throws Main\NotSupportedException
1351  * @throws Main\ObjectNotFoundException
1352  */
1353  private function syncOrderAndPayments(Payment $payment = null)
1354  {
1355  $result = new Result();
1356 
1357  $oldPaid = $this->getField('PAYED');
1358  $paymentCollection = $this->getPaymentCollection();
1359  $sumPaid = $paymentCollection->getPaidSum();
1360 
1361  if ($payment)
1362  {
1363  $finalSumPaid = $sumPaid;
1364 
1365  if ($payment->isPaid())
1366  {
1367  if ($sumPaid > $this->getPrice())
1368  {
1369  $finalSumPaid = $this->getSumPaid() + $payment->getSum();
1370  }
1371  }
1372  else
1373  {
1374  $r = $this->syncOrderPaymentPaid($payment);
1375  if ($r->isSuccess())
1376  {
1377  $paidResult = $r->getData();
1378  if (isset($paidResult['SUM_PAID']))
1379  {
1380  $finalSumPaid = $paidResult['SUM_PAID'];
1381  }
1382  }
1383  else
1384  {
1385  return $result->addErrors($r->getErrors());
1386  }
1387  }
1388  }
1389  else
1390  {
1391  $finalSumPaid = $this->getSumPaid();
1392 
1393  $r = $this->syncOrderPaid();
1394  if (!$r->isSuccess())
1395  {
1396  return $result->addErrors($r->getErrors());
1397  }
1398 
1399  $paidResult = $r->getData();
1400  if (isset($paidResult['SUM_PAID']))
1401  {
1402  $finalSumPaid = $paidResult['SUM_PAID'];
1403  }
1404  }
1405 
1406  $paid = false;
1407 
1408  if ($finalSumPaid >= 0 && $paymentCollection->hasPaidPayment()
1409  && PriceMaths::roundPrecision($this->getPrice()) <= PriceMaths::roundPrecision($finalSumPaid))
1410  {
1411  $paid = true;
1412  }
1413 
1414  $this->setFieldNoDemand('PAYED', $paid ? "Y" : "N");
1415 
1416  if ($this->getFields()->isChanged('PAYED'))
1417  {
1419  $this->getInternalId(),
1421  [
1422  'ENTITY' => $this,
1423  ]
1424  );
1425 
1427  $this->getInternalId(),
1429  [
1430  'ENTITY' => $this,
1431  ]
1432  );
1433  }
1434 
1435  if ($finalSumPaid > 0 && $finalSumPaid > $this->getPrice())
1436  {
1437  if (!$payment || $payment->isPaid())
1438  {
1439  Internals\UserBudgetPool::addPoolItem($this, $finalSumPaid - $this->getPrice(), Internals\UserBudgetPool::BUDGET_TYPE_EXCESS_SUM_PAID, $payment);
1440  }
1441 
1442  $finalSumPaid = $this->getPrice();
1443  }
1444 
1445  $this->setFieldNoDemand('SUM_PAID', $finalSumPaid);
1446 
1447  $r = $this->onAfterSyncPaid($oldPaid);
1448  if (!$r->isSuccess())
1449  {
1450  $result->addErrors($r->getErrors());
1451  }
1452  elseif ($r->hasWarnings())
1453  {
1454  $result->addWarnings($r->getWarnings());
1455  }
1456 
1457  return $result;
1458  }
1459 
1460  /**
1461  * @param Payment $payment
1462  * @return Result
1463  */
1464  private function syncOrderPaymentPaid(Payment $payment)
1465  {
1466  $result = new Result();
1467 
1468  if ($payment->isPaid())
1469  return $result;
1470 
1472  $sumPaid = $paymentCollection->getPaidSum();
1473 
1475 
1476  $debitSum = $payment->getSum();
1477 
1478  $maxPaid = $payment->getSum() + $sumPaid - $this->getSumPaid();
1479 
1480  if ($maxPaid >= $payment->getSum())
1481  {
1482  $finalSumPaid = $this->getSumPaid();
1483  }
1484  else
1485  {
1486  $debitSum = $maxPaid;
1487  $finalSumPaid = $sumPaid;
1488  }
1489 
1490  if ($debitSum > 0 && $payment->isInner())
1491  {
1492  if (PriceMaths::roundPrecision($debitSum) > PriceMaths::roundPrecision($userBudget))
1493  {
1494  $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_PAYMENT_CANCELLED_PAID'), 'SALE_ORDER_PAYMENT_NOT_ENOUGH_USER_BUDGET_SYNCPAID') );
1495  return $result;
1496  }
1497 
1498  Internals\UserBudgetPool::addPoolItem($this, ($debitSum * -1), Internals\UserBudgetPool::BUDGET_TYPE_ORDER_CANCEL_PART, $payment);
1499  }
1500 
1501  $result->setData(array('SUM_PAID' => $finalSumPaid));
1502 
1503  return $result;
1504  }
1505 
1506  /**
1507  * @return Result
1508  */
1509  private function syncOrderPaid()
1510  {
1511  $result = new Result();
1512 
1513  if ($this->getSumPaid() == $this->getPrice())
1514  return $result;
1515 
1516  $debitSum = $this->getPrice() - $this->getSumPaid();
1517 
1519  $sumPaid = $paymentCollection->getPaidSum();
1521 
1522  $bePaid = $sumPaid - $this->getSumPaid();
1523 
1524  if ($bePaid > 0)
1525  {
1526  if ($debitSum > $bePaid)
1527  {
1528  $debitSum = $bePaid;
1529  }
1530 
1531  if ($debitSum >= $userBudget)
1532  {
1533  $debitSum = $userBudget;
1534  }
1535 
1536  if ($userBudget >= $debitSum && $debitSum > 0)
1537  {
1539 
1540  $finalSumPaid = $this->getSumPaid() + $debitSum;
1541  $result->setData(['SUM_PAID' => $finalSumPaid]);
1542  }
1543  }
1544 
1545  return $result;
1546  }
1547 
1548  /**
1549  * @return mixed
1550  * @throws Main\ArgumentException
1551  */
1552  protected function getStatusOnPaid()
1553  {
1554  $registry = Registry::getInstance(static::getRegistryType());
1555 
1556  $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
1557  return $orderStatus = $optionClassName::get('sale', 'status_on_paid', '');
1558  }
1559 
1560  /**
1561  * @return mixed
1562  * @throws Main\ArgumentException
1563  * @throws Main\SystemException
1564  */
1565  protected function getStatusOnPartialPaid()
1566  {
1567  $registry = Registry::getInstance(static::getRegistryType());
1568 
1569  $optionClassName = $registry->get(Registry::ENTITY_OPTIONS);
1570  return $orderStatus = $optionClassName::get('sale', 'status_on_half_paid', '');
1571  }
1572 
1573  /**
1574  * @param null $oldPaid
1575  * @return Result
1576  * @throws Main\ArgumentException
1577  * @throws Main\ArgumentNullException
1578  * @throws Main\NotImplementedException
1579  * @throws Main\NotSupportedException
1580  * @throws Main\ObjectNotFoundException
1581  */
1582  private function onAfterSyncPaid($oldPaid = null)
1583  {
1584  $result = new Result();
1585  /** @var PaymentCollection $paymentCollection */
1586  if (!$paymentCollection = $this->getPaymentCollection())
1587  {
1588  throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
1589  }
1590 
1591  /** @var ShipmentCollection $shipmentCollection */
1592  if (!$shipmentCollection = $this->getShipmentCollection())
1593  {
1594  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1595  }
1596 
1597  $orderStatus = null;
1598 
1599  $allowSetStatus = false;
1600 
1601  if ($oldPaid == "N")
1602  {
1603  if ($this->isPaid())
1604  {
1605  $orderStatus = $this->getStatusOnPaid();
1606  }
1607  elseif ($paymentCollection->hasPaidPayment())
1608  {
1609  $orderStatus = $this->getStatusOnPartialPaid();
1610  }
1611 
1612  $allowSetStatus = ($this->getField('STATUS_ID') != static::getFinalStatus());
1613  }
1614 
1615  if ($orderStatus !== null && $allowSetStatus)
1616  {
1617  if (strval($orderStatus) != '')
1618  {
1619  $r = $this->setField('STATUS_ID', $orderStatus);
1620  if (!$r->isSuccess())
1621  {
1622  $result->addErrors($r->getErrors());
1623  }
1624  elseif ($r->hasWarnings())
1625  {
1626  $result->addWarnings($r->getWarnings());
1627 
1628  $registry = Registry::getInstance(static::getRegistryType());
1629 
1630  /** @var EntityMarker $entityMarker */
1631  $entityMarker = $registry->getEntityMarkerClassName();
1632  $entityMarker::addMarker($this, $this, $r);
1633  $this->setField('MARKED', 'Y');
1634  }
1635  }
1636 
1637  }
1638 
1640  {
1641  if ($paymentCollection->hasPaidPayment())
1642  {
1643  $r = $shipmentCollection->tryReserve();
1644  if (!$r->isSuccess())
1645  {
1646  $result->addErrors($r->getErrors());
1647  }
1648  elseif ($r->hasWarnings())
1649  {
1650  $result->addWarnings($r->getWarnings());
1651  }
1652  }
1653  else
1654  {
1655  $r = $shipmentCollection->tryUnreserve();
1656  if (!$r->isSuccess())
1657  {
1658  $result->addErrors($r->getErrors());
1659  }
1660  elseif ($r->hasWarnings())
1661  {
1662  $result->addWarnings($r->getWarnings());
1663  }
1664  }
1665  }
1667  {
1668  if ($oldPaid == "N" && $this->isPaid())
1669  {
1670  $r = $shipmentCollection->tryReserve();
1671  if (!$r->isSuccess())
1672  {
1673  $result->addErrors($r->getErrors());
1674  }
1675  elseif ($r->hasWarnings())
1676  {
1677  $result->addWarnings($r->getWarnings());
1678  }
1679  }
1680  elseif ($oldPaid == "Y" && !$this->isPaid())
1681  {
1682  $r = $shipmentCollection->tryUnreserve();
1683  if (!$r->isSuccess())
1684  {
1685  $result->addErrors($r->getErrors());
1686  }
1687  elseif ($r->hasWarnings())
1688  {
1689  $result->addWarnings($r->getWarnings());
1690  }
1691  }
1692  }
1693 
1694  $allowDelivery = null;
1695 
1697  {
1698  if ($oldPaid == "N" && $paymentCollection->hasPaidPayment())
1699  {
1700  $allowDelivery = true;
1701  }
1702  elseif ($oldPaid == "Y" && !$paymentCollection->hasPaidPayment())
1703  {
1704  $allowDelivery = false;
1705  }
1706  }
1708  {
1709  if ($oldPaid == "N" && $this->isPaid())
1710  {
1711  $allowDelivery = true;
1712  }
1713  elseif ($oldPaid == "Y" && !$this->isPaid())
1714  {
1715  $allowDelivery = false;
1716  }
1717  }
1718 
1719  if ($allowDelivery !== null)
1720  {
1721  if ($allowDelivery)
1722  {
1723  $r = $shipmentCollection->allowDelivery();
1724  if (!$r->isSuccess())
1725  {
1726  $result->addErrors($r->getErrors());
1727  }
1728  }
1729  elseif (!$allowDelivery)
1730  {
1731  $r = $shipmentCollection->disallowDelivery();
1732  if (!$r->isSuccess())
1733  {
1734  $result->addErrors($r->getErrors());
1735  }
1736  }
1737  }
1738 
1739  return $result;
1740  }
1741 
1742  /**
1743  * @param $select
1744  * @return Result
1745  * @throws Main\ArgumentException
1746  * @throws Main\ArgumentOutOfRangeException
1747  * @throws Main\NotSupportedException
1748  * @throws Main\ObjectNotFoundException
1749  */
1750  protected function refreshInternal()
1751  {
1752  $result = parent::refreshInternal();
1753  if (!$result->isSuccess())
1754  {
1755  return $result;
1756  }
1757 
1758  /** @var ShipmentCollection $shipmentCollection */
1760  {
1761  throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
1762  }
1763 
1764  return $shipmentCollection->refreshData();
1765  }
1766 
1767  /**
1768  * @internal
1769  *
1770  * @param array $data
1771  * @return Result
1772  * @throws Main\ArgumentNullException
1773  * @throws Main\ArgumentOutOfRangeException
1774  * @throws Main\NotSupportedException
1775  */
1776  public function applyDiscount(array $data)
1777  {
1778  $r = parent::applyDiscount($data);
1779  if (!$r->isSuccess())
1780  {
1781  return $r;
1782  }
1783 
1784  if (isset($data['SHIPMENT']) && intval($data['SHIPMENT']) > 0
1785  && (isset($data['PRICE_DELIVERY']) && floatval($data['PRICE_DELIVERY']) >= 0
1786  || isset($data['DISCOUNT_PRICE']) && floatval($data['DISCOUNT_PRICE']) >= 0))
1787  {
1788  /** @var ShipmentCollection $shipmentCollection */
1790  {
1791  /** @var Shipment $shipment */
1792  if ($shipment = $shipmentCollection->getItemByShipmentCode($data['SHIPMENT']))
1793  {
1794  if (!$shipment->isCustomPrice())
1795  {
1796  $data['PRICE_DELIVERY'] = (float)$data['PRICE_DELIVERY'];
1797  $data['DISCOUNT_PRICE'] = (float)$data['DISCOUNT_PRICE'];
1798  if ($data['PRICE_DELIVERY'] >= 0 && $data['PRICE_DELIVERY'] != $shipment->getPrice())
1799  {
1800  $data['PRICE_DELIVERY'] = PriceMaths::roundPrecision(floatval($data['PRICE_DELIVERY']));
1801  $shipment->setField('PRICE_DELIVERY', $data['PRICE_DELIVERY']);
1802  }
1803 
1804  if ($data['DISCOUNT_PRICE'] != $shipment->getField('DISCOUNT_PRICE'))
1805  {
1806  $data['DISCOUNT_PRICE'] = PriceMaths::roundPrecision(floatval($data['DISCOUNT_PRICE']));
1807  $shipment->setField('DISCOUNT_PRICE', $data['DISCOUNT_PRICE']);
1808  }
1809  }
1810 
1811  }
1812  }
1813  }
1814 
1815  return new Result();
1816  }
1817 
1818  /**
1819  * Lock order.
1820  *
1821  * @param int $id Order id.
1822  * @return Entity\UpdateResult|Result
1823  * @throws \Exception
1824  */
1825  public static function lock($id)
1826  {
1827  global $USER;
1828 
1829  $result = new Result();
1830  $id = (int)$id;
1831  if ($id <= 0)
1832  {
1833  $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1834  return $result;
1835  }
1836 
1837  return static::updateInternal($id, array(
1838  'DATE_LOCK' => new Main\Type\DateTime(),
1839  'LOCKED_BY' => (is_object($USER) ? $USER->GetID(): false)
1840  ));
1841  }
1842 
1843  /**
1844  * Unlock order.
1845  *
1846  * @param int $id Order id.
1847  * @return Entity\UpdateResult|Result
1848  * @throws Main\ArgumentNullException
1849  * @throws \Exception
1850  */
1851  public static function unlock($id)
1852  {
1853  global $USER;
1854 
1855  $result = new Result();
1856  $id = (int)$id;
1857  if ($id <= 0)
1858  {
1859  $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_WRONG_ID'), 'SALE_ORDER_WRONG_ID') );
1860  return $result;
1861  }
1862 
1863  if(!$order = static::load($id))
1864  {
1865  $result->addError( new ResultError(Loc::getMessage('SALE_ORDER_ENTITY_NOT_FOUND'), 'SALE_ORDER_ENTITY_NOT_FOUND') );
1866  return $result;
1867  }
1868 
1869  $userRights = \CMain::getUserRight("sale", $USER->getUserGroupArray(), "Y", "Y");
1870 
1871  if (($userRights >= "W") || ($order->getField("LOCKED_BY") == $USER->getID()))
1872  {
1873  return static::updateInternal($id, array(
1874  'DATE_LOCK' => null,
1875  'LOCKED_BY' => null
1876  ));
1877  }
1878 
1879  return $result;
1880  }
1881 
1882  /**
1883  * Return TRUE if order is locked.
1884  *
1885  * @param int $id
1886  * @return bool
1887  */
1888  public static function isLocked($id)
1889  {
1890  /** @var Result $r */
1891  $r = static::getLockedStatus($id);
1892  if ($r->isSuccess())
1893  {
1894  $lockResultData = $r->getData();
1895 
1896  if (array_key_exists('LOCK_STATUS', $lockResultData)
1897  && $lockResultData['LOCK_STATUS'] == static::SALE_ORDER_LOCK_STATUS_RED)
1898  {
1899  return true;
1900  }
1901  }
1902 
1903  return false;
1904  }
1905 
1906  /**
1907  * Return order locked status.
1908  *
1909  * @param int $id Order id.
1910  * @return Result
1911  * @throws Main\ArgumentException
1912  */
1913  public static function getLockedStatus($id)
1914  {
1915  $result = new Result();
1916 
1917  $res = static::getList(array(
1918  'filter' => array('=ID' => $id),
1919  'select' => array(
1920  'LOCKED_BY',
1921  'LOCK_STATUS',
1922  'DATE_LOCK'
1923  )
1924  ));
1925 
1926  if ($data = $res->fetch())
1927  {
1928  $result->addData(array(
1929  'LOCKED_BY' => $data['LOCKED_BY'],
1930  'LOCK_STATUS' => $data['LOCK_STATUS'],
1931  'DATE_LOCK' => $data['DATE_LOCK'],
1932  ));
1933  }
1934 
1935  return $result;
1936  }
1937 
1938  /**
1939  * @return Result
1940  * @throws Main\ArgumentException
1941  * @throws Main\ArgumentNullException
1942  * @throws Main\ArgumentOutOfRangeException
1943  * @throws Main\NotSupportedException
1944  * @throws Main\ObjectNotFoundException
1945  */
1946  public function verify()
1947  {
1948  $result = parent::verify();
1949 
1950  /** @var PaymentCollection $paymentCollection */
1952  {
1953  $r = $paymentCollection->verify();
1954  if (!$r->isSuccess())
1955  {
1956  $result->addErrors($r->getErrors());
1957  }
1958  }
1959 
1960  /** @var ShipmentCollection $shipmentCollection */
1962  {
1963  $r = $shipmentCollection->verify();
1964  if (!$r->isSuccess())
1965  {
1966  $result->addErrors($r->getErrors());
1967  }
1968  }
1969 
1970  return $result;
1971  }
1972 
1973  /**
1974  * @param $mapping
1975  * @return Order|null|string
1976  */
1977  public function getBusinessValueProviderInstance($mapping)
1978  {
1979  $providerInstance = null;
1980 
1981  if (is_array($mapping))
1982  {
1983  switch ($mapping['PROVIDER_KEY'])
1984  {
1985  case 'ORDER':
1986  case 'PROPERTY':
1987  $providerInstance = $this;
1988  break;
1989  case 'USER':
1990  $providerInstance = $this->getField('USER_ID');
1991  break;
1992  case 'COMPANY':
1993  $providerInstance = $this->getField('COMPANY_ID');
1994  break;
1995  }
1996  }
1997 
1998  return $providerInstance;
1999  }
2000 
2001  /**
2002  * @param array $parameters
2003  *
2004  * @return Main\DB\Result
2005  * @throws Main\ArgumentException
2006  */
2007  public static function getList(array $parameters = array())
2008  {
2009  return Internals\OrderTable::getList($parameters);
2010  }
2011 
2012  /**
2013  * @param \SplObjectStorage $cloneEntity
2014  * @throws Main\ArgumentException
2015  * @throws Main\NotImplementedException
2016  * @throws Main\SystemException
2017  */
2018  protected function cloneEntities(\SplObjectStorage $cloneEntity)
2019  {
2020  /** @var Order $orderClone */
2021  parent::cloneEntities($cloneEntity);
2022 
2023  $orderClone = $cloneEntity[$this];
2024 
2025  /** @var ShipmentCollection $shipmentCollection */
2027  {
2028  $orderClone->shipmentCollection = $shipmentCollection->createClone($cloneEntity);
2029  }
2030 
2031  /** @var PaymentCollection $paymentCollection */
2033  {
2034  $orderClone->paymentCollection = $paymentCollection->createClone($cloneEntity);
2035  }
2036 
2037  /** @var TradeBindingCollection $tradeBindingCollection */
2039  {
2040  $orderClone->tradeBindingCollection = $tradeBindingCollection->createClone($cloneEntity);
2041  }
2042  }
2043 
2044  /**
2045  * @return bool
2046  */
2047  public function isChanged()
2048  {
2049  if (parent::isChanged())
2050  return true;
2051 
2052  /** @var PaymentCollection $paymentCollection */
2054  {
2055  if ($paymentCollection->isChanged())
2056  {
2057  return true;
2058  }
2059  }
2060 
2061  /** @var ShipmentCollection $shipmentCollection */
2063  {
2064  if ($shipmentCollection->isChanged())
2065  {
2066  return true;
2067  }
2068  }
2069 
2070  return false;
2071  }
2072 
2073  /**
2074  * @internal
2075  * @return void
2076  */
2077  public function clearChanged()
2078  {
2079  parent::clearChanged();
2080 
2082  {
2083  $paymentCollection->clearChanged();
2084  }
2085 
2087  {
2088  $shipmentCollection->clearChanged();
2089  }
2090 
2091  if ($tradeCollection = $this->getTradeBindingCollection())
2092  {
2093  $tradeCollection->clearChanged();
2094  }
2095  }
2096 
2097  /**
2098  * @return array
2099  * @throws Main\ArgumentException
2100  * @throws Main\ArgumentNullException
2101  */
2102  public function getDeliveryIdList()
2103  {
2104  $result = array();
2105 
2106  /** @var ShipmentCollection $collection */
2107  $collection = $this->getShipmentCollection();
2108 
2109  /** @var Shipment $shipment */
2110  foreach ($collection->getNotSystemItems() as $shipment)
2111  {
2112  if ($shipment->getDeliveryId() > 0)
2113  {
2114  $result[] = $shipment->getDeliveryId();
2115  }
2116  }
2117 
2118  return $result;
2119  }
2120 
2121  /**
2122  * @return array
2123  * @throws Main\ObjectNotFoundException
2124  */
2125  public function getPaySystemIdList()
2126  {
2127  $result = array();
2128  /** @var PaymentCollection $paymentCollection */
2129  if (!$paymentCollection = $this->getPaymentCollection())
2130  {
2131  throw new Main\ObjectNotFoundException('Entity "PaymentCollection" not found');
2132  }
2133 
2134  /** @var Payment $payment */
2135  foreach ($paymentCollection as $payment)
2136  {
2137  if ($payment->getPaymentSystemId() > 0)
2138  {
2139  $result[] = $payment->getPaymentSystemId();
2140  }
2141  }
2142 
2143  return $result;
2144  }
2145 
2146  /**
2147  * @return array
2148  */
2149  protected function calculateVat()
2150  {
2151  $vatInfo = parent::calculateVat();
2152 
2154  if ($shipmentCollection)
2155  {
2156  /** @var Shipment $shipment */
2157  foreach ($shipmentCollection as $shipment)
2158  {
2159  $rate = $shipment->getVatRate();
2160  if ($rate)
2161  {
2162  $vatInfo['VAT_SUM'] += $shipment->getVatSum();
2163  $vatInfo['VAT_RATE'] = max($vatInfo['VAT_RATE'], $rate);
2164  }
2165  }
2166  }
2167 
2168  return $vatInfo;
2169  }
2170 
2171  /**
2172  * @return Result
2173  */
2174  protected function saveEntities()
2175  {
2176  $result = parent::saveEntities();
2177 
2178  /** @var PaymentCollection $paymentCollection */
2180 
2181  /** @var Result $r */
2182  $r = $paymentCollection->save();
2183  if (!$r->isSuccess())
2184  {
2185  $result->addWarnings($r->getErrors());
2186  }
2187 
2188  // user budget
2190 
2191  /** @var ShipmentCollection $shipmentCollection */
2193 
2194  /** @var Result $r */
2195  $r = $shipmentCollection->save();
2196  if (!$r->isSuccess())
2197  {
2198  $result->addWarnings($r->getErrors());
2199  }
2200 
2201  /** @var TradeBindingCollection $tradeBindingCollection */
2203 
2204  /** @var Result $r */
2205  $r = $tradeBindingCollection->save();
2206  if (!$r->isSuccess())
2207  {
2208  $result->addWarnings($r->getErrors());
2209  }
2210 
2212  if (!$res->isSuccess())
2213  {
2214  $result->addWarnings($res->getErrors());
2215 
2216  $warningResult = new Result();
2217  $warningResult->addWarnings($res->getErrors());
2218 
2219  $registry = Registry::getInstance(static::getRegistryType());
2220  /** @var EntityMarker $entityMarker */
2221  $entityMarker = $registry->getEntityMarkerClassName();
2222  $entityMarker::addMarker($this, $this, $warningResult);
2223  static::updateInternal($this->getId(), array('MARKED' => 'Y'));
2224  }
2225 
2226  return $result;
2227  }
2228 
2229  /**
2230  * @return float
2231  */
2232  protected function calculatePrice()
2233  {
2234  $price = parent::calculatePrice();
2236 
2237  return $price + $shipmentCollection->getPriceDelivery();
2238  }
2239 
2240  /**
2241  * @return Result
2242  * @throws Main\ArgumentException
2243  * @throws Main\ArgumentNullException
2244  * @throws Main\SystemException
2245  * @throws \Exception
2246  */
2247  protected function onBeforeSave()
2248  {
2249  $registry = Registry::getInstance(static::getRegistryType());
2250 
2251  /** @var EntityMarker $entityMarker */
2252  $entityMarker = $registry->getEntityMarkerClassName();
2253 
2254  /** @var Result $result */
2255  $result = Internals\Catalog\Provider::save($this);
2256  if ($result->hasWarnings())
2257  {
2258  $entityMarker::addMarker($this, $this, $result);
2259  if ($this->getId() > 0)
2260  {
2261  static::updateInternal($this->getId(), ['MARKED' => 'Y']);
2262  }
2263  }
2264 
2265  $entityMarker::refreshMarkers($this);
2266 
2267  if (!$result->isSuccess())
2268  {
2269  $resultPool = $entityMarker::getPoolAsResult($this);
2270  if (!$resultPool->isSuccess())
2271  {
2272  foreach ($resultPool->getErrors() as $errorPool)
2273  {
2274  foreach ($result->getErrors() as $error)
2275  {
2276  if ($errorPool->getCode() == $error->getCode() && $errorPool->getMessage() == $error->getMessage())
2277  {
2278  continue 2;
2279  }
2280  }
2281 
2282  $result->addError($errorPool);
2283  }
2284  }
2285 
2286  $entityMarker::saveMarkers($this);
2287  }
2288 
2289  return $result;
2290  }
2291 
2292  /**
2293  * @return Result
2294  */
2295  protected function onAfterSave()
2296  {
2297  $result = parent::onAfterSave();
2298  if (!$result->isSuccess())
2299  {
2300  return $result;
2301  }
2302 
2303  global $CACHE_MANAGER;
2304 
2305  if (defined("CACHED_b_sale_order")
2306  && (
2307  $this->isNew
2308  || (
2309  $this->isChanged()
2310  && $this->getField("UPDATED_1C") != "Y"
2311  )
2312  )
2313  )
2314  {
2315  $CACHE_MANAGER->Read(CACHED_b_sale_order, "sale_orders");
2316  $CACHE_MANAGER->SetImmediate("sale_orders", true);
2317  }
2318 
2319  return new Result();
2320  }
2321 
2322  /**
2323  * @return Result
2324  * @throws Main\ArgumentException
2325  * @throws Main\ArgumentNullException
2326  * @throws Main\ArgumentOutOfRangeException
2327  * @throws \Exception
2328  */
2329  public function save()
2330  {
2331  $result = parent::save();
2332 
2333  $registry = Registry::getInstance(static::getRegistryType());
2334 
2335  /** @var OrderHistory $orderHistory */
2336  $orderHistory = $registry->getOrderHistoryClassName();
2337  $orderHistory::collectEntityFields('ORDER', $this->getId(), $this->getId());
2338 
2339  /** @var EntityMarker $entityMarker */
2340  $entityMarker = $registry->getEntityMarkerClassName();
2341  if ($entityMarker::hasErrors($this))
2342  {
2343  $entityMarker::saveMarkers($this);
2344  static::updateInternal($this->getId(), array("MARKED" => "Y"));
2345  }
2346 
2347  return $result;
2348  }
2349 
2350  /**
2351  * @return Result
2352  * @throws Main\ArgumentException
2353  * @throws Main\ArgumentNullException
2354  * @throws Main\ArgumentOutOfRangeException
2355  * @throws Main\NotImplementedException
2356  * @throws Main\ObjectException
2357  * @throws Main\SystemException
2358  */
2359  protected function add()
2360  {
2361  $result = parent::add();
2362 
2363  $registry = Registry::getInstance(static::getRegistryType());
2364 
2365  /** @var OrderHistory $orderHistory */
2366  $orderHistory = $registry->getOrderHistoryClassName();
2367  $orderHistory::addAction('ORDER', $result->getId(), 'ORDER_ADDED', $result->getId(), $this);
2368 
2369  return $result;
2370  }
2371 
2372  /**
2373  * @return Result
2374  * @throws Main\ArgumentException
2375  */
2376  protected function update()
2377  {
2378  $result = parent::update();
2379 
2380  $registry = Registry::getInstance(static::getRegistryType());
2381  /** @var OrderHistory $orderHistory */
2382  $orderHistory = $registry->getOrderHistoryClassName();
2383 
2384  if (!$result->isSuccess())
2385  {
2386  $orderHistory::addAction(
2387  'ORDER',
2388  $this->getId(),
2389  'ORDER_UPDATE_ERROR',
2390  $this->getId(),
2391  $this,
2392  array("ERROR" => $result->getErrorMessages())
2393  );
2394  }
2395  else
2396  {
2397  $orderHistory::addAction(
2398  'ORDER',
2399  $this->getId(),
2400  'ORDER_UPDATED',
2401  $this->getId(),
2402  $this,
2403  array(),
2405  );
2406  }
2407 
2408  return $result;
2409  }
2410 
2411  /**
2412  * @throws Main\ArgumentException
2413  * @return void
2414  */
2415  protected function callEventOnSaleOrderEntitySaved()
2416  {
2417  parent::callEventOnSaleOrderEntitySaved();
2418 
2419  $changeMeaningfulFields = array(
2420  "PERSON_TYPE_ID",
2421  "CANCELED",
2422  "STATUS_ID",
2423  "MARKED",
2424  "PRICE",
2425  "SUM_PAID",
2426  "USER_ID",
2427  "EXTERNAL_ORDER",
2428  );
2429 
2430  if ($this->isChanged())
2431  {
2432  $logFields = array();
2433 
2434  if (!$this->isNew)
2435  {
2436  $fields = $this->getFields();
2437  $originalValues = $fields->getOriginalValues();
2438 
2439  foreach($originalValues as $originalFieldName => $originalFieldValue)
2440  {
2441  if (in_array($originalFieldName, $changeMeaningfulFields) && $this->getField($originalFieldName) != $originalFieldValue)
2442  {
2443  $logFields[$originalFieldName] = $this->getField($originalFieldName);
2444  $logFields['OLD_'.$originalFieldName] = $originalFieldValue;
2445  }
2446  }
2447 
2448  $registry = Registry::getInstance(static::getRegistryType());
2449 
2450  /** @var OrderHistory $orderHistory */
2451  $orderHistory = $registry->getOrderHistoryClassName();
2452  $orderHistory::addLog(
2453  'ORDER',
2454  $this->getId(),
2455  "ORDER_UPDATE",
2456  $this->getId(),
2457  $this,
2458  $logFields,
2459  $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2460  );
2461  }
2462  }
2463  }
2464 
2465  /**
2466  * @throws Main\ArgumentException
2467  * @return void
2468  */
2469  protected function callEventOnSaleOrderSaved()
2470  {
2471  $registry = Registry::getInstance(static::getRegistryType());
2472 
2473  /** @var OrderHistory $orderHistory */
2474  $orderHistory = $registry->getOrderHistoryClassName();
2475  $orderHistory::addLog(
2476  'ORDER',
2477  $this->getId(),
2478  'ORDER_EVENT_ON_ORDER_SAVED',
2479  null,
2480  null,
2481  array(),
2482  $orderHistory::SALE_ORDER_HISTORY_LOG_LEVEL_1
2483  );
2484 
2485  parent::callEventOnSaleOrderSaved();
2486  }
2487 
2488  /**
2489  * @param array $data
2490  * @return Entity\AddResult
2491  * @throws \Exception
2492  */
2493  protected function addInternal(array $data)
2494  {
2495  return Internals\OrderTable::add($data);
2496  }
2497 
2498  /**
2499  * @param $primary
2500  * @param array $data
2501  * @return Entity\UpdateResult
2502  * @throws \Exception
2503  */
2504  protected static function updateInternal($primary, array $data)
2505  {
2506  return Internals\OrderTable::update($primary, $data);
2507  }
2508 
2509  /**
2510  * @param $primary
2511  * @return Entity\DeleteResult
2512  * @throws \Exception
2513  */
2514  protected static function deleteInternal($primary)
2515  {
2517  }
2518 
2519  /**
2520  * @param $orderId
2521  * @throws Main\ArgumentException
2522  */
2523  protected static function deleteExternalEntities($orderId)
2524  {
2525  parent::deleteExternalEntities($orderId);
2526 
2527  $registry = Registry::getInstance(static::getRegistryType());
2528 
2529  TradingPlatform\OrderTable::deleteByOrderId($orderId);
2531 
2532  /** @var EntityMarker $entityMarker */
2533  $entityMarker = $registry->getEntityMarkerClassName();
2534  $entityMarker::deleteByOrderId($orderId);
2535 
2536  /** @var OrderHistory $orderHistory */
2537  $orderHistory = $registry->getOrderHistoryClassName();
2538  $orderHistory::deleteByOrderId($orderId);
2539  }
2540 
2541  /**
2542  * Save field modify to history.
2543  *
2544  * @param string $name Field name.
2545  * @param null|string $oldValue Old value.
2546  * @param null|string $value New value.
2547  */
2548  protected function addChangesToHistory($name, $oldValue = null, $value = null)
2549  {
2550  if ($this->getId() > 0)
2551  {
2552  $historyFields = array();
2553  if ($name == "PRICE")
2554  {
2555  $historyFields['CURRENCY'] = $this->getCurrency();
2556  }
2557  if ($name == "RESPONSIBLE_ID")
2558  {
2559  $historyFields[$name] = $value;
2560  }
2561 
2562  $historyFields['OLD_'.$name] = $oldValue;
2563 
2564  $registry = Registry::getInstance(static::getRegistryType());
2565 
2566  /** @var OrderHistory $orderHistory */
2567  $orderHistory = $registry->getOrderHistoryClassName();
2568  $orderHistory::addField(
2569  'ORDER',
2570  $this->getId(),
2571  $name,
2572  $oldValue,
2573  $value,
2574  $this->getId(),
2575  $this,
2576  $historyFields
2577  );
2578  }
2579  }
2580 
2581  /**
2582  * @deprecated
2583  *
2584  * @return array
2585  * @throws Main\ObjectNotFoundException
2586  */
2587  public function getDeliverySystemId()
2588  {
2589  return $this->getDeliveryIdList();
2590  }
2591 
2592  /**
2593  * @deprecated
2594  * @return array
2595  * @throws Main\ObjectNotFoundException
2596  */
2597  public function getPaymentSystemId()
2598  {
2599  return $this->getPaySystemIdList();
2600  }
2601 
2602  /**
2603  * @return array
2604  * @throws Main\ArgumentException
2605  * @throws Main\SystemException
2606  */
2607  public function toArray() : array
2608  {
2609  $result = parent::toArray();
2610 
2611  $result['PAYMENTS'] = $this->getPaymentCollection()->toArray();
2612  $result['SHIPMENTS'] = $this->getShipmentCollection()->toArray();
2613  $result['TRADE_BINDINGS'] = $this->getTradeBindingCollection()->toArray();
2614 
2615  return $result;
2616  }
2617 }
Exception is thrown when the type of an argument is not accepted by function.
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
static getMessage($code, $replace=null, $language=null)
Returns translation by message code.
Definition: loc.php:29
Exception is thrown when operation is not implemented but should be.
Exception is thrown when an object is not present.
static getList(array $parameters=array())
static create(array $settings)
static needShipOnAllowDelivery()
Returns true, if current condition - delivery.
static getProductReservationCondition()
Returns current reservation condition.
static addEvent(Order $order, $type, $event)
Definition: eventspool.php:24
static save(Sale\Order $order, array $context=array())
static getMap()
Returns entity map definition.
static getUserBudgetByOrder(Sale\Order $order)
static addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment=null)
isPaid()
Return TRUE, if order is payed.
Definition: orderbase.php:1913
getUserId()
Return user id.
Definition: orderbase.php:815
getPrice()
Return order price.
Definition: orderbase.php:743
isMathActionOnly()
Return TRUE if calculations are based on current values.
Definition: orderbase.php:2049
getCurrency()
Return currency.
Definition: orderbase.php:805
refreshData()
Full order refresh.
Definition: orderbase.php:660
doFinalAction($hasMeaningfulField=false)
Definition: orderbase.php:2417
isStartField($isMeaningfulField=false)
Definition: orderbase.php:2077
isCanceled()
Return TRUE, if order is canceled.
Definition: orderbase.php:1943
getInternalId()
Return internal index of order.
Definition: orderbase.php:94
setFieldNoDemand($name, $value)
Definition: orderbase.php:526
getSumPaid()
Return paid sum.
Definition: orderbase.php:753
setField($name, $value)
Set value with call events on field modify.
Definition: orderbase.php:463
getDiscount()
Return discount object.
Definition: orderbase.php:2346
getField($name)
Return field value.
Definition: orderbase.php:568
const SALE_ORDER_HISTORY_ACTION_LOG_LEVEL_1
onFieldModify($name, $oldValue, $value)
loadShipmentCollection()
Load shipment collection.
static deleteEntitiesNoDemand($orderId)
setBasket(BasketBase $basket)
onOrderModify($name, $oldValue, $value)
applyDiscount(array $data)
getBusinessValueProviderInstance($mapping)
onBasketModify($action, BasketItemBase $basketItem, $name=null, $oldValue=null, $value=null)
Modify basket.
static deleteExternalEntities($orderId)
const SALE_ORDER_LOCK_STATUS_YELLOW
onShipmentCollectionModify($action, Shipment $shipment, $name=null, $oldValue=null, $value=null)
Modify shipment collection.
static getList(array $parameters=array())
static updateInternal($primary, array $data)
const SALE_ORDER_LOCK_STATUS_GREEN
const SALE_ORDER_LOCK_STATUS_RED
getPrintedChecks()
Return printed check list.
onBeforeBasketItemDelete(BasketItem $basketItem)
resetData($select=array('PRICE'))
static isLocked($id)
Return TRUE if order is locked.
onPaymentCollectionModify($action, Payment $payment, $name=null, $oldValue=null, $value=null)
static getLockedStatus($id)
Return order locked status.
addInternal(array $data)
addChangesToHistory($name, $oldValue=null, $value=null)
Save field modify to history.
getShipmentCollection()
Return shipment collection.
addPrintedCheck($check)
Add printed check to order.
static deleteEntities(OrderBase $order)
static deleteInternal($primary)
getPaymentCollection()
Return payment collection.
cloneEntities(\SplObjectStorage $cloneEntity)
static unlock($id)
Unlock order.
appendBasket(BasketBase $basket)
static getRegistryType()
static lock($id)
Lock order.
getTradeBindingCollection()
Return trade binding collection.
loadPaymentCollection()
Load payment collection.
static roundPrecision($value)
Definition: pricemaths.php:17
setField($name, $value)
Sets new value to specified field of shipment item.
Definition: shipment.php:778