Skip to content

Commit

Permalink
Update the seed script to grab half of the items with the lowest tota…
Browse files Browse the repository at this point in the history
…l quantity and for each one assign a random minimum and recommended value based off the max and min values in the total quantity
  • Loading branch information
grantmca committed Jul 9, 2024
1 parent b284855 commit 154674b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,20 @@ def random_record_for_org(org, klass)

# Set minimum and recomended inventory levels for items at the Pawnee Diaper Bank Organization
half_items_count = (pdx_org.items.count/2).to_i
min_value = 1500
recomended_value = 2000
pdx_org.items.limit(half_items_count).update_all(on_hand_minimum_quantity: min_value, on_hand_recommended_quantity: recomended_value)
low_items = pdx_org.items.left_joins(:inventory_items)
.select('items.*, SUM(inventory_items.quantity) AS total_quantity')
.group('items.id')
.order('total_quantity')
.limit(half_items_count)

min_qty = low_items.first.total_quantity
max_qty = low_items.last.total_quantity

low_items.each do |item|
min_value = rand((min_qty / 10).floor..(max_qty/10).ceil) * 10
recomended_value = rand((min_value/10).ceil..1000) * 10
item.update(on_hand_minimum_quantity: min_value, on_hand_recommended_quantity: recomended_value)
end

# ----------------------------------------------------------------------------
# Request Units
Expand Down

0 comments on commit 154674b

Please sign in to comment.