Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shipmentitemstore.php
1<?php
2namespace Bitrix\Sale;
3
7
8Loc::loadMessages(__FILE__);
9
15 implements \IEntityMarker
16{
20 public static function getAvailableFields()
21 {
22 return [
23 'ORDER_DELIVERY_BASKET_ID', 'STORE_ID', 'QUANTITY',
24 'BARCODE', 'BASKET_ID', 'MARKING_CODE',
25 ];
26 }
27
31 protected static function getMeaningfulFields()
32 {
33 return [];
34 }
35
41 private static function createShipmentItemStoreObject(array $itemData = array())
42 {
43 $registry = Registry::getInstance(static::getRegistryType());
44 $shipmentItemStoreClassName = $registry->getShipmentItemStoreClassName();
45
46 return new $shipmentItemStoreClassName($itemData);
47 }
48
52 public static function getRegistryType()
53 {
55 }
56
60 public static function getRegistryEntity()
61 {
63 }
64
71 public static function create(ShipmentItemStoreCollection $collection, BasketItem $basketItem)
72 {
73 if (!$basketItem->isReservableItem())
74 {
75 throw new Main\SystemException('Basket item is not available for reservation');
76 }
77
78 $shipmentItemStore = static::createShipmentItemStoreObject();
79 $shipmentItemStore->setCollection($collection);
80
81 $shipmentItem = $collection->getShipmentItem();
82 if ($shipmentItem)
83 {
84 $fields = array(
85 'ORDER_DELIVERY_BASKET_ID' => $collection->getShipmentItem()->getId(),
86 'BASKET_ID' => $shipmentItem->getBasketItem()->getId(),
87 );
88
89 $shipmentItemStore->setFieldsNoDemand($fields);
90 }
91
92 return $shipmentItemStore;
93
94 }
95
104 protected function onFieldModify($name, $oldValue, $value)
105 {
106 global $USER;
107
108 if (is_object($USER) && $USER->isAuthorized())
109 {
110 $this->setFieldNoDemand('MODIFIED_BY', $USER->GetID());
111 }
112
113 $this->setFieldNoDemand('DATE_MODIFY', new Main\Type\DateTime());
114
115 $result = parent::onFieldModify($name, $oldValue, $value);
116 if (!$result->isSuccess())
117 {
118 return $result;
119 }
120
121 if (
122 $name === 'STORE_ID'
123 && $this->needMoveReserve()
124 )
125 {
126 $shipmentItem = $this->getCollection()->getShipmentItem();
127 $basketItem = $shipmentItem->getBasketItem();
128
130 $reserveCollection = $basketItem->getReserveQuantityCollection();
131
132 if (
133 $reserveCollection
134 && $shipmentItem->getReservedQuantity() > 0
135 && $this->getQuantity() > 0
136 )
137 {
138 $storeIdFrom = (int)$oldValue ?: Configuration::getDefaultStoreId();
139 $storeIdTo = (int)$value;
140
141 if (
142 $storeIdFrom === 0
143 || $storeIdTo === 0
144 || $storeIdFrom === $storeIdTo
145 )
146 {
147 return $result;
148 }
149
150 $reserveTo = $reserveFrom = null;
151
153 foreach ($reserveCollection as $reserve)
154 {
155 if ($reserve->getStoreId() === $storeIdFrom)
156 {
157 $reserveFrom = $reserve;
158 }
159 elseif ($reserve->getStoreId() === $storeIdTo)
160 {
161 $reserveTo = $reserve;
162 }
163
164 if ($reserveTo && $reserveFrom)
165 {
166 break;
167 }
168 }
169
170 if ($reserveFrom)
171 {
172 $settableQuantity = $reserveFrom->getQuantity() - $this->getQuantity();
173 if ($settableQuantity > 0)
174 {
175 $reserveFrom->setField('QUANTITY', $settableQuantity);
176 }
177 else
178 {
179 $reserveFrom->delete();
180 }
181 }
182
183 if (!$reserveTo)
184 {
185 $reserveTo = $reserveCollection->create();
186 $reserveTo->setStoreId($storeIdTo);
187 }
188
189 $reserveTo->setQuantity($reserveTo->getQuantity() + $this->getQuantity());
190 }
191 }
192 elseif (
193 $name === 'QUANTITY'
194 && $this->needMoveReserve()
195 )
196 {
197 $shipmentItem = $this->getCollection()->getShipmentItem();
198 $basketItem = $shipmentItem->getBasketItem();
199
201 $reserveCollection = $basketItem->getReserveQuantityCollection();
202
203 if ($reserveCollection && $shipmentItem->getReservedQuantity() > 0)
204 {
205 if ($value > $oldValue)
206 {
207 $storeIdFrom = Configuration::getDefaultStoreId();
208 $storeIdTo = $this->getStoreId();
209 }
210 else
211 {
213 $storeIdFrom = $this->getStoreId();
214 }
215
216 if (
217 $storeIdFrom === 0
218 || $storeIdTo === 0
219 || $storeIdFrom === $storeIdTo
220 )
221 {
222 return $result;
223 }
224
225 $reserveTo = $reserveFrom = null;
226
228 foreach ($reserveCollection as $reserve)
229 {
230 if ($reserve->getStoreId() === $storeIdFrom)
231 {
232 $reserveFrom = $reserve;
233 }
234 elseif ($reserve->getStoreId() === $storeIdTo)
235 {
236 $reserveTo = $reserve;
237 }
238
239 if ($reserveTo && $reserveFrom)
240 {
241 break;
242 }
243 }
244
245 $delta = abs($oldValue - $value);
246
247 if ($reserveFrom)
248 {
249 $settableQuantity = $reserveFrom->getQuantity() - $delta;
250 if ($settableQuantity > 0)
251 {
252 $reserveFrom->setField('QUANTITY', $settableQuantity);
253 }
254 else
255 {
256 $reserveFrom->delete();
257 }
258 }
259
260 if (!$reserveTo)
261 {
262 $reserveTo = $reserveCollection->create();
263 $reserveTo->setStoreId($storeIdTo);
264 }
265
266 $reserveTo->setQuantity($reserveTo->getQuantity() + $delta);
267 }
268 }
269
270 return $result;
271 }
272
273 protected function needMoveReserve() : bool
274 {
275 return true;
276 }
277
284 public function delete()
285 {
286 $result = new Result();
287
288 $oldEntityValues = $this->fields->getOriginalValues();
289
290 $event = new Main\Event('sale', "OnBeforeSaleShipmentItemStoreEntityDeleted", [
291 'ENTITY' => $this,
292 'VALUES' => $oldEntityValues,
293 ]);
294 $event->send();
295
296 foreach ($event->getResults() as $eventResult)
297 {
298 if ($eventResult->getType() == Main\EventResult::ERROR)
299 {
300 $eventResultData = $eventResult->getParameters();
301 if ($eventResultData instanceof ResultError)
302 {
303 return $result->addError($eventResultData);
304 }
305 }
306 }
307
308 $r = parent::delete();
309 if (!$r->isSuccess())
310 {
311 return $result->addErrors($r->getErrors());
312 }
313
314 $shipmentItem = $this->getCollection()->getShipmentItem();
315 $basketItem = $shipmentItem->getBasketItem();
316
318 $reserveCollection = $basketItem->getReserveQuantityCollection();
319
320 if ($reserveCollection && $shipmentItem->getReservedQuantity() > 0)
321 {
323 $storeIdFrom = $this->getStoreId();
324
325 if ($storeIdFrom === $storeIdTo)
326 {
327 return $result;
328 }
329
330 $reserveTo = $reserveFrom = null;
331
333 foreach ($reserveCollection as $reserve)
334 {
335 if ($reserve->getStoreId() === $storeIdFrom)
336 {
337 $reserveFrom = $reserve;
338 }
339 elseif ($reserve->getStoreId() === $storeIdTo)
340 {
341 $reserveTo = $reserve;
342 }
343
344 if ($reserveTo && $reserveFrom)
345 {
346 break;
347 }
348 }
349
350 $delta = $this->getQuantity();
351
352 if ($reserveFrom)
353 {
354 $settableQuantity = $reserveFrom->getQuantity() - $delta;
355 if ($settableQuantity > 0)
356 {
357 $reserveFrom->setField('QUANTITY', $settableQuantity);
358 }
359 else
360 {
361 $reserveFrom->delete();
362 }
363 }
364
365 if (!$reserveTo)
366 {
367 $reserveTo = $reserveCollection->create();
368 $reserveTo->setStoreId($storeIdTo);
369 }
370
371 $reserveTo->setQuantity($reserveTo->getQuantity() + $delta);
372 }
373
374 $event = new Main\Event('sale', "OnSaleShipmentItemStoreEntityDeleted", array(
375 'ENTITY' => $this,
376 'VALUES' => $this->fields->getOriginalValues(),
377 ));
378 $event->send();
379
380 foreach ($event->getResults() as $eventResult)
381 {
382 if ($eventResult->getType() == Main\EventResult::ERROR)
383 {
384 $eventResultData = $eventResult->getParameters();
385 if ($eventResultData instanceof ResultError)
386 {
387 $result->addError($eventResultData);
388 }
389 }
390 }
391
392 return $result;
393 }
394
398 public function getBasketId() : int
399 {
400 return (int)$this->getField('BASKET_ID');
401 }
402
406 public function getQuantity() : float
407 {
408 return (float)$this->getField('QUANTITY');
409 }
410
414 public function getStoreId() : int
415 {
416 return (int)$this->getField('STORE_ID');
417 }
418
422 public function getBarcode() : string
423 {
424 return (string)$this->getField('BARCODE');
425 }
426
430 public function getMarkingCode() : string
431 {
432 return (string)$this->getField('MARKING_CODE');
433 }
434
443 public static function loadForShipmentItem($id)
444 {
445 if ((int)$id <= 0)
446 {
447 throw new Main\ArgumentNullException("id");
448 }
449
450 $items = [];
451
452 $itemDataList = static::getList([
453 'filter' => ['=ORDER_DELIVERY_BASKET_ID' => $id],
454 'order' => ['DATE_CREATE' => 'ASC', 'ID' => 'ASC']
455 ]);
456
457 while ($itemData = $itemDataList->fetch())
458 {
459 $items[] = static::createShipmentItemStoreObject($itemData);
460 }
461
462 return $items;
463 }
464
470 public function verify()
471 {
472 $result = new Result();
473
474 if ($this->getBarcode() === "")
475 {
477 $itemStoreCollection = $this->getCollection();
478
480 $basketItem = $itemStoreCollection->getShipmentItem()->getBasketItem();
481
483 $shipment = $itemStoreCollection->getShipmentItem()->getCollection()->getShipment();
484
485 if ($basketItem->isBarcodeMulti() && $shipment->isShipped())
486 {
487 $result->addError(
488 new ResultError(
490 'SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY',
491 [
492 '#PRODUCT_NAME#' => $basketItem->getField('NAME'),
493 '#STORE_ID#' => $this->getStoreId(),
494 ]
495 ),
496 'SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY'
497 )
498 );
499 }
500 }
501
502 return $result;
503 }
504
508 protected function checkCallingContext()
509 {
511 $itemStoreCollection = $this->getCollection();
512
514 $itemCollection = $itemStoreCollection->getShipmentItem()->getCollection();
515
517 $shipmentCollection = $itemCollection->getShipment()->getCollection();
518
519 if (!$shipmentCollection->getOrder()->isSaveRunning())
520 {
521 trigger_error("Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
522 }
523 }
524
533 public function save()
534 {
535 $result = new Result();
536
537 $this->checkCallingContext();
538
539 if (!$this->isChanged() || $this->getQuantity() === 0)
540 {
541 return $result;
542 }
543
544 $this->callEventOnBeforeItemStoreEntitySaved();
545
546 $id = $this->getId();
547
548 if ($id > 0)
549 {
550 $r = $this->updateInternal($id, $this->getFields()->getChangedValues());
551 }
552 else
553 {
555 $itemStoreCollection = $this->getCollection();
556
557 if (!$this->getField("ORDER_DELIVERY_BASKET_ID"))
558 {
559 $this->setFieldNoDemand('ORDER_DELIVERY_BASKET_ID', $itemStoreCollection->getShipmentItem()->getId());
560 }
561
562 if (!$this->getField("BASKET_ID"))
563 {
564 $this->setFieldNoDemand('BASKET_ID', $itemStoreCollection->getShipmentItem()->getBasketItem()->getId());
565 }
566
567 $this->setFieldNoDemand('DATE_CREATE', new Main\Type\DateTime());
568
569 $r = $this->addInternal($this->getFields()->getValues());
570 if ($r->isSuccess())
571 {
572 $id = $r->getId();
573
574 $this->setFieldNoDemand('ID', $id);
575 }
576 }
577
578 if (!$r->isSuccess())
579 {
580 $this->addErrorMessagesToHistory($r->getErrorMessages());
581
582 $result->addErrors($r->getErrors());
583
584 return $result;
585 }
586
587 if ($id > 0)
588 {
589 $result->setId($id);
590 }
591
592 $this->callEventOnItemStoreEntitySaved();
593
594 return $result;
595 }
596
600 protected function callEventOnBeforeItemStoreEntitySaved()
601 {
603 $event = new Main\Event('sale', 'OnBeforeSaleShipmentItemStoreEntitySaved', [
604 'ENTITY' => $this,
605 'VALUES' => $this->fields->getOriginalValues()
606 ]);
607
608 $event->send();
609 }
610
614 protected function callEventOnItemStoreEntitySaved()
615 {
617 $event = new Main\Event('sale', 'OnSaleShipmentItemStoreEntitySaved', [
618 'ENTITY' => $this,
619 'VALUES' => $this->fields->getOriginalValues(),
620 ]);
621
622 $event->send();
623 }
624
631 protected function addErrorMessagesToHistory($errors)
632 {
633 $registry = Registry::getInstance(static::getRegistryType());
634
636 $shipmentItemStoreCollection = $this->getCollection();
637
639 $shipmentItemCollection = $shipmentItemStoreCollection->getShipmentItem()->getCollection();
640
641 $order = $shipmentItemCollection->getShipment()->getOrder();
642
644 $orderHistory = $registry->getOrderHistoryClassName();
645 $orderHistory::addAction(
646 'SHIPMENT',
647 $order->getId(),
648 ($this->getId() > 0) ? 'SHIPMENT_ITEM_STORE_UPDATE_ERROR' : 'SHIPMENT_ITEM_STORE_ADD_ERROR',
649 ($this->getId() > 0) ? $this->getId() : null,
650 $this,
651 [
652 "ERROR" => $errors
653 ]
654 );
655 }
656
665 protected function addChangesToHistory($name, $oldValue = null, $value = null)
666 {
667 if ($this->getId() <= 0)
668 {
669 return;
670 }
671
673 $shipmentItem = $this->getCollection()->getShipmentItem();
674
676 $shipmentItemCollection = $shipmentItem->getCollection();
677
678 $shipmentItemCollection->getShipment()->getOrder();
679
680 $shipment = $shipmentItemCollection->getShipment();
681 if ($shipment->isSystem())
682 {
683 return;
684 }
685
686 $basketItem = $shipmentItem->getBasketItem();
687
688 $registry = Registry::getInstance(static::getRegistryType());
689
691 $orderHistory = $registry->getOrderHistoryClassName();
692 $orderHistory::addField(
693 'SHIPMENT_ITEM_STORE',
694 $shipment->getOrder()->getId(),
695 $name,
696 $oldValue,
697 $value,
698 $this->getId(),
699 $this,
700 [
701 'NAME' => $basketItem->getField('NAME'),
702 'PRODUCT_ID' => $basketItem->getField('PRODUCT_ID'),
703 ]
704 );
705 }
706
714 public static function getList(array $parameters = array())
715 {
716 return Sale\Internals\ShipmentItemStoreTable::getList($parameters);
717 }
718
724 public function getErrorEntity($value)
725 {
726 static $className = null;
727 $errorsList = static::getAutoFixErrorsList();
728 if (is_array($errorsList) && in_array($value, $errorsList))
729 {
730 if ($className === null)
731 {
732 $className = static::getClassName();
733 }
734 }
735 return $className;
736 }
737
743 public function canAutoFixError($value)
744 {
745 $errorsList = static::getAutoFixErrorsList();
746 return (is_array($errorsList) && in_array($value, $errorsList));
747 }
748
752 public function getAutoFixErrorsList()
753 {
754 return array();
755 }
756
762 public function tryFixError($code)
763 {
764 return new Result();
765 }
766
770 public function canMarked()
771 {
772 return false;
773 }
774
778 public function getMarkField()
779 {
780 return null;
781 }
782
787 protected function addInternal(array $data)
788 {
789 return Internals\ShipmentItemStoreTable::add($data);
790 }
791
797 protected function updateInternal($primary, array $data)
798 {
799 return Internals\ShipmentItemStoreTable::update($primary, $data);
800 }
801
805 protected static function getFieldsMap()
806 {
807 return Internals\ShipmentItemStoreTable::getMap();
808 }
809
815 public static function getEntityEventName()
816 {
817 return 'SaleShipmentItemStore';
818 }
819}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getInstance($type)
Definition registry.php:183
const ENTITY_SHIPMENT_ITEM_STORE
Definition registry.php:39
updateInternal($primary, array $data)
static getList(array $parameters=array())
static create(ShipmentItemStoreCollection $collection, BasketItem $basketItem)