|
| 1 | +Spree::Order.class_eval do |
| 2 | + |
| 3 | + # Updates +invoice_number+ without calling ActiveRecord callbacks |
| 4 | + # |
| 5 | + # Only updates if number is not already present and if |
| 6 | + # +Spree::PrintInvoice::Config.next_number+ is set and greater than zero. |
| 7 | + # |
| 8 | + # Also sets +invoice_date+ to current date. |
| 9 | + # |
| 10 | + def update_invoice_number! |
| 11 | + return unless Spree::PrintInvoice::Config.use_sequential_number? |
| 12 | + return if invoice_number.present? |
| 13 | + |
| 14 | + update_columns( |
| 15 | + invoice_number: Spree::PrintInvoice::Config.increase_invoice_number, |
| 16 | + invoice_date: Date.today |
| 17 | + ) |
| 18 | + end |
| 19 | + |
| 20 | + # Returns the given template as pdf binary suitable for Rails send_data |
| 21 | + # |
| 22 | + # If the file is already present it returns this |
| 23 | + # else it generates a new file, stores and returns this. |
| 24 | + # |
| 25 | + # You can disable the pdf file generation with setting |
| 26 | + # |
| 27 | + # Spree::PrintInvoice::Config.store_pdf to false |
| 28 | + # |
| 29 | + def pdf_file(template) |
| 30 | + if Spree::PrintInvoice::Config.store_pdf |
| 31 | + send_or_create_pdf(template) |
| 32 | + else |
| 33 | + render_pdf(template) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + # = The PDF filename |
| 38 | + # |
| 39 | + # Tries to take invoice_number attribute. |
| 40 | + # If this is not present it takes the order number. |
| 41 | + # |
| 42 | + def pdf_filename |
| 43 | + invoice_number.present? ? invoice_number : number |
| 44 | + end |
| 45 | + |
| 46 | + # = PDF file path for template name |
| 47 | + # |
| 48 | + def pdf_file_path(template) |
| 49 | + Rails.root.join(pdf_storage_path(template), "#{pdf_filename}.pdf") |
| 50 | + end |
| 51 | + |
| 52 | + # = PDF storage folder path for given template name |
| 53 | + # |
| 54 | + # Configure the storage path with +Spree::PrintInvoice::Config.storage_path+ |
| 55 | + # |
| 56 | + # Each template type gets it own pluralized folder inside |
| 57 | + # of +Spree::PrintInvoice::Config.storage_path+ |
| 58 | + # |
| 59 | + # == Example: |
| 60 | + # |
| 61 | + # pdf_storage_path('invoice') => "tmp/pdf_prints/invoices" |
| 62 | + # |
| 63 | + # Creates the folder if it's not present yet. |
| 64 | + # |
| 65 | + def pdf_storage_path(template) |
| 66 | + storage_path = Rails.root.join(Spree::PrintInvoice::Config.storage_path, template.pluralize) |
| 67 | + FileUtils.mkdir_p(storage_path) |
| 68 | + storage_path |
| 69 | + end |
| 70 | + |
| 71 | + # Renders the prawn template for give template name in context of ActionView. |
| 72 | + # |
| 73 | + # Prawn templates need to be placed in +app/views/spree/admin/orders/+ folder. |
| 74 | + # |
| 75 | + # Assigns +@order+ instance variable |
| 76 | + # |
| 77 | + def render_pdf(template) |
| 78 | + ActionView::Base.new( |
| 79 | + ActionController::Base.view_paths, |
| 80 | + {order: self} |
| 81 | + ).render(template: "spree/admin/orders/#{template}.pdf.prawn") |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + # Sends stored pdf for given template from disk. |
| 87 | + # |
| 88 | + # Renders and stores it if it's not yet present. |
| 89 | + # |
| 90 | + def send_or_create_pdf(template) |
| 91 | + file_path = pdf_file_path(template) |
| 92 | + |
| 93 | + unless File.exist?(file_path) |
| 94 | + File.open(file_path, "wb") { |f| f.puts render_pdf(template) } |
| 95 | + end |
| 96 | + |
| 97 | + IO.binread(file_path) |
| 98 | + end |
| 99 | +end |
0 commit comments