예제 #1
0
 /**
  * Filling the shipment collection  data from request
  *
  * @internal
  *
  * @param Sale\ShipmentCollection $shipmentCollection	Entity shipment collection.
  * @param array $fields									An array of request data.
  * @param Sale\ShipmentCollection $shipmentCollection
  * @param array $fields
  *
  * @return Sale\Result
  * @throws Main\ArgumentNullException
  * @throws Main\ArgumentOutOfRangeException
  * @throws Main\NotSupportedException
  * @throws Main\ObjectNotFoundException
  */
 public function fillShipmentCollectionFromRequest(Sale\ShipmentCollection $shipmentCollection, array $fields)
 {
     $result = new Sale\Result();
     /** @var Sale\Order $order */
     if (!($order = $shipmentCollection->getOrder())) {
         throw new Main\ObjectNotFoundException('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 = \Bitrix\Sale\Delivery\Services\Table::getIdByCode($deliveryCode);
     }
     if ($order->getId() > 0) {
         //todo: check $deliveryId
         if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment() && $deliveryId > 0) {
             /** @var Sale\Shipment $shipment */
             foreach ($shipmentCollection as $shipment) {
                 if ($shipment->isSystem()) {
                     continue;
                 }
                 unset($fields['DELIVERY_ID']);
                 if ($deliveryId != $shipment->getDeliveryId()) {
                     /** @var Sale\Result $r */
                     $r = $shipment->setField('DELIVERY_ID', $deliveryId);
                     if (!$r->isSuccess()) {
                         $result->addErrors($r->getErrors());
                     }
                 }
                 if (!empty($fields['PRICE_DELIVERY']) && (double) $fields['PRICE_DELIVERY'] != $shipment->getField('PRICE_DELIVERY')) {
                     $fields['BASE_PRICE_DELIVERY'] = (double) $fields['PRICE_DELIVERY'] + (!empty($fields['PRICE_DELIVERY']) ? floatval($fields['PRICE_DELIVERY']) : 0);
                     unset($fields['PRICE_DELIVERY']);
                 }
                 $shipmentFields = static::convertDateFields($fields, static::getShipmentDateFields());
                 unset($shipmentFields['ALLOW_DELIVERY']);
                 unset($shipmentFields['DEDUCTED']);
                 /** @var Sale\Result $r */
                 $r = $shipment->setFields(static::clearFields($shipmentFields, static::getShipmentAvailableFields()));
                 if ($r->isSuccess()) {
                     $orderFields = array();
                     foreach (static::getShipmentFieldsToConvert() as $checkField) {
                         $checkOrderField = $order->getField($checkField);
                         $isDate = false;
                         if (array_key_exists($checkField, static::getShipmentDateFields())) {
                             $isDate = true;
                             $checkOrderField = static::convertDateFieldToOldFormat($order->getField($checkField));
                         }
                         if (!empty($fields[$checkField]) && $checkOrderField != trim($fields[$checkField])) {
                             $setValue = $shipment->getField($checkField);
                             if ($isDate) {
                                 $setValue = static::convertDateField($checkOrderField, $shipment->getField($checkField), static::getShipmentDateFields());
                             }
                             if (in_array($checkField, $this->getAvailableFields())) {
                                 $order->setFieldNoDemand($checkField, $setValue);
                             }
                         }
                     }
                 } else {
                     $result->addErrors($r->getErrors());
                 }
                 if ($shipment !== null) {
                     DiscountCompatibility::setShipment($order->getId(), $shipment->getId());
                 }
             }
         }
     } else {
         if (intval($deliveryId) == 0) {
             $deliveryId = \Bitrix\Sale\Delivery\Services\EmptyDeliveryService::getEmptyDeliveryServiceId();
         }
         if (intval($deliveryId) > 0) {
             /** @var Sale\Shipment $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());
                 }
             }
         }
     }
     if ($basket = $order->getBasket()) {
         /** @var Sale\Result $r */
         $r = BasketCompatibility::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
             return $result;
         }
     }
     /** @var Sale\Result $r */
     $r = static::syncShipmentCollectionFromRequest($shipmentCollection, $fields);
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
         return $result;
     }
     if ($basket) {
         /** @var Sale\Shipment $shipment */
         foreach ($shipmentCollection as $shipment) {
             if ($shipment->isSystem()) {
                 continue;
             }
             /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
             if (!($shipmentItemCollection = $shipment->getShipmentItemCollection())) {
                 throw new Main\ObjectNotFoundException('Entity "ShipmentItemCollection" not found');
             }
             if (!empty($fields['BARCODE_LIST']) && is_array($fields['BARCODE_LIST'])) {
                 /** @var Sale\Result $r */
                 $r = static::fillShipmentItemCollectionFromRequest($shipmentItemCollection, $fields['BARCODE_LIST'], $basket);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
             }
         }
     }
     return $result;
 }