         deferred.resolve unsyncedOrders
     deferred.promise
 
+  getTaxCategory: (rest) ->
+    deferred = Q.defer()
+    rest.GET "/tax-categories?limit=1", (error, response, body) ->
+      if error?
+        deferred.reject "Error on fetching tax category: " + error
+      else if response.statusCode isnt 200
+        deferred.reject "Problem on fetching tax category (status: #{response.statusCode}): " + body
+      else
+        if _.size(body.results) is 1
+          deferred.resolve body.results[0]
+        else
+          deferred.reject "Can't find tax category."
+    deferred.promise
+
   getRetailerProductByMasterSKU: (sku) ->
     deferred = Q.defer()
     query = encodeURIComponent "variants.attributes.mastersku:\"#{sku.toLowerCase()}\""
     @retailerRest.GET "/product-projections/search?staged=true&lang=de&filter=#{query}" , (error, response, body) ->
-      if error
+      if error?
         deferred.reject "Error on fetching products: " + error
       else if response.statusCode isnt 200
         deferred.reject "Problem on fetching products (status: #{response.statusCode}): " + body

  distribute: (masterOrder) ->
    masterSKUs = @extractSKUs masterOrder

    gets = _.map masterSKUs, (sku) =>
      @getRetailerProductByMasterSKU(sku)

    Q.all(gets)
    .then (retailerProducts) =>
      masterSKU2retailerSKU = @matchSKUs retailerProducts

      retailerOrder = @replaceSKUs masterOrder, masterSKU2retailerSKU
      retailerOrder = @removeIdsAndVariantData retailerOrder

      @importOrder(retailerOrder)
    .then (newOrder) =>
      Q.all([
        @inventoryUpdater.ensureChannelByKey @masterRest, @retailerRest._options.config.project_key, CHANNEL_ROLES
        @inventoryUpdater.ensureChannelByKey @retailerRest, 'master', CHANNEL_ROLES
      ])
      .spread (channelInMaster, channelInRetailer) =>
        Q.all([
          @addSyncInfo(@masterRest, masterOrder.id, masterOrder.version, channelInMaster.id, newOrder.id)
          @addSyncInfo(@retailerRest, newOrder.id, newOrder.version, channelInRetailer.id, masterOrder.id)
        ])

  matchSKUs: (retailerProducts) ->
    allVariants = _.flatten _.map(retailerProducts, (p) -> [p.masterVariant].concat(p.variants or []))


    order

  replaceTaxCategories: (order, taxCategory) ->
    if order.shippingInfo? and order.shippingInfo.taxCategory?
      order.shippingInfo.taxCategory[id] = taxCategory.id

    order

  removeIdsAndVariantData: (order) ->
    _.each order.lineItems or [], (li) ->
      delete li.supplyChannel if li.supplyChannel?
      delete li.id if li.id?
      delete li.productId if li.productId?
      delete li.states if li.states?
      if li.variant?
