From d27b46a81b43e69f9039ba912c8e8f5f1a43f8f8 Mon Sep 17 00:00:00 2001 From: Adrian Hinz Date: Sun, 22 Apr 2018 09:00:51 +0200 Subject: [PATCH] added pdf to order email --- app/controllers/ps_admin/order_controller.rb | 5 +- app/mailers/order_mailer.rb | 3 + app/services/order_to_pdf.rb | 64 ++++++++++++++++++++ app/views/layouts/pdf.html.erb | 0 config/initializers/mime_types.rb | 1 + config/initializers/wicked_pdf.rb | 22 +++++++ 6 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 app/services/order_to_pdf.rb create mode 100644 app/views/layouts/pdf.html.erb create mode 100644 config/initializers/wicked_pdf.rb diff --git a/app/controllers/ps_admin/order_controller.rb b/app/controllers/ps_admin/order_controller.rb index bcbe122..21db86a 100644 --- a/app/controllers/ps_admin/order_controller.rb +++ b/app/controllers/ps_admin/order_controller.rb @@ -8,7 +8,10 @@ module PsAdmin set_orders end - def show; end + def show + otp = OrderToPdf.new(@order).call + puts "#{otp.inspect}" + end def destroy return if @order.blank? diff --git a/app/mailers/order_mailer.rb b/app/mailers/order_mailer.rb index f507371..5819802 100644 --- a/app/mailers/order_mailer.rb +++ b/app/mailers/order_mailer.rb @@ -12,6 +12,9 @@ class OrderMailer < ApplicationMailer def new_order_to_shop_owner(order) @order = order ss = ScSetting.first + otp = OrderToPdf.new(@order) + attachments[otp.file_name] = otp.call + sleep(5) mail(subject: "Nowe zamówienie nr: #{@order.beauty_id}", from: "#{ss.shop_name} ", to: ss.shop_owners_emails) diff --git a/app/services/order_to_pdf.rb b/app/services/order_to_pdf.rb new file mode 100644 index 0000000..c3b237b --- /dev/null +++ b/app/services/order_to_pdf.rb @@ -0,0 +1,64 @@ +# Creates PDF for order +class OrderToPdf + def initialize(order) + @order = order + end + + def call + order_to_pdf + end + + def path_to_file + "#{Rails.root}/storage/pdfs/zamowienie_#{order.beauty_id}.pdf" + end + + def file_name + "zamowienie_#{order.beauty_id}.pdf" + end + + private + + attr_reader :order + + def order_to_html_string + ret = "

Zamówienie: '\ + "#{order.beauty_id}


"\ + ''\ + '' + order.order_products.each do |op| + ret += "" + end + ret += ''\ + ''\ + ""\ + ''\ + ""\ + ''\ + ''\ + ""\ + "
Produkt'\ + 'Cena jednostkowaIlośćSuma
#{op.product.name}PLN #{format('%.2f', op.price)}<"\ + "/td>#{op.quantity}PLN #{format('%.2f', op.multiple_price)}
TowarPLN #{format('%.2f', @order.order_value)}
WysyłkaPLN #{format('%.2f', @order.shipping.price)}
Razem do zapłatyPLN #{format('%.2f', @order.order_summary)}

Adres dostawy


#{order.full_name}
"\ + "#{order.address.gsub(/(\r\n|\n\r|\r|\n)/, '
')}"\ + '

Wiadomość od klienta:

'\ + "#{order.message_from_client.gsub(/(\r\n|\n\r|\r|\n)/, '
')}"\ + '' + ret + end + + def order_to_pdf + pdf = WickedPdf.new.pdf_from_string(order_to_html_string) + save_path = "#{Rails.root}/storage/pdfs" + FileUtils.mkdir_p save_path + file_name = "zamowienie_#{order.beauty_id}.pdf" + save_path += "/#{file_name}" + File.open(save_path, 'wb') do |file| + file << pdf + end + pdf + end +end diff --git a/app/views/layouts/pdf.html.erb b/app/views/layouts/pdf.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index dc18996..968093e 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -2,3 +2,4 @@ # Add new mime types for use in respond_to blocks: # Mime::Type.register "text/richtext", :rtf +Mime::Type.register 'application/pdf', :pdf diff --git a/config/initializers/wicked_pdf.rb b/config/initializers/wicked_pdf.rb new file mode 100644 index 0000000..beb8191 --- /dev/null +++ b/config/initializers/wicked_pdf.rb @@ -0,0 +1,22 @@ +# WickedPDF Global Configuration +# +# Use this to set up shared configuration options for your entire application. +# Any of the configuration options shown here can also be applied to single +# models by passing arguments to the `render :pdf` call. +# +# To learn more, check out the README: +# +# https://github.com/mileszs/wicked_pdf/blob/master/README.md + +WickedPdf.config = { + # Path to the wkhtmltopdf executable: This usually isn't needed if using + # one of the wkhtmltopdf-binary family of gems. + # exe_path: '/usr/local/bin/wkhtmltopdf', + # or + # exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf') + exe_path: 'c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' + # exe_path: '/usr/bin/wkhtmltopdf' + # Layout file to be used for all PDFs + # (but can be overridden in `render :pdf` calls) + # layout: 'pdf.html', +}