Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
shipmentitemstorecollection.php
1<?php
2
3
4namespace Bitrix\Sale;
5
8
9Loc::loadMessages(__FILE__);
10
12{
14 private $shipmentItem;
15
19 protected function getEntityParent()
20 {
21 return $this->getShipmentItem();
22 }
23
28 private static function createShipmentItemStoreCollectionObject()
29 {
30 $registry = Registry::getInstance(static::getRegistryType());
31 $shipmentItemStoreCollectionClassName = $registry->getShipmentItemStoreCollectionClassName();
32
33 return new $shipmentItemStoreCollectionClassName();
34 }
35
39 public static function getRegistryType()
40 {
42 }
43
53 public static function load(ShipmentItem $shipmentItem)
54 {
56 $shipmentItemStoreCollection = static::createShipmentItemStoreCollectionObject();
57 $shipmentItemStoreCollection->shipmentItem = $shipmentItem;
58
59 if ($shipmentItem->getId() > 0)
60 {
61 $registry = Registry::getInstance(static::getRegistryType());
62
64 $itemClassName = $registry->getShipmentItemStoreClassName();
65
66 $shipmentItemStoreList = $itemClassName::loadForShipmentItem($shipmentItem->getId());
67
69 foreach ($shipmentItemStoreList as $shipmentItemStore)
70 {
71 $shipmentItemStore->setCollection($shipmentItemStoreCollection);
72 $shipmentItemStoreCollection->addItem($shipmentItemStore);
73 }
74 }
75
76 return $shipmentItemStoreCollection;
77 }
78
84 public function createItem(BasketItem $basketItem)
85 {
86 $registry = Registry::getInstance(static::getRegistryType());
87
89 $itemClassName = $registry->getShipmentItemStoreClassName();
90
91 $item = $itemClassName::create($this, $basketItem);
92
93 $this->addItem($item);
94
95 return $item;
96 }
97
102 public function getQuantityByBasketCode($basketCode)
103 {
104 $quantity = 0;
105
107 foreach ($this->collection as $item)
108 {
109 $quantity += $item->getQuantity();
110 }
111
112 return $quantity;
113 }
114
115
119 public function getShipmentItem()
120 {
121 return $this->shipmentItem;
122 }
123
135 public function onShipmentItemModify($action, ShipmentItem $shipmentItem, $name = null, $oldValue = null, $value = null)
136 {
137 if ($action !== EventActions::UPDATE)
138 {
139 return new Result();
140 }
141
142 if ($name == "QUANTITY")
143 {
144 return $this->syncQuantityAfterModify($shipmentItem, $oldValue, $value);
145 }
146
147 return new Result();
148 }
149
159 protected function syncQuantityAfterModify(ShipmentItem $shipmentItem, $oldValue = null, $value = null)
160 {
161 if (!($basketItem = $shipmentItem->getBasketItem()) || $basketItem->getId() == 0)
162 return new Result();
163
164 $result = new Result();
165
166 $deltaQuantity = $value - $oldValue;
167
168 if ($deltaQuantity >= 0)
169 return $result;
170
171 $barcodeList = array();
173 foreach($this->collection as $shipmentItemStore)
174 {
175 if (strval($shipmentItemStore->getBarcode()) == "")
176 {
177 $barcodeList[$shipmentItemStore->getId()] = $shipmentItemStore;
178 }
179 }
180
181 if ($basketItem->isBarcodeMulti())
182 {
183 if (count($barcodeList) < $oldValue)
184 {
185 return $result;
186 }
187
188 $oldItemsList = array();
189
191 foreach ($this->collection as $shipmentItemStore)
192 {
193 $oldItemsList[$shipmentItemStore->getId()] = $shipmentItemStore;
194 }
195
196 $cutBarcodeList = array_slice($barcodeList, 0, $deltaQuantity, true);
197 if (!empty($oldItemsList) && is_array($oldItemsList))
198 {
203 foreach($oldItemsList as $oldItemId => $oldItem)
204 {
205 if (!isset($cutBarcodeList[$oldItemId]))
206 {
207 $oldItem->delete();
208 }
209 }
210 }
211 }
212 elseif (count($barcodeList) == 1)
213 {
215 $barcodeItem = reset($barcodeList);
216
217 if ($barcodeItem->getQuantity() < $oldValue)
218 return new Result();
219
221 $r = $barcodeItem->setField(
222 "QUANTITY",
223 $barcodeItem->getField("QUANTITY") + $deltaQuantity
224 );
225
226 if (!$r->isSuccess())
227 {
228 $result->addErrors($r->getErrors());
229 return $result;
230 }
231 }
232
233 return $result;
234 }
235
244 public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
245 {
246 $result = new Result();
247
248 if ($name == "QUANTITY")
249 {
250 $r = $this->checkAvailableQuantity($item);
251 if (!$r->isSuccess())
252 {
253 return $result->addErrors($r->getErrors());
254 }
255 }
256
257 return new Result();
258 }
259
267 public function checkAvailableQuantity(Internals\CollectableEntity $item)
268 {
269 $result = new Result();
270
271 if (!$item instanceof ShipmentItemStore)
272 {
273 return $result;
274 }
275
276 $shipmentItem = $this->getShipmentItem();
277
278 $itemStoreQuantity = (float)$this->getQuantityByBasketCode($shipmentItem->getBasketCode());
279
280 if (
281 (float)$item->getQuantity() > $shipmentItem->getQuantity()
282 ||
283 $itemStoreQuantity > $shipmentItem->getQuantity()
284 )
285 {
286 $result->addError(new Main\Error(
288 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED',
289 ['#PRODUCT_NAME#' => $this->getShipmentItem()->getBasketItem()->getField('NAME')]
290 ),
291 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED'
292 )
293 );
294 }
295
296 return $result;
297 }
298
307 public function save()
308 {
309 $result = new Main\Entity\Result();
310
311 $originalItemValues = $this->getOriginalItemValues();
312
314 foreach ($this->collection as $item)
315 {
316 $r = $item->save();
317 if (!$r->isSuccess())
318 {
319 $result->addErrors($r->getErrors());
320 }
321
322 if (isset($originalItemValues[$item->getId()]))
323 {
324 unset($originalItemValues[$item->getId()]);
325 }
326 }
327
328 if ($originalItemValues)
329 {
330 foreach ($originalItemValues as $id => $itemValues)
331 {
332 $this->callEventOnBeforeSaleShipmentItemStoreDeleted($itemValues);
333
334 $this->deleteInternal($id);
335
336 $this->callEventOnSaleShipmentItemStoreDeleted($itemValues);
337 }
338 }
339
340 return $result;
341 }
342
346 protected function getOriginalItemValues() : array
347 {
348 $itemsFromDb = array();
349
350 if ($this->getShipmentItem()->getId() > 0)
351 {
352 $itemsFromDbList = static::getList(
353 array(
354 "filter" => array("ORDER_DELIVERY_BASKET_ID" => $this->getShipmentItem()->getId()),
355 )
356 );
357 while ($itemsFromDbItem = $itemsFromDbList->fetch())
358 {
359 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
360 }
361 }
362
363 return $itemsFromDb;
364 }
365
369 protected function callEventOnBeforeSaleShipmentItemStoreDeleted(array $itemValues)
370 {
371 $itemValues['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
372
374 $event = new Main\Event('sale', "OnBeforeSaleShipmentItemStoreDeleted", ['VALUES' => $itemValues]);
375 $event->send();
376 }
377
381 protected function callEventOnSaleShipmentItemStoreDeleted(array $itemValues)
382 {
383 $itemValues['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
384
386 $event = new Main\Event('sale', "OnSaleShipmentItemStoreDeleted", ['VALUES' => $itemValues]);
387 $event->send();
388 }
389
396 public function setBarcodeQuantityFromArray(array $values)
397 {
398 $result = new Result();
399 $requestBarcodeList = static::getBarcodeListFromArray($values);
400
401 $plusList = array();
402 $oldQuantityList = $this->getAllBarcodeList();
403
404 foreach ($requestBarcodeList as $storeId => $barcodeDat)
405 {
406 foreach ($barcodeDat as $barcodeValue => $barcode)
407 {
408 if (isset($oldQuantityList[$storeId][$barcodeValue])
409 && $oldQuantityList[$storeId][$barcodeValue]['ID'] == $barcode['ID'])
410 {
411 $oldBarcode = $oldQuantityList[$storeId][$barcodeValue];
412 if ($barcode['QUANTITY'] == $oldBarcode['QUANTITY'])
413 {
414 continue;
415 }
416 elseif ($barcode['QUANTITY'] < $oldBarcode['QUANTITY'])
417 {
419 $item = $this->getItemById($oldBarcode['ID']);
420 if ($item)
421 $item->setField('QUANTITY', $barcode['QUANTITY']);
422 }
423 else
424 {
425 $plusList[$barcodeValue] = array(
426 'ID' => $barcode['ID'],
427 'QUANTITY' => $barcode['QUANTITY']
428 );
429 }
430 }
431 }
432 }
433
434 foreach ($plusList as $barcode)
435 {
436 if ($barcode['ID'] <= 0)
437 continue;
438
439 $item = $this->getItemById($barcode['ID']);
440 if ($item)
441 {
443 $r = $item->setField('QUANTITY', $barcode['QUANTITY']);
444 if (!$r->isSuccess())
445 {
446 $result->addErrors($r->getErrors());
447 }
448 }
449 }
450
451 return $result;
452 }
453
454
459 private function getBarcodeListFromArray(array $values)
460 {
461 $result = array();
462
463 foreach ($values['BARCODE_INFO'] as $barcodeDat)
464 {
465 $storeId = $barcodeDat['STORE_ID'];
466
467 if (!isset($barcodeDat['BARCODE']) || !is_array($barcodeDat['BARCODE']))
468 continue;
469
470 if (count($barcodeDat['BARCODE']) > 1)
471 {
472 $quantity = floatval($barcodeDat['QUANTITY'] / count($barcodeDat['BARCODE']));
473 }
474 else
475 {
476 $quantity = floatval($barcodeDat['QUANTITY']);
477 }
478
479 foreach ($barcodeDat['BARCODE'] as $barcode)
480 {
481 if (!isset($result[$storeId]))
482 $result[$storeId] = array();
483
484 $result[$storeId][$barcode['VALUE']] = array(
485 "QUANTITY" => $quantity,
486 );
487
488 if (isset($barcode['ID']) && intval($barcode['ID']) > 0)
489 {
490 $result[$storeId][$barcode['VALUE']]['ID'] = intval($barcode['ID']);
491 }
492 }
493 }
494
495 return $result;
496 }
497
501 public function getAllBarcodeList()
502 {
503 $result = [];
504
506 foreach ($this->collection as $item)
507 {
508 if (!isset($result[$item->getStoreId()]))
509 {
510 $result[$item->getStoreId()] = [];
511 }
512
513 $result[$item->getStoreId()][$item->getBarcode()] = [
514 'ID' => $item->getId(),
515 'QUANTITY' => $item->getQuantity(),
516 ];
517 }
518
519 return $result;
520 }
521
526 public function getItemByBarcode($barcode)
527 {
529 foreach ($this->collection as $item)
530 {
531 if ((string)$item->getBarcode() === (string)$barcode)
532 {
533 return $item;
534 }
535 }
536
537 return null;
538 }
539
540 public function getItemsByStoreId(int $storeId) : Internals\CollectionFilterIterator
541 {
542 $callback = function (ShipmentItemStore $itemStore) use ($storeId)
543 {
544 return $itemStore->getStoreId() === $storeId;
545 };
546
547 return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
548 }
549
563 public function createClone(\SplObjectStorage $cloneEntity)
564 {
565 if ($this->isClone() && $cloneEntity->contains($this))
566 {
567 return $cloneEntity[$this];
568 }
569
571 $shipmentItemStoreCollectionClone = parent::createClone($cloneEntity) ;
572
574 if ($shipmentItem = $this->shipmentItem)
575 {
576 if (!$cloneEntity->contains($shipmentItem))
577 {
578 $cloneEntity[$shipmentItem] = $shipmentItem->createClone($cloneEntity);
579 }
580
581 if ($cloneEntity->contains($shipmentItem))
582 {
583 $shipmentItemStoreCollectionClone->shipmentItem = $cloneEntity[$shipmentItem];
584 }
585 }
586
587 return $shipmentItemStoreCollectionClone;
588 }
589
594 public function getErrorEntity($value)
595 {
596 $className = null;
597
599 foreach ($this->collection as $shipmentItemStore)
600 {
601 if ($className = $shipmentItemStore->getErrorEntity($value))
602 {
603 break;
604 }
605 }
606
607 return $className;
608 }
609
615 public function canAutoFixError($value)
616 {
617 $autoFix = false;
619 foreach ($this->collection as $shipmentItemStore)
620 {
621 if ($autoFix = $shipmentItemStore->canAutoFixError($value))
622 {
623 break;
624 }
625 }
626 return $autoFix;
627 }
628
636 public static function getList(array $parameters = array())
637 {
638 return Internals\ShipmentItemStoreTable::getList($parameters);
639 }
640
646 protected function deleteInternal($primary)
647 {
648 return Internals\ShipmentItemStoreTable::delete($primary);
649 }
650}
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
onShipmentItemModify($action, ShipmentItem $shipmentItem, $name=null, $oldValue=null, $value=null)
checkAvailableQuantity(Internals\CollectableEntity $item)
onItemModify(Internals\CollectableEntity $item, $name=null, $oldValue=null, $value=null)