Read paid invoices from Redmine instead of QuickBooks

Invoices paid in full in the month come from redmine_qbo's cached
invoices (their sales tax and paid-in-full day, redmine_qbo
2026.11.21), so viewing a month no longer reads every paid invoice
changed since it began, with their payments and credit memos, from
QuickBooks. Sales receipts, refund receipts and credit memos dated in
the month are still read from QuickBooks, together, and cached.
Read from QuickBooks and Approve first sync the invoices changed since
the last sync. Approvers are warned while paid invoices still lack
their paid-in-full day (a full sync fills it in).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-17 18:34:21 -05:00
1 parent 4229426621
commit ccc70171b4
6 files changed
+158 -159

No files matched your search

+74 -84
View File
@@ -9,44 +9,53 @@
#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 RedmineQboCommission
# Reads a month's paid sales and deductions from QuickBooks Online (SOP-OPS-013 §4):
# A month's paid sales and deductions (SOP-OPS-013 §4):
#
# * Invoices paid in full that month. An invoice is paid in full on the later of its own date and the date of
# the last payment or credit memo applied to it, so a deposit taken months before the work counts in the
# month it was invoiced and the last of it paid. QuickBooks updates an invoice when a payment is applied to
# it, so the invoices with a $0 balance changed since the month began are all that need reading.
# * Sales receipts dated in the month (counter sales paid on the spot), if the settings say so.
# * Refund receipts and credit memos dated in the month, as deductions, if the settings say so. Credit memos
# made only of the excluded products/services (e.g. tech bonus credits) aren't returns and are left out.
# * Invoices paid in full that month, from Redmine's own copy of the invoices (redmine_qbo keeps each invoice's
# sales tax and the day it was paid in full: the later of its date and the date of the last payment or credit
# memo applied to it, so a deposit taken before the work counts in the month the job was invoiced).
# * Sales receipts dated in the month (counter sales paid on the spot), if the settings say so, read from
# QuickBooks.
# * Refund receipts and credit memos dated in the month, as deductions, if the settings say so, read from
# QuickBooks. Credit memos made only of the excluded products/services (e.g. tech bonus credits) aren't
# returns and are left out.
#
# Voided transactions (a $0 total) don't count. Amounts are kept without sales tax.
# What's read from QuickBooks is kept for CACHE_TTL; the three reads run at the same time. Voided transactions
# (a $0 total) don't count. Amounts are kept without sales tax.
class QboSource
CACHE_TTL = 15.minutes
BATCH = 1000
IDS_PER_QUERY = 100
# The month's transactions, read from QuickBooks at most every CACHE_TTL (refresh: true reads them now)
# The month's transactions. refresh: true first brings Redmine's invoices up to date with QuickBooks (only
# those changed since the last sync), and reads the receipts and credit memos again now.
def self.transactions(month, refresh: false)
Rails.cache.delete(cache_key(month)) if refresh
cached = Rails.cache.fetch(cache_key(month), expires_in: CACHE_TTL) do
{ 'read_at' => Time.current.iso8601, 'txns' => new(month).fetch.map(&:to_cache) }
if refresh
sync_invoices
Rails.cache.delete(cache_key(month))
end
cached['txns'].map { |hash| Txn.from_h(hash) }
cached = Rails.cache.fetch(cache_key(month), expires_in: CACHE_TTL) do
{ 'read_at' => Time.current.iso8601, 'txns' => new(month).receipts.map(&:to_cache) }
end
(new(month).invoices + cached['txns'].map { |hash| Txn.from_h(hash) }).sort_by { |txn| [txn.txn_date, txn.doc_number.to_s] }
end
# When the cached transactions were read from QuickBooks, or nil when they aren't cached
# When the receipts and credit memos were read from QuickBooks, or nil when they aren't cached
def self.read_at(month)
value = Rails.cache.read(cache_key(month))
value && Time.zone.parse(value['read_at'].to_s)
end
def self.cache_key(month)
"redmine_qbo_commission/txns/#{month.to_param}"
"redmine_qbo_commission/receipts/#{month.to_param}"
end
# The day an invoice was paid in full: the later of its date and the dates of what was applied to it
def self.paid_on(txn_date, applied_dates)
[txn_date, *applied_dates.compact].compact.max
# Paid invoices synced before redmine_qbo kept the day they were paid in full (a full invoice sync fills it in)
def self.missing_paid_on?
Invoice.where(balance: 0, paid_on: nil).where('total_amount > 0').exists?
end
def self.sync_invoices
InvoiceSyncService.new.sync
end
attr_reader :month
@@ -55,81 +64,62 @@ module RedmineQboCommission
@month = month
end
def fetch
txns = invoices
txns += dated(Quickbooks::Service::SalesReceipt, 'SalesReceipt', 'sales_receipt') if Settings.include_sales_receipts?
txns += dated(Quickbooks::Service::RefundReceipt, 'RefundReceipt', 'refund_receipt') if Settings.deduct_refund_receipts?
txns += credit_memos if Settings.deduct_credit_memos?
txns.sort_by { |txn| [txn.txn_date, txn.doc_number.to_s] }
# Invoices paid in full in the month, from Redmine's invoices
def invoices
Invoice.where(paid_on: month.range).where('total_amount > 0').includes(:customer).map do |invoice|
Txn.new(kind: 'invoice', txn_id: invoice.id.to_s, doc_number: invoice.doc_number.to_s,
customer_name: invoice.customer&.name.to_s, txn_date: invoice.paid_on,
total: invoice.total_amount.to_d, tax: invoice.tax_amount.to_d)
end
end
# Sales receipts, refund receipts and credit memos dated in the month, read from QuickBooks together
def receipts
reads = []
reads << ['SalesReceipt', 'sales_receipt'] if Settings.include_sales_receipts?
reads << ['RefundReceipt', 'refund_receipt'] if Settings.deduct_refund_receipts?
reads << ['CreditMemo', 'credit_memo'] if Settings.deduct_credit_memos?
return [] if reads.empty?
excluded = Settings.excluded_item_ids
services = reads.to_h { |entity, _| [entity, "Quickbooks::Service::#{entity}".constantize] }
qbo = QboConnectionService.current!
# The threads only call QuickBooks; everything else is done here, after they're joined
records = qbo.perform_authenticated_request do |access_token|
threads = reads.map do |entity, _|
service = services[entity].new(company_id: qbo.realm_id, access_token: access_token)
sql = "SELECT * FROM #{entity} WHERE TxnDate >= '#{month.start_date.iso8601}' AND TxnDate <= '#{month.end_date.iso8601}'"
Thread.new { query(service, sql) }
end
reads.map(&:last).zip(threads.map(&:value))
end
records.flat_map do |kind, list|
list.filter_map do |record|
next unless record.total.to_d.positive?
next if kind == 'credit_memo' && !return?(record, excluded)
txn(kind, record)
end
end
end
private
def invoices
since = month.start_date.in_time_zone.iso8601
remote = query(Quickbooks::Service::Invoice,
"SELECT * FROM Invoice WHERE Balance = '0' AND MetaData.LastUpdatedTime >= '#{since}'")
remote = remote.select { |invoice| invoice.total.to_d.positive? && invoice.txn_date && invoice.txn_date <= month.end_date }
dates = applied_dates(remote)
remote.filter_map do |invoice|
applied = linked(invoice).map { |link| dates[[link.txn_type, link.txn_id.to_s]] }
paid_on = self.class.paid_on(invoice.txn_date, applied)
txn('invoice', invoice, paid_on) if month.range.cover?(paid_on)
end
# A credit memo is a return unless it's made only of the excluded products/services
def return?(memo, excluded)
items = Array(memo.line_items).filter_map { |line| line.sales_item_line_detail&.item_ref&.value&.to_s }
excluded.empty? || items.empty? || !items.all? { |item| excluded.include?(item) }
end
def credit_memos
excluded = Settings.excluded_item_ids
dated(Quickbooks::Service::CreditMemo, 'CreditMemo', 'credit_memo') do |memo|
items = Array(memo.line_items).filter_map { |line| line.sales_item_line_detail&.item_ref&.value&.to_s }
excluded.empty? || items.empty? || !items.all? { |item| excluded.include?(item) }
end
end
# The transactions of a type dated in the month (that the block keeps)
def dated(service_class, entity, kind)
remote = query(service_class, "SELECT * FROM #{entity} WHERE TxnDate >= '#{month.start_date.iso8601}' " \
"AND TxnDate <= '#{month.end_date.iso8601}'")
remote.filter_map do |record|
next unless record.total.to_d.positive?
next if block_given? && !yield(record)
txn(kind, record, record.txn_date)
end
end
# { ["Payment", "123"] => Date } for the payments and credit memos applied to the invoices
def applied_dates(invoices)
ids = Hash.new { |hash, type| hash[type] = [] }
invoices.each { |invoice| linked(invoice).each { |link| ids[link.txn_type] << link.txn_id.to_s } }
{ 'Payment' => Quickbooks::Service::Payment, 'CreditMemo' => Quickbooks::Service::CreditMemo }
.each_with_object({}) do |(type, service_class), dates|
ids[type].uniq.each_slice(IDS_PER_QUERY) do |slice|
list = slice.map { |id| "'#{id.gsub(/\D/, '')}'" }.join(',')
query(service_class, "SELECT * FROM #{type} WHERE Id IN (#{list})").each do |record|
dates[[type, record.id.to_s]] = record.txn_date
end
end
end
end
def linked(invoice)
Array(invoice.linked_transactions).select { |link| %w[Payment CreditMemo].include?(link.txn_type) }
end
def txn(kind, record, date)
def txn(kind, record)
Txn.new(kind: kind, txn_id: record.id.to_s, doc_number: record.doc_number.to_s,
customer_name: record.customer_ref&.name.to_s, txn_date: date,
customer_name: record.customer_ref&.name.to_s, txn_date: record.txn_date,
total: record.total.to_d, tax: record.txn_tax_detail&.total_tax.to_d)
end
def query(service_class, sql)
def query(service, sql)
results = []
qbo = QboConnectionService.current!
qbo.perform_authenticated_request do |access_token|
service = service_class.new(company_id: qbo.realm_id, access_token: access_token)
service.query_in_batches(sql, per_page: BATCH) { |batch| results.concat(Array(batch)) }
end
service.query_in_batches(sql, per_page: BATCH) { |batch| results.concat(Array(batch)) }
results
end
end