downloadmultiple as zip
This commit is contained in:
parent
6058b2f239
commit
1933f21835
7 changed files with 47 additions and 5 deletions
|
|
@ -3,11 +3,15 @@ module Concerns::SendGroupOrderInvoicePdf
|
|||
|
||||
protected
|
||||
|
||||
def send_group_order_invoice_pdf(group_order_invoice)
|
||||
def create_invoice_pdf(group_order_invoice)
|
||||
invoice_data = group_order_invoice.load_data_for_invoice
|
||||
invoice_data[:title] = t('documents.group_order_invoice_pdf.title', supplier: invoice_data[:supplier])
|
||||
invoice_data[:no_footer] = true
|
||||
pdf = GroupOrderInvoicePdf.new invoice_data
|
||||
GroupOrderInvoicePdf.new invoice_data
|
||||
end
|
||||
|
||||
def send_group_order_invoice_pdf(group_order_invoice)
|
||||
pdf = create_invoice_pdf(group_order_invoice)
|
||||
send_data pdf.to_pdf, filename: pdf.filename, type: 'application/pdf'
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -56,4 +56,32 @@ class GroupOrderInvoicesController < ApplicationController
|
|||
rescue => error
|
||||
redirect_back fallback_location: root_path, notice: 'Something went wrong', :alert => I18n.t('errors.general_msg', :msg => error)
|
||||
end
|
||||
|
||||
def download_all
|
||||
order = Order.find(params[:order_id])
|
||||
|
||||
invoices = order.group_orders.map(&:group_order_invoice)
|
||||
pdf = {}
|
||||
|
||||
temp_file = Tempfile.new("all_invoices_for_order_#{order.id}.zip")
|
||||
|
||||
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
|
||||
invoices.each do |invoice|
|
||||
pdf = create_invoice_pdf(invoice)
|
||||
invoice_file = Tempfile.new("#{pdf.filename}")
|
||||
File.open(invoice_file.path, 'w:ASCII-8BIT') do |file|
|
||||
file.write(pdf.to_pdf)
|
||||
end
|
||||
zipfile.add("#{pdf.filename}", invoice_file.path) unless zipfile.find_entry("#{pdf.filename}")
|
||||
end
|
||||
end
|
||||
|
||||
zip_data = File.read(temp_file.path)
|
||||
|
||||
respond_to do |format|
|
||||
format.html {
|
||||
send_data(zip_data, type: 'application/zip', filename: "#{l order.ends, format: :file}-#{order.supplier.name}-#{order.id}.zip", disposition: 'attachment')
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue