added pdf to order email

This commit is contained in:
Adrian Hinz 2018-04-22 09:00:51 +02:00
parent 9e467d9fbf
commit d27b46a81b
6 changed files with 94 additions and 1 deletions

View File

@ -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?

View File

@ -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} <kaktusiarnia.rumia@gmail.com>",
to: ss.shop_owners_emails)

View File

@ -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 = "<html><head><meta charset='utf-8' /><style>body{font-family: "\
'Verdana, Geneva, sans-serif;}tr.border_bottom td {'\
'border-bottom:1pt solid black;}</style></head><body><h2>Zamówienie: '\
"#{order.beauty_id}</h2><br />"\
'<table width="100%"><thead>'\
'<tr><th>Produkt</th><th>'\
'Cena jednostkowa</th><th>Ilość</th><th>Suma</th></tr></thead><tbody>'
order.order_products.each do |op|
ret += "<tr class='border_bottom'><td><strong>#{op.product.name}</strong"\
"></td><td style='text-align:center;'>PLN #{format('%.2f', op.price)}<"\
"/td><td style='text-align:center;'>#{op.quantity}</td><td style='text"\
"-align:right;'>PLN #{format('%.2f', op.multiple_price)}</td></tr>"
end
ret += '</tbody><tfoot>'\
'<tr style="text-align:right;"><td colspan="3">Towar</td>'\
"<td>PLN #{format('%.2f', @order.order_value)}</td></tr>"\
'<tr style="text-align:right;"><td colspan="3">Wysyłka</td>'\
"<td >PLN #{format('%.2f', @order.shipping.price)}</td></tr>"\
'<tr style="text-align:right; font-weight:bold;">'\
'<td colspan="3">Razem do zapłaty</td>'\
"<td>PLN #{format('%.2f', @order.order_summary)}</td></tr></tfoot>"\
"</table><br/><h3>Adres dostawy</h3> <br/>#{order.full_name}<br/>"\
"#{order.address.gsub(/(\r\n|\n\r|\r|\n)/, '<br \>')}"\
'<br/><h3>Wiadomość od klienta:</h3>'\
"#{order.message_from_client.gsub(/(\r\n|\n\r|\r|\n)/, '<br \>')}"\
'</body></html>'
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

View File

View File

@ -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

View File

@ -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',
}