diff --git a/README.md b/README.md index 1fe6190..54cb1c7 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Returns, canceled orders and refunded deposits are deducted from the month's sal Once the month is over, **Approve for payroll** reads QuickBooks one last time. It records the sales, deductions, threshold, rates and payouts, so the month no longer changes with QuickBooks or the settings. **Payroll CSV** downloads each rep's payout. **Reopen** puts an approved month back to being worked out from QuickBooks; the unticked items, adjustments and notes are kept. -Approvers see each invoice linked to its page in Redmine (or its PDF without the Write invoices permission), the sales receipts, refund receipts and credit memos linked to QuickBooks, and the list of months with the month picker. +Approvers see each invoice linked to its page in Redmine (or its PDF without the Write invoices permission), each sales receipt, refund receipt and credit memo linked to QuickBooks' PDF of it (opened through Redmine, without signing in to QuickBooks), and the list of months with the month picker. ## Requirements diff --git a/app/controllers/sales_commissions_controller.rb b/app/controllers/sales_commissions_controller.rb index 1d77eea..360704d 100644 --- a/app/controllers/sales_commissions_controller.rb +++ b/app/controllers/sales_commissions_controller.rb @@ -16,7 +16,7 @@ class SalesCommissionsController < ApplicationController menu_item :sales_commissions before_action :authorize_global - before_action :find_month, except: [:index, :current] + before_action :find_month, except: [:index, :current, :txn_pdf] before_action :find_open_record, only: [:update, :add_adjustment, :remove_adjustment] helper :sales_commissions @@ -120,6 +120,19 @@ class SalesCommissionsController < ApplicationController render_404 end + # QuickBooks' PDF of a sales receipt, refund receipt or credit memo, shown in the browser + def txn_pdf + pdf = RedmineQboCommission::Txn.pdf(params[:kind], params[:txn_id]) + send_data pdf, type: 'application/pdf', disposition: 'inline', + filename: "#{params[:kind].to_s.dasherize}-#{params[:txn_id].to_i}.pdf" + rescue KeyError + render_404 + rescue StandardError => e + Rails.logger.error "[redmine_qbo_commission] PDF of #{params[:kind]} #{params[:txn_id]}: #{e.message}" + flash[:error] = l(:error_sales_commission_qbo, error: e.message) + redirect_back_or_default sales_commissions_path + end + private def find_month diff --git a/app/helpers/sales_commissions_helper.rb b/app/helpers/sales_commissions_helper.rb index 33abde2..263a49b 100644 --- a/app/helpers/sales_commissions_helper.rb +++ b/app/helpers/sales_commissions_helper.rb @@ -17,8 +17,9 @@ module SalesCommissionsHelper RedmineQboCommission.format_rate(rate) end - # The transaction's number, linked for those who approve: an invoice opens in Redmine (its invoice page, or its - # PDF without the Write invoices permission); the others, which Redmine has no page for, open in QuickBooks + # The transaction's number, linked for those who approve: an invoice opens its page in Redmine (or its PDF + # without the Write invoices permission); a sales receipt, refund receipt or credit memo, which Redmine has no + # page for, opens QuickBooks' PDF of it def commission_txn_link(txn, can_approve) label = txn.doc_number.present? ? "##{txn.doc_number}" : txn.kind_name return label unless can_approve @@ -26,7 +27,7 @@ module SalesCommissionsHelper if txn.kind == 'invoice' link_to(label, RedmineQboBilling.invoice_writer? ? edit_qbo_invoice_path(txn.txn_id) : invoice_path(txn.txn_id)) else - link_to(label, txn.qbo_url, target: '_blank', rel: 'noopener') + link_to(label, sales_commission_txn_pdf_path(txn.kind, txn.txn_id), target: '_blank', rel: 'noopener') end end diff --git a/config/routes.rb b/config/routes.rb index 92cbc64..8127a49 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,6 +25,10 @@ RedmineApp::Application.routes.draw do delete 'sales_commission/:month/adjustments/:id', to: 'sales_commissions#remove_adjustment', as: :sales_commission_adjustment, constraints: month + # QuickBooks' PDF of a sales receipt, refund receipt or credit memo (e.g. /sales_commission/pdf/credit_memo/123) + get 'sales_commission/pdf/:kind/:txn_id', to: 'sales_commissions#txn_pdf', as: :sales_commission_txn_pdf, + constraints: { kind: /sales_receipt|refund_receipt|credit_memo/, txn_id: /\d+/ } + # Administration ยป Commission reps resources :commission_reps, except: [:show] diff --git a/init.rb b/init.rb index b50342b..37b44d6 100644 --- a/init.rb +++ b/init.rb @@ -34,7 +34,7 @@ Redmine::Plugin.register :redmine_qbo_commission do # The months, adjusting and approving a month for payroll, reopening it, the payroll CSV permission :approve_sales_commission, { sales_commissions: [:index, :current, :show, :update, :refresh, :reopen, - :add_adjustment, :remove_adjustment] }, + :add_adjustment, :remove_adjustment, :txn_pdf] }, global: true menu :top_menu, :sales_commissions, { controller: 'sales_commissions', action: 'current' }, diff --git a/lib/redmine_qbo_commission/txn.rb b/lib/redmine_qbo_commission/txn.rb index dcde1bb..9fb4685 100644 --- a/lib/redmine_qbo_commission/txn.rb +++ b/lib/redmine_qbo_commission/txn.rb @@ -50,12 +50,18 @@ module RedmineQboCommission I18n.t("label_sales_commission_kind_#{kind}") end - # The transaction's page in QuickBooks - def qbo_url - host = defined?(RedmineQbo) && RedmineQbo.respond_to?(:sandbox_mode?) && RedmineQbo.sandbox_mode? ? 'sandbox' : 'app' - page = { 'invoice' => 'invoice', 'sales_receipt' => 'salesreceipt', 'refund_receipt' => 'refundreceipt', - 'credit_memo' => 'creditmemo' }[kind] - "https://#{host}.qbo.intuit.com/app/#{page}?txnId=#{txn_id}" + # The QuickBooks services that print the transactions Redmine has no page for + PDF_SERVICES = { 'sales_receipt' => 'SalesReceipt', 'refund_receipt' => 'RefundReceipt', + 'credit_memo' => 'CreditMemo' }.freeze + + # QuickBooks' PDF of a sales receipt, refund receipt or credit memo (an invoice opens in Redmine instead) + def self.pdf(kind, txn_id) + entity = PDF_SERVICES.fetch(kind.to_s) + qbo = QboConnectionService.current! + qbo.perform_authenticated_request do |access_token| + service = "Quickbooks::Service::#{entity}".constantize.new(company_id: qbo.realm_id, access_token: access_token) + service.pdf(Struct.new(:id).new(txn_id.to_s)) + end end end end diff --git a/test/integration/sales_commissions_test.rb b/test/integration/sales_commissions_test.rb index bc9e1b9..d5ece3c 100644 --- a/test/integration/sales_commissions_test.rb +++ b/test/integration/sales_commissions_test.rb @@ -57,10 +57,10 @@ class SalesCommissionsTest < Redmine::IntegrationTest assert_select 'input[name=?][value=?][checked]', 'included[]', 'refund_receipt:3' assert_select 'input[name=?][value="2"][checked]', 'eligible[]' assert_select 'input[name=approve]' - # Invoices open in Redmine, the refund receipt (no Redmine page) in QuickBooks + # Invoices open in Redmine, the refund receipt (no Redmine page) as QuickBooks' PDF through Redmine assert_select '#sales-commission-sales a[href=?]', '/qbo_invoices/1/edit' - assert_select '#sales-commission-sales a[href*="qbo.intuit.com"]', count: 0 - assert_select '#sales-commission-deductions a[href*="qbo.intuit.com/app/refundreceipt?txnId=3"]' + assert_select '#sales-commission-deductions a[href=?]', '/sales_commission/pdf/refund_receipt/3' + assert_select 'a[href*="qbo.intuit.com"]', count: 0 end def test_view_only @@ -181,4 +181,22 @@ class SalesCommissionsTest < Redmine::IntegrationTest post '/sales_commission/2026-10/refresh' assert_redirected_to '/sales_commission/2026-10' end + + def test_txn_pdf + RedmineQboCommission::Txn.expects(:pdf).with('credit_memo', '123').returns('%PDF-1.4 credit memo') + log_user('admin', 'admin') + get '/sales_commission/pdf/credit_memo/123' + assert_response :success + assert_equal 'application/pdf', response.media_type + assert_equal '%PDF-1.4 credit memo', response.body + get '/sales_commission/pdf/invoice/123' + assert_response :not_found + end + + def test_txn_pdf_needs_approve + Role.find(1).add_permission!(:view_sales_commission) + log_user('jsmith', 'jsmith') + get '/sales_commission/pdf/sales_receipt/5' + assert_response :forbidden + end end