23 'ORDER_DELIVERY_BASKET_ID',
'STORE_ID',
'QUANTITY',
24 'BARCODE',
'BASKET_ID',
'MARKING_CODE',
41 private static function createShipmentItemStoreObject(array $itemData = array())
44 $shipmentItemStoreClassName = $registry->getShipmentItemStoreClassName();
46 return new $shipmentItemStoreClassName($itemData);
78 $shipmentItemStore = static::createShipmentItemStoreObject();
85 'ORDER_DELIVERY_BASKET_ID' =>
$collection->getShipmentItem()->getId(),
86 'BASKET_ID' => $shipmentItem->getBasketItem()->getId(),
89 $shipmentItemStore->setFieldsNoDemand($fields);
92 return $shipmentItemStore;
108 if (is_object($USER) && $USER->isAuthorized())
110 $this->setFieldNoDemand(
'MODIFIED_BY', $USER->GetID());
113 $this->setFieldNoDemand(
'DATE_MODIFY',
new Main\
Type\DateTime());
115 $result = parent::onFieldModify($name, $oldValue, $value);
116 if (!$result->isSuccess())
127 $basketItem = $shipmentItem->getBasketItem();
130 $reserveCollection = $basketItem->getReserveQuantityCollection();
134 && $shipmentItem->getReservedQuantity() > 0
135 && $this->getQuantity() > 0
138 $storeIdFrom = (int)$oldValue ?: Configuration::getDefaultStoreId();
139 $storeIdTo = (int)$value;
144 || $storeIdFrom === $storeIdTo
150 $reserveTo = $reserveFrom =
null;
153 foreach ($reserveCollection as $reserve)
155 if ($reserve->getStoreId() === $storeIdFrom)
157 $reserveFrom = $reserve;
159 elseif ($reserve->getStoreId() === $storeIdTo)
161 $reserveTo = $reserve;
164 if ($reserveTo && $reserveFrom)
172 $settableQuantity = $reserveFrom->getQuantity() - $this->
getQuantity();
173 if ($settableQuantity > 0)
175 $reserveFrom->setField(
'QUANTITY', $settableQuantity);
179 $reserveFrom->delete();
185 $reserveTo = $reserveCollection->create();
186 $reserveTo->setStoreId($storeIdTo);
189 $reserveTo->setQuantity($reserveTo->getQuantity() + $this->getQuantity());
198 $basketItem = $shipmentItem->getBasketItem();
201 $reserveCollection = $basketItem->getReserveQuantityCollection();
203 if ($reserveCollection && $shipmentItem->getReservedQuantity() > 0)
205 if ($value > $oldValue)
219 || $storeIdFrom === $storeIdTo
225 $reserveTo = $reserveFrom =
null;
228 foreach ($reserveCollection as $reserve)
230 if ($reserve->getStoreId() === $storeIdFrom)
232 $reserveFrom = $reserve;
234 elseif ($reserve->getStoreId() === $storeIdTo)
236 $reserveTo = $reserve;
239 if ($reserveTo && $reserveFrom)
245 $delta = abs($oldValue - $value);
249 $settableQuantity = $reserveFrom->getQuantity() - $delta;
250 if ($settableQuantity > 0)
252 $reserveFrom->setField(
'QUANTITY', $settableQuantity);
256 $reserveFrom->delete();
262 $reserveTo = $reserveCollection->create();
263 $reserveTo->setStoreId($storeIdTo);
266 $reserveTo->setQuantity($reserveTo->getQuantity() + $delta);
284 public function delete()
288 $oldEntityValues = $this->fields->getOriginalValues();
290 $event =
new Main\Event(
'sale',
"OnBeforeSaleShipmentItemStoreEntityDeleted", [
292 'VALUES' => $oldEntityValues,
296 foreach ($event->getResults() as $eventResult)
298 if ($eventResult->getType() == Main\EventResult::ERROR)
300 $eventResultData = $eventResult->getParameters();
303 return $result->addError($eventResultData);
308 $r = parent::delete();
309 if (!$r->isSuccess())
311 return $result->addErrors($r->getErrors());
315 $basketItem = $shipmentItem->getBasketItem();
318 $reserveCollection = $basketItem->getReserveQuantityCollection();
320 if ($reserveCollection && $shipmentItem->getReservedQuantity() > 0)
325 if ($storeIdFrom === $storeIdTo)
330 $reserveTo = $reserveFrom =
null;
333 foreach ($reserveCollection as $reserve)
335 if ($reserve->getStoreId() === $storeIdFrom)
337 $reserveFrom = $reserve;
339 elseif ($reserve->getStoreId() === $storeIdTo)
341 $reserveTo = $reserve;
344 if ($reserveTo && $reserveFrom)
354 $settableQuantity = $reserveFrom->getQuantity() - $delta;
355 if ($settableQuantity > 0)
357 $reserveFrom->setField(
'QUANTITY', $settableQuantity);
361 $reserveFrom->delete();
367 $reserveTo = $reserveCollection->create();
368 $reserveTo->setStoreId($storeIdTo);
371 $reserveTo->setQuantity($reserveTo->getQuantity() + $delta);
374 $event =
new Main\Event(
'sale',
"OnSaleShipmentItemStoreEntityDeleted", array(
376 'VALUES' => $this->fields->getOriginalValues(),
380 foreach ($event->getResults() as $eventResult)
382 if ($eventResult->getType() == Main\EventResult::ERROR)
384 $eventResultData = $eventResult->getParameters();
385 if ($eventResultData instanceof ResultError)
387 $result->addError($eventResultData);
400 return (
int)$this->getField(
'BASKET_ID');
408 return (
float)$this->getField(
'QUANTITY');
416 return (
int)$this->getField(
'STORE_ID');
424 return (
string)$this->getField(
'BARCODE');
432 return (
string)$this->getField(
'MARKING_CODE');
452 $itemDataList = static::getList([
453 'filter' => [
'=ORDER_DELIVERY_BASKET_ID' => $id],
454 'order' => [
'DATE_CREATE' =>
'ASC',
'ID' =>
'ASC']
457 while ($itemData = $itemDataList->fetch())
459 $items[] = static::createShipmentItemStoreObject($itemData);
470 public function verify()
480 $basketItem = $itemStoreCollection->getShipmentItem()->getBasketItem();
483 $shipment = $itemStoreCollection->getShipmentItem()->getCollection()->getShipment();
485 if ($basketItem->isBarcodeMulti() && $shipment->isShipped())
490 'SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY',
492 '#PRODUCT_NAME#' => $basketItem->getField(
'NAME'),
493 '#STORE_ID#' => $this->getStoreId(),
496 'SHIPMENT_ITEM_STORE_BARCODE_MULTI_EMPTY'
508 protected function checkCallingContext()
514 $itemCollection = $itemStoreCollection->getShipmentItem()->getCollection();
517 $shipmentCollection = $itemCollection->getShipment()->getCollection();
519 if (!$shipmentCollection->getOrder()->isSaveRunning())
521 trigger_error(
"Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
533 public function save()
535 $result =
new Result();
537 $this->checkCallingContext();
544 $this->callEventOnBeforeItemStoreEntitySaved();
546 $id = $this->getId();
557 if (!$this->getField(
"ORDER_DELIVERY_BASKET_ID"))
559 $this->setFieldNoDemand(
'ORDER_DELIVERY_BASKET_ID', $itemStoreCollection->getShipmentItem()->getId());
562 if (!$this->getField(
"BASKET_ID"))
564 $this->setFieldNoDemand(
'BASKET_ID', $itemStoreCollection->getShipmentItem()->getBasketItem()->getId());
567 $this->setFieldNoDemand(
'DATE_CREATE',
new Main\
Type\DateTime());
574 $this->setFieldNoDemand(
'ID', $id);
578 if (!$r->isSuccess())
580 $this->addErrorMessagesToHistory($r->getErrorMessages());
582 $result->addErrors($r->getErrors());
592 $this->callEventOnItemStoreEntitySaved();
600 protected function callEventOnBeforeItemStoreEntitySaved()
603 $event =
new Main\Event(
'sale',
'OnBeforeSaleShipmentItemStoreEntitySaved', [
605 'VALUES' => $this->fields->getOriginalValues()
614 protected function callEventOnItemStoreEntitySaved()
617 $event =
new Main\Event(
'sale',
'OnSaleShipmentItemStoreEntitySaved', [
619 'VALUES' => $this->fields->getOriginalValues(),
631 protected function addErrorMessagesToHistory($errors)
639 $shipmentItemCollection = $shipmentItemStoreCollection->getShipmentItem()->getCollection();
641 $order = $shipmentItemCollection->getShipment()->getOrder();
644 $orderHistory = $registry->getOrderHistoryClassName();
645 $orderHistory::addAction(
648 ($this->getId() > 0) ?
'SHIPMENT_ITEM_STORE_UPDATE_ERROR' :
'SHIPMENT_ITEM_STORE_ADD_ERROR',
649 ($this->getId() > 0) ? $this->getId() : null,
665 protected function addChangesToHistory($name, $oldValue =
null, $value =
null)
667 if ($this->getId() <= 0)
676 $shipmentItemCollection = $shipmentItem->getCollection();
678 $shipmentItemCollection->getShipment()->getOrder();
680 $shipment = $shipmentItemCollection->getShipment();
681 if ($shipment->isSystem())
686 $basketItem = $shipmentItem->getBasketItem();
691 $orderHistory = $registry->getOrderHistoryClassName();
692 $orderHistory::addField(
693 'SHIPMENT_ITEM_STORE',
694 $shipment->getOrder()->getId(),
701 'NAME' => $basketItem->getField(
'NAME'),
702 'PRODUCT_ID' => $basketItem->getField(
'PRODUCT_ID'),
714 public static function getList(array $parameters = array())
716 return Sale\Internals\ShipmentItemStoreTable::getList($parameters);
726 static $className =
null;
727 $errorsList = static::getAutoFixErrorsList();
728 if (is_array($errorsList) && in_array($value, $errorsList))
730 if ($className ===
null)
732 $className = static::getClassName();
745 $errorsList = static::getAutoFixErrorsList();
746 return (is_array($errorsList) && in_array($value, $errorsList));
789 return Internals\ShipmentItemStoreTable::add($data);
799 return Internals\ShipmentItemStoreTable::update($primary, $data);
807 return Internals\ShipmentItemStoreTable::getMap();
817 return 'SaleShipmentItemStore';