Class: Workarea::InventoryAdjustment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart) ⇒ InventoryAdjustment

Returns a new instance of InventoryAdjustment.



5
6
7
# File 'app/services/workarea/inventory_adjustment.rb', line 5

def initialize(cart)
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



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

def cart
  @cart
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Instance Method Details

#performObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/services/workarea/inventory_adjustment.rb', line 9

def perform
  @errors = []

  insufficiencies.each do |sku, quantity_short|
    item = cart.items.detect { |i| i.sku == sku }
    next unless item.present? && quantity_short > 0

    new_quantity = item.quantity - quantity_short

    if new_quantity == 0
      cart.remove_item(item.id)
      @errors << I18n.t('workarea.errors.messages.sku_unavailable', sku: sku)
    else
      cart.update_item(item.id, quantity: new_quantity)
      @errors << I18n.t(
        'workarea.errors.messages.sku_limited_quantity',
        quantity: new_quantity,
        sku: item[:sku]
      )
    end
  end
end