diff --git a/Gemfile b/Gemfile index da26278454..8aa3b747c7 100644 --- a/Gemfile +++ b/Gemfile @@ -43,6 +43,7 @@ gem "uglifier", ">= 1.3.0" gem "therubyracer", "~> 0.12", platforms: :ruby gem "yajl-ruby" gem "toastr-rails" +gem "sucker_punch", "~> 2.0" group :development, :test do gem "awesome_print" diff --git a/Gemfile.lock b/Gemfile.lock index 16620db519..11a5d8f3e7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -393,6 +393,8 @@ GEM net-ssh (>= 2.8.0) sshkit-interactive (0.3.0) sshkit (~> 1.12) + sucker_punch (2.1.1) + concurrent-ruby (~> 1.0) terminal-notifier (2.0.0) terminal-notifier-guard (1.7.0) terrapin (0.6.0) @@ -492,6 +494,7 @@ DEPENDENCIES spring spring-watcher-listen sprockets (~> 3.7.2) + sucker_punch (~> 2.0) terminal-notifier terminal-notifier-guard therubyracer (~> 0.12) @@ -507,4 +510,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.16.6 + 1.16.6 \ No newline at end of file diff --git a/app/controllers/distributions_controller.rb b/app/controllers/distributions_controller.rb index 88e9511203..6b8e1d9cf1 100644 --- a/app/controllers/distributions_controller.rb +++ b/app/controllers/distributions_controller.rb @@ -3,7 +3,15 @@ class DistributionsController < ApplicationController def print @distribution = Distribution.find(params[:id]) - @filename = format("%s %s.pdf", @distribution.partner.name, sortable_date(@distribution.created_at)) + respond_to do |format| + format.any do + pdf = DistributionPdf.new(current_organization, @distribution) + send_data pdf.render, + filename: format("%s %s.pdf", @distribution.partner.name, sortable_date(@distribution.created_at)), + type: "application/pdf", + disposition: "inline" + end + end end def reclaim @@ -37,6 +45,7 @@ def create @distribution.storage_location.distribute!(@distribution) if @distribution.save + send_notification(current_organization, @distribution) flash[:notice] = "Distribution created!" redirect_to distributions_path else @@ -107,6 +116,10 @@ def insufficient_amount! private + def send_notification(org, dist) + PartnerMailerJob.perform_async(org, dist) if Flipper.enabled?(:email_active) + end + def distribution_params params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, line_items_attributes: %i(item_id quantity _destroy)) end diff --git a/app/jobs/partner_mailer_job.rb b/app/jobs/partner_mailer_job.rb new file mode 100644 index 0000000000..0d651cb2d2 --- /dev/null +++ b/app/jobs/partner_mailer_job.rb @@ -0,0 +1,8 @@ +class PartnerMailerJob + include SuckerPunch::Job + workers 2 + + def perform(current_organization, distribution) + DistributionMailer.partner_mailer(current_organization, distribution) + end +end diff --git a/app/mailers/distribution_mailer.rb b/app/mailers/distribution_mailer.rb new file mode 100644 index 0000000000..4434e458e2 --- /dev/null +++ b/app/mailers/distribution_mailer.rb @@ -0,0 +1,13 @@ +class DistributionMailer < ApplicationMailer + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.distribution_mailer.partner_mailer.subject + # + def partner_mailer(current_organization, distribution) + @partner = distribution.partner + @distribution = distribution + attachments[format("%s %s.pdf", @partner.name, @distribution.created_at.strftime("%Y-%m-%d"))] = DistributionPdf.new(current_organization, @distribution).render + mail to: @partner.email + end +end diff --git a/app/pdfs/distribution_pdf.rb b/app/pdfs/distribution_pdf.rb new file mode 100644 index 0000000000..f92f9c5889 --- /dev/null +++ b/app/pdfs/distribution_pdf.rb @@ -0,0 +1,125 @@ +class DistributionPdf + include Prawn::View + + def initialize(organization, distribution) + @distribution = distribution + image organization.logo_path, fit: [325, 110] + bounding_box [bounds.right - 225, bounds.top - 20], width: 225 do + text organization.name, align: :right + text organization.address, align: :right + text organization.email, align: :right + end + data = [["Items Received", "Quantity"]] + data += @distribution.line_items.sorted.map do |c| + [c.item.name, c.quantity] + end + data += [["", ""], ["Total Items Received", @distribution.line_items.total]] + + move_down 55 + + font "Helvetica" + text "Issued to:", style: :bold + text @distribution.partner.name + move_down 10 + + text "Issued on:", style: :bold + text @distribution.distributed_at + move_down 10 + + text "Comments:", style: :bold + text @distribution.comment + + move_down 20 + + # Line item table + table(data) do + self.header = true + self.cell_style = { + padding: [5, 20, 5, 20] + } + self.row_colors = %w(dddddd ffffff) + + cells.borders = [] + + # Header row + row(0).borders = [:bottom] + row(0).border_width = 2 + row(0).font_style = :bold + row(0).column(-1).borders = %i(bottom left) + + # Total Items footer row + row(-1).borders = [:top] + row(-1).font_style = :bold + row(-1).column(-1).borders = %i(top left) + + # Footer spacing row + row(-2).borders = [:top] + row(-2).padding = [2, 0, 2, 0] + + column(0).width = 400 + + # Quantity column + column(1).row(1..-3).borders = [:left] + column(1).row(1..-3).border_left_color = "aaaaaa" + column(1).style align: :right + end + + move_down 50 + + summary = [["Distribution Breakdown", "Quantity"]] + summary += @distribution.line_items.quantities_by_category.to_a + + table(summary) do + self.header = true + self.cell_style = { + padding: [5, 20, 5, 20] + } + self.row_colors = %w(dddddd ffffff) + + cells.borders = [] + + # Header row + row(0).borders = [:bottom] + row(0).border_width = 2 + row(0).font_style = :bold + row(0).column(-1).borders = %i(bottom left) + + column(0).width = 400 + + # Quantity column + column(1).row(1..-1).borders = [:left] + column(1).row(1..-1).border_left_color = "aaaaaa" + column(1).style align: :right + end + + number_pages "Page of ", + start_count_at: 1, + at: [bounds.right - 130, 22], + align: :right + + repeat :all do + # Page footer + bounding_box [bounds.left, bounds.bottom + 35], width: bounds.width do + stroke_bounds + font "Helvetica" + stroke_horizontal_rule + move_down(5) + # table([ + # [organization.name, organization.address_inline, ""], + # ]) do + # self.width = bounds.width + # cells.borders = [] + # column(0).width = 125 + # column(2).width = 125 + # column(1).style align: :center + # column(2).style align: :right + # end + 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 + end +end diff --git a/app/views/distribution_mailer/partner_mailer.html.erb b/app/views/distribution_mailer/partner_mailer.html.erb new file mode 100644 index 0000000000..8e7f44c9f4 --- /dev/null +++ b/app/views/distribution_mailer/partner_mailer.html.erb @@ -0,0 +1,9 @@ +

+ Hi <%= @partner.name %>, + <%= @distribution.inspect %> +
+ Please find an attached pdf containg details about your distribution. +

+

+ You can pick up your distribution +

diff --git a/app/views/distribution_mailer/partner_mailer.text.erb b/app/views/distribution_mailer/partner_mailer.text.erb new file mode 100644 index 0000000000..4c45fd6689 --- /dev/null +++ b/app/views/distribution_mailer/partner_mailer.text.erb @@ -0,0 +1,3 @@ +Distribution#partner_mailer + +<%= @greeting %>, find me in app/views/distribution_mailer/partner_mailer.text.erb diff --git a/app/views/distributions/print.pdf.prawn b/app/views/distributions/print.pdf.prawn deleted file mode 100644 index e0d7d1f5d4..0000000000 --- a/app/views/distributions/print.pdf.prawn +++ /dev/null @@ -1,126 +0,0 @@ -prawn_document do |pdf| - pdf.image current_organization.logo_path, fit: [325, 110] - - pdf.bounding_box [pdf.bounds.right - 225, pdf.bounds.top - 20], width: 225 do - pdf.text current_organization.name, align: :right - pdf.text current_organization.address, align: :right - pdf.text current_organization.email, align: :right - end - - data = [["Items Received", "Quantity"]] - data += @distribution.line_items.sorted.map do |c| - [c.item.name, c.quantity] - end - data += [["", ""], ["Total Items Received", @distribution.line_items.total]] - - pdf.move_down 55 - - pdf.font "Helvetica" - pdf.text "Issued to:", style: :bold - pdf.text @distribution.partner.name - pdf.move_down 10 - - pdf.text "Issued on:", style: :bold - pdf.text @distribution.distributed_at - pdf.move_down 10 - - pdf.text "Comments:", style: :bold - pdf.text @distribution.comment - - - pdf.move_down 20 - - # Line item table - pdf.table(data) do - self.header = true - self.cell_style = { - padding: [5, 20, 5, 20] - } - self.row_colors = ["dddddd", "ffffff"] - - cells.borders = [] - - # Header row - row(0).borders = [:bottom] - row(0).border_width = 2 - row(0).font_style = :bold - row(0).column(-1).borders = [:bottom, :left] - - # Total Items footer row - row(-1).borders = [:top] - row(-1).font_style = :bold - row(-1).column(-1).borders = [:top, :left] - - # Footer spacing row - row(-2).borders = [:top] - row(-2).padding = [2, 0, 2, 0] - - column(0).width = 400 - - # Quantity column - column(1).row(1..-3).borders = [:left] - column(1).row(1..-3).border_left_color = "aaaaaa" - column(1).style align: :right - end - - pdf.move_down 50 - - summary = [["Distribution Breakdown", "Quantity"]] - summary += @distribution.line_items.quantities_by_category.to_a - - pdf.table(summary) do - self.header = true - self.cell_style = { - padding: [5, 20, 5, 20] - } - self.row_colors = ["dddddd", "ffffff"] - - cells.borders = [] - - # Header row - row(0).borders = [:bottom] - row(0).border_width = 2 - row(0).font_style = :bold - row(0).column(-1).borders = [:bottom, :left] - - column(0).width = 400 - - # Quantity column - column(1).row(1..-1).borders = [:left] - column(1).row(1..-1).border_left_color = "aaaaaa" - column(1).style align: :right - end - - pdf.number_pages "Page of ", { - start_count_at: 1, - at: [pdf.bounds.right - 130, 22], - align: :right - } - - pdf.repeat :all do - # Page footer - pdf.bounding_box [pdf.bounds.left, pdf.bounds.bottom + 35], width: pdf.bounds.width do - pdf.stroke_bounds - pdf.font "Helvetica" - pdf.stroke_horizontal_rule - pdf.move_down(5) - # pdf.table([ - # [current_organization.name, current_organization.address_inline, ""], - # ]) do - # self.width = pdf.bounds.width - # cells.borders = [] - # column(0).width = 125 - # column(2).width = 125 - # column(1).style align: :center - # column(2).style align: :right - # end - logo_offset = (pdf.bounds.width - 190) / 2 - pdf.bounding_box([logo_offset, 0], width: 190, height: 33) do - pdf.text "Lovingly created with", valign: :center - pdf.image Organization::DIAPER_APP_LOGO, width: 75, vposition: :center, position: :right - end - end - - end -end - diff --git a/app/views/donations/_donation_form.html.erb b/app/views/donations/_donation_form.html.erb index 3f630bc724..d061b0602c 100644 --- a/app/views/donations/_donation_form.html.erb +++ b/app/views/donations/_donation_form.html.erb @@ -38,6 +38,7 @@ collection: @storage_locations, label: "Storage Location", error: "Where is it being stored?", + selected: current_organization.intake_location, wrapper: :vertical_input_group %> diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index fd024efa63..cab9a3a8fd 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -25,17 +25,10 @@