Class: Workarea::CreateFulfillment

Inherits:
Object
  • Object
show all
Defined in:
app/services/workarea/create_fulfillment.rb

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ CreateFulfillment

Returns a new instance of CreateFulfillment.



3
4
5
# File 'app/services/workarea/create_fulfillment.rb', line 3

def initialize(order)
  @order = order
end

Instance Method Details

#fulfillmentObject



7
8
9
# File 'app/services/workarea/create_fulfillment.rb', line 7

def fulfillment
  @fulfillment ||= Fulfillment.find_or_initialize_by(id: @order.id)
end

#performObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/workarea/create_fulfillment.rb', line 11

def perform
  @order.items.each do |item|
    next if fulfillment.items.detect { |i| i.order_item_id == item.id.to_s }
    fulfillment.items.build(order_item_id: item.id, quantity: item.quantity)

    Fulfillment::Sku.process!(
      item.sku,
      order_item: item,
      fulfillment: fulfillment
    )
  end

  fulfillment.save!
end