11use \Bitrix\Sale\Delivery\Requests;
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,
1202 return $order->getId();
1219 public function getShipmentItemCollection()
1221 if (empty($this->shipmentItemCollection))
1226 $itemCollectionClassName = $registry->getShipmentItemCollectionClassName();
1227 $this->shipmentItemCollection = $itemCollectionClassName::load($this);
1253 $shipment = static::create(
$collection, $deliveryService);
1254 $shipment->markSystem();
1256 if ($deliveryService ===
null)
1258 $shipment->setFieldNoDemand(
'DELIVERY_ID',
Delivery\Services\Manager::getEmptyDeliveryServiceId());
1269 return (
float)$this->getField(
'PRICE_DELIVERY');
1278 return $this->isMarkedFieldCustom(
'PRICE_DELIVERY');
1284 $name ===
'BASE_PRICE_DELIVERY'
1285 || $name ===
'PRICE_DELIVERY'
1286 || $name ===
'DISCOUNT_PRICE'
1295 return (
string)$this->getField(
'CURRENCY');
1303 return (
int)$this->getField(
'DELIVERY_ID');
1311 return (
string)$this->getField(
'DELIVERY_NAME');
1319 $this->
setField(
'ORDER_ID', $orderId);
1329 if ($this->service ===
null)
1346 return Delivery\Services\Manager::getObjectById($deliveryId);
1358 return $this->getField(
'SYSTEM') ===
'Y';
1364 return $this->getField(
'CANCELED') ===
'Y';
1372 return $this->getField(
'DEDUCTED') ===
'Y';
1380 return $this->getField(
'DATE_DEDUCTED');
1388 return $this->getField(
'EMP_DEDUCTED_ID');
1396 return (
string)$this->getField(
'REASON_UNDO_DEDUCTED');
1404 return $this->getField(
'MARKED') ===
"Y";
1413 public function isReserved()
1416 foreach ($this->getShipmentItemCollection() as $shipmentItem)
1418 if ($shipmentItem->getReservedQuantity() !== $shipmentItem->getQuantity())
1432 return $this->getField(
'ALLOW_DELIVERY') ===
"Y";
1440 return $this->getShipmentItemCollection()->isEmpty();
1448 return $this->getField(
'DATE_ALLOW_DELIVERY');
1456 return (
int)$this->getField(
'EMP_ALLOW_DELIVERY_ID');
1464 return (
int)$this->getField(
'COMPANY_ID');
1475 return Internals\Catalog\Provider::tryReserveShipment($this);
1485 return Internals\Catalog\Provider::tryUnreserveShipment($this);
1493 public function tryShip()
1498 $r = Internals\Catalog\Provider::tryShipShipment($this);
1499 if ($r->isSuccess())
1501 $resultList = $r->getData();
1503 if (!empty($resultList) && is_array($resultList))
1506 foreach ($resultList as $resultDat)
1508 if (!$resultDat->isSuccess())
1510 $result->addErrors( $resultDat->getErrors() );
1517 $result->addErrors( $r->getErrors() );
1520 if ($r->hasWarnings())
1522 $result->addWarnings( $r->getWarnings() );
1534 return $this->tryShip();
1542 if ($this->fields->isChanged(
'DEDUCTED'))
1544 if ($this->getField(
'DEDUCTED') ===
"Y")
1548 elseif ($this->getField(
'DEDUCTED') ===
"N" && $this->getId() != 0)
1563 $result = Internals\Catalog\Provider::deliver($this);
1564 if ($result->isSuccess())
1579 return $this->
setField(
'ALLOW_DELIVERY',
"Y");
1589 return $this->
setField(
'ALLOW_DELIVERY',
"N");
1608 if (!$r->isSuccess())
1610 return $result->addErrors($r->getErrors());
1615 return $this->syncQuantityAfterModify($basketItem);
1635 public function onBasketModify($action,
BasketItem $basketItem, $name =
null, $oldValue =
null, $value =
null)
1646 return $this->getShipmentItemCollection()->onBasketModify($action, $basketItem, $name, $oldValue, $value);
1650 if ($name ===
"QUANTITY")
1654 return $this->syncQuantityAfterModify($basketItem, $value, $oldValue);
1661 if ($r->isSuccess())
1666 if ($deliveryCalculate->isSuccess())
1668 $this->
setField(
'BASE_PRICE_DELIVERY', $deliveryCalculate->getPrice());
1672 $result->addWarnings($deliveryCalculate->getErrors());
1678 $result->addErrors($r->getErrors());
1681 elseif ($name ===
'WEIGHT')
1683 if (!$this->isMarkedFieldCustom(
'WEIGHT'))
1685 if ($this->getShipmentItemCollection()->isExistBasketItem($basketItem))
1691 elseif ($name ===
'PRICE')
1695 if ($this->getShipmentItemCollection()->isExistBasketItem($basketItem))
1698 if ($r->isSuccess())
1700 $this->
setField(
'BASE_PRICE_DELIVERY', $r->getPrice());
1704 $result->addErrors($r->getErrors());
1730 $result =
new Result();
1732 if ($name ===
'DELIVERY_ID')
1737 $this->service ===
null
1738 || $this->service->getId() !== (
int)$value
1742 $service = Delivery\Services\Manager::getObjectById($value);
1747 $this->
setField(
'DELIVERY_NAME', $this->service->getName());
1753 elseif ($name ===
"MARKED")
1755 if ($oldValue !=
"Y")
1757 $this->
setField(
'DATE_MARKED',
new Main\
Type\DateTime());
1759 if (is_object($USER))
1761 $this->
setField(
'EMP_MARKED_ID', $USER->GetID());
1764 elseif ($value ===
"N")
1766 $this->
setField(
'REASON_MARKED',
'');
1769 elseif ($name ===
"ALLOW_DELIVERY")
1771 $this->
setField(
'DATE_ALLOW_DELIVERY',
new Main\
Type\DateTime());
1773 if (is_object($USER))
1775 $this->
setField(
'EMP_ALLOW_DELIVERY_ID', $USER->GetID());
1778 if ($oldValue ===
'N')
1780 $shipmentStatus = Main\Config\Option::get(
'sale',
'shipment_status_on_allow_delivery',
'');
1784 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
1787 $shipmentStatus !==
''
1788 && $this->getField(
'STATUS_ID') != $deliveryStatusClassName::getFinalStatus()
1791 $r = $this->setStatus($shipmentStatus);
1792 if (!$r->isSuccess())
1794 $result->addErrors($r->getErrors());
1799 Internals\EventsPool::addEvent(
1804 'VALUES' => $this->fields->getOriginalValues()
1808 elseif ($name ===
"DEDUCTED")
1810 $this->
setField(
'DATE_DEDUCTED',
new Main\
Type\DateTime());
1812 if (is_object($USER))
1814 $this->
setField(
'EMP_DEDUCTED_ID', $USER->GetID());
1817 if ($oldValue ===
'N')
1819 $shipmentStatus = Main\Config\Option::get(
'sale',
'shipment_status_on_shipped',
'');
1823 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
1825 if (strval($shipmentStatus) !=
'' && $this->getField(
'STATUS_ID') != $deliveryStatusClassName::getFinalStatus())
1827 $r = $this->setStatus($shipmentStatus);
1828 if (!$r->isSuccess())
1830 $result->addErrors($r->getErrors());
1838 foreach ($this->getShipmentItemCollection() as $shipmentItem)
1840 $r = $shipmentItem->checkMarkingCodeOnDeducted();
1841 if (!$r->isSuccess())
1843 $result->addErrors($r->getErrors());
1848 Internals\EventsPool::addEvent(
1853 'VALUES' => $this->fields->getOriginalValues()
1857 Cashbox\Internals\Pool::addDoc($this->
getOrder()->getInternalId(), $this);
1859 elseif ($name ===
"STATUS_ID")
1861 $event =
new Main\Event(
1867 'OLD_VALUE' => $oldValue,
1872 Internals\EventsPool::addEvent(
1878 'OLD_VALUE' => $oldValue,
1882 Internals\EventsPool::addEvent(
1888 'OLD_VALUE' => $oldValue,
1892 elseif ($name ===
'RESPONSIBLE_ID')
1894 $this->
setField(
'DATE_RESPONSIBLE_ID',
new Main\
Type\DateTime());
1896 elseif ($name ===
'TRACKING_NUMBER')
1900 Internals\EventsPool::addEvent(
1905 'VALUES' => $this->
getFields()->getOriginalValues(),
1911 $r = parent::onFieldModify($name, $oldValue, $value);
1912 if (!$r->isSuccess())
1914 return $result->addErrors($r->getErrors());
1918 $name ===
'BASE_PRICE_DELIVERY'
1919 && !$this->isMarkedFieldCustom(
'PRICE_DELIVERY')
1922 $value -= $this->getField(
'DISCOUNT_PRICE');
1924 $r = $this->
setField(
'PRICE_DELIVERY', $value);
1925 if (!$r->isSuccess())
1927 $result->addErrors($r->getErrors());
1931 if ($r->hasWarnings())
1933 $result->addWarnings($r->getWarnings());
1936 $result->addData($r->getData());
1938 if ($result->isSuccess())
1959 protected function syncQuantityAfterModify(BasketItem $basketItem, $value =
null, $oldValue =
null)
1961 $result =
new Result();
1970 if ($shipmentItem !==
null)
1972 $shipmentItem->setFieldNoDemand(
'QUANTITY', 0);
1978 if ($shipmentItem ===
null)
1983 $deltaQuantity = $value - $oldValue;
1985 if ($deltaQuantity > 0)
1987 $shipmentItem->setFieldNoDemand(
1989 $shipmentItem->getField(
"QUANTITY") + $deltaQuantity
1997 Internals\Catalog\Provider::tryReserveShipmentItem($shipmentItem);
2002 if (floatval($shipmentItem->getField(
"QUANTITY")) <= 0)
2004 return new Result();
2007 if ($value != 0 && roundEx($shipmentItem->getField(
"QUANTITY"), SALE_VALUE_PRECISION) < roundEx(-$deltaQuantity, SALE_VALUE_PRECISION))
2012 array(
"#NAME#",
"#QUANTITY#",
"#DELTA_QUANTITY#"),
2013 array($basketItem->getField(
"NAME"), $shipmentItem->getField(
"QUANTITY"), abs($deltaQuantity)),
2016 'SALE_SHIPMENT_SYSTEM_QUANTITY_ERROR'
2024 $shipmentItem->setFieldNoDemand(
2026 $shipmentItem->getField(
"QUANTITY") + $deltaQuantity
2034 Internals\Catalog\Provider::tryReserveShipmentItem($shipmentItem);
2048 $params = $this->getField(
'PARAMS');
2049 return isset($params[
"SERVICE_PARAMS"]) ? $params[
"SERVICE_PARAMS"] : array();
2058 $params = $this->getField(
'PARAMS');
2059 $params[
"SERVICE_PARAMS"] = $serviceParams;
2060 $this->
setField(
"PARAMS", $params);
2068 if($this->extraServices ===
null)
2071 Delivery\ExtraServices\Manager::getValuesForShipment(
2094 return Delivery\ExtraServices\Manager::getObjectsForShipment(
2106 return Delivery\ExtraServices\Manager::saveValuesForShipment($this->getId(), $this->
getExtraServices());
2114 if($this->storeId ===
null)
2117 Delivery\ExtraServices\Manager::getStoreIdForShipment(
2139 return Delivery\ExtraServices\Manager::saveStoreIdForShipment($this->getId(), $this->
getDeliveryId(), $this->
getStoreId());
2147 return (
float)$this->getField(
'WEIGHT');
2156 return $this->
setField(
'WEIGHT', $weight);
2175 return Delivery\Services\Manager::calculateDeliveryPrice($this);
2186 $this->
setField(
'BASE_PRICE_DELIVERY', 0);
2197 public function getBasketItemQuantity(
BasketItem $basketItem)
2211 protected function addChangesToHistory($name, $oldValue =
null, $value =
null)
2213 if ($this->getId() > 0 && !$this->
isSystem())
2217 if ($order && $order->getId() > 0)
2222 $orderHistory = $registry->getOrderHistoryClassName();
2223 $orderHistory::addField(
2242 public function isExistBasketItem(BasketItem $basketItem)
2247 throw new Main\ObjectNotFoundException(
'Entity "ShipmentItemCollection" not found');
2259 public function verify()
2261 $result =
new Result();
2268 'SALE_SHIPMENT_DELIVERY_SERVICE_EMPTY'
2279 $r = $shipmentItem->verify();
2280 if (!$r->isSuccess())
2282 $result->addErrors($r->getErrors());
2297 public function setAccountNumber($id)
2299 $result =
new Result();
2304 $result->addError(
new ResultError(
Loc::getMessage(
'SALE_PAYMENT_GENERATE_ACCOUNT_NUMBER_ORDER_NUMBER_WRONG_ID'),
'SALE_PAYMENT_GENERATE_ACCOUNT_NUMBER_ORDER_NUMBER_WRONG_ID'));
2308 $value = Internals\AccountNumberGenerator::generateForShipment($this);
2313 $r = static::updateInternal($id, array(
"ACCOUNT_NUMBER" => $value));
2314 $res = $r->isSuccess(
true);
2316 catch (Main\DB\SqlQueryException $exception)
2335 $providerInstance =
null;
2337 if (is_array($mapping) && isset($mapping[
'PROVIDER_KEY']))
2339 switch ($mapping[
'PROVIDER_KEY'])
2341 case 'SHIPMENT': $providerInstance = $this;
break;
2342 case 'COMPANY' : $providerInstance = $this->getField(
'COMPANY_ID');
break;
2347 $providerInstance = $order->getBusinessValueProviderInstance($mapping);
2352 return $providerInstance;
2363 return $order->getPersonTypeId();
2378 return Internals\ShipmentTable::getList($parameters);
2391 public function createClone(\SplObjectStorage $cloneEntity)
2393 if ($this->
isClone() && $cloneEntity->contains($this))
2395 return $cloneEntity[$this];
2399 $shipmentClone = parent::createClone($cloneEntity);
2418 if (!$cloneEntity->contains(
$service))
2423 if ($cloneEntity->contains(
$service))
2425 $shipmentClone->service = $cloneEntity[
$service];
2429 return $shipmentClone;
2440 protected function setStatus($status)
2444 $result =
new Result();
2448 $deliveryStatusClassName = $registry->getDeliveryStatusClassName();
2450 if (is_object($USER) && $USER->isAuthorized())
2452 $statusesList = $deliveryStatusClassName::getAllowedUserStatuses($USER->getID(), $this->getField(
'STATUS_ID'));
2456 $statusesList = $deliveryStatusClassName::getAllStatuses();
2459 if($this->getField(
'STATUS_ID') != $status && array_key_exists($status, $statusesList))
2462 $r = $this->
setField(
'STATUS_ID', $status);
2463 if (!$r->isSuccess())
2465 $result->addErrors($r->getErrors());
2481 $errorsList = static::getAutoFixErrorsList();
2482 if (is_array($errorsList) && in_array($value, $errorsList))
2484 $className = static::getClassName();
2506 $errorsList = static::getAutoFixErrorsList();
2507 if (is_array($errorsList) && in_array($value, $errorsList))
2528 return array_keys(static::getAutoFixRules());
2540 $method = static::getFixMethod($code);
2541 $r = call_user_func_array($method, array($this));
2542 if (!$r->isSuccess())
2544 $result->addErrors($r->getErrors());
2546 elseif ($r->hasWarnings())
2548 $result->addWarnings($r->getWarnings());
2560 $codeList = static::getAutoFixRules();
2562 if (!empty($codeList[$code]))
2564 return $codeList[$code];
2580 if (!$r->isSuccess())
2582 $result->addErrors($r->getErrors());
2584 elseif ($r->hasWarnings())
2586 $result->addWarnings($r->getWarnings());
2602 $r = $entity->
setField(
'DEDUCTED',
'Y');
2603 if (!$r->isSuccess())
2605 if (!$r->isSuccess())
2607 $result->addErrors($r->getErrors());
2611 $r = $entity->tryShip();
2612 if (!$r->isSuccess())
2614 if (!$r->isSuccess())
2616 $result->addErrors($r->getErrors());
2629 'PROVIDER_BASKET_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2630 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2631 'PROVIDER_UNRESERVE_SHIPMENT_ITEM_WRONG_AVAILABLE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2632 'SALE_PROVIDER_RESERVE_SHIPMENT_ITEM_QUANTITY_NOT_ENOUGH' => [
'\Bitrix\Sale\Shipment',
"fixReserveErrors"],
2634 'SALE_PROVIDER_SHIPMENT_SHIPPED_LESS_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2635 'SALE_PROVIDER_SHIPMENT_SHIPPED_MORE_QUANTITY' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2636 'DDCT_DEDUCTION_QUANTITY_STORE_ERROR' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2637 'SALE_PROVIDER_SHIPMENT_QUANTITY_NOT_ENOUGH' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2638 'DDCT_DEDUCTION_QUANTITY_ERROR' => [
'\Bitrix\Sale\Shipment',
"fixShipErrors"],
2666 if (parent::isChanged())
2671 return $this->getShipmentItemCollection()->isChanged();
2677 public function clearChanged()
2679 parent::clearChanged();
2686 $shipmentItem->clearChanged();
2706 if (!Main\Loader::includeModule(
'catalog'))
2717 $dbRes = VatTable::getById($vatId);
2718 $vatInfo = $dbRes->fetch();
2721 $vatRate = $vatInfo[
'RATE'] / 100;
2736 $price = $this->
getPrice() * $vatRate / (1 + $vatRate);
2748 return Internals\ShipmentTable::add($data);
2759 return Internals\ShipmentTable::update($primary, $data);
2770 return Internals\ShipmentTable::deleteWithItems($primary);
2778 return Internals\ShipmentTable::getMap();
2786 return Internals\ShipmentTable::getUfId();
2802 if ($custom ===
true)
2804 $this->markFieldCustom(
'PRICE_DELIVERY');
2807 $r = $this->
setField(
'BASE_PRICE_DELIVERY', $value);
2808 if (!$r->isSuccess())
2810 $result->addErrors($r->getErrors());
2823 return 'SaleShipment';
2833 $result = parent::toArray();
2835 $result[
'ITEMS'] = $this->getShipmentItemCollection()->toArray();
2842 if(empty($this->propertyCollection))
2844 $this->propertyCollection = $this->loadPropertyCollection();
2854 $propertyCollectionClassName = $registry->getShipmentPropertyValueCollectionClassName();
2856 return $propertyCollectionClassName::load($this);
static update($id, array $data)
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
static isEnableAutomaticReservation()
static getProductReservationCondition()
static getEmptyDeliveryServiceId()
const EVENT_ON_SHIPMENT_DEDUCTED
const EVENT_ON_BEFORE_SHIPMENT_RESERVE
const EVENT_ON_BEFORE_SHIPMENT_STATUS_CHANGE
const EVENT_ON_SHIPMENT_TRACKING_NUMBER_CHANGE
const EVENT_ON_SHIPMENT_ALLOW_DELIVERY
const EVENT_ON_SHIPMENT_STATUS_CHANGE_SEND_MAIL
const EVENT_ON_SHIPMENT_RESERVED
const EVENT_ON_SHIPMENT_STATUS_CHANGE
const ACTION_ENTITY_SHIPMENT_COLLECTION_RESERVED_QUANTITY
onFieldModify($name, $oldValue, $value)
static roundPrecision($value)
static repeat(Order $order, array $resultList)
static getInstance($type)
const REGISTRY_TYPE_ORDER
setDeliveryService(Delivery\Services\Base $service)
setBasePriceDelivery($value, $custom=false)
static deleteNoDemand($orderId)
getExtraServicesObjects()
static getRegistryEntity()
updateInternal($primary, array $data)
setServiceParams(array $serviceParams)
checkValueBeforeSet($name, $value)
static getFixMethod($code)
getBusinessValueProviderInstance($mapping)
static getMeaningfulFields()
static getParametersForLoad($id)
static getCustomizableFields()
onBeforeBasketItemDelete(BasketItem $basketItem)
static getEntityEventName()
static createSystem(ShipmentCollection $collection, Delivery\Services\Base $deliveryService=null)
static getAvailableFields()
static fixReserveErrors(Shipment $entity)
static getList(array $parameters)
onBeforeSetFields(array $values)
isPriceField(string $name)
static fixShipErrors(Shipment $entity)
setFieldNoDemand($name, $value)
static deleteInternal($primary)
setDeliveryRequestMarker()
normalizeValue($name, $value)
setExtraServices(array $extraServices)
onShipmentItemCollectionModify($action, ShipmentItem $shipmentItem, $name=null, $oldValue=null, $value=null)
setFieldNoDemand($name, $value)