|
| 1 | +class RequestItemizedBreakdownService |
| 2 | + class << self |
| 3 | + def call(organization:, request_ids:, format: :hash) |
| 4 | + data = fetch(organization: organization, request_ids: request_ids) |
| 5 | + (format == :csv) ? convert_to_csv(data) : data |
| 6 | + end |
| 7 | + |
| 8 | + def fetch(organization:, request_ids:) |
| 9 | + inventory = View::Inventory.new(organization.id) |
| 10 | + current_onhand = current_onhand_quantities(inventory) |
| 11 | + current_min_onhand = current_onhand_minimums(inventory) |
| 12 | + items_requested = fetch_items_requested(organization: organization, request_ids: request_ids) |
| 13 | + |
| 14 | + items_requested.each do |item| |
| 15 | + id = item[:item_id] |
| 16 | + |
| 17 | + on_hand = current_onhand[id] |
| 18 | + minimum = current_min_onhand[id] |
| 19 | + below_onhand_minimum = on_hand && minimum && on_hand < minimum |
| 20 | + |
| 21 | + item.merge!( |
| 22 | + on_hand: on_hand, |
| 23 | + onhand_minimum: minimum, |
| 24 | + below_onhand_minimum: below_onhand_minimum |
| 25 | + ) |
| 26 | + end |
| 27 | + |
| 28 | + items_requested.sort_by { |item| [item[:name], item[:unit].to_s] } |
| 29 | + end |
| 30 | + |
| 31 | + def csv(organization:, request_ids:) |
| 32 | + convert_to_csv(fetch(organization: organization, request_ids: request_ids)) |
| 33 | + end |
| 34 | + |
| 35 | + private |
| 36 | + |
| 37 | + def current_onhand_quantities(inventory) |
| 38 | + inventory.all_items.group_by(&:item_id).to_h { |item_id, rows| [item_id, rows.sum { |r| r.quantity.to_i }] } |
| 39 | + end |
| 40 | + |
| 41 | + def current_onhand_minimums(inventory) |
| 42 | + inventory.all_items.group_by(&:item_id).to_h { |item_id, rows| [item_id, rows.map(&:on_hand_minimum_quantity).compact.max] } |
| 43 | + end |
| 44 | + |
| 45 | + def fetch_items_requested(organization:, request_ids:) |
| 46 | + Partners::ItemRequest |
| 47 | + .includes(:item) |
| 48 | + .where(partner_request_id: request_ids) |
| 49 | + .group_by { |ir| [ir.item_id, ir.request_unit] } |
| 50 | + .map do |(item_id, unit), grouped| |
| 51 | + item = grouped.first.item |
| 52 | + { |
| 53 | + item_id: item.id, |
| 54 | + name: item.name, |
| 55 | + unit: unit, |
| 56 | + quantity: grouped.sum { |ri| ri.quantity.to_i } |
| 57 | + } |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def convert_to_csv(items_requested_data) |
| 62 | + CSV.generate do |csv| |
| 63 | + csv << ["Item", "Total Requested", "Total On Hand"] |
| 64 | + items_requested_data.each do |item| |
| 65 | + csv << [item[:name], item[:quantity], item[:on_hand]] |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments