From b2848551473a24dce47fe5b848d4b289caba9966 Mon Sep 17 00:00:00 2001 From: Grant Date: Sat, 6 Jul 2024 15:16:22 -0500 Subject: [PATCH 1/2] Added some logic to the seed script to populate half of the times from Pawnee Diaper Bank Org with a on_hand_minimum_quantity and a on_hand_recommended_quantity for demo and testing purposes --- db/seeds.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 0a138b6301..ad76bd2575 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -77,6 +77,12 @@ def random_record_for_org(org, klass) end end +# 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) + # ---------------------------------------------------------------------------- # Request Units # ---------------------------------------------------------------------------- From 381c7b866ec4409e4cd2b34fb9976451af2b3886 Mon Sep 17 00:00:00 2001 From: Grant Date: Mon, 8 Jul 2024 21:18:18 -0500 Subject: [PATCH 2/2] Update the seed script to grab half of the items with the lowest total quantity and for each one assign a random minimum and recommended value based off the max and min values in the total quantity --- db/seeds.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index ad76bd2575..68af89b303 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -77,12 +77,6 @@ def random_record_for_org(org, klass) end end -# 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) - # ---------------------------------------------------------------------------- # Request Units # ---------------------------------------------------------------------------- @@ -428,6 +422,23 @@ def random_record_for_org(org, klass) end Organization.all.each { |org| SnapshotEvent.publish(org) } +# Set minimum and recomended inventory levels for items at the Pawnee Diaper Bank Organization +half_items_count = (pdx_org.items.count/2).to_i +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 + # ---------------------------------------------------------------------------- # Product Drives # ----------------------------------------------------------------------------