Sha256: 5cabed8f20d309c9793a7907995616a4903f222333f5f3c4d9598ca150429976

Contents?: true

Size: 1.05 KB

Versions: 62

Compression:

Stored size: 1.05 KB

Contents

module Workarea
  class InventoryAdjustment
    attr_reader :cart, :errors

    def initialize(cart)
      @cart = cart
    end

    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

    private

    def insufficiencies
      @insufficiencies ||= Inventory.find_insufficiencies(serialized_items)
    end

    def serialized_items
      cart.items.inject({}) do |memo, item|
        memo[item.sku] = item.quantity
        memo
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.13 app/services/workarea/inventory_adjustment.rb
workarea-core-3.4.12 app/services/workarea/inventory_adjustment.rb