14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'app/services/item/materials/checks/moq.rb', line 14
def process(container:, options:{})
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? hash_counters[item] ||= 0
hash_counters[item] += line_quantity
end
item.kit_target_item_relations.eager_load(:target_item).each do |tir|
target_item = tir.target_item
next unless target_item.moq.to_i.positive? hash_counters[target_item] ||= 0
hash_counters[target_item] += tir.quantity * line_quantity
end
end
hash_counters.each do |item, quantity|
if quantity < item.moq
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)
end
end
Result.new(status: :ok, alerts: material_alerts)
end
|