Class: Rawbotz::OrderLinker

Inherits:
Object
  • Object
show all
Includes:
RawgentoModels
Defined in:
lib/rawbotz/processors/order_linker.rb

Defined Under Namespace

Classes: OrderItemLine, OrderItemRecord

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderLinker

Returns a new instance of OrderLinker.



14
15
16
# File 'lib/rawbotz/processors/order_linker.rb', line 14

def initialize(order)
  @order = order
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



12
13
14
# File 'lib/rawbotz/processors/order_linker.rb', line 12

def order
  @order
end

#orphansObject

Returns the value of attribute orphans.



12
13
14
# File 'lib/rawbotz/processors/order_linker.rb', line 12

def orphans
  @orphans
end

#refundsObject

Returns the value of attribute refunds.



12
13
14
# File 'lib/rawbotz/processors/order_linker.rb', line 12

def refunds
  @refunds
end

Instance Method Details

#link!Object

Links a local rawbotz order to a remote (magento) order.

Set @orphans (remote order items that do not directly match, and @matched_order_items



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rawbotz/processors/order_linker.rb', line 23

def link!
  # My, thats a hairy regex
  shop_order_id = @order.remote_order_link[/\d+/]
  @remote_order_lines = Rawbotz.mech.products_from_order(shop_order_id).map do |line|
    OrderItemLine.new(line[0],
                      line[2],
                      line[3])
  end
  matches, orphans = @remote_order_lines.partition{|line| !order_item(line).nil?}
  @matched_order_items = matches.map{|line| order_item line}
  @orphans = orphans
  @refunds = @matched_order_items.map do |oi|
    [oi, @remote_order_lines.find{|l| l.remote_product_name == oi.local_product.remote_product.name}]
  end.to_h
end