From 704dff2a72c1493d7ae7e57d6d0c6df18b985ac7 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Mon, 19 Jan 2026 19:33:29 -0500 Subject: [PATCH] Multiple Invoices to PDF --- Gemfile | 1 + app/controllers/invoice_controller.rb | 31 ++++++++++++++++++++++----- app/views/invoices/_list.html.erb | 17 ++++++++++++--- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index caa0961..ebd7f88 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'will_paginate' gem 'rails-jquery-autocomplete' gem 'jquery-ui-rails' gem 'rexml' +gem 'combine_pdf' group :assets do gem 'coffee-rails' diff --git a/app/controllers/invoice_controller.rb b/app/controllers/invoice_controller.rb index c193fcd..52edbc2 100644 --- a/app/controllers/invoice_controller.rb +++ b/app/controllers/invoice_controller.rb @@ -1,6 +1,6 @@ #The MIT License (MIT) # -#Copyright (c) 2024 rick barrette +#Copyright (c) 2026 rick barrette # #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # @@ -10,7 +10,8 @@ class InvoiceController < ApplicationController include AuthHelper - + require 'combine_pdf' + before_action :require_user, :unless => proc {|c| session[:token].nil? } skip_before_action :verify_authenticity_token, :check_if_login_required, :unless => proc {|c| session[:token].nil? } @@ -18,13 +19,33 @@ class InvoiceController < ApplicationController # Downloads and forwards the invoice pdf # def show + logger.info("Processing request for URL: #{request.original_url}") begin qbo = Qbo.first qbo.perform_authenticated_request do |access_token| service = Quickbooks::Service::Invoice.new(:company_id => qbo.realm_id, :access_token => access_token) - invoice = service.fetch_by_id(params[:id]) - @pdf = service.pdf(invoice) - send_data @pdf, filename: "invoice #{invoice.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" + + # If multiple id's then pull each pdf & combine them + if params[:item_ids] + logger.info("Grabbing pdfs for " + params[:item_ids].join(', ')) + ref = "" + params[:item_ids].each do |i| + logger.info("processing " + i) + invoice = service.fetch_by_id(i) + ref += " #{invoice.doc_number}" + @pdf << CombinePDF.parse(service.pdf(invoice)) unless @pdf.nil? + if @pdf.nil? + @pdf = CombinePDF.parse(service.pdf(invoice)) + end + end + @pdf = @pdf.to_pdf + else + invoice = service.fetch_by_id(params[:id]) + @pdf = service.pdf(invoice) + ref = invoice.doc_number + end + + send_data @pdf, filename: "invoice #{ref}.pdf", :disposition => 'inline', :type => "application/pdf" end rescue redirect_to :back, :flash => { :error => "Invoice not found" } diff --git a/app/views/invoices/_list.html.erb b/app/views/invoices/_list.html.erb index 212eb8d..a38b3db 100644 --- a/app/views/invoices/_list.html.erb +++ b/app/views/invoices/_list.html.erb @@ -1,9 +1,20 @@ <% if @customer.present? %> - <% @customer.invoices.order(id: :desc).each do |invoice| %> -
- <%= link_to "##{invoice.doc_number}", invoice_path(invoice), target: :_blank %> <%= invoice.txn_date %> + <%= form_with(url: invoice_path, method: :get) do |form| %> + +
+ <%= check_box_tag "select_all", "1", false, id: "select-all-batches" %> + <%= label_tag "select-all-batches", "Select All Items" %>
+ + <% @customer.invoices.order(id: :desc).each do |invoice| %> +
+ <%= check_box_tag "item_ids[]", invoice.id, false,data: { checkbox_select_all_target: "checkbox", class: "item-checkbox" } %> + <%= link_to "##{invoice.doc_number}", invoice_path(invoice), target: :_blank %> <%= invoice.txn_date %> +
+ <% end %> + + <%= form.submit "Bulk PDF" %> <% end %> <% else %>