56 if ($this->internalId === 0)
58 if ($this->getId() > 0)
60 $this->internalId = $this->getId();
64 static::$idShipment++;
65 $this->internalId = static::$idShipment;
78 "BASE_PRICE_DELIVERY",
81 "DATE_ALLOW_DELIVERY",
82 "EMP_ALLOW_DELIVERY_ID",
86 "REASON_UNDO_DEDUCTED",
104 "DATE_RESPONSIBLE_ID",
105 "EMP_RESPONSIBLE_ID",
108 "CUSTOM_PRICE_DELIVERY",
111 "VERSION_1C",
"ID_1C",
113 "TRACKING_LAST_CHECK",
114 "TRACKING_DESCRIPTION",
125 return [
'PRICE_DELIVERY' =>
'PRICE_DELIVERY',
'WEIGHT' =>
'WEIGHT'];
134 if (isset($values[
'DEDUCTED']))
136 if ($this->getField(
'DEDUCTED') ===
'Y')
138 if ($values[
'DEDUCTED'] ===
'N')
140 $values = [
'DEDUCTED' => $values[
'DEDUCTED']] + $values;
145 if ($values[
'DEDUCTED'] ===
'Y')
148 unset($values[
'DEDUCTED']);
149 $values[
'DEDUCTED'] =
'Y';
162 return array(
'BASE_PRICE_DELIVERY',
'DELIVERY_ID');
175 $result = $this->
setField(
"DELIVERY_ID", $service->getId());
176 if ($result->isSuccess())
178 $this->
setField(
"DELIVERY_NAME", $service->getName());
194 'DELIVERY_ID' => $emptyService[
'ID'],
195 'DELIVERY_NAME' => $emptyService[
'NAME'],
196 'ALLOW_DELIVERY' =>
'N',
198 'CUSTOM_PRICE_DELIVERY' =>
'N',
202 'XML_ID' => static::generateXmlId(),
209 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
210 $fields[
'STATUS_ID'] = $deliveryStatusClassName::getInitialStatus();
212 $shipment = static::createShipmentObject();
213 $shipment->setFieldsNoDemand($fields);
218 $shipment->setDeliveryService(
$service);
229 return uniqid(
'bx_');
237 private static function createShipmentObject(array $fields = array())
240 $shipmentClassName = $registry->getShipmentClassName();
242 return new $shipmentClassName($fields);
264 if ($condition === ReserveCondition::ON_CREATE)
269 if ($condition === ReserveCondition::ON_PAY
270 || $condition === ReserveCondition::ON_FULL_PAY)
273 if ($condition === ReserveCondition::ON_FULL_PAY)
275 return $order->isPaid();
278 return $order->getPaymentCollection()->hasPaidPayment();
288 $condition === ReserveCondition::ON_ALLOW_DELIVERY
292 $condition === ReserveCondition::ON_SHIP
308 private function transferItem2SystemShipment(
ShipmentItem $sourceItem, $quantity)
311 if ($this !== $sourceItemCollection->getShipment())
316 $quantity = floatval($quantity);
319 $systemShipment = $this->
getCollection()->getSystemShipment();
325 $order = $basketItem->getCollection()->getOrder();
335 $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
337 $systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($shipmentItemCode);
338 if (is_null($systemShipmentItem))
340 $systemShipmentItem = $systemShipmentItemCollection->createItem($basketItem);
343 $newSystemShipmentItemQuantity = $systemShipmentItem->getQuantity() + $quantity;
344 if ($newSystemShipmentItemQuantity < 0)
346 $result =
new Result();
350 [
"#NAME#",
"#QUANTITY#"],
351 [$sourceItem->
getBasketItem()->getField(
"NAME"), abs($quantity)],
354 'SALE_SHIPMENT_QUANTITY_MISMATCH'
360 $systemShipmentItem->setFieldNoDemand(
'QUANTITY', $newSystemShipmentItemQuantity);
361 if ($newSystemShipmentItemQuantity <= 1e-10)
363 $systemShipmentItem->delete();
366 $affectedQuantity = 0;
372 $affectedQuantity = $quantity;
373 $originalQuantity = $sourceItem->
getQuantity() + $quantity;
380 elseif ($quantity < 0)
382 if ($systemShipmentItem->getReservedQuantity() > 0)
384 $affectedQuantity = $quantity;
385 if ($systemShipmentItem->getReservedQuantity() < -$affectedQuantity)
387 $affectedQuantity = -1 * $systemShipmentItem->getReservedQuantity();
392 if ($affectedQuantity != 0)
394 $sourceItem->getFields()->set(
396 $sourceItem->getField(
'RESERVED_QUANTITY') - $affectedQuantity
399 $systemShipmentItem->getFields()->set(
401 $systemShipmentItem->getField(
'RESERVED_QUANTITY') + $affectedQuantity
406 ($systemShipmentItem->getField(
"RESERVED_QUANTITY") > 0) ?
"Y" :
"N"
409 $shipmentItemForPool = $sourceItem;
410 $sourceShipmentItemForPool = $systemShipmentItem;
414 $shipmentItemForPool = $systemShipmentItem;
415 $sourceShipmentItemForPool = $sourceItem;
421 $poolItems = Internals\ItemsPool::get($order->getInternalId(), $productId);
422 if (!empty($poolItems))
424 foreach ($poolItems as $poolIndex => $poolItem)
426 if ($poolItem->getInternalIndex() === $shipmentItemForPool->getInternalIndex())
432 $sourceShipmentItemForPool
433 && $poolItem instanceof ShipmentItem
434 && $poolItem->getInternalIndex() === $sourceShipmentItemForPool->getInternalIndex()
437 $reserveQuantity = $sourceShipmentItemForPool->getReservedQuantity();
438 if (abs($reserveQuantity) <= 1e-6)
440 Internals\ItemsPool::delete($order->getInternalId(), $productId, $poolIndex);
448 Internals\ItemsPool::add($order->getInternalId(), $productId, $shipmentItemForPool);
452 $tryReserveResult =
null;
458 if ($systemShipment->needReservation())
460 $tryReserveResult = Internals\Catalog\Provider::tryReserveShipmentItem($systemShipmentItem);
464 $tryReserveResult = Internals\Catalog\Provider::tryUnreserveShipmentItem($systemShipmentItem);
468 elseif ($quantity < 0)
472 && $sourceItemCollection->getShipment()->needReservation()
475 $tryReserveResult = Internals\Catalog\Provider::tryReserveShipmentItem($sourceItem);
481 if ($tryReserveResult ===
null)
486 if ($tryReserveResult !==
null && ($tryReserveResult->isSuccess() && ($tryReserveResultData = $tryReserveResult->getData())))
488 if (array_key_exists(
'CAN_RESERVE', $tryReserveResultData))
490 $canReserve = $tryReserveResultData[
'CAN_RESERVE'];
496 && $systemShipment->needReservation()
502 !Internals\ActionEntity::isTypeExists(
503 $order->getInternalId(),
508 Internals\ActionEntity::add(
509 $order->getInternalId(),
512 'METHOD' =>
'Bitrix\Sale\ShipmentCollection::updateReservedFlag',
513 'PARAMS' => [$systemShipment->getCollection()]
529 public static function updateReservedFlag(Shipment $shipment)
531 $shipmentReserved =
true;
533 $shipmentItemList = $shipment->getShipmentItemCollection()->getShippableItems();
535 if ($shipmentItemList->count() === 0)
537 $shipmentReserved =
false;
541 foreach ($shipmentItemList as $shipmentItem)
543 if ($shipmentItem->getQuantity() - $shipmentItem->getReservedQuantity())
545 $shipmentReserved =
false;
550 $shipmentReservedValue = $shipmentReserved ?
"Y" :
"N";
551 $currentValue = $shipment->getField(
'RESERVED');
552 if ($shipment->getField(
'RESERVED') != $shipmentReservedValue)
554 $eventManager = Main\EventManager::getInstance();
556 if (!empty($eventsList))
560 'ENTITY' => $shipment,
561 'VALUE' => $shipmentReservedValue,
566 if ($event->getResults())
568 $result =
new Result();
570 foreach($event->getResults() as $eventResult)
572 if($eventResult->getType() === Main\EventResult::ERROR)
574 $errorMsg =
new ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_SHIPMENT_RESERVE_ERROR'),
'SALE_EVENT_ON_BEFORE_SHIPMENT_RESERVE_ERROR');
576 $eventResultData = $eventResult->getParameters();
577 if ($eventResultData)
579 if (isset($eventResultData) && $eventResultData instanceof ResultError)
582 $errorMsg = $eventResultData;
586 $result->addError($errorMsg);
591 if (!$result->isSuccess())
598 $shipment->setFieldNoDemand(
'RESERVED', $shipmentReserved ?
"Y" :
"N");
601 'ENTITY' => $shipment,
602 'VALUE' => $shipmentReservedValue,
603 'OLD_VALUE' => $currentValue,
628 && $name !=
'RESERVED_QUANTITY'
634 if ($name ===
"QUANTITY")
636 $result = $this->transferItem2SystemShipment($shipmentItem, $oldValue - $value);
638 if (!$this->isMarkedFieldCustom(
'WEIGHT'))
642 $this->getShipmentItemCollection()->
getWeight()
648 elseif ($name ===
'RESERVED_QUANTITY')
652 !Internals\ActionEntity::isTypeExists(
653 $order->getInternalId(),
658 Internals\ActionEntity::add(
659 $order->getInternalId(),
662 'METHOD' =>
'Bitrix\Sale\ShipmentCollection::updateReservedFlag',
663 'PARAMS' => [$this->getCollection()]
685 $shipmentDataList = static::getList(
687 "filter" => [
"=ORDER_ID" => $orderId],
692 while ($shipment = $shipmentDataList->fetch())
694 $res = static::deleteInternal($shipment[
'ID']);
696 if ($res -> isSuccess())
698 Internals\ShipmentExtraServiceTable::deleteByShipmentId($shipment[
'ID']);
702 $result->addErrors($res->getErrors());
720 public function delete()
725 return $result->addError(
728 'SALE_SHIPMENT_EXIST_SHIPPED'
735 $this->
setField(
'BASE_PRICE_DELIVERY', 0);
737 if ($this->getFields()->isMarkedCustom(
'PRICE_DELIVERY'))
739 $this->
setField(
'PRICE_DELIVERY', 0);
748 $this->getShipmentItemCollection()->clearCollection();
750 return parent::delete();
758 Requests\Manager::onBeforeShipmentDelete($this);
767 elseif ($name ===
'REASON_MARKED')
769 $value = (string)$value;
770 if (mb_strlen($value) > 255)
772 $value = mb_substr($value, 0, 255);
776 return parent::normalizeValue($name, $value);
796 if ($name ===
'CUSTOM_PRICE_DELIVERY')
800 $this->markFieldCustom(
'PRICE_DELIVERY');
804 $this->unmarkFieldCustom(
'PRICE_DELIVERY');
808 return parent::setField($name, $value);
820 $result = parent::checkValueBeforeSet($name, $value);
822 if ($name ===
"DELIVERY_ID")
824 if (intval($value) > 0 && !
Delivery\Services\Manager::isServiceExist($value))
829 'SALE_SHIPMENT_WRONG_DELIVERY_SERVICE'
834 elseif ($name ===
'ACCOUNT_NUMBER')
836 $dbRes = static::getList([
838 'filter' => [
'=ACCOUNT_NUMBER' => $value]
865 if ($name ===
'CUSTOM_PRICE_DELIVERY')
869 $this->markFieldCustom(
'PRICE_DELIVERY');
873 $this->unmarkFieldCustom(
'PRICE_DELIVERY');
877 parent::setFieldNoDemand($name, $value);
888 if (intval($id) <= 0)
895 $shipmentDataList = static::getList(static::getParametersForLoad($id));
896 while ($shipmentData = $shipmentDataList->fetch())
898 $shipments[] = static::createShipmentObject($shipmentData);
913 'DATE_INSERT' =>
'ASC',
929 public function save()
931 $this->checkCallingContext();
935 $id = $this->getId();
936 $this->isNew = ($this->getId() === 0);
938 $this->callEventOnBeforeEntitySaved();
954 if (!$r->isSuccess())
956 $result->addErrors($r->getErrors());
964 $controller = Internals\CustomFieldsController::getInstance();
965 $controller->save($this);
974 $this->callEventOnEntitySaved();
976 $this->callDelayedEvents();
980 if (!$r->isSuccess())
982 $result->addErrors($r->getErrors());
991 $orderHistory = $registry->getOrderHistoryClassName();
992 $orderHistory::collectEntityFields(
'SHIPMENT', $this->
getParentOrderId(), $id);
1000 if (!$res->isSuccess())
1002 $result->addWarnings($res->getErrors());
1007 $this->isNew =
false;
1015 private function checkCallingContext()
1019 if (!$order->isSaveRunning())
1021 trigger_error(
"Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity", E_USER_WARNING);
1033 private function add()
1035 $result =
new Result();
1041 $r = static::addInternal($this->
getFields()->getValues());
1042 if (!$r->isSuccess())
1045 $orderHistory = $registry->getOrderHistoryClassName();
1047 $orderHistory::addAction(
1050 'SHIPMENT_ADD_ERROR',
1053 [
"ERROR" => $r->getErrorMessages()]
1056 $result->addErrors($r->getErrors());
1062 $result->setId($id);
1064 $this->setAccountNumber($id);
1069 $orderHistory = $registry->getOrderHistoryClassName();
1071 $orderHistory::addAction(
1087 private function update()
1089 $result =
new Result();
1095 $r = static::updateInternal($this->getId(), $this->
getFields()->getChangedValues());
1096 if (!$r->isSuccess())
1099 $orderHistory = $registry->getOrderHistoryClassName();
1101 $orderHistory::addAction(
1104 'SHIPMENT_UPDATE_ERROR',
1107 [
"ERROR" => $r->getErrorMessages()]
1110 $result->addErrors($r->getErrors());
1123 Requests\Manager::onBeforeShipmentSave($order, $this);
1131 private function callDelayedEvents()
1133 $eventList = Internals\EventsPool::getEvents(
's'.$this->
getInternalIndex());
1137 foreach ($eventList as $eventName => $eventData)
1139 $event =
new Main\Event(
'sale', $eventName, $eventData);
1145 $notifyClassName = $registry->getNotifyClassName();
1146 $notifyClassName::callNotify($this, $eventName);
1156 private function callEventOnBeforeEntitySaved()
1159 $event =
new Main\Event(
'sale',
'OnBeforeSaleShipmentEntitySaved', [
1161 'VALUES' => $this->fields->getOriginalValues()
1170 private function callEventOnEntitySaved()
1173 $event =
new Main\Event(
'sale',
'OnSaleShipmentEntitySaved', [
1175 'VALUES' => $this->fields->getOriginalValues(),
1176 'IS_NEW' => $this->isNew,
1189 $this->getFields()->
isChanged(
'DEDUCTED')
1193 Internals\Catalog\Provider::changeProductBatchBalance($this);
1208 return $order->getId();
1225 public function getShipmentItemCollection()
1227 if (empty($this->shipmentItemCollection))
1232 $itemCollectionClassName = $registry->getShipmentItemCollectionClassName();
1233 $this->shipmentItemCollection = $itemCollectionClassName::load($this);
1259 $shipment = static::create(
$collection, $deliveryService);
1260 $shipment->markSystem();
1262 if ($deliveryService ===
null)
1264 $shipment->setFieldNoDemand(
'DELIVERY_ID',
Delivery\Services\Manager::getEmptyDeliveryServiceId());
1275 return (
float)$this->getField(
'PRICE_DELIVERY');
1284 return $this->isMarkedFieldCustom(
'PRICE_DELIVERY');
1290 $name ===
'BASE_PRICE_DELIVERY'
1291 || $name ===
'PRICE_DELIVERY'
1292 || $name ===
'DISCOUNT_PRICE'
1301 return (
string)$this->getField(
'CURRENCY');
1309 return (
int)$this->getField(
'DELIVERY_ID');
1317 return (
string)$this->getField(
'DELIVERY_NAME');
1325 $this->
setField(
'ORDER_ID', $orderId);
1335 if ($this->service ===
null)
1352 return Delivery\Services\Manager::getObjectById($deliveryId);
1364 return $this->getField(
'SYSTEM') ===
'Y';
1370 return $this->getField(
'CANCELED') ===
'Y';
1378 return $this->getField(
'DEDUCTED') ===
'Y';
1386 return $this->getField(
'DATE_DEDUCTED');
1394 return $this->getField(
'EMP_DEDUCTED_ID');
1402 return (
string)$this->getField(
'REASON_UNDO_DEDUCTED');
1410 return $this->getField(
'MARKED') ===
"Y";
1419 public function isReserved()
1422 foreach ($this->getShipmentItemCollection() as $shipmentItem)
1424 if ($shipmentItem->getReservedQuantity() !== $shipmentItem->getQuantity())
1438 return $this->getField(
'ALLOW_DELIVERY') ===
"Y";
1446 return $this->getShipmentItemCollection()->isEmpty();
1454 return $this->getField(
'DATE_ALLOW_DELIVERY');
1462 return (
int)$this->getField(
'EMP_ALLOW_DELIVERY_ID');
1470 return (
int)$this->getField(
'COMPANY_ID');
1481 return Internals\Catalog\Provider::tryReserveShipment($this);
1491 return Internals\Catalog\Provider::tryUnreserveShipment($this);
1499 public function tryShip()
1504 $r = Internals\Catalog\Provider::tryShipShipment($this);
1505 if ($r->isSuccess())
1507 $resultList = $r->getData();
1509 if (!empty($resultList) && is_array($resultList))
1512 foreach ($resultList as $resultDat)
1514 if (!$resultDat->isSuccess())
1516 $result->addErrors( $resultDat->getErrors() );
1523 $result->addErrors( $r->getErrors() );
1526 if ($r->hasWarnings())
1528 $result->addWarnings( $r->getWarnings() );
1540 return $this->tryShip();
1548 if ($this->fields->isChanged(
'DEDUCTED'))
1550 if ($this->getField(
'DEDUCTED') ===
"Y")
1554 elseif ($this->getField(
'DEDUCTED') ===
"N" && $this->getId() != 0)
1569 $result = Internals\Catalog\Provider::deliver($this);
1570 if ($result->isSuccess())
1585 return $this->
setField(
'ALLOW_DELIVERY',
"Y");
1595 return $this->
setField(
'ALLOW_DELIVERY',
"N");
1614 if (!$r->isSuccess())
1616 return $result->addErrors($r->getErrors());
1621 return $this->syncQuantityAfterModify($basketItem);
1641 public function onBasketModify($action,
BasketItem $basketItem, $name =
null, $oldValue =
null, $value =
null)
1652 return $this->getShipmentItemCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1656 if ($name ===
"QUANTITY")
1660 return $this->syncQuantityAfterModify($basketItem, $value, $oldValue);
1667 if ($r->isSuccess())
1672 if ($deliveryCalculate->isSuccess())
1674 $this->
setField(
'BASE_PRICE_DELIVERY', $deliveryCalculate->getPrice());
1678 $result->addWarnings($deliveryCalculate->getErrors());
1684 $result->addErrors($r->getErrors());
1687 elseif ($name ===
'WEIGHT')
1689 if (!$this->isMarkedFieldCustom(
'WEIGHT'))
1691 if ($this->getShipmentItemCollection()->isExistBasketItem($basketItem))
1697 elseif ($name ===
'PRICE')
1701 if ($this->getShipmentItemCollection()->isExistBasketItem($basketItem))
1704 if ($r->isSuccess())
1706 $this->
setField(
'BASE_PRICE_DELIVERY', $r->getPrice());
1710 $result->addErrors($r->getErrors());
1736 $result =
new Result();
1738 if ($name ===
'DELIVERY_ID')
1743 $this->service ===
null
1744 || $this->service->getId() !== (
int)$value
1748 $service = Delivery\Services\Manager::getObjectById($value);
1753 $this->
setField(
'DELIVERY_NAME', $this->service->getName());
1759 elseif ($name ===
"MARKED")
1761 if ($oldValue !=
"Y")
1763 $this->
setField(
'DATE_MARKED',
new Main\
Type\DateTime());
1765 if (is_object($USER))
1767 $this->
setField(
'EMP_MARKED_ID', $USER->GetID());
1770 elseif ($value ===
"N")
1772 $this->
setField(
'REASON_MARKED',
'');
1775 elseif ($name ===
"ALLOW_DELIVERY")
1777 $this->
setField(
'DATE_ALLOW_DELIVERY',
new Main\
Type\DateTime());
1779 if (is_object($USER))
1781 $this->
setField(
'EMP_ALLOW_DELIVERY_ID', $USER->GetID());
1784 if ($oldValue ===
'N')
1786 $shipmentStatus = Main\Config\Option::get(
'sale',
'shipment_status_on_allow_delivery',
'');
1790 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
1793 $shipmentStatus !==
''
1794 && $this->getField(
'STATUS_ID') != $deliveryStatusClassName::getFinalStatus()
1797 $r = $this->setStatus($shipmentStatus);
1798 if (!$r->isSuccess())
1800 $result->addErrors($r->getErrors());
1805 Internals\EventsPool::addEvent(
1810 'VALUES' => $this->fields->getOriginalValues()
1814 elseif ($name ===
"DEDUCTED")
1816 $this->
setField(
'DATE_DEDUCTED',
new Main\
Type\DateTime());
1818 if (is_object($USER))
1820 $this->
setField(
'EMP_DEDUCTED_ID', $USER->GetID());
1823 if ($oldValue ===
'N')
1825 $shipmentStatus = Main\Config\Option::get(
'sale',
'shipment_status_on_shipped',
'');
1829 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
1831 if (strval($shipmentStatus) !=
'' && $this->getField(
'STATUS_ID') != $deliveryStatusClassName::getFinalStatus())
1833 $r = $this->setStatus($shipmentStatus);
1834 if (!$r->isSuccess())
1836 $result->addErrors($r->getErrors());
1844 foreach ($this->getShipmentItemCollection() as $shipmentItem)
1846 $r = $shipmentItem->checkMarkingCodeOnDeducted();
1847 if (!$r->isSuccess())
1849 $result->addErrors($r->getErrors());
1854 Internals\EventsPool::addEvent(
1859 'VALUES' => $this->fields->getOriginalValues()
1863 Cashbox\Internals\Pool::addDoc($this->
getOrder()->getInternalId(), $this);
1865 elseif ($name ===
"STATUS_ID")
1867 $event =
new Main\Event(
1873 'OLD_VALUE' => $oldValue,
1878 Internals\EventsPool::addEvent(
1884 'OLD_VALUE' => $oldValue,
1888 Internals\EventsPool::addEvent(
1894 'OLD_VALUE' => $oldValue,
1898 elseif ($name ===
'RESPONSIBLE_ID')
1900 $this->
setField(
'DATE_RESPONSIBLE_ID',
new Main\
Type\DateTime());
1902 elseif ($name ===
'TRACKING_NUMBER')
1906 Internals\EventsPool::addEvent(
1911 'VALUES' => $this->
getFields()->getOriginalValues(),
1917 $r = parent::onFieldModify($name, $oldValue, $value);
1918 if (!$r->isSuccess())
1920 return $result->addErrors($r->getErrors());
1924 $name ===
'BASE_PRICE_DELIVERY'
1925 && !$this->isMarkedFieldCustom(
'PRICE_DELIVERY')
1928 $value -= $this->getField(
'DISCOUNT_PRICE');
1930 $r = $this->
setField(
'PRICE_DELIVERY', $value);
1931 if (!$r->isSuccess())
1933 $result->addErrors($r->getErrors());
1937 if ($r->hasWarnings())
1939 $result->addWarnings($r->getWarnings());
1942 $result->addData($r->getData());
1944 if ($result->isSuccess())
1965 protected function syncQuantityAfterModify(BasketItem $basketItem, $value =
null, $oldValue =
null)
1967 $result =
new Result();
1976 if ($shipmentItem !==
null)
1978 $shipmentItem->setFieldNoDemand(
'QUANTITY', 0);
1984 if ($shipmentItem ===
null)
1989 $deltaQuantity = $value - $oldValue;
1991 if ($deltaQuantity > 0)
1993 $shipmentItem->setFieldNoDemand(
1995 $shipmentItem->getField(
"QUANTITY") + $deltaQuantity
2003 Internals\Catalog\Provider::tryReserveShipmentItem($shipmentItem);
2008 if (floatval($shipmentItem->getField(
"QUANTITY")) <= 0)
2010 return new Result();
2013 if ($value != 0 && roundEx($shipmentItem->getField(
"QUANTITY"), SALE_VALUE_PRECISION) < roundEx(-$deltaQuantity, SALE_VALUE_PRECISION))
2018 array(
"#NAME#",
"#QUANTITY#",
"#DELTA_QUANTITY#"),
2019 array($basketItem->getField(
"NAME"), $shipmentItem->getField(
"QUANTITY"), abs($deltaQuantity)),
2022 'SALE_SHIPMENT_SYSTEM_QUANTITY_ERROR'
2030 $shipmentItem->setFieldNoDemand(
2032 $shipmentItem->getField(
"QUANTITY") + $deltaQuantity
2040 Internals\Catalog\Provider::tryReserveShipmentItem($shipmentItem);
2054 $params = $this->getField(
'PARAMS');
2055 return isset($params[
"SERVICE_PARAMS"]) ? $params[
"SERVICE_PARAMS"] : array();
2064 $params = $this->getField(
'PARAMS');
2065 $params[
"SERVICE_PARAMS"] = $serviceParams;
2066 $this->
setField(
"PARAMS", $params);
2074 if($this->extraServices ===
null)
2077 Delivery\ExtraServices\Manager::getValuesForShipment(
2100 return Delivery\ExtraServices\Manager::getObjectsForShipment(
2112 return Delivery\ExtraServices\Manager::saveValuesForShipment($this->getId(), $this->
getExtraServices());
2120 if($this->storeId ===
null)
2123 Delivery\ExtraServices\Manager::getStoreIdForShipment(
2145 return Delivery\ExtraServices\Manager::saveStoreIdForShipment($this->getId(), $this->
getDeliveryId(), $this->
getStoreId());
2153 return (
float)$this->getField(
'WEIGHT');
2162 return $this->
setField(
'WEIGHT', $weight);
2181 return Delivery\Services\Manager::calculateDeliveryPrice($this);
2192 $this->
setField(
'BASE_PRICE_DELIVERY', 0);
2203 public function getBasketItemQuantity(
BasketItem $basketItem)
2217 protected function addChangesToHistory($name, $oldValue =
null, $value =
null)
2219 if ($this->getId() > 0 && !$this->
isSystem())
2223 if ($order && $order->getId() > 0)
2228 $orderHistory = $registry->getOrderHistoryClassName();
2229 $orderHistory::addField(
2248 public function isExistBasketItem(BasketItem $basketItem)
2253 throw new Main\ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2265 public function verify()
2267 $result =
new Result();
2274 'SALE_SHIPMENT_DELIVERY_SERVICE_EMPTY'
2285 $r = $shipmentItem->verify();
2286 if (!$r->isSuccess())
2288 $result->addErrors($r->getErrors());
2303 public function setAccountNumber($id)
2305 $result =
new Result();
2310 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_PAYMENT_GENERATE_ACCOUNT_NUMBER_ORDER_NUMBER_WRONG_ID'),
'SALE_PAYMENT_GENERATE_ACCOUNT_NUMBER_ORDER_NUMBER_WRONG_ID'));
2314 $value = Internals\AccountNumberGenerator::generateForShipment($this);
2319 $r = static::updateInternal($id, array(
"ACCOUNT_NUMBER" => $value));
2320 $res = $r->isSuccess(
true);
2322 catch (Main\DB\SqlQueryException $exception)
2341 $providerInstance =
null;
2343 if (is_array($mapping) && isset($mapping[
'PROVIDER_KEY']))
2345 switch ($mapping[
'PROVIDER_KEY'])
2347 case 'SHIPMENT': $providerInstance = $this;
break;
2348 case 'COMPANY' : $providerInstance = $this->getField(
'COMPANY_ID');
break;
2353 $providerInstance = $order->getBusinessValueProviderInstance($mapping);
2358 return $providerInstance;
2369 return $order->getPersonTypeId();
2384 return Internals\ShipmentTable::getList($parameters);
2397 public function createClone(\SplObjectStorage $cloneEntity)
2399 if ($this->
isClone() && $cloneEntity->contains($this))
2401 return $cloneEntity[$this];
2405 $shipmentClone = parent::createClone($cloneEntity);
2424 if (!$cloneEntity->contains(
$service))
2429 if ($cloneEntity->contains(
$service))
2431 $shipmentClone->service = $cloneEntity[
$service];
2435 return $shipmentClone;
2446 protected function setStatus($status)
2450 $result =
new Result();
2454 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
2456 if (is_object($USER) && $USER->isAuthorized())
2458 $statusesList = $deliveryStatusClassName::getAllowedUserStatuses($USER->getID(), $this->getField(
'STATUS_ID'));
2462 $statusesList = $deliveryStatusClassName::getAllStatuses();
2465 if($this->getField(
'STATUS_ID') != $status && array_key_exists($status, $statusesList))
2468 $r = $this->
setField(
'STATUS_ID', $status);
2469 if (!$r->isSuccess())
2471 $result->addErrors($r->getErrors());
2487 $errorsList = static::getAutoFixErrorsList();
2488 if (is_array($errorsList) && in_array($value, $errorsList))
2490 $className = static::getClassName();
2512 $errorsList = static::getAutoFixErrorsList();
2513 if (is_array($errorsList) && in_array($value, $errorsList))
2534 return array_keys(static::getAutoFixRules());
2546 $method = static::getFixMethod($code);
2547 $r = call_user_func_array($method, array($this));
2548 if (!$r->isSuccess())
2550 $result->addErrors($r->getErrors());
2552 elseif ($r->hasWarnings())
2554 $result->addWarnings($r->getWarnings());
2566 $codeList = static::getAutoFixRules();
2568 if (!empty($codeList[$code]))
2570 return $codeList[$code];
2586 if (!$r->isSuccess())
2588 $result->addErrors($r->getErrors());
2590 elseif ($r->hasWarnings())
2592 $result->addWarnings($r->getWarnings());
2608 $r = $entity->
setField(
'DEDUCTED',
'Y');
2609 if (!$r->isSuccess())
2611 if (!$r->isSuccess())
2613 $result->addErrors($r->getErrors());
2617 $r = $entity->tryShip();
2618 if (!$r->isSuccess())
2620 if (!$r->isSuccess())
2622 $result->addErrors($r->getErrors());
2635 'PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2636 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2637 'PROVIDER_UNRESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2638 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_QUANTITY_NOT_ENOUGH' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2640 'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2641 'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2642 'DDCT_DEDUCTION_QUANTITY_STORE_ERROR' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2643 'SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2644 'DDCT_DEDUCTION_QUANTITY_ERROR' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2672 if (parent::isChanged())
2677 return $this->getShipmentItemCollection()->isChanged();
2683 public function clearChanged()
2685 parent::clearChanged();
2692 $shipmentItem->clearChanged();
2712 if (!Main\Loader::includeModule(
'catalog'))
2723 $dbRes = VatTable::getById($vatId);
2724 $vatInfo = $dbRes->fetch();
2727 $vatRate = $vatInfo[
'RATE'] / 100;
2742 $price = $this->
getPrice() * $vatRate / (1 + $vatRate);
2754 return Internals\ShipmentTable::add($data);
2765 return Internals\ShipmentTable::update($primary, $data);
2776 return Internals\ShipmentTable::deleteWithItems($primary);
2784 return Internals\ShipmentTable::getMap();
2792 return Internals\ShipmentTable::getUfId();
2808 if ($custom ===
true)
2810 $this->markFieldCustom(
'PRICE_DELIVERY');
2813 $r = $this->
setField(
'BASE_PRICE_DELIVERY', $value);
2814 if (!$r->isSuccess())
2816 $result->addErrors($r->getErrors());
2829 return 'SaleShipment';
2839 $result = parent::toArray();
2841 $result[
'ITEMS'] = $this->getShipmentItemCollection()->toArray();
2848 if(empty($this->propertyCollection))
2850 $this->propertyCollection = $this->loadPropertyCollection();
2860 $propertyCollectionClassName = $registry->getShipmentPropertyValueCollectionClassName();
2862 return $propertyCollectionClassName::load($this);