-
-
Notifications
You must be signed in to change notification settings - Fork 572
Resolves #5225: Implemented "Itemized Request Report" #5300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cielf
merged 9 commits into
rubyforgood:main
from
seabeeberry:5225-itemized-request-report
Aug 24, 2025
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a1c4a9
Implemented Itemized Requests Report
8b94612
Added test for itemized breakdown of requested items
759cd30
Applied requested changes for displaying red items in the overview wi…
6aa1810
Fixed display of Unknown when a quantity of an item is zero to displa…
f2bb2df
Deleted fallback in fetch method and added fallback to the view inste…
0a3cd08
Implemented Itemized Request Report correctly now using Partners::It…
87b581f
Implemented tests using TestInventory.create_item
cf686c1
Implemented ItemizedRequestBreakdownService using class-level methods
d88243a
Fetching items by their IDs instead of their names
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| class RequestItemizedBreakdownService | ||
| # | ||
| # Initializes the RequestItemizedBreakdownService whose | ||
| # purpose is to construct an itemized breakdown of requested items | ||
| # | ||
| # @param organization [Organization] | ||
| # @param request_ids [Array<Integer>] | ||
| # @return [RequestItemizedBreakdownService] | ||
| def initialize(organization:, request_ids:) | ||
| @organization = organization | ||
| @request_ids = request_ids | ||
| end | ||
|
|
||
| # | ||
| # Returns a hash containing the itemized breakdown of | ||
| # requested items. | ||
| # | ||
| # @return [Array] | ||
| def fetch | ||
| inventory = View::Inventory.new(@organization.id) | ||
| current_onhand = current_onhand_quantities(inventory) | ||
| current_min_onhand = current_onhand_minimums(inventory) | ||
| items_requested = fetch_items_requested | ||
|
|
||
| items_requested.map! do |item| | ||
seabeeberry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| item_id = item[:item_id] | ||
|
|
||
| on_hand = current_onhand[item_id] | ||
| minimum = current_min_onhand[item_id] | ||
|
|
||
| below_onhand_minimum = on_hand && minimum && on_hand < minimum | ||
|
|
||
| item.merge( | ||
| on_hand: on_hand, | ||
| onhand_minimum: minimum, | ||
| below_onhand_minimum: below_onhand_minimum | ||
| ) | ||
| end | ||
|
|
||
| items_requested.sort_by { |item| [item[:name], item[:unit].to_s] } | ||
| end | ||
|
|
||
| # | ||
| # Returns a CSV string representation of the itemized breakdown of | ||
| # what was requested | ||
| # | ||
| # @return [String] | ||
| def fetch_csv | ||
| convert_to_csv(fetch) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :organization, :request_ids | ||
|
|
||
| def current_onhand_quantities(inventory) | ||
| inventory.all_items.group_by(&:id).to_h do |id, items| | ||
| [id, items.sum(&:quantity)] | ||
| end | ||
| end | ||
|
|
||
| def current_onhand_minimums(inventory) | ||
| inventory.all_items.group_by(&:id).to_h do |id, items| | ||
| [id, items.map(&:on_hand_minimum_quantity).compact.max] | ||
| end | ||
| end | ||
|
|
||
| def fetch_items_requested | ||
| Request | ||
| .includes(:partner, :organization, :item_requests) | ||
| .where(id: @request_ids) | ||
| .flat_map do |request| | ||
| request.request_items.map do |json_item| | ||
seabeeberry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| RequestItem.from_json(json_item, request) | ||
| end | ||
| end | ||
| .group_by { |ri| [ri.item.id, ri.unit] } | ||
| .map do |(item_id, unit), grouped| | ||
| item = grouped.first.item | ||
| { | ||
| item_id: item.id, | ||
| name: item.name, | ||
| unit: unit, | ||
| quantity: grouped.sum { |ri| ri.quantity.to_i } | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def convert_to_csv(items_requested_data) | ||
| CSV.generate do |csv| | ||
| csv << ["Item", "Total Requested", "Total On Hand"] | ||
| items_requested_data.each do |item| | ||
| csv << [item[:name], item[:quantity], item[:on_hand]] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def inventory | ||
| @inventory ||= View::Inventory.new(@organization.id) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <%= render( | ||
| "shared/filtered_card", | ||
| id: "purchases", | ||
| gradient: "secondary", | ||
| title: "Itemized Requests", | ||
| subtitle: @selected_date_range, | ||
| type: :table, | ||
| filter_url: reports_itemized_requests_path | ||
| ) do %> | ||
|
|
||
| <% if @itemized_request_data.empty? %> | ||
| <div class="alert alert-warning" role="alert"> | ||
| No itemized requests found for the selected date range. | ||
| </div> | ||
| <% else %> | ||
| <table class="table table-hover striped text-left"> | ||
| <thead> | ||
| <tr> | ||
| <th>Item</th> | ||
| <th class="text-right">Total Requested</th> | ||
| <th class="text-right">Total On Hand</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <% @itemized_request_data.each do |item| %> | ||
| <tr> | ||
| <td> | ||
| <%= item[:name] %> | ||
| <% if item[:unit].present? %> | ||
| (<%= h(item[:unit]) %>) | ||
| <% end %> | ||
| </td> | ||
| <td class="text-right"><%= item[:quantity] %></td> | ||
| <td class="text-right <%= 'table-danger' if item[:below_onhand_minimum] %>"> | ||
| <%= item[:on_hand] || 0 %> | ||
| </td> | ||
| </tr> | ||
| <% end %> | ||
| </tbody> | ||
| </table> | ||
| <% end %> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| RSpec.describe RequestItemizedBreakdownService, type: :service do | ||
| let(:organization) { create(:organization) } | ||
|
|
||
| let(:item_a) do | ||
| create(:item, organization: organization, on_hand_minimum_quantity: 4, name: "A Diapers") | ||
| end | ||
| let(:item_b) do | ||
| create(:item, organization: organization, on_hand_minimum_quantity: 8, name: "B Diapers") | ||
| end | ||
|
|
||
| let(:request_1) do | ||
| create(:request, organization: organization, request_items: [ | ||
| {"item_id" => item_a.id, "quantity" => 5} | ||
| ]) | ||
| end | ||
|
|
||
| let(:request_2) do | ||
| create(:request, organization: organization, request_items: [ | ||
| {"item_id" => item_b.id, "quantity" => 10} | ||
| ]) | ||
| end | ||
|
|
||
| let(:expected_output) do | ||
| [ | ||
| {name: item_a.name, item_id: item_a.id, unit: nil, quantity: 5, on_hand: 3, onhand_minimum: 4, below_onhand_minimum: true}, | ||
| {name: item_b.name, item_id: item_b.id, unit: nil, quantity: 10, on_hand: 20, onhand_minimum: 8, below_onhand_minimum: false} | ||
| ] | ||
| end | ||
|
|
||
| before do | ||
| allow_any_instance_of(View::Inventory).to receive(:quantity_for).with(item_id: item_a.id).and_return(3) | ||
| allow_any_instance_of(View::Inventory).to receive(:quantity_for).with(item_id: item_b.id).and_return(20) | ||
| allow_any_instance_of(View::Inventory).to receive(:all_items).and_return([ | ||
seabeeberry marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| OpenStruct.new(id: item_a.id, quantity: 3, on_hand_minimum_quantity: 4), | ||
| OpenStruct.new(id: item_b.id, quantity: 20, on_hand_minimum_quantity: 8) | ||
| ]) | ||
| end | ||
|
|
||
| describe "#fetch" do | ||
| subject { service.fetch } | ||
| let(:service) { described_class.new(organization: organization, request_ids: [request_1.id, request_2.id]) } | ||
|
|
||
| it "should include the break down of requested items" do | ||
| expect(subject).to eq(expected_output) | ||
| end | ||
| end | ||
|
|
||
| describe "#fetch_csv" do | ||
| subject { service.fetch_csv } | ||
| let(:service) { described_class.new(organization: organization, request_ids: [request_1.id, request_2.id]) } | ||
|
|
||
| it "should output the expected output but in CSV format" do | ||
| expected_csv = <<~CSV | ||
| Item,Total Requested,Total On Hand | ||
| A Diapers,5,3 | ||
| B Diapers,10,20 | ||
| CSV | ||
|
|
||
| expect(subject).to eq(expected_csv) | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.