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:
1 parent
4229426621
commit
ccc70171b4
6 files changed
+158
-159
No files matched your search
@@ -13,42 +13,66 @@ require File.expand_path('../test_helper', __dir__)
|
||||
class QboSourceTest < ActiveSupport::TestCase
|
||||
include QboCommissionTestHelper
|
||||
|
||||
Source = RedmineQboCommission::QboSource
|
||||
|
||||
# Stands in for the QuickBooks connection: runs the block with a token
|
||||
class FakeQbo
|
||||
def realm_id
|
||||
'123'
|
||||
end
|
||||
|
||||
def perform_authenticated_request
|
||||
yield OAuth2::AccessToken.new(OAuth2::Client.new('id', 'secret'), 'token')
|
||||
end
|
||||
end
|
||||
|
||||
def setup
|
||||
Invoice.where(id: 9001..9099).delete_all
|
||||
Customer.where(id: 9500).delete_all
|
||||
customer = Customer.new(id: 9500, name: 'Jane Customer')
|
||||
customer.skip_qbo_push = true
|
||||
customer.save!
|
||||
QboConnectionService.stubs(:current!).returns(FakeQbo.new)
|
||||
end
|
||||
|
||||
def local_invoice(id, total, paid_on:, tax: 0, balance: 0)
|
||||
invoice = Invoice.new(id: id, doc_number: "10#{id}", txn_date: MONTH_START, customer_id: 9500,
|
||||
total_amount: total, tax_amount: tax, balance: balance, paid_on: paid_on)
|
||||
invoice.skip_qbo_push = true
|
||||
invoice.save!
|
||||
end
|
||||
|
||||
# Answers the source's QuickBooks queries from lists of records, by the entity queried
|
||||
class FakeSource < RedmineQboCommission::QboSource
|
||||
attr_reader :queries
|
||||
|
||||
def initialize(month, records)
|
||||
def initialize(month, records, queries)
|
||||
super(month)
|
||||
@records = records
|
||||
@queries = []
|
||||
@queries = queries
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def query(_service_class, sql)
|
||||
def query(_service, sql)
|
||||
@queries << sql
|
||||
entity = sql[/FROM (\w+)/, 1]
|
||||
list = @records.fetch(entity, [])
|
||||
ids = sql[/Id IN \(([^)]*)\)/, 1]
|
||||
ids ? list.select { |record| ids.include?("'#{record.id}'") } : list
|
||||
@records.fetch(sql[/FROM (\w+)/, 1], [])
|
||||
end
|
||||
end
|
||||
|
||||
def ref(name)
|
||||
Quickbooks::Model::BaseReference.new(name)
|
||||
# Every QboSource made from now on answers from the records (QboSource.transactions makes its own)
|
||||
def stub_queries(records)
|
||||
@queries = []
|
||||
queries = @queries
|
||||
Source.define_singleton_method(:new) { |month| FakeSource.allocate.tap { |f| f.send(:initialize, month, records, queries) } }
|
||||
end
|
||||
|
||||
def link(type, id)
|
||||
linked = Quickbooks::Model::LinkedTransaction.new
|
||||
linked.txn_type = type
|
||||
linked.txn_id = id.to_s
|
||||
linked
|
||||
def teardown
|
||||
Source.singleton_class.send(:remove_method, :new) if Source.singleton_methods(false).include?(:new)
|
||||
end
|
||||
|
||||
def record(klass, id, total, date, tax: nil, links: [], items: [])
|
||||
def record(klass, id, total, date, tax: nil, items: [])
|
||||
record = klass.new
|
||||
record.id = id.to_s
|
||||
record.doc_number = "D#{id}" if record.respond_to?(:doc_number=)
|
||||
record.doc_number = "D#{id}"
|
||||
record.txn_date = date
|
||||
record.total = BigDecimal(total.to_s)
|
||||
record.customer_ref = Quickbooks::Model::BaseReference.new(id.to_s).tap { |r| r.name = "Customer #{id}" }
|
||||
@@ -56,7 +80,6 @@ class QboSourceTest < ActiveSupport::TestCase
|
||||
record.txn_tax_detail = Quickbooks::Model::TransactionTaxDetail.new
|
||||
record.txn_tax_detail.total_tax = BigDecimal(tax.to_s)
|
||||
end
|
||||
record.linked_transactions = links if record.respond_to?(:linked_transactions=)
|
||||
if items.any?
|
||||
record.line_items = items.map do |item_id|
|
||||
line = Quickbooks::Model::Line.new
|
||||
@@ -68,84 +91,65 @@ class QboSourceTest < ActiveSupport::TestCase
|
||||
record
|
||||
end
|
||||
|
||||
def payment(id, date)
|
||||
record(Quickbooks::Model::Payment, id, 1, date)
|
||||
end
|
||||
|
||||
def invoice(id, total, date, **options)
|
||||
record(Quickbooks::Model::Invoice, id, total, date, **options)
|
||||
end
|
||||
|
||||
def test_paid_on
|
||||
assert_equal Date.new(2026, 10, 9), RedmineQboCommission::QboSource.paid_on(Date.new(2026, 10, 2), [Date.new(2026, 10, 9), nil])
|
||||
# A deposit taken before the invoice: paid in full on the invoice's date
|
||||
assert_equal Date.new(2026, 10, 2), RedmineQboCommission::QboSource.paid_on(Date.new(2026, 10, 2), [Date.new(2026, 8, 1)])
|
||||
end
|
||||
|
||||
def test_invoices_count_in_the_month_paid_in_full
|
||||
records = {
|
||||
'Invoice' => [
|
||||
invoice(1, 1070, MONTH_START + 4, tax: 70, links: [link('Payment', 50)]), # paid in October
|
||||
invoice(2, 500, Date.new(2026, 9, 20), links: [link('Payment', 51)]), # September, paid in October
|
||||
invoice(3, 800, MONTH_START + 2, links: [link('Payment', 52)]), # paid in November
|
||||
invoice(4, 300, MONTH_START + 6, links: [link('Payment', 53), link('Estimate', 9)]), # deposit from August
|
||||
invoice(5, 0, MONTH_START + 6), # voided
|
||||
invoice(6, 200, Date.new(2026, 11, 3), links: [link('Payment', 50)]), # dated November
|
||||
invoice(7, 100, Date.new(2026, 9, 3), links: [link('CreditMemo', 70)]) # settled by a September credit memo
|
||||
],
|
||||
'Payment' => [payment(50, MONTH_START + 9), payment(51, MONTH_START + 1), payment(52, Date.new(2026, 11, 2)),
|
||||
payment(53, Date.new(2026, 8, 1))],
|
||||
'CreditMemo' => [record(Quickbooks::Model::CreditMemo, 70, 100, Date.new(2026, 9, 30))]
|
||||
}
|
||||
with_commission_settings('include_sales_receipts' => '0', 'deduct_refund_receipts' => '0', 'deduct_credit_memos' => '0') do
|
||||
source = FakeSource.new(commission_month, records)
|
||||
txns = source.fetch
|
||||
assert_equal %w[2 4 1], txns.map(&:txn_id)
|
||||
first = txns.find { |txn| txn.txn_id == '1' }
|
||||
assert_equal MONTH_START + 9, first.txn_date
|
||||
assert_equal BigDecimal('1000'), first.net
|
||||
assert_equal 'Customer 1', first.customer_name
|
||||
assert_equal MONTH_START + 6, txns.find { |txn| txn.txn_id == '4' }.txn_date
|
||||
assert_match(/Balance = '0' AND MetaData.LastUpdatedTime >= '2026-10-01T00:00:00/, source.queries.first)
|
||||
end
|
||||
def test_invoices_paid_in_full_in_the_month_come_from_redmine
|
||||
local_invoice(9001, 1070, tax: 70, paid_on: MONTH_START + 9)
|
||||
local_invoice(9002, 500, paid_on: MONTH_START + 1)
|
||||
local_invoice(9003, 800, paid_on: Date.new(2026, 11, 2)) # paid in November
|
||||
local_invoice(9004, 300, paid_on: nil, balance: 300) # open
|
||||
local_invoice(9005, 0, paid_on: MONTH_START + 3) # voided
|
||||
txns = Source.new(commission_month).invoices.sort_by(&:txn_id)
|
||||
assert_equal %w[9001 9002], txns.map(&:txn_id)
|
||||
assert_equal BigDecimal('1000'), txns.first.net
|
||||
assert_equal MONTH_START + 9, txns.first.txn_date
|
||||
assert_equal 'Jane Customer', txns.first.customer_name
|
||||
end
|
||||
|
||||
def test_receipts_and_credit_memos
|
||||
records = {
|
||||
stub_queries(
|
||||
'SalesReceipt' => [record(Quickbooks::Model::SalesReceipt, 1, 107, MONTH_START + 3, tax: 7)],
|
||||
'RefundReceipt' => [record(Quickbooks::Model::RefundReceipt, 2, 50, MONTH_START + 4)],
|
||||
'CreditMemo' => [record(Quickbooks::Model::CreditMemo, 3, 40, MONTH_START + 5, items: [12]),
|
||||
record(Quickbooks::Model::CreditMemo, 4, 100, MONTH_START + 5, items: [31]), # tech bonus
|
||||
record(Quickbooks::Model::CreditMemo, 5, 60, MONTH_START + 5, items: [31, 12])]
|
||||
}
|
||||
)
|
||||
with_commission_settings('excluded_item_ids' => ['31']) do
|
||||
source = FakeSource.new(commission_month, records)
|
||||
txns = source.fetch
|
||||
txns = Source.new(commission_month).receipts
|
||||
assert_equal [%w[sales_receipt 1], %w[refund_receipt 2], %w[credit_memo 3], %w[credit_memo 5]],
|
||||
txns.map { |txn| [txn.kind, txn.txn_id] }
|
||||
assert_equal BigDecimal('100'), txns.first.net
|
||||
assert(source.queries.any? { |sql| sql.include?("TxnDate >= '2026-10-01' AND TxnDate <= '2026-10-31'") })
|
||||
assert @queries.all? { |sql| sql.include?("TxnDate >= '2026-10-01' AND TxnDate <= '2026-10-31'") }
|
||||
assert_equal 3, @queries.size
|
||||
end
|
||||
end
|
||||
|
||||
def test_settings_leave_out_receipts_and_credit_memos
|
||||
records = { 'SalesReceipt' => [record(Quickbooks::Model::SalesReceipt, 1, 100, MONTH_START)],
|
||||
'RefundReceipt' => [record(Quickbooks::Model::RefundReceipt, 2, 50, MONTH_START)],
|
||||
'CreditMemo' => [record(Quickbooks::Model::CreditMemo, 3, 40, MONTH_START)] }
|
||||
stub_queries({})
|
||||
with_commission_settings('include_sales_receipts' => '0', 'deduct_refund_receipts' => '0', 'deduct_credit_memos' => '0') do
|
||||
assert_equal [], FakeSource.new(commission_month, records).fetch
|
||||
assert_equal [], Source.new(commission_month).receipts
|
||||
assert_empty @queries
|
||||
end
|
||||
end
|
||||
|
||||
def test_transactions_are_cached
|
||||
source = FakeSource.new(commission_month, { 'SalesReceipt' => [record(Quickbooks::Model::SalesReceipt, 1, 100, MONTH_START)] })
|
||||
RedmineQboCommission::QboSource.expects(:new).once.returns(source)
|
||||
def test_transactions_and_refresh
|
||||
local_invoice(9001, 200, paid_on: MONTH_START + 2)
|
||||
stub_queries('RefundReceipt' => [record(Quickbooks::Model::RefundReceipt, 2, 50, MONTH_START + 4)])
|
||||
Source.expects(:sync_invoices).once
|
||||
with_memory_cache do
|
||||
2.times { assert_equal [BigDecimal('100')], RedmineQboCommission::QboSource.transactions(commission_month).map(&:net) }
|
||||
assert RedmineQboCommission::QboSource.read_at(commission_month)
|
||||
2.times { assert_equal %w[invoice refund_receipt], Source.transactions(commission_month).map(&:kind) }
|
||||
assert_equal 3, @queries.size # read once, then cached
|
||||
assert Source.read_at(commission_month)
|
||||
Source.transactions(commission_month, refresh: true)
|
||||
assert_equal 6, @queries.size
|
||||
end
|
||||
end
|
||||
|
||||
def test_missing_paid_on
|
||||
local_invoice(9001, 200, paid_on: MONTH_START)
|
||||
assert_not Source.missing_paid_on?
|
||||
local_invoice(9002, 200, paid_on: nil)
|
||||
assert Source.missing_paid_on?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_memory_cache
|
||||
|
||||
Reference in new issue
Block a user