/**
  * @internal
  * @param string $action
  * @param array $fields
  *
  * @return Sale\Result
  * @throws Main\ArgumentNullException
  * @throws Main\ObjectNotFoundException
  */
 public static function modifyOrder($action, array $fields)
 {
     $result = new Sale\Result();
     try {
         $adminSection = defined('ADMIN_SECTION') && ADMIN_SECTION === true;
         /** @var Sale\Compatible\OrderCompatibility $orderCompatibility */
         $orderCompatibility = Sale\Compatible\OrderCompatibility::create($fields);
         /** @var Sale\Order $order */
         $order = $orderCompatibility->getOrder();
         $oldPrice = $order->getPrice();
         //			$isStartField = $order->isStartField();
         /** @var Sale\Basket $basket */
         $basket = $order->getBasket();
         if (!$basket && $action == static::ORDER_COMPAT_ACTION_SAVE) {
             $fUserId = Sale\Fuser::getIdByUserId($order->getUserId());
             if (!$adminSection) {
                 $siteId = !empty($fields["SITE_ID"]) ? $fields["SITE_ID"] : (!empty($fields["LID"]) ? $fields['LID'] : null);
                 $basket = Sale\Basket::loadItemsForFUser($fUserId, $siteId);
             }
             if (!$basket) {
                 $basket = Sale\Basket::create($order->getSiteId(), $fUserId);
             }
         }
         if ($basket) {
             $basketCompatibility = BasketCompatibility::create($orderCompatibility);
             //				$orderCompatibility->resetOrderPrice($basket, $fields);
             /** @var Sale\Result $r */
             $r = $basketCompatibility->fillBasket($basket, $fields);
             if (!$r->isSuccess()) {
                 $result->addErrors($r->getErrors());
                 return $result;
             }
             if ($action == static::ORDER_COMPAT_ACTION_SAVE && $order->getId() == 0 && count($basket->getOrderableItems()) > 0) {
                 $order->setMathActionOnly(true);
                 $order->setBasket($basket->getOrderableItems());
                 $order->setMathActionOnly(false);
             }
             if ($orderCompatibility->isExistPrice() && $oldPrice == $order->getPrice()) {
                 $order->setFieldNoDemand('PRICE', $orderCompatibility->externalPrice);
             }
         }
         /** @var Sale\Result $r */
         $r = Sale\Compatible\OrderCompatibility::fillTaxFromRequest($order->getTax(), $fields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
             return $result;
         }
         /** @var Sale\Result $r */
         $r = OrderCompatibility::fillShipmentCollectionFromRequest($order->getShipmentCollection(), $fields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
             return $result;
         }
         $order->setMathActionOnly(false);
         /** @var Sale\Result $r */
         $r = Sale\Compatible\OrderCompatibility::fillPaymentCollectionFromRequest($order->getPaymentCollection(), $fields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
             return $result;
         }
         /** @var Sale\Result $r */
         $r = Sale\Compatible\OrderCompatibility::fillOrderFromRequest($order, $fields);
         if (!$r->isSuccess()) {
             $result->addErrors($r->getErrors());
             return $result;
         }
     } catch (Sale\UserMessageException $e) {
         $result->addError(new Sale\ResultError($e->getMessage(), $e->getCode()));
         return $result;
     }
     static::transformationLocation($order);
     /** @var Sale\Result $r */
     $r = $order->save();
     if ($r->isSuccess()) {
         if ($orderData = $r->getData()) {
             $result->setData($orderData);
         }
         if ($orderId = $r->getId()) {
             $result->setId($orderId);
         }
         /** @var Sale\Result $r */
         $r = $orderCompatibility->saveRawFields();
     }
     if (!$r->isSuccess()) {
         $result->addErrors($r->getErrors());
     } else {
         $oldFields = static::convertDateFieldsToOldFormat($order->getFieldValues());
         $oldFields = $oldFields + $orderCompatibility->rawFields;
         $result->setData(array('OLD_FIELDS' => $oldFields));
     }
     return $result;
 }