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>
181 lines
7.6 KiB
Ruby
181 lines
7.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 SalesCommissionsTest < Redmine::IntegrationTest
|
|
include QboCommissionTestHelper
|
|
|
|
def setup
|
|
stub_qbo
|
|
create_rep(2)
|
|
create_rep(3)
|
|
end
|
|
|
|
def test_needs_a_permission
|
|
log_user('jsmith', 'jsmith')
|
|
get '/sales_commission/2026-10'
|
|
assert_response :forbidden
|
|
|
|
Role.find(1).add_permission!(:approve_sales_commission)
|
|
get '/sales_commission'
|
|
assert_response :success
|
|
assert_select '#top-menu a.sales-commissions[href=?]', '/sales_commission/current'
|
|
end
|
|
|
|
def test_menu_opens_this_month
|
|
log_user('admin', 'admin')
|
|
travel_to Date.new(2026, 11, 18) do
|
|
get '/sales_commission/current'
|
|
assert_redirected_to '/sales_commission/2026-11'
|
|
end
|
|
end
|
|
|
|
def test_month_picker
|
|
log_user('admin', 'admin')
|
|
get '/sales_commission', params: { month: '2026-10' }
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
end
|
|
|
|
def test_open_month
|
|
log_user('admin', 'admin')
|
|
get '/sales_commission/2026-10'
|
|
assert_response :success
|
|
assert_select 'h2', text: /October 2026/
|
|
assert_select '.sales-commission-summary .value', text: '$120,000.00'
|
|
assert_select '.sales-commission-summary .value', text: '$119,000.00'
|
|
assert_select '.sales-commission-summary .value', text: '$595.00'
|
|
assert_select '#sales-commission-payouts td', text: '$595.00', count: 2
|
|
assert_select '#sales-commission-sales tbody tr', 2
|
|
assert_select 'input[name=?][value=?][checked]', 'included[]', 'refund_receipt:3'
|
|
assert_select 'input[name=?][value="2"][checked]', 'eligible[]'
|
|
assert_select 'input[name=approve]'
|
|
end
|
|
|
|
def test_view_only
|
|
Role.find(1).add_permission!(:view_sales_commission)
|
|
log_user('jsmith', 'jsmith')
|
|
get '/sales_commission'
|
|
assert_response :forbidden
|
|
get '/sales_commission/2026-10'
|
|
assert_response :success
|
|
assert_select '.sales-commission-summary .value', text: '$595.00'
|
|
assert_select 'h2 a[href=?]', '/sales_commission', count: 0
|
|
assert_select 'input[name=?]', 'included[]', count: 0
|
|
assert_select 'input[name=approve]', count: 0
|
|
assert_select 'a[href*="qbo.intuit.com"]', count: 0
|
|
|
|
post '/sales_commission/2026-10', params: { approve: '1' }
|
|
assert_response :forbidden
|
|
assert_nil CommissionMonth.for(commission_month)
|
|
get '/sales_commission/2026-10.csv'
|
|
assert_response :forbidden
|
|
end
|
|
|
|
def test_untick_and_approve
|
|
log_user('admin', 'admin')
|
|
post '/sales_commission/2026-10', params: { shown: '1', shown_deductions: ['refund_receipt:3'], included: [],
|
|
shown_reps: %w[2 3], eligible: ['2'], notes: 'Checked with bookkeeping' }
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
record = CommissionMonth.for(commission_month)
|
|
assert_equal ['refund_receipt:3'], record.excluded_keys
|
|
assert_equal [3], record.unpaid_user_ids
|
|
assert_not record.approved?
|
|
|
|
get '/sales_commission/2026-10'
|
|
assert_select '.sales-commission-summary .value', text: '$600.00'
|
|
assert_select 'input[name=?][value=?]:not([checked])', 'included[]', 'refund_receipt:3'
|
|
assert_select 'input[name=?][value="3"]:not([checked])', 'eligible[]'
|
|
|
|
post '/sales_commission/2026-10', params: { shown: '1', shown_deductions: ['refund_receipt:3'], included: [],
|
|
shown_reps: %w[2 3], eligible: ['2'], approve: '1' }
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
record.reload
|
|
assert record.approved?
|
|
assert_equal BigDecimal('600'), record.pool
|
|
assert_equal({ 2 => BigDecimal('600'), 3 => BigDecimal('0') }, record.payouts.to_h { |p| [p.user_id, p.amount] })
|
|
|
|
get '/sales_commission/2026-10'
|
|
assert_select '.sales-commission-approved', text: /Approved for payroll by/
|
|
assert_select 'input[name=approve]', count: 0
|
|
assert_select 'a[href=?]', '/sales_commission/2026-10.csv'
|
|
|
|
get '/sales_commission/2026-10.csv'
|
|
assert_response :success
|
|
assert_match %r{\Atext/csv}, response.content_type
|
|
rows = CSV.parse(response.body)
|
|
assert_equal 3, rows.size
|
|
assert_includes rows, ['2026-10', 'jsmith', 'John Smith', '600.00', 'Yes', nil]
|
|
|
|
get '/sales_commission'
|
|
assert_select 'td', text: '$600.00'
|
|
end
|
|
|
|
def test_approved_month_is_locked_until_reopened
|
|
log_user('admin', 'admin')
|
|
post '/sales_commission/2026-10', params: { approve: '1' }
|
|
record = CommissionMonth.for(commission_month)
|
|
assert record.approved?
|
|
|
|
post '/sales_commission/2026-10/adjustments', params: { description: 'Late refund', amount: '50' }
|
|
assert_equal 0, record.adjustments.count
|
|
assert_equal 'This month is approved. Reopen it to change it.', flash[:error]
|
|
|
|
post '/sales_commission/2026-10/reopen'
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
assert_not record.reload.approved?
|
|
end
|
|
|
|
def test_current_month_cannot_be_approved
|
|
log_user('admin', 'admin')
|
|
month = RedmineQboCommission::Month.containing(Date.current)
|
|
get "/sales_commission/#{month.label}"
|
|
assert_select 'input[name=approve]', count: 0
|
|
assert_select 'p.warning'
|
|
post "/sales_commission/#{month.label}", params: { approve: '1' }
|
|
assert_equal "The month isn't over yet.", flash[:error]
|
|
end
|
|
|
|
def test_adjustments
|
|
log_user('admin', 'admin')
|
|
post '/sales_commission/2026-10/adjustments', params: { description: 'Returned lift kit, refunded by check', amount: '$19,000' }
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
adjustment = CommissionAdjustment.last
|
|
assert_equal BigDecimal('19000'), adjustment.amount
|
|
assert_equal User.find(1), adjustment.author
|
|
|
|
get '/sales_commission/2026-10'
|
|
assert_select '.sales-commission-summary .value', text: '$100,000.00'
|
|
assert_select '.sales-commission-summary .value', text: '$500.00'
|
|
|
|
post '/sales_commission/2026-10/adjustments', params: { description: '', amount: 'abc' }
|
|
assert flash[:error].present?
|
|
assert_equal 1, CommissionAdjustment.count
|
|
|
|
delete "/sales_commission/2026-10/adjustments/#{adjustment.id}"
|
|
assert_equal 0, CommissionAdjustment.count
|
|
end
|
|
|
|
def test_quickbooks_error
|
|
RedmineQboCommission::QboSource.stubs(:transactions).raises(StandardError, 'QBO not connected')
|
|
log_user('admin', 'admin')
|
|
get '/sales_commission/2026-10'
|
|
assert_response :success
|
|
assert_select 'p.conflict', text: /QBO not connected/
|
|
end
|
|
|
|
def test_refresh
|
|
RedmineQboCommission::QboSource.expects(:transactions).with(commission_month, refresh: true).returns([])
|
|
log_user('admin', 'admin')
|
|
post '/sales_commission/2026-10/refresh'
|
|
assert_redirected_to '/sales_commission/2026-10'
|
|
end
|
|
end
|