   @_options = options
   @sync = new InventorySync Config
   @rest = @sync._rest
+  @existingStocks = {}
+  @sku2index = {}
   @
 
 exports.StockXmlImport.prototype.elasticio = (msg, cfg, cb, snapshot) ->

  throw new Error 'String required' unless _.isString xmlString
  throw new Error 'Callback must be a function' unless _.isFunction callback

  xmlHelpers.xmlTransform xmlHelpers.xmlFix(xmlString), (err, result) =>
    if err
      @returnResult false, "Error on parsing XML: " + err, callback
    else
      stocks = @mapStock result.root, "TODO"
      @createOrUpdate stocks, callback

exports.StockXmlImport.prototype.returnResult = (positiveFeedback, msg, callback) ->
  d =

  callback d

exports.StockXmlImport.prototype.createOrUpdate = (stocks, callback) ->
  posts = []
  bar = new ProgressBar 'Updating stock [:bar] :percent done', { width: 50, total: stocks.length }
  for s in stocks
    es = @match(s)
    if es
      posts.push @update(s, es, bar)
    else
      posts.push @create(s, bar)

  Q.all(posts).then (v) =>
    if v.length is 1
      v = v[0]
    else
      v = "#{v.length} Done"
    @returnResult true, v, callback
  .fail (v) =>
    @returnResult false, v, callback

exports.StockXmlImport.prototype.initMatcher = (rest) ->
  deferred = Q.defer()
  @rest.GET "/inventory?limit=0", (error, response, body) =>
    if error
      deferred.reject 'Problem on getting existing stock information.'
      return
    if response.statusCode != 200
      deferred.reject 'Can not fetch stock information.'
      return
    @existingStocks = JSON.parse(body).results
    for es, i in @existingStocks
      @sku2index[es.sku] = i
    deferred.resolve @existingStocks
  deferred.promise

exports.StockXmlImport.prototype.match = (s) ->
  if @sku2index[s.sku] >= 0
    @existingStocks[@sku2index[s.sku]]

exports.StockXmlImport.prototype.update = (s, es, bar) ->
  deferred = Q.defer()
