#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 MonthReportTest < ActiveSupport::TestCase include QboCommissionTestHelper def report(txns = october_txns, record: nil) RedmineQboCommission::MonthReport.new(commission_month, txns: txns, commission_month: record) end def test_totals report = report() assert_equal BigDecimal('120000'), report.gross_sales assert_equal BigDecimal('1000'), report.deductions_total assert_equal BigDecimal('119000'), report.net_sales assert_equal BigDecimal('595'), report.pool.amount assert_equal %w[1 2], report.sales.map(&:txn_id) assert_equal %w[3], report.deductions.map(&:txn_id) end def test_unticked_deductions_and_adjustments record = CommissionMonth.create!(month_start: MONTH_START) record.excluded_keys = ['refund_receipt:3'] record.save! record.adjustments.create!(description: 'Returned lift kit, refunded by check', amount: 2_500) record.adjustments.create!(description: 'Missed sale', amount: -500) report = report(record: record.reload) assert_not report.included?(october_txns.last) assert_equal BigDecimal('2000'), report.deductions_total assert_equal BigDecimal('118000'), report.net_sales end def test_every_eligible_rep_gets_the_whole_pool create_rep(2) create_rep(3) create_rep(4, probation_until: MONTH_START + 5) create_rep(5, active: false) payouts = report.payouts.index_by { |payout| payout.user.id } assert_equal [2, 3, 4], payouts.keys.sort assert_equal BigDecimal('595'), payouts[2].amount assert_equal BigDecimal('595'), payouts[3].amount assert_equal BigDecimal('0'), payouts[4].amount assert_match(/probation/i, payouts[4].ineligible_reason) assert_equal BigDecimal('1190'), report.payouts_total end def test_probation_ending_before_the_month create_rep(2, probation_until: MONTH_START - 1) assert report.payouts.first.eligible? end def test_unticked_rep create_rep(2) create_rep(3) record = CommissionMonth.create!(month_start: MONTH_START) record.update!(unpaid_user_ids: [3]) payouts = report(record: record).payouts.index_by { |payout| payout.user.id } assert_equal BigDecimal('595'), payouts[2].amount assert_equal BigDecimal('0'), payouts[3].amount assert_not payouts[3].eligible? end def test_below_the_threshold_pays_nothing create_rep(2) report = report([sale(1, 99_000)]) assert_equal BigDecimal('0'), report.payouts.first.amount assert report.payouts.first.eligible? end end