From 5b6c1ed2c23eb5e07de8b62f7ff84962ce70ef66 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Tue, 17 Nov 2026 18:34:21 -0500 Subject: [PATCH] Cache invoices' sales tax and the day they were paid in full New invoices columns tax_amount and paid_on, filled in by the invoice sync. An invoice is paid in full on the later of its date and the dates of the payments and credit memos applied to it (local payments first; QuickBooks only for ones not synced yet and credit memos), so reports such as the monthly sales commission don't have to read invoices from QuickBooks. A full sync fills them in for older invoices; it now syncs payments before invoices. Co-Authored-By: Claude Opus 5.5 --- app/jobs/qbo_sync_dispatcher.rb | 5 +- app/services/invoice_sync_service.rb | 3 + db/migrate/054_add_invoice_tax_and_paid_on.rb | 20 +++++ lib/redmine_qbo_billing/invoice_paid_on.rb | 65 +++++++++++++++ test/unit/invoice_paid_on_test.rb | 79 +++++++++++++++++++ 5 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 db/migrate/054_add_invoice_tax_and_paid_on.rb create mode 100644 lib/redmine_qbo_billing/invoice_paid_on.rb create mode 100644 test/unit/invoice_paid_on_test.rb diff --git a/app/jobs/qbo_sync_dispatcher.rb b/app/jobs/qbo_sync_dispatcher.rb index eb8737c..8104493 100644 --- a/app/jobs/qbo_sync_dispatcher.rb +++ b/app/jobs/qbo_sync_dispatcher.rb @@ -10,12 +10,13 @@ class QboSyncDispatcher + # Payments before invoices: an invoice's paid-in-full day is worked out from the local payments' dates SYNC_JOBS = [ Customer, Estimate, + Payment, Invoice, - Employee, - Payment + Employee ].freeze # Dispatches all synchronization jobs to perform a full sync of QuickBooks entities with the local database. diff --git a/app/services/invoice_sync_service.rb b/app/services/invoice_sync_service.rb index 8b2a39c..0bf536b 100644 --- a/app/services/invoice_sync_service.rb +++ b/app/services/invoice_sync_service.rb @@ -27,6 +27,9 @@ class InvoiceSyncService < SyncServiceBase # Man-hours: quantities of the labor products/services (plugin settings) map_attribute(:hours) { |remote| RedmineQboBilling::ManHours.hours(remote) } map_attribute :qbo_updated_at, "meta_data.last_updated_time" + # Sales tax, and the day it was paid in full (nil while open), for reports such as monthly paid sales + map_attribute(:tax_amount) { |remote| RedmineQboBilling::InvoicePaidOn.tax(remote) } + map_attribute(:paid_on) { |remote| RedmineQboBilling::InvoicePaidOn.paid_on(remote) } map_belongs_to :customer end \ No newline at end of file diff --git a/db/migrate/054_add_invoice_tax_and_paid_on.rb b/db/migrate/054_add_invoice_tax_and_paid_on.rb new file mode 100644 index 0000000..1def1ae --- /dev/null +++ b/db/migrate/054_add_invoice_tax_and_paid_on.rb @@ -0,0 +1,20 @@ +#The MIT License (MIT) +# +#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: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +# An invoice's sales tax and the day it was paid in full, cached like its total and balance, so reports (e.g. +# redmine_qbo_commission's monthly paid sales) don't have to read invoices from QuickBooks. A full invoice sync +# fills them in for invoices synced before. +class AddInvoiceTaxAndPaidOn < ActiveRecord::Migration[7.0] + def change + add_column :invoices, :tax_amount, :decimal, precision: 15, scale: 2 unless column_exists?(:invoices, :tax_amount) + add_column :invoices, :paid_on, :date unless column_exists?(:invoices, :paid_on) + add_index :invoices, :paid_on unless index_exists?(:invoices, :paid_on) + end +end diff --git a/lib/redmine_qbo_billing/invoice_paid_on.rb b/lib/redmine_qbo_billing/invoice_paid_on.rb new file mode 100644 index 0000000..2c93e7c --- /dev/null +++ b/lib/redmine_qbo_billing/invoice_paid_on.rb @@ -0,0 +1,65 @@ +#The MIT License (MIT) +# +#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: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +module RedmineQboBilling + # An invoice's sales tax and the day it was paid in full, worked out from the QuickBooks invoice when it's synced + # (InvoiceSyncService). + # + # An invoice is paid in full on the later of its own date and the dates of the payments and credit memos applied + # to it, so a deposit taken before the work counts on the day the job was invoiced. The payments' dates come from + # the local payments (synced from QuickBooks); QuickBooks is only asked for the ones not synced yet and for credit + # memos. + module InvoicePaidOn + APPLIED_TYPES = %w[Payment CreditMemo].freeze + IDS_PER_QUERY = 100 + + module_function + + def tax(remote) + tax = remote.respond_to?(:txn_tax_detail) ? remote.txn_tax_detail&.total_tax : nil + tax.nil? ? BigDecimal('0') : BigDecimal(tax.to_s) + end + + # The day the invoice was paid in full, or nil while it's open (or voided: a $0 total) + def paid_on(remote, dates: nil) + return nil unless remote.txn_date + return nil unless BigDecimal(remote.balance.to_s.presence || '1').zero? + return nil unless BigDecimal(remote.total.to_s.presence || '0').positive? + + dates ||= applied_dates(applied(remote)) + [remote.txn_date, *applied(remote).map { |type, id| dates[[type, id]] }.compact].max + end + + # [["Payment", "123"], ...] applied to the invoice + def applied(remote) + Array(remote.linked_transactions).select { |link| APPLIED_TYPES.include?(link.txn_type.to_s) } + .map { |link| [link.txn_type.to_s, link.txn_id.to_s] } + end + + # { ["Payment", "123"] => Date }: local payments first, the rest read from QuickBooks + def applied_dates(links) + dates = {} + payment_ids = links.select { |type, _| type == 'Payment' }.map(&:last) + Payment.where(id: payment_ids).pluck(:id, :txn_date).each { |id, date| dates[['Payment', id.to_s]] = date if date } + missing = links.reject { |link| dates.key?(link) } + missing.group_by(&:first).each do |type, list| + list.map(&:last).uniq.each_slice(IDS_PER_QUERY) do |ids| + query = "SELECT * FROM #{type} WHERE Id IN (#{ids.map { |id| "'#{id.gsub(/\D/, '')}'" }.join(',')})" + records = QboConnectionService.with_qbo_service(entity: type) { |service| service.query(query).entries } + Array(records).each { |record| dates[[type, record.id.to_s]] = record.txn_date } + end + end + dates + rescue StandardError => e + Rails.logger.warn "[RedmineQboBilling] Could not read the dates of what was applied to an invoice: #{e.message}" + dates + end + end +end diff --git a/test/unit/invoice_paid_on_test.rb b/test/unit/invoice_paid_on_test.rb new file mode 100644 index 0000000..a77839d --- /dev/null +++ b/test/unit/invoice_paid_on_test.rb @@ -0,0 +1,79 @@ +#The MIT License (MIT) +# +#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: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +require File.expand_path('../test_helper', __dir__) + +class InvoicePaidOnTest < ActiveSupport::TestCase + PaidOn = RedmineQboBilling::InvoicePaidOn + + def setup + Payment.where(id: [9301, 9302]).delete_all + end + + def link(type, id) + link = Quickbooks::Model::LinkedTransaction.new + link.txn_type = type + link.txn_id = id.to_s + link + end + + def invoice(date: Date.new(2026, 10, 30), total: 2207.99, balance: 0, links: [], tax: nil) + invoice = Quickbooks::Model::Invoice.new + invoice.id = '601' + invoice.txn_date = date + invoice.total = BigDecimal(total.to_s) + invoice.balance = BigDecimal(balance.to_s) + invoice.linked_transactions = links + if tax + invoice.txn_tax_detail = Quickbooks::Model::TransactionTaxDetail.new + invoice.txn_tax_detail.total_tax = BigDecimal(tax.to_s) + end + invoice + end + + def local_payment(id, date) + payment = Payment.new(id: id, txn_date: date, total_amount: 100) + payment.skip_qbo_push = true + payment.save!(validate: false) + end + + def test_tax + assert_equal BigDecimal('80.30'), PaidOn.tax(invoice(tax: '80.30')) + assert_equal BigDecimal('0'), PaidOn.tax(invoice) + end + + def test_paid_in_full_on_the_last_payment + local_payment(9301, Date.new(2026, 10, 8)) # deposit before the invoice + local_payment(9302, Date.new(2026, 11, 3)) # final payment + remote = invoice(links: [link('Payment', 9301), link('Payment', 9302), link('Estimate', 598)]) + QboConnectionService.expects(:with_qbo_service).never + assert_equal Date.new(2026, 11, 3), PaidOn.paid_on(remote) + end + + def test_deposit_before_the_invoice_counts_on_the_invoice_date + local_payment(9301, Date.new(2026, 10, 8)) + assert_equal Date.new(2026, 10, 30), PaidOn.paid_on(invoice(links: [link('Payment', 9301)])) + end + + def test_payments_not_synced_and_credit_memos_are_read_from_quickbooks + memo = Quickbooks::Model::CreditMemo.new + memo.id = '70' + memo.txn_date = Date.new(2026, 11, 9) + service = mock('service') + service.stubs(:query).returns(stub(entries: [memo])) + QboConnectionService.expects(:with_qbo_service).with(entity: 'CreditMemo').yields(service).returns([memo]) + assert_equal Date.new(2026, 11, 9), PaidOn.paid_on(invoice(links: [link('CreditMemo', 70)])) + end + + def test_open_and_voided_invoices_arent_paid + assert_nil PaidOn.paid_on(invoice(balance: 10)) + assert_nil PaidOn.paid_on(invoice(total: 0)) + end +end