/**
  * Update data element baskets
  *
  * @param $id
  * @param array $fields
  *
  * @return Sale\Result
  * @throws Main\ArgumentNullException
  * @throws Main\ArgumentTypeException
  * @throws Main\ObjectNotFoundException
  */
 public static function update($id, array $fields)
 {
     $result = new Sale\Result();
     $item = null;
     $basket = null;
     $order = null;
     $orderId = null;
     foreach (GetModuleEvents("sale", "OnBeforeBasketUpdateAfterCheck", true) as $event) {
         if (ExecuteModuleEventEx($event, array($id, &$fields)) === false) {
             return false;
         }
     }
     /** @var Sale\Result $itemResult */
     $itemResult = static::loadEntityFromBasket($id);
     if ($itemResult->isSuccess()) {
         $itemResultData = $itemResult->getData();
         if (isset($itemResultData['BASKET_ITEM'])) {
             /** @var Sale\BasketItem $item */
             $item = $itemResultData['BASKET_ITEM'];
         }
         if (isset($itemResultData['ORDER'])) {
             $order = $itemResultData['ORDER'];
         }
     }
     if (!$item) {
         $result->addError(new Sale\ResultError(Main\Localization\Loc::getMessage('SALE_BASKET_COMPATIBLE_BASKET_ITEM_NOT_FOUND'), 'BASKET_ITEM_NOT_FOUND'));
         return $result;
     }
     if (!empty($fields["PROPS"]) && is_array($fields["PROPS"])) {
         /** @var Sale\BasketPropertiesCollection $property */
         $property = $item->getPropertyCollection();
         $property->setProperty($fields["PROPS"]);
     }
     if ($order !== null && isset($fields['PRICE'])) {
         if ($fields['PRICE'] != $item->getPrice()) {
             $fields['CUSTOM_PRICE'] = 'Y';
         }
     }
     $r = $item->setFields(static::clearFields($fields));
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     if (!DiscountCompatibility::isInited()) {
         DiscountCompatibility::init();
     }
     if (DiscountCompatibility::usedByClient()) {
         if (isset($fields['BASE_PRICE']) && isset($fields['CURRENCY'])) {
             DiscountCompatibility::setBasketItemBasePrice($id, $fields['BASE_PRICE'], $fields['CURRENCY']);
         }
         if (!empty($fields['DISCOUNT_LIST'])) {
             DiscountCompatibility::setBasketItemDiscounts($id, $fields['DISCOUNT_LIST']);
         }
         DiscountCompatibility::setBasketCode($id, $item->getBasketCode());
     }
     if ($order === null && !empty($fields['ORDER_ID']) && intval($fields['ORDER_ID']) > 0) {
         $orderId = intval($fields['ORDER_ID']);
         /** @var Sale\Order $order */
         if ($order = Sale\Order::load($orderId)) {
             /** @var Sale\Basket $basket */
             if ($basket = $order->getBasket()) {
                 $basket->addItem($item);
                 /** @var Sale\ShipmentCollection $shipmentCollection */
                 if (!($shipmentCollection = $order->getShipmentCollection())) {
                     throw new Main\ObjectNotFoundException('Entity "ShipmentCollection" not found');
                 }
                 OrderCompatibility::createShipmentFromShipmentSystem($shipmentCollection);
                 /** @var Sale\Result $r */
                 $r = static::syncShipmentAndBasketItem($shipmentCollection, $item);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 /** @var Sale\Result $r */
                 $r = static::syncShipmentCollectionAndBasket($shipmentCollection, $basket);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 $r = $order->refreshData();
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
                 $r = $order->doFinalAction(true);
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                     return $result;
                 }
             }
         }
     }
     if ($order !== null) {
         /** @var Sale\ShipmentCollection $shipmentCollection */
         if ($shipmentCollection = $order->getShipmentCollection()) {
             if (count($shipmentCollection) == 2 && $shipmentCollection->isExistsSystemShipment()) {
                 /** @var Sale\Shipment $shipment */
                 foreach ($shipmentCollection as $shipment) {
                     if ($shipment->isSystem()) {
                         continue;
                     }
                     /** @var Sale\Shipment $systemShipment */
                     $systemShipment = $shipmentCollection->getSystemShipment();
                     /** @var Sale\ShipmentItemCollection $systemShipmentItemCollection */
                     $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
                     /** @var Sale\ShipmentItem $systemShipmentItem */
                     if (!($systemShipmentItem = $systemShipmentItemCollection->getItemByBasketCode($item->getBasketCode()))) {
                         continue;
                     }
                     /** @var Sale\ShipmentItemCollection $shipmentItemCollection */
                     if (!($shipmentItemCollection = $shipment->getShipmentItemCollection())) {
                         continue;
                     }
                     /** @var Sale\ShipmentItem $shipmentItem */
                     if (!($shipmentItem = $shipmentItemCollection->getItemByBasketCode($item->getBasketCode()))) {
                         continue;
                     }
                     if ($systemShipmentItem->getQuantity() > 0) {
                         $r = $shipmentItem->setQuantity($shipmentItem->getQuantity() + $systemShipmentItem->getQuantity());
                         if (!$r->isSuccess()) {
                             $result->addErrors($r->getErrors());
                         }
                     }
                 }
             }
         }
         if (!$result->isSuccess()) {
             return $result;
         }
         $r = $order->save();
     } else {
         if (!$result->isSuccess()) {
             return $result;
         }
         $r = $item->save();
     }
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     }
     return $result;
 }