",
+ start_count_at: 1,
+ at: [bounds.right - 130, 22],
+ align: :right
+
+ repeat :all do
+ # Page footer
+ bounding_box [bounds.left, bounds.bottom + footer_height], width: bounds.width do
+ stroke_bounds
+ font "OpenSans"
+ font_size 9
+ stroke_horizontal_rule
+ move_down(5)
+
+ logo_offset = (bounds.width - 190) / 2
+ bounding_box([logo_offset, 0], width: 190, height: 33) do
+ text "Lovingly created with", valign: :center
+ image Organization::DIAPER_APP_LOGO, width: 75, vposition: :center, position: :right
+ end
+ end
+ end
+
+ render
+ end
+
+ def donation_data
+ data = [["Items Received",
+ "Value/item",
+ "In-Kind Value",
+ "Quantity"]]
+ data += @donation.line_items.sorted.map do |c|
+ [c.item.name,
+ dollar_value(c.item.value_in_cents),
+ dollar_value(c.value_per_line_item),
+ c.quantity]
+ end
+ data + [["", "", "", ""],
+ ["Total Items Received",
+ "",
+ dollar_value(@donation.value_per_itemizable),
+ @donation.line_items.total]]
+ end
+
+ def hide_columns(data)
+ column_names_to_hide.each do |col_name|
+ col_index = data.first.find_index(col_name)
+ data.each { |line| line.delete_at(col_index) } if col_index.present?
+ end
+ end
+
+ private
+
+ def column_names_to_hide
+ in_kind_column_name = "In-Kind Value"
+ columns_to_hide = []
+ columns_to_hide.push("Value/item", in_kind_column_name) if @organization.hide_value_columns_on_receipt
+ columns_to_hide
+ end
+end
diff --git a/app/views/donations/_donation_row.html.erb b/app/views/donations/_donation_row.html.erb
index 50ea5a8618..9ce06cc75f 100644
--- a/app/views/donations/_donation_row.html.erb
+++ b/app/views/donations/_donation_row.html.erb
@@ -9,6 +9,7 @@
<%= truncate donation_row.comment, length: 140, separator: /\w+/ %>
|
<%= view_button_to donation_path(donation_row) %>
+ <%= print_button_to print_donation_path(donation_row, format: :pdf) %>
|
diff --git a/app/views/donations/show.html.erb b/app/views/donations/show.html.erb
index 8e45413ae2..145d251a41 100644
--- a/app/views/donations/show.html.erb
+++ b/app/views/donations/show.html.erb
@@ -87,6 +87,7 @@
If you need to delete this donation or make a correction, please make the following items active: <%= @donation.inactive_items.map(&:name).join(", ") %>
<% end %>
+ <%= print_button_to print_donation_path(@donation, format: :pdf), { size: "md" } %>
diff --git a/config/routes.rb b/config/routes.rb
index b44654f925..261370cd2e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -50,6 +50,9 @@ def set_up_flipper
resources :distributions, only: [:index] do
get :print, on: :member
end
+ resources :donations, only: [:index] do
+ get :print, on: :member
+ end
end
# This is where a superadmin CRUDs all the things
@@ -221,6 +224,7 @@ def set_up_flipper
resources :product_drives
resources :donations do
+ get :print, on: :member
patch :add_item, on: :member
patch :remove_item, on: :member
end
diff --git a/spec/pdfs/donation_pdf_spec.rb b/spec/pdfs/donation_pdf_spec.rb
new file mode 100644
index 0000000000..21a8ee2174
--- /dev/null
+++ b/spec/pdfs/donation_pdf_spec.rb
@@ -0,0 +1,68 @@
+describe DonationPdf do
+ let(:donation_site) { create(:donation_site, name: "Site X", address: "1500 Remount Road, Front Royal, VA 22630", email: "test@example.com") }
+ let(:organization) { create(:organization) }
+ let(:donation) do
+ create(:donation, organization: organization, donation_site: donation_site, source: Donation::SOURCES[:donation_site],
+ comment: "A donation comment")
+ end
+ let(:item1) { FactoryBot.create(:item, name: "Item 1", package_size: 50, value_in_cents: 100) }
+ let(:item2) { FactoryBot.create(:item, name: "Item 2", value_in_cents: 200) }
+ let(:item3) { FactoryBot.create(:item, name: "Item 3", value_in_cents: 300) }
+ let(:item4) { FactoryBot.create(:item, name: "Item 4", package_size: 25, value_in_cents: 400) }
+
+ let(:org_hiding_packages_and_values) do
+ FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME,
+ hide_value_columns_on_receipt: true, hide_package_column_on_receipt: true)
+ end
+ let(:org_hiding_packages) { FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_package_column_on_receipt: true) }
+ let(:org_hiding_values) { FactoryBot.create(:organization, name: DEFAULT_TEST_ORGANIZATION_NAME, hide_value_columns_on_receipt: true) }
+
+ before(:each) do
+ create(:line_item, itemizable: donation, item: item1, quantity: 50)
+ create(:line_item, itemizable: donation, item: item2, quantity: 100)
+ end
+
+ specify "#donation_data" do
+ results = described_class.new(organization, donation).donation_data
+ expect(results).to eq([
+ ["Items Received", "Value/item", "In-Kind Value", "Quantity"],
+ ["Item 1", "$1.00", "$50.00", 50],
+ ["Item 2", "$2.00", "$200.00", 100],
+ ["", "", "", ""],
+ ["Total Items Received", "", "$250.00", 150]
+ ])
+ end
+
+ context "with donation data" do
+ it "hides value and package columns when true on organization" do
+ pdf = described_class.new(org_hiding_packages_and_values, donation)
+ data = pdf.donation_data
+ pdf.hide_columns(data)
+ expect(data).to eq([
+ ["Items Received", "Quantity"],
+ ["Item 1", 50],
+ ["Item 2", 100],
+ ["", ""],
+ ["Total Items Received", 150]
+ ])
+ end
+ end
+
+ context "render pdf" do
+ it "renders correctly" do
+ pdf = described_class.new(organization, donation)
+ pdf_test = PDF::Reader.new(StringIO.new(pdf.compute_and_render))
+ expect(pdf_test.page(1).text).to include(donation_site.name)
+ expect(pdf_test.page(1).text).to include(donation_site.address)
+ expect(pdf_test.page(1).text).to include(donation_site.email)
+ if donation.comment
+ expect(pdf_test.page(1).text).to include(donation.comment)
+ end
+ expect(pdf_test.page(1).text).to include("Money Raised In Dollars: $0.00")
+ expect(pdf_test.page(1).text).to include("Items Received")
+ expect(pdf_test.page(1).text).to match(/Item 1\s+\$1\.00\s+\$50\.00\s+50/)
+ expect(pdf_test.page(1).text).to match(/Item 2\s+\$2\.00\s+\$200\.00\s+100/)
+ expect(pdf_test.page(1).text).to include("Total Items Received")
+ end
+ end
+end
diff --git a/spec/requests/donations_requests_spec.rb b/spec/requests/donations_requests_spec.rb
index 9f2e72e25d..325edcc1e6 100644
--- a/spec/requests/donations_requests_spec.rb
+++ b/spec/requests/donations_requests_spec.rb
@@ -28,6 +28,11 @@
expect(subject.body).to include("Source | ")
expect(subject.body).to include("Details | ")
end
+ it "shows a print button" do
+ page = Nokogiri::HTML(subject.body)
+ pdf = page.at_css("a[href*='print.pdf']")
+ expect(pdf.text).to include("Print")
+ end
context "when given a product drive" do
let(:product_drive) { create(:product_drive, name: "Drive Name") }
@@ -83,10 +88,27 @@
end
end
+ describe "GET #print" do
+ let(:item) { create(:item) }
+ let!(:donation) { create(:donation, :with_items, item: item) }
+
+ it "returns http success" do
+ get print_donation_path(id: donation.id)
+ expect(response).to be_successful
+ end
+ end
+
describe "GET #show" do
let(:item) { create(:item) }
let!(:donation) { create(:donation, :with_items, item: item) }
+ it "shows a print button" do
+ get donation_path(id: donation.id)
+ page = Nokogiri::HTML(response.body)
+ pdf = page.at_css("a[href*='#{print_donation_path(id: donation.id)}']")
+ expect(pdf.text).to include("Print")
+ end
+
it "shows an enabled edit button" do
get donation_path(id: donation.id)
page = Nokogiri::HTML(response.body)