/**
  * @param Sale\Order $order
  * @param Sale\Basket $basket
  *
  * @return Sale\Result
  * @throws Main\ArgumentNullException
  * @throws Main\ArgumentTypeException
  * @throws Main\NotSupportedException
  */
 protected static function appendBasketToOrder(Sale\Order $order, Sale\Basket $basket)
 {
     $result = new Sale\Result();
     $orderBasketCollection = $order->getBasket();
     $shipmentCollection = $order->getShipmentCollection();
     $systemShipment = $shipmentCollection->getSystemShipment();
     $systemShipmentItemCollection = $systemShipment->getShipmentItemCollection();
     /** @var Sale\BasketItem $basketItem */
     foreach ($basket as $basketItem) {
         $basketProperty = array();
         if ($basketPropertyCollection = $basketItem->getPropertyCollection()) {
             $basketProperty = $basketPropertyCollection->getPropertyValues();
         }
         if ($orderBasketItem = $orderBasketCollection->getExistsItem($basketItem->getField('MODULE'), $basketItem->getField('PRODUCT_ID'), $basketProperty)) {
             $fields = $basketItem->getFieldValues();
             $orderBasketItem->setFields(static::clearFields($fields));
         } else {
             /** @var Sale\BasketItem $orderBasketItem */
             $orderBasketCollection->addItem($basketItem);
             $basketItem->setCollection($orderBasketCollection);
             $systemShipmentItemCollection->resetCollection($orderBasketCollection);
         }
     }
     /** @var Sale\BasketItem $basketItem */
     foreach ($basket as $basketItem) {
         if ($systemShipment->getDeliveryId() > 0) {
             /** @var Sale\Shipment $shipment */
             $shipment = OrderCompatibility::getShipmentByDeliveryId($shipmentCollection, $systemShipment->getDeliveryId());
             if (!$shipment) {
                 if ($service = Sale\Delivery\Services\Manager::getService($systemShipment->getDeliveryId())) {
                     /** @var Sale\Shipment $shipment */
                     $shipment = $shipmentCollection->createItem($service);
                 }
             }
             if ($shipment) {
                 $shipmentItemCollection = $shipment->getShipmentItemCollection();
                 if (!($shipmentItem = $shipmentItemCollection->getItemByBasketCode($basketItem->getBasketCode()))) {
                     $shipmentItem = $shipmentItemCollection->createItem($basketItem);
                 }
                 /** @var Sale\Result $r */
                 $r = $shipmentItem->setQuantity($basketItem->getQuantity());
                 if (!$r->isSuccess()) {
                     $result->addErrors($r->getErrors());
                 }
             }
         }
     }
     return $result;
 }