14 private $shipmentItem;
28 private static function createShipmentItemStoreCollectionObject()
31 $shipmentItemStoreCollectionClassName = $registry->getShipmentItemStoreCollectionClassName();
33 return new $shipmentItemStoreCollectionClassName();
56 $shipmentItemStoreCollection = static::createShipmentItemStoreCollectionObject();
57 $shipmentItemStoreCollection->shipmentItem = $shipmentItem;
59 if ($shipmentItem->getId() > 0)
64 $itemClassName = $registry->getShipmentItemStoreClassName();
66 $shipmentItemStoreList = $itemClassName::loadForShipmentItem($shipmentItem->getId());
69 foreach ($shipmentItemStoreList as $shipmentItemStore)
71 $shipmentItemStore->setCollection($shipmentItemStoreCollection);
72 $shipmentItemStoreCollection->addItem($shipmentItemStore);
76 return $shipmentItemStoreCollection;
84 public function createItem(BasketItem $basketItem)
89 $itemClassName = $registry->getShipmentItemStoreClassName();
91 $item = $itemClassName::create($this, $basketItem);
93 $this->addItem($item);
102 public function getQuantityByBasketCode($basketCode)
107 foreach ($this->collection as $item)
109 $quantity += $item->getQuantity();
121 return $this->shipmentItem;
142 if ($name ==
"QUANTITY")
144 return $this->syncQuantityAfterModify($shipmentItem, $oldValue, $value);
159 protected function syncQuantityAfterModify(
ShipmentItem $shipmentItem, $oldValue =
null, $value =
null)
161 if (!($basketItem = $shipmentItem->
getBasketItem()) || $basketItem->getId() == 0)
166 $deltaQuantity = $value - $oldValue;
168 if ($deltaQuantity >= 0)
171 $barcodeList = array();
173 foreach($this->collection as $shipmentItemStore)
175 if (strval($shipmentItemStore->getBarcode()) ==
"")
177 $barcodeList[$shipmentItemStore->getId()] = $shipmentItemStore;
181 if ($basketItem->isBarcodeMulti())
183 if (
count($barcodeList) < $oldValue)
188 $oldItemsList = array();
191 foreach ($this->collection as $shipmentItemStore)
193 $oldItemsList[$shipmentItemStore->getId()] = $shipmentItemStore;
196 $cutBarcodeList = array_slice($barcodeList, 0, $deltaQuantity,
true);
197 if (!empty($oldItemsList) && is_array($oldItemsList))
203 foreach($oldItemsList as $oldItemId => $oldItem)
205 if (!isset($cutBarcodeList[$oldItemId]))
212 elseif (
count($barcodeList) == 1)
215 $barcodeItem = reset($barcodeList);
217 if ($barcodeItem->getQuantity() < $oldValue)
221 $r = $barcodeItem->setField(
223 $barcodeItem->getField(
"QUANTITY") + $deltaQuantity
226 if (!$r->isSuccess())
228 $result->addErrors($r->getErrors());
248 if ($name ==
"QUANTITY")
251 if (!$r->isSuccess())
253 return $result->addErrors($r->getErrors());
278 $itemStoreQuantity = (float)$this->getQuantityByBasketCode($shipmentItem->
getBasketCode());
281 (
float)$item->getQuantity() > $shipmentItem->
getQuantity()
286 $result->addError(
new Main\
Error(
288 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED',
289 [
'#PRODUCT_NAME#' => $this->
getShipmentItem()->getBasketItem()->getField(
'NAME')]
291 'SALE_SHIPMENT_ITEM_STORE_QUANTITY_LARGER_ALLOWED'
307 public function save()
309 $result =
new Main\Entity\Result();
314 foreach ($this->collection as $item)
317 if (!$r->isSuccess())
319 $result->addErrors($r->getErrors());
322 if (isset($originalItemValues[$item->getId()]))
324 unset($originalItemValues[$item->getId()]);
328 if ($originalItemValues)
330 foreach ($originalItemValues as $id => $itemValues)
332 $this->callEventOnBeforeSaleShipmentItemStoreDeleted($itemValues);
336 $this->callEventOnSaleShipmentItemStoreDeleted($itemValues);
348 $itemsFromDb = array();
352 $itemsFromDbList = static::getList(
354 "filter" => array(
"ORDER_DELIVERY_BASKET_ID" => $this->
getShipmentItem()->getId()),
357 while ($itemsFromDbItem = $itemsFromDbList->fetch())
359 $itemsFromDb[$itemsFromDbItem[
"ID"]] = $itemsFromDbItem;
369 protected function callEventOnBeforeSaleShipmentItemStoreDeleted(array $itemValues)
371 $itemValues[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
374 $event =
new Main\Event(
'sale',
"OnBeforeSaleShipmentItemStoreDeleted", [
'VALUES' => $itemValues]);
381 protected function callEventOnSaleShipmentItemStoreDeleted(array $itemValues)
383 $itemValues[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
386 $event =
new Main\Event(
'sale',
"OnSaleShipmentItemStoreDeleted", [
'VALUES' => $itemValues]);
396 public function setBarcodeQuantityFromArray(array $values)
398 $result =
new Result();
399 $requestBarcodeList = static::getBarcodeListFromArray($values);
402 $oldQuantityList = $this->getAllBarcodeList();
404 foreach ($requestBarcodeList as $storeId => $barcodeDat)
406 foreach ($barcodeDat as $barcodeValue => $barcode)
408 if (isset($oldQuantityList[$storeId][$barcodeValue])
409 && $oldQuantityList[$storeId][$barcodeValue][
'ID'] == $barcode[
'ID'])
411 $oldBarcode = $oldQuantityList[$storeId][$barcodeValue];
412 if ($barcode[
'QUANTITY'] == $oldBarcode[
'QUANTITY'])
416 elseif ($barcode[
'QUANTITY'] < $oldBarcode[
'QUANTITY'])
421 $item->setField(
'QUANTITY', $barcode[
'QUANTITY']);
425 $plusList[$barcodeValue] = array(
426 'ID' => $barcode[
'ID'],
427 'QUANTITY' => $barcode[
'QUANTITY']
434 foreach ($plusList as $barcode)
436 if ($barcode[
'ID'] <= 0)
443 $r = $item->setField(
'QUANTITY', $barcode[
'QUANTITY']);
444 if (!$r->isSuccess())
446 $result->addErrors($r->getErrors());
459 private function getBarcodeListFromArray(array $values)
463 foreach ($values[
'BARCODE_INFO'] as $barcodeDat)
467 if (!isset($barcodeDat[
'BARCODE']) || !is_array($barcodeDat[
'BARCODE']))
470 if (
count($barcodeDat[
'BARCODE']) > 1)
472 $quantity = floatval($barcodeDat[
'QUANTITY'] /
count($barcodeDat[
'BARCODE']));
476 $quantity = floatval($barcodeDat[
'QUANTITY']);
479 foreach ($barcodeDat[
'BARCODE'] as $barcode)
481 if (!isset($result[$storeId]))
485 "QUANTITY" => $quantity,
488 if (isset($barcode[
'ID']) && intval($barcode[
'ID']) > 0)
501 public function getAllBarcodeList()
506 foreach ($this->collection as $item)
508 if (!isset($result[$item->getStoreId()]))
510 $result[$item->getStoreId()] = [];
513 $result[$item->getStoreId()][$item->getBarcode()] = [
514 'ID' => $item->getId(),
515 'QUANTITY' => $item->getQuantity(),
526 public function getItemByBarcode($barcode)
529 foreach ($this->collection as $item)
531 if ((
string)$item->getBarcode() === (
string)$barcode)
544 return $itemStore->getStoreId() === $storeId;
563 public function createClone(\SplObjectStorage $cloneEntity)
565 if ($this->
isClone() && $cloneEntity->contains($this))
567 return $cloneEntity[$this];
571 $shipmentItemStoreCollectionClone = parent::createClone($cloneEntity) ;
574 if ($shipmentItem = $this->shipmentItem)
576 if (!$cloneEntity->contains($shipmentItem))
578 $cloneEntity[$shipmentItem] = $shipmentItem->createClone($cloneEntity);
581 if ($cloneEntity->contains($shipmentItem))
583 $shipmentItemStoreCollectionClone->shipmentItem = $cloneEntity[$shipmentItem];
587 return $shipmentItemStoreCollectionClone;
594 public function getErrorEntity($value)
599 foreach ($this->collection as $shipmentItemStore)
601 if ($className = $shipmentItemStore->getErrorEntity($value))
615 public function canAutoFixError($value)
619 foreach ($this->collection as $shipmentItemStore)
621 if ($autoFix = $shipmentItemStore->canAutoFixError($value))
636 public static function getList(array $parameters = array())
638 return Internals\ShipmentItemStoreTable::getList($parameters);
648 return Internals\ShipmentItemStoreTable::delete($primary);