#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 ApproverTest < ActiveSupport::TestCase include QboCommissionTestHelper def setup User.current = User.find(1) create_rep(2) create_rep(3, probation_until: MONTH_START + 10) end def approve(month = commission_month) RedmineQboCommission::Approver.new(month, user: User.find(1)).approve end def test_records_the_month RedmineQboCommission::QboSource.expects(:transactions).with(commission_month, refresh: true).returns(october_txns) record = approve assert record.approved? assert_equal User.find(1), record.approved_by assert_equal BigDecimal('120000'), record.gross_sales assert_equal BigDecimal('1000'), record.deductions assert_equal BigDecimal('119000'), record.net_sales assert_equal BigDecimal('595'), record.pool assert_equal BigDecimal('100000'), record.threshold assert_equal 3, record.lines.count payouts = record.payouts.index_by(&:user_id) assert_equal BigDecimal('595'), payouts[2].amount assert payouts[2].eligible? assert_equal BigDecimal('0'), payouts[3].amount assert_not payouts[3].eligible? end def test_approved_month_no_longer_follows_quickbooks_or_settings stub_qbo approve stub_qbo([sale(9, 200_000)]) with_commission_settings('base_rate' => '5') do report = CommissionMonth.for(commission_month).report assert_equal BigDecimal('119000'), report.net_sales assert_equal BigDecimal('595'), report.pool.amount assert_equal [BigDecimal('595'), BigDecimal('0')], report.payouts.sort_by { |p| p.user.id }.map(&:amount) end end def test_keeps_unticked_deductions stub_qbo record = CommissionMonth.create!(month_start: MONTH_START) record.update!(excluded_keys: ['refund_receipt:3']) record = approve assert_equal BigDecimal('120000'), record.net_sales assert_equal [false], record.lines.where(kind: 'refund_receipt').pluck(:included) assert_not record.report.included?(october_txns.last) end def test_month_must_be_over stub_qbo month = RedmineQboCommission::Month.containing(Date.current) assert_raises(RedmineQboCommission::Approver::Error) { approve(month) } end def test_cannot_approve_twice stub_qbo approve assert_raises(RedmineQboCommission::Approver::Error) { approve } end def test_reopen stub_qbo record = approve record.adjustments.create!(description: 'Check refund', amount: 100) RedmineQboCommission::Approver.reopen(record) record.reload assert_not record.approved? assert_nil record.approved_by assert_equal 0, record.lines.count assert_equal 0, record.payouts.count assert_equal 1, record.adjustments.count end end