Skip to content

Commit

Permalink
Display units in distribution PDF as needed (#4610)
Browse files Browse the repository at this point in the history
* Display units in distribution PDF as needed

* Clean out some lint

* Extract common display logic [#4404]
  • Loading branch information
awwaiid authored Oct 14, 2024
1 parent 140bbfe commit 1b1e251
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
21 changes: 14 additions & 7 deletions app/pdfs/distribution_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def compute_and_render
# Quantity column
column(1..-1).row(1..-3).borders = [:left]
column(1..-1).row(1..-3).border_left_color = "aaaaaa"
column(1).style align: :right
column(-1).row(-1).borders = [:left, :bottom]
end

Expand Down Expand Up @@ -178,20 +177,20 @@ def request_data
end

data += line_items.map do |c|
request_item = request_items.find { |i| i.item.id == c.item_id }
request_item = request_items.find { |i| i.item&.id == c.item_id }
[c.item.name,
request_item&.quantity || "",
request_display_qty(request_item),
c.quantity,
dollar_value(c.item.value_in_cents),
dollar_value(c.value_per_line_item),
c.package_count]
end

data += requested_not_received.sort_by(&:name).map do |c|
[c.item.name,
c.quantity,
data += requested_not_received.sort_by(&:name).map do |request_item|
[request_item.item.name,
request_display_qty(request_item),
"",
dollar_value(c.item.value_in_cents),
dollar_value(request_item.item.value_in_cents),
nil,
nil]
end
Expand Down Expand Up @@ -276,4 +275,12 @@ def signature_lines_for(label)
draw_text "(Print Name)", at: [0, cursor]
draw_text "(Signature and Date)", at: [right_start, cursor]
end

def request_display_qty(request_item)
if Flipper.enabled?(:enable_packs) && request_item&.unit
"#{request_item.quantity} #{request_item.unit.pluralize(request_item.quantity)}"
else
request_item&.quantity || ""
end
end
end
24 changes: 21 additions & 3 deletions spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
before(:each) do
create(:line_item, itemizable: distribution, item: item1, quantity: 50)
create(:line_item, itemizable: distribution, item: item2, quantity: 100)
create(:request, distribution: distribution,
request_items: [{"item_id" => item2.id, "quantity" => 30},
{"item_id" => item3.id, "quantity" => 50}, {"item_id" => item4.id, "quantity" => 120}])
create(:item_unit, item: item4, name: "pack")
create(:request, :with_item_requests, distribution: distribution,
request_items: [
{"item_id" => item2.id, "quantity" => 30},
{"item_id" => item3.id, "quantity" => 50},
{"item_id" => item4.id, "quantity" => 120, "request_unit" => "pack"}
])
end

specify "#request_data" do
Expand All @@ -36,6 +40,20 @@
])
end

specify "#request_data with custom units feature" do
Flipper.enable(:enable_packs)
results = described_class.new(organization, distribution).request_data
expect(results).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received", "Packages"],
["Item 1", "", 50, "$1.00", "$50.00", "1"],
["Item 2", 30, 100, "$2.00", "$200.00", nil],
["Item 3", 50, "", "$3.00", nil, nil],
["Item 4", "120 packs", "", "$4.00", nil, nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00", ""]
])
end

specify "#non_request_data" do
results = described_class.new(organization, distribution).non_request_data
expect(results).to eq([
Expand Down

0 comments on commit 1b1e251

Please sign in to comment.