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>
163 lines
6.6 KiB
Ruby
163 lines
6.6 KiB
Ruby
#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 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
|
|
def initialize(month, records, queries)
|
|
super(month)
|
|
@records = records
|
|
@queries = queries
|
|
end
|
|
|
|
private
|
|
|
|
def query(_service, sql)
|
|
@queries << sql
|
|
@records.fetch(sql[/FROM (\w+)/, 1], [])
|
|
end
|
|
end
|
|
|
|
# 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 teardown
|
|
Source.singleton_class.send(:remove_method, :new) if Source.singleton_methods(false).include?(:new)
|
|
end
|
|
|
|
def record(klass, id, total, date, tax: nil, items: [])
|
|
record = klass.new
|
|
record.id = id.to_s
|
|
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}" }
|
|
if tax
|
|
record.txn_tax_detail = Quickbooks::Model::TransactionTaxDetail.new
|
|
record.txn_tax_detail.total_tax = BigDecimal(tax.to_s)
|
|
end
|
|
if items.any?
|
|
record.line_items = items.map do |item_id|
|
|
line = Quickbooks::Model::Line.new
|
|
line.sales_item_line_detail = Quickbooks::Model::SalesItemLineDetail.new
|
|
line.sales_item_line_detail.item_ref = Quickbooks::Model::BaseReference.new(item_id.to_s)
|
|
line
|
|
end
|
|
end
|
|
record
|
|
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
|
|
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
|
|
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 @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
|
|
stub_queries({})
|
|
with_commission_settings('include_sales_receipts' => '0', 'deduct_refund_receipts' => '0', 'deduct_credit_memos' => '0') do
|
|
assert_equal [], Source.new(commission_month).receipts
|
|
assert_empty @queries
|
|
end
|
|
end
|
|
|
|
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 %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
|
|
saved = Rails.cache
|
|
Rails.cache = ActiveSupport::Cache::MemoryStore.new
|
|
yield
|
|
ensure
|
|
Rails.cache = saved
|
|
end
|
|
end
|