Reads the month's paid sales from QuickBooks Online (invoices paid in full that month, sales receipts), deducts refund receipts, credit memos and adjustments, works out the commission pool with the threshold and marginal brackets, and records each sales rep's payout once the month is approved for payroll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
7.4 KiB
Ruby
159 lines
7.4 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
|
|
|
|
# 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)
|
|
super(month)
|
|
@records = records
|
|
@queries = []
|
|
end
|
|
|
|
private
|
|
|
|
def query(_service_class, 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
|
|
end
|
|
end
|
|
|
|
def ref(name)
|
|
Quickbooks::Model::BaseReference.new(name)
|
|
end
|
|
|
|
def link(type, id)
|
|
linked = Quickbooks::Model::LinkedTransaction.new
|
|
linked.txn_type = type
|
|
linked.txn_id = id.to_s
|
|
linked
|
|
end
|
|
|
|
def record(klass, id, total, date, tax: nil, links: [], items: [])
|
|
record = klass.new
|
|
record.id = id.to_s
|
|
record.doc_number = "D#{id}" if record.respond_to?(:doc_number=)
|
|
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
|
|
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
|
|
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 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
|
|
end
|
|
|
|
def test_receipts_and_credit_memos
|
|
records = {
|
|
'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
|
|
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'") })
|
|
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)] }
|
|
with_commission_settings('include_sales_receipts' => '0', 'deduct_refund_receipts' => '0', 'deduct_credit_memos' => '0') do
|
|
assert_equal [], FakeSource.new(commission_month, records).fetch
|
|
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)
|
|
with_memory_cache do
|
|
2.times { assert_equal [BigDecimal('100')], RedmineQboCommission::QboSource.transactions(commission_month).map(&:net) }
|
|
assert RedmineQboCommission::QboSource.read_at(commission_month)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def with_memory_cache
|
|
saved = Rails.cache
|
|
Rails.cache = ActiveSupport::Cache::MemoryStore.new
|
|
yield
|
|
ensure
|
|
Rails.cache = saved
|
|
end
|
|
end
|