Skip to content

Commit c0edf94

Browse files
authored
Merge pull request #5196 from rubyforgood/item-request
Request Item → Item Request
2 parents ef48193 + aaa6f2d commit c0edf94

File tree

10 files changed

+74
-95
lines changed

10 files changed

+74
-95
lines changed

app/controllers/requests_controller.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ def index
2929

3030
def show
3131
@request = Request.find(params[:id])
32-
@request_items = load_items
32+
@item_requests = @request.item_requests.includes(:item)
33+
34+
@inventory = View::Inventory.new(@request.organization_id)
35+
@default_storage_location = @request.partner.default_storage_location_id || @request.organization.default_storage_location
36+
@location = StorageLocation.find_by(id: @default_storage_location)
37+
38+
@custom_units = Flipper.enabled?(:enable_packs) && @request.item_requests.any? { |item| item.request_unit }
3339
end
3440

3541
# Clicking the "New Distribution" button will set the the request to started
@@ -86,13 +92,6 @@ def print_unfulfilled
8692

8793
private
8894

89-
def load_items
90-
return unless @request.request_items
91-
92-
inventory = View::Inventory.new(@request.organization_id)
93-
@request.request_items.map { |json| RequestItem.from_json(json, @request, inventory) }
94-
end
95-
9695
helper_method \
9796
def filter_params
9897
return {} unless params.key?(:filters)

app/mailers/requests_confirmation_mailer.rb

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class RequestsConfirmationMailer < ApplicationMailer
22
def confirmation_email(request)
33
@organization = request.organization
44
@partner = request.partner
5-
@request_items = fetch_items(request)
5+
@item_requests = request.item_requests.includes(:item)
66
requester = request.requester
77
@requester_user_name = requester.is_a?(User) ? requester.name : nil # Requester can be the partner, if no user is specified
88
# If the organization has opted in to receiving an email when a request is made, CC them
@@ -15,25 +15,4 @@ def confirmation_email(request)
1515
cc.uniq!
1616
mail(to: requester.email, cc: cc, subject: "#{@organization.name} - Requests Confirmation")
1717
end
18-
19-
private
20-
21-
# TODO: remove the need to de-duplicate items in the request
22-
def fetch_items(request)
23-
combined = combined_items(request)
24-
item_ids = combined&.map { |item| item['item_id'] }
25-
db_items = Item.where(id: item_ids).select(:id, :name)
26-
combined.each { |i| i['name'] = db_items.find { |db_item| i["item_id"] == db_item.id }.name }
27-
combined.sort_by { |i| i['name'] }
28-
end
29-
30-
def combined_items(request)
31-
return [] if request.request_items.size == 0
32-
# convert items into a hash of (id => list of items with that ID)
33-
grouped = request.request_items.group_by { |i| [i['item_id'], i['request_unit']] }
34-
# convert hash into an array of items with combined quantities
35-
grouped.map do |id_unit, items|
36-
{ 'item_id' => id_unit.first, 'quantity' => items.map { |i| i['quantity'] }.sum, "unit" => id_unit.last }
37-
end
38-
end
3918
end

app/models/partners/item_request.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ def request_unit_is_supported
3535
end
3636
end
3737

38+
# Usually the item_name, but fall back to our local copy
39+
def item_name
40+
item&.name || name
41+
end
42+
43+
def quantity_with_units
44+
if Flipper.enabled?(:enable_packs) && request_unit.present?
45+
"#{quantity} #{request_unit.pluralize(quantity.to_i)}"
46+
else
47+
quantity
48+
end
49+
end
50+
3851
def name_with_unit(quantity_override = nil)
3952
if Flipper.enabled?(:enable_packs) && request_unit.present?
40-
"#{item&.name || name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
53+
"#{item_name} - #{request_unit.pluralize(quantity_override || quantity.to_i)}"
4154
else
42-
item&.name || name
55+
item_name
4356
end
4457
end
4558
end

app/pdfs/distribution_pdf.rb

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -180,38 +180,35 @@ def request_data
180180
"Value/item",
181181
"In-Kind Value Received",
182182
"Packages"]]
183-
inventory = View::Inventory.new(@distribution.organization_id)
184-
request_items = @distribution.request.request_items.map do |request_item|
185-
RequestItem.from_json(request_item, @distribution.request, inventory)
186-
end
183+
item_requests = @distribution.request.item_requests.includes(:item)
187184
line_items = @distribution.line_items.sorted
188185

189-
requested_not_received = request_items.select do |request_item|
190-
line_items.none? { |i| i.item_id == request_item.item.id }
186+
requested_not_received = item_requests.select do |item_request|
187+
line_items.none? { |i| i.item_id == item_request.item_id }
191188
end
192189

193190
data += line_items.map do |c|
194-
request_item = request_items.find { |i| i.item&.id == c.item_id }
191+
item_request = item_requests.find { |i| i.item_id == c.item_id }
195192
[c.item.name,
196-
request_display_qty(request_item),
193+
request_display_qty(item_request),
197194
c.quantity,
198195
dollar_value(c.item.value_in_cents),
199196
dollar_value(c.value_per_line_item),
200197
c.package_count]
201198
end
202199

203-
data += requested_not_received.sort_by(&:name).map do |request_item|
204-
[request_item.item.name,
205-
request_display_qty(request_item),
200+
data += requested_not_received.sort_by(&:name).map do |item_request|
201+
[item_request.item.name,
202+
request_display_qty(item_request),
206203
"",
207-
dollar_value(request_item.item.value_in_cents),
204+
dollar_value(item_request.item.value_in_cents),
208205
nil,
209206
nil]
210207
end
211208

212209
data + [["", "", "", "", ""],
213210
["Total Items Received",
214-
request_items.map(&:quantity).sum,
211+
item_requests.sum { |ir| ir.quantity.to_i },
215212
@distribution.line_items.total,
216213
"",
217214
dollar_value(@distribution.value_per_itemizable),
@@ -290,11 +287,11 @@ def signature_lines_for(label)
290287
draw_text "(Signature and Date)", at: [right_start, cursor]
291288
end
292289

293-
def request_display_qty(request_item)
294-
if Flipper.enabled?(:enable_packs) && request_item&.unit
295-
"#{request_item.quantity} #{request_item.unit.pluralize(request_item.quantity)}"
290+
def request_display_qty(item_request)
291+
if Flipper.enabled?(:enable_packs) && item_request&.request_unit
292+
"#{item_request.quantity} #{item_request.request_unit.pluralize(item_request.quantity.to_i)}"
296293
else
297-
request_item&.quantity || ""
294+
item_request&.quantity || ""
298295
end
299296
end
300297
end

app/views/requests/show.html.erb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,29 @@
6767
<tr>
6868
<th>Item</th>
6969
<th>Quantity</th>
70-
<% custom_units = Flipper.enabled?(:enable_packs) &&
71-
@request.item_requests.any? { |item| item.request_unit } %>
72-
<% if custom_units %>
70+
<% if @custom_units %>
7371
<th>Units (if applicable)</th>
7472
<% end %>
75-
<% default_storage_location = @request.partner.default_storage_location_id ||
76-
@request.organization.default_storage_location %>
77-
<% if default_storage_location %>
73+
<% if @default_storage_location %>
7874
<th>Default storage location inventory</th>
7975
<% end %>
8076
<th>Total Inventory</th>
8177
</tr>
8278
</thead>
8379
<tbody>
84-
<% @request_items.each do |item| %>
80+
<% @item_requests.each do |item_request| %>
8581
<tr>
86-
<td><%= item.name %></td>
87-
<td><%= item.quantity %></td>
88-
<% if custom_units %>
89-
<td><%= item.unit&.pluralize(item.quantity) %></td>
82+
<td><%= item_request.item_name %></td>
83+
<td><%= item_request.quantity %></td>
84+
<% if @custom_units %>
85+
<td><%= item_request.request_unit&.pluralize(item_request.quantity.to_i) %></td>
9086
<% end %>
91-
<% if default_storage_location %>
92-
<td><%= item.on_hand_for_location %></td>
87+
<% if @default_storage_location %>
88+
<% on_hand_for_location = @inventory.quantity_for(storage_location: @location&.id, item_id: item_request.item_id) %>
89+
<td><%= on_hand_for_location&.positive? ? on_hand_for_location : 'N/A' %></td>
9390
<% end %>
94-
<td><%= item.on_hand %></td>
91+
<% on_hand = @inventory.quantity_for(item_id: item_request.item_id) %>
92+
<td><%= on_hand || 0 %></td>
9593
</tr>
9694
<% end %>
9795
<tr>

app/views/requests_confirmation_mailer/confirmation_email.html.erb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22

33
<p>This email confirms that <%= @organization.name %> has received a request<%= " submitted by #{@requester_user_name}" if @requester_user_name.present? %> for:</p>
44
<ul>
5-
<% @request_items.each do |item| %>
6-
<li><%= item['name'] %> -
7-
<% if Flipper.enabled?(:enable_packs) && item['unit'] %>
8-
<%= pluralize(item['quantity'] || item['person_count'], item['unit']) %>
9-
<% else %>
10-
<%= item['quantity'] || item['person_count'] %>
11-
<% end %>
12-
</li>
13-
5+
<% @item_requests.each do |item_request| %>
6+
<li><%= item_request.item_name %> - <%= item_request.quantity_with_units %></li>
147
<% end %>
158
</ul>
169

app/views/requests_confirmation_mailer/confirmation_email.text.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Hello <%= @partner.name %>,
22

33
This email confirms that <%= @organization.name %> has received a request<%= " submitted by #{@requester_user_name}" if @requester_user_name.present? %> for:
4-
<% @request_items.each do |item| %>
5-
<%= item['name'] %> - <%= item['quantity'] || item['person_count'] %>
4+
<% @item_requests.each do |item_request| %>
5+
<%= item_request.item_name %> - <%= item_request.quantity_with_units %>
66
<% end %>
77

88
You will receive a notification when a distribution has been created of the pick-up time and date.

spec/mailers/requests_confirmation_mailer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
let(:request) { create(:request, organization:, partner_user:) }
55
let(:mail) { RequestsConfirmationMailer.confirmation_email(request) }
66

7-
let(:request_w_varied_quantities) { create(:request, :with_varied_quantities, organization: organization) }
7+
let(:request_w_varied_quantities) { create(:request, :with_varied_quantities, :with_item_requests, organization: organization) }
88
let(:mail_w_varied_quantities) { RequestsConfirmationMailer.confirmation_email(request_w_varied_quantities) }
99

1010
describe "#confirmation_email" do
@@ -69,7 +69,7 @@
6969
{item_id: item1.id, quantity: 1, request_unit: "Pack"},
7070
{item_id: item2.id, quantity: 7, request_unit: "Pack"}
7171
]
72-
request = create(:request, :pending, request_items:)
72+
request = create(:request, :pending, :with_item_requests, request_items:)
7373
email = RequestsConfirmationMailer.confirmation_email(request)
7474
expect(email.body.encoded).to match("1 Pack")
7575
expect(email.body.encoded).to match("7 Packs")
@@ -79,7 +79,7 @@
7979
Flipper.enable(:enable_packs)
8080
item = create(:item, organization:)
8181
create(:item_unit, item: item, name: "Pack")
82-
request = create(:request, :pending, request_items: [{item_id: item.id, quantity: 7}])
82+
request = create(:request, :pending, :with_item_requests, request_items: [{item_id: item.id, quantity: 7}])
8383
email = RequestsConfirmationMailer.confirmation_email(request)
8484

8585
expect(email.body.encoded).not_to match("7 Packs")

spec/pdfs/distribution_pdf_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
expect(results).to eq([
2828
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received", "Packages"],
2929
["Item 1", "", 50, "$1.00", "$50.00", "1"],
30-
["Item 2", 30, 100, "$2.00", "$200.00", nil],
31-
["Item 3", 50, "", "$3.00", nil, nil],
30+
["Item 2", "30", 100, "$2.00", "$200.00", nil],
31+
["Item 3", "50", "", "$3.00", nil, nil],
3232
["Item 4", "120 packs", "", "$4.00", nil, nil],
3333
["", "", "", "", ""],
3434
["Total Items Received", 200, 150, "", "$250.00", ""]
@@ -55,9 +55,9 @@
5555
expect(data).to eq([
5656
["Items Received", "Requested", "Received"],
5757
["Item 1", "", 50],
58-
["Item 2", 30, 100],
59-
["Item 3", 50, ""],
60-
["Item 4", 120, ""],
58+
["Item 2", "30", 100],
59+
["Item 3", "50", ""],
60+
["Item 4", "120", ""],
6161
["", "", ""],
6262
["Total Items Received", 200, 150]
6363
])
@@ -70,9 +70,9 @@
7070
expect(data).to eq([
7171
["Items Received", "Requested", "Received", "Packages"],
7272
["Item 1", "", 50, "1"],
73-
["Item 2", 30, 100, nil],
74-
["Item 3", 50, "", nil],
75-
["Item 4", 120, "", nil],
73+
["Item 2", "30", 100, nil],
74+
["Item 3", "50", "", nil],
75+
["Item 4", "120", "", nil],
7676
["", "", ""],
7777
["Total Items Received", 200, 150, ""]
7878
])
@@ -88,9 +88,9 @@
8888
expect(data).to eq([
8989
["Items Received", "Requested", "Received"],
9090
["Item 1", "", 50],
91-
["Item 2", 30, 100],
92-
["Item 3", 50, ""],
93-
["Item 4", 120, ""],
91+
["Item 2", "30", 100],
92+
["Item 3", "50", ""],
93+
["Item 4", "120", ""],
9494
["", "", ""],
9595
["Total Items Received", 200, 150]
9696
])
@@ -103,9 +103,9 @@
103103
expect(data).to eq([
104104
["Items Received", "Requested", "Received", "Packages"],
105105
["Item 1", "", 50, "1"],
106-
["Item 2", 30, 100, nil],
107-
["Item 3", 50, "", nil],
108-
["Item 4", 120, "", nil],
106+
["Item 2", "30", 100, nil],
107+
["Item 3", "50", "", nil],
108+
["Item 4", "120", "", nil],
109109
["", "", ""],
110110
["Total Items Received", 200, 150, ""]
111111
])
@@ -121,9 +121,9 @@
121121
expect(data).to eq([
122122
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received"],
123123
["Item 1", "", 50, "$1.00", "$50.00"],
124-
["Item 2", 30, 100, "$2.00", "$200.00"],
125-
["Item 3", 50, "", "$3.00", nil],
126-
["Item 4", 120, "", "$4.00", nil],
124+
["Item 2", "30", 100, "$2.00", "$200.00"],
125+
["Item 3", "50", "", "$3.00", nil],
126+
["Item 4", "120", "", "$4.00", nil],
127127
["", "", "", "", ""],
128128
["Total Items Received", 200, 150, "", "$250.00"]
129129
])

spec/system/request_system_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
{ item_id: item2.id, quantity: 100}
163163
]
164164
}
165-
let!(:request) { create(:request, request_items: request_items, organization: organization) }
165+
let!(:request) { create(:request, :with_item_requests, request_items: request_items, organization: organization) }
166166

167167
it "should show the request with a request sender if a partner user is set" do
168168
visit subject

0 commit comments

Comments
 (0)