1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
shipmentitemstorecollection.php
См. документацию.
1<?php
2
3
4namespace Bitrix\Sale;
5
6use Bitrix\Main;
7use Bitrix\Main\Localization\Loc;
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
48 public static function load(ShipmentItem $shipmentItem)
49 {
51 $shipmentItemStoreCollection = static::createShipmentItemStoreCollectionObject();
52 $shipmentItemStoreCollection->shipmentItem = $shipmentItem;
53
54 if ($shipmentItem->getId() > 0)
55 {
56 $registry = Registry::getInstance(static::getRegistryType());
57
59 $itemClassName = $registry->getShipmentItemStoreClassName();
60
61 $shipmentItemStoreList = $itemClassName::loadForShipmentItem($shipmentItem->getId());
62
64 foreach ($shipmentItemStoreList as $shipmentItemStore)
65 {
66 $shipmentItemStore->setCollection($shipmentItemStoreCollection);
67 $shipmentItemStoreCollection->bindItem($shipmentItemStore);
68 }
69 }
70
71 return $shipmentItemStoreCollection;
72 }
73
79 public function createItem(BasketItem $basketItem)
80 {
81 $registry = Registry::getInstance(static::getRegistryType());
82
84 $itemClassName = $registry->getShipmentItemStoreClassName();
85
86 $item = $itemClassName::create($this, $basketItem);
87
88 $this->addItem($item);
89
90 return $item;
91 }
92
97 public function getQuantityByBasketCode($basketCode)
98 {
99 $quantity = 0;
100
102 foreach ($this->collection as $item)
103 {
104 $quantity += $item->getQuantity();
105 }
106
107 return $quantity;
108 }
109
110
114 public function getShipmentItem()
115 {
116 return $this->shipmentItem;
117 }
118
130 public function onShipmentItemModify($action, ShipmentItem $shipmentItem, $name = null, $oldValue = null, $value = null)
131 {
133 {
134 return new Result();
135 }
136
137 if ($name == "QUANTITY")
138 {
139 return $this->syncQuantityAfterModify($shipmentItem, $oldValue, $value);
140 }
141
142 return new Result();
143 }
144
154 protected function syncQuantityAfterModify(ShipmentItem $shipmentItem, $oldValue = null, $value = null)
155 {
156 if (!($basketItem = $shipmentItem->getBasketItem()) || $basketItem->getId() == 0)
157 return new Result();
158
159 $result = new Result();
160
161 $deltaQuantity = $value - $oldValue;
162
163 if ($deltaQuantity >= 0)
164 return $result;
165
166 $barcodeList = array();
168 foreach($this->collection as $shipmentItemStore)
169 {
170 if (strval($shipmentItemStore->getBarcode()) == "")
171 {
172 $barcodeList[$shipmentItemStore->getId()] = $shipmentItemStore;
173 }
174 }
175
176 if ($basketItem->isBarcodeMulti())
177 {
178 if (count($barcodeList) < $oldValue)
179 {
180 return $result;
181 }
182
183 $oldItemsList = array();
184
186 foreach ($this->collection as $shipmentItemStore)
187 {
188 $oldItemsList[$shipmentItemStore->getId()] = $shipmentItemStore;
189 }
190
191 $cutBarcodeList = array_slice($barcodeList, 0, $deltaQuantity, true);
192 if (!empty($oldItemsList) && is_array($oldItemsList))
193 {
198 foreach($oldItemsList as $oldItemId => $oldItem)
199 {
200 if (!isset($cutBarcodeList[$oldItemId]))
201 {
202 $oldItem->delete();
203 }
204 }
205 }
206 }
207 elseif (count($barcodeList) == 1)
208 {
210 $barcodeItem = reset($barcodeList);
211
212 if ($barcodeItem->getQuantity() < $oldValue)
213 return new Result();
214
216 $r = $barcodeItem->setField(
217 "QUANTITY",
218 $barcodeItem->getField("QUANTITY") + $deltaQuantity
219 );
220
221 if (!$r->isSuccess())
222 {
223 $result->addErrors($r->getErrors());
224 return $result;
225 }
226 }
227
228 return $result;
229 }
230
239 public function onItemModify(Internals\CollectableEntity $item, $name = null, $oldValue = null, $value = null)
240 {
241 $result = new Result();
242
243 if ($name == "QUANTITY")
244 {
245 $r = $this->checkAvailableQuantity($item);
246 if (!$r->isSuccess())
247 {
248 return $result->addErrors($r->getErrors());
249 }
250 }
251
252 return new Result();
253 }
254
263 {
264 $result = new Result();
265
266 if (!$item instanceof ShipmentItemStore)
267 {
268 return $result;
269 }
270
271 $shipmentItem = $this->getShipmentItem();
272
273 $itemStoreQuantity = (float)$this->getQuantityByBasketCode($shipmentItem->getBasketCode());
274
275 if (
276 (float)$item->getQuantity() > $shipmentItem->getQuantity()
277 ||
278 $itemStoreQuantity > $shipmentItem->getQuantity()
279 )
280 {
281 $result->addError(new Main\Error(
282 Loc::getMessage(
283 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED',
284 ['#PRODUCT_NAME#' => $this->getShipmentItem()->getBasketItem()->getField('NAME')]
285 ),
286 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED'
287 )
288 );
289 }
290
291 return $result;
292 }
293
302 public function save()
303 {
304 $result = new Main\Entity\Result();
305
306 $originalItemValues = $this->getOriginalItemValues();
307
309 foreach ($this->collection as $item)
310 {
311 $r = $item->save();
312 if (!$r->isSuccess())
313 {
314 $result->addErrors($r->getErrors());
315 }
316
317 if (isset($originalItemValues[$item->getId()]))
318 {
319 unset($originalItemValues[$item->getId()]);
320 }
321 }
322
323 if ($originalItemValues)
324 {
325 foreach ($originalItemValues as $id => $itemValues)
326 {
327 $this->callEventOnBeforeSaleShipmentItemStoreDeleted($itemValues);
328
329 $this->deleteInternal($id);
330
331 $this->callEventOnSaleShipmentItemStoreDeleted($itemValues);
332 }
333 }
334
335 return $result;
336 }
337
341 protected function getOriginalItemValues() : array
342 {
343 $itemsFromDb = array();
344
345 if ($this->getShipmentItem()->getId() > 0)
346 {
347 $itemsFromDbList = static::getList(
348 array(
349 "filter" => array("ORDER_DELIVERY_BASKET_ID" => $this->getShipmentItem()->getId()),
350 )
351 );
352 while ($itemsFromDbItem = $itemsFromDbList->fetch())
353 {
354 $itemsFromDb[$itemsFromDbItem["ID"]] = $itemsFromDbItem;
355 }
356 }
357
358 return $itemsFromDb;
359 }
360
364 protected function callEventOnBeforeSaleShipmentItemStoreDeleted(array $itemValues)
365 {
366 $itemValues['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
367
369 $event = new Main\Event('sale', "OnBeforeSaleShipmentItemStoreDeleted", ['VALUES' => $itemValues]);
370 $event->send();
371 }
372
376 protected function callEventOnSaleShipmentItemStoreDeleted(array $itemValues)
377 {
378 $itemValues['ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
379
381 $event = new Main\Event('sale', "OnSaleShipmentItemStoreDeleted", ['VALUES' => $itemValues]);
382 $event->send();
383 }
384
391 public function setBarcodeQuantityFromArray(array $values)
392 {
393 $result = new Result();
394 $requestBarcodeList = static::getBarcodeListFromArray($values);
395
396 $plusList = array();
397 $oldQuantityList = $this->getAllBarcodeList();
398
399 foreach ($requestBarcodeList as $storeId => $barcodeDat)
400 {
401 foreach ($barcodeDat as $barcodeValue => $barcode)
402 {
403 if (isset($oldQuantityList[$storeId][$barcodeValue])
404 && $oldQuantityList[$storeId][$barcodeValue]['ID'] == $barcode['ID'])
405 {
406 $oldBarcode = $oldQuantityList[$storeId][$barcodeValue];
407 if ($barcode['QUANTITY'] == $oldBarcode['QUANTITY'])
408 {
409 continue;
410 }
411 elseif ($barcode['QUANTITY'] < $oldBarcode['QUANTITY'])
412 {
414 $item = $this->getItemById($oldBarcode['ID']);
415 if ($item)
416 $item->setField('QUANTITY', $barcode['QUANTITY']);
417 }
418 else
419 {
420 $plusList[$barcodeValue] = array(
421 'ID' => $barcode['ID'],
422 'QUANTITY' => $barcode['QUANTITY']
423 );
424 }
425 }
426 }
427 }
428
429 foreach ($plusList as $barcode)
430 {
431 if ($barcode['ID'] <= 0)
432 continue;
433
434 $item = $this->getItemById($barcode['ID']);
435 if ($item)
436 {
438 $r = $item->setField('QUANTITY', $barcode['QUANTITY']);
439 if (!$r->isSuccess())
440 {
441 $result->addErrors($r->getErrors());
442 }
443 }
444 }
445
446 return $result;
447 }
448
449
454 private function getBarcodeListFromArray(array $values)
455 {
456 $result = array();
457
458 foreach ($values['BARCODE_INFO'] as $barcodeDat)
459 {
460 $storeId = $barcodeDat['STORE_ID'];
461
462 if (!isset($barcodeDat['BARCODE']) || !is_array($barcodeDat['BARCODE']))
463 continue;
464
465 if (count($barcodeDat['BARCODE']) > 1)
466 {
467 $quantity = floatval($barcodeDat['QUANTITY'] / count($barcodeDat['BARCODE']));
468 }
469 else
470 {
471 $quantity = floatval($barcodeDat['QUANTITY']);
472 }
473
474 foreach ($barcodeDat['BARCODE'] as $barcode)
475 {
476 if (!isset($result[$storeId]))
478
479 $result[$storeId][$barcode['VALUE']] = array(
480 "QUANTITY" => $quantity,
481 );
482
483 if (isset($barcode['ID']) && intval($barcode['ID']) > 0)
484 {
485 $result[$storeId][$barcode['VALUE']]['ID'] = intval($barcode['ID']);
486 }
487 }
488 }
489
490 return $result;
491 }
492
496 public function getAllBarcodeList()
497 {
498 $result = [];
499
501 foreach ($this->collection as $item)
502 {
503 if (!isset($result[$item->getStoreId()]))
504 {
505 $result[$item->getStoreId()] = [];
506 }
507
508 $result[$item->getStoreId()][$item->getBarcode()] = [
509 'ID' => $item->getId(),
510 'QUANTITY' => $item->getQuantity(),
511 ];
512 }
513
514 return $result;
515 }
516
521 public function getItemByBarcode($barcode)
522 {
524 foreach ($this->collection as $item)
525 {
526 if ((string)$item->getBarcode() === (string)$barcode)
527 {
528 return $item;
529 }
530 }
531
532 return null;
533 }
534
536 {
537 $callback = function (ShipmentItemStore $itemStore) use ($storeId)
538 {
539 return $itemStore->getStoreId() === $storeId;
540 };
541
542 return new Internals\CollectionFilterIterator($this->getIterator(), $callback);
543 }
544
558 public function createClone(\SplObjectStorage $cloneEntity)
559 {
560 if ($this->isClone() && $cloneEntity->contains($this))
561 {
562 return $cloneEntity[$this];
563 }
564
566 $shipmentItemStoreCollectionClone = parent::createClone($cloneEntity) ;
567
569 if ($shipmentItem = $this->shipmentItem)
570 {
571 if (!$cloneEntity->contains($shipmentItem))
572 {
573 $cloneEntity[$shipmentItem] = $shipmentItem->createClone($cloneEntity);
574 }
575
576 if ($cloneEntity->contains($shipmentItem))
577 {
578 $shipmentItemStoreCollectionClone->shipmentItem = $cloneEntity[$shipmentItem];
579 }
580 }
581
582 return $shipmentItemStoreCollectionClone;
583 }
584
589 public function getErrorEntity($value)
590 {
591 $className = null;
592
594 foreach ($this->collection as $shipmentItemStore)
595 {
596 if ($className = $shipmentItemStore->getErrorEntity($value))
597 {
598 break;
599 }
600 }
601
602 return $className;
603 }
604
610 public function canAutoFixError($value)
611 {
612 $autoFix = false;
614 foreach ($this->collection as $shipmentItemStore)
615 {
616 if ($autoFix = $shipmentItemStore->canAutoFixError($value))
617 {
618 break;
619 }
620 }
621 return $autoFix;
622 }
623
631 public static function getList(array $parameters = array())
632 {
633 return Internals\ShipmentItemStoreTable::getList($parameters);
634 }
635
641 protected function deleteInternal($primary)
642 {
643 return Internals\ShipmentItemStoreTable::delete($primary);
644 }
645}
Определения error.php:15
Определения event.php:5
const UPDATE
Определения eventactions.php:9
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
static getList(array $parameters=array())
onShipmentItemModify($action, ShipmentItem $shipmentItem, $name=null, $oldValue=null, $value=null)
checkAvailableQuantity(Internals\CollectableEntity $item)
onItemModify(Internals\CollectableEntity $item, $name=null, $oldValue=null, $value=null)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$action
Определения file_dialog.php:21