- Модуль: sale
- Путь к файлу: ~/bitrix/modules/sale/lib/compatible/ordercompatibility.php
- Класс: BitrixSaleCompatibleOrderCompatibility
- Вызов: OrderCompatibility::fillShipmentCollectionFromRequest
public function fillShipmentCollectionFromRequest(SaleShipmentCollection $shipmentCollection, array $fields)
{
$result = new SaleResult();
/** @var SaleOrder $order */
if (!$order = $shipmentCollection->getOrder())
{
throw new MainObjectNotFoundException('Entity "Order" not found');
}
$shipment = null;
$deliveryId = null;
$deliveryCode = isset($fields['DELIVERY_ID']) && strval(trim($fields['DELIVERY_ID'])) != '' ? trim($fields['DELIVERY_ID']) : null;
if (strval(trim($deliveryCode)) != '')
{
$deliveryId = CSaleDelivery::getIdByCode($deliveryCode);
}
if ($order->getId() > 0)
{
//todo: check $deliveryId
if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment())
{
/** @var SaleShipment $shipment */
foreach($shipmentCollection as $shipment)
{
if ($shipment->isSystem())
continue;
if ($deliveryId > 0 && $deliveryId != $shipment->getDeliveryId())
{
/** @var SaleResult $r */
$r = $shipment->setField('DELIVERY_ID', $deliveryId);
if ($r->isSuccess())
{
/** @var ServicesBase $deliveryService */
$deliveryService = SaleDeliveryServicesManager::getObjectById($deliveryId);
if ($deliveryService)
{
$fields['DELIVERY_NAME'] = $deliveryService->isProfile()
? $deliveryService->getNameWithParent()
: $deliveryService->getName();
}
else
{
$fields['DELIVERY_NAME'] = 'Not found [' . $deliveryId . ']';
}
}
else
{
$result->addErrors($r->getErrors());
}
}
elseif (intval($deliveryId) == 0 && array_key_exists('DELIVERY_ID', $fields) || (intval($deliveryId) !== intval($deliveryCode)))
{
unset($fields['DELIVERY_ID']);
}
if (array_key_exists('PRICE_DELIVERY', $fields) && (float)$fields['PRICE_DELIVERY'] != $shipment->getField('PRICE_DELIVERY'))
{
$fields['BASE_PRICE_DELIVERY'] = (float)$fields['PRICE_DELIVERY'];
$fields['CUSTOM_PRICE_DELIVERY'] = "Y";
unset($fields['PRICE_DELIVERY']);
}
$shipmentFields = static::convertDateFields($fields, static::getEntityDateFields($shipment));
unset($shipmentFields['ALLOW_DELIVERY']);
unset($shipmentFields['DEDUCTED']);
if ($fields['CURRENCY'] != $shipmentFields['CURRENCY'])
{
$shipmentFields['CURRENCY'] = $fields['CURRENCY'];
}
/** @var SaleResult $r */
$r = $shipment->setFields(static::clearFields($shipmentFields, static::getShipmentAvailableFields()));
if ($r->isSuccess())
{
static::fillOrderFieldsFromEntity($order, $shipment, $fields, static::getShipmentFieldsToConvert());
}
else
{
$result->addErrors($r->getErrors());
}
if ($shipment !== null)
{
DiscountCompatibility::setShipment($order->getId(), $shipment->getId());
}
unset($fields['DELIVERY_ID']);
}
}
}
else
{
if (intval($deliveryId) == 0)
{
$deliveryId = BitrixSaleDeliveryServicesEmptyDeliveryService::getEmptyDeliveryServiceId();
}
if (intval($deliveryId) > 0)
{
/** @var SaleShipment $shipment */
if ($shipment = static::createShipmentFromRequest($shipmentCollection, $deliveryId, $fields))
{
if (isset($fields['TRACKING_NUMBER']) && strval($fields['TRACKING_NUMBER']) != '')
{
$shipment->setField('TRACKING_NUMBER', $fields['TRACKING_NUMBER']);
}
if (isset($fields['DELIVERY_EXTRA_SERVICES']) && is_array($fields['DELIVERY_EXTRA_SERVICES']))
{
$shipment->setExtraServices($fields['DELIVERY_EXTRA_SERVICES']);
}
if (isset($fields['STORE_ID']) && intval($fields['STORE_ID']) > 0)
{
$shipment->setStoreId($fields['STORE_ID']);
}
if ($shipment !== null)
{
DiscountCompatibility::setShipment($order->getId(), $shipment->getId());
}
static::fillOrderFieldsFromEntity($order, $shipment, $fields, static::getShipmentFieldsToConvert());
}
}
}
if ($basket = $order->getBasket())
{
/** @var BasketCompatibility $basketCompatibilityClassName */
$basketCompatibilityClassName = static::getBasketCompatibilityClassName();
/** @var SaleResult $r */
$r = $basketCompatibilityClassName::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
if (!$r->isSuccess())
{
$result->addErrors($r->getErrors());
return $result;
}
}
/** @var SaleResult $r */
$r = static::syncShipmentCollectionFromRequest($shipmentCollection, $fields);
if (!$r->isSuccess())
{
$result->addErrors($r->getErrors());
return $result;
}
if ($basket)
{
/** @var SaleShipment $shipment */
foreach ($shipmentCollection as $shipment)
{
if ($shipment->isSystem())
continue;
/** @var SaleShipmentItemCollection $shipmentItemCollection */
if (!$shipmentItemCollection = $shipment->getShipmentItemCollection())
{
throw new MainObjectNotFoundException('Entity "ShipmentItemCollection" not found');
}
if (!empty($fields['BARCODE_LIST']) && is_array($fields['BARCODE_LIST']))
{
/** @var SaleResult $r */
$r = static::fillShipmentItemCollectionFromRequest($shipmentItemCollection, $fields['BARCODE_LIST'], $basket);
if (!$r->isSuccess())
{
$result->addErrors($r->getErrors());
return $result;
}
}
}
}
return $result;
}