Class: Item::Materials::Checks::Moq

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/item/materials/checks/moq.rb

Overview

Service object: moq.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Instance Method Details

#process(container:, options: {}) ⇒ Result

rubocop:disable Lint/UnusedMethodArgument
options: is part of the shared Item::Materials::Checks polymorphic API
invoked by Item::Materials::Check#perform_checks_raw. Even though this
particular check ignores the value, the kwarg name has to stay as
options: so the bulk dispatch site keeps working.
:reek:UnusedParameters

Parameters:

  • container (Quote, Order, RoomConfiguration)

    the resource to check

  • options (Hash) (defaults to: {})

    shared check options passed by Item::Materials::Check

Options Hash (options:):

  • :for_www (Boolean)

    whether the check runs for the public site

  • :for_cart (Boolean)

    whether the check runs for the cart

Returns:

  • (Result)

    status and any material alerts



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/services/item/materials/checks/moq.rb', line 28

def process(container:, options: {})
  # rubocop:enable Lint/UnusedMethodArgument
  return Result.new(status: :skipped) unless qualify?(container)

  material_alerts = []
  hash_counters = {}
  container.line_items.goods.joins(:item).group_by(&:item).each do |item, lines|
    line_quantity = lines.sum(&:quantity)
    if item.moq.to_i.positive? # If this item has a moq store it
      hash_counters[item] ||= 0
      hash_counters[item] += line_quantity
    end
    item.kit_target_item_relations.eager_load(:target_item).find_each do |tir|
      target_item = tir.target_item
      next unless target_item.moq.to_i.positive? # If this item has a moq store it

      hash_counters[target_item] ||= 0
      hash_counters[target_item] += tir.quantity * line_quantity
    end
  end
  hash_counters.each do |item, quantity|
    material_alerts << Item::Materials::Alert.new(name: "#{item.name} has a minimum order requirement of #{item.moq} but only #{quantity} present in order", container: container) if quantity < item.moq
  end

  Result.new(status: :ok, alerts: material_alerts)
end

#qualify?(container) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'app/services/item/materials/checks/moq.rb', line 9

def qualify?(container)
  # Limits don't matter for warehouse, this is typically a logistics shipping requiremente
  return false if container.respond_to?(:shipping_address) && container.shipping_address&.is_warehouse
  return false unless container.is_a?(Order) && container.is_subject_to_minimum_qty_rules? # we skip unless this order is subject to minimum order qty...

  true
end