New plugin: weekly technician bonus credit memos (SOP-OPS-012)

Totals the shop's billable hours for a Sunday-Saturday week (picked
projects, trackers and users) against the SOP-OPS-012 target, and
credits each qualifying technician's QuickBooks customer account with a
credit memo: a "Tech Bonus <week> <total>/<goal> hours (total/goal)"
line, plus a separate tier line for every full 15 hours over the goal.
Credit memos are emailed through QuickBooks.

- Bonus technicians (admin): user, QuickBooks customer, role
  (technician/foreman), probation end
- Tech bonuses (global "Run tech bonuses" permission): week preview,
  attendance check, run, PDF and email again; re-running a week never
  credits anyone twice

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-16 12:13:30 -05:00
commit a2ecb74c1d
37 files changed
+2274

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
#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 CreditMemoWriterTest < ActiveSupport::TestCase
include QboBonusTestHelper
FakeItem = Struct.new(:id, :name)
def setup
RedmineQboBonus::Settings.stubs(:item).returns(FakeItem.new('31', 'Tech Bonus'))
end
def build(hours)
tech = create_technician(2)
log_time(2, hours)
report = RedmineQboBonus::WeekReport.new(bonus_week)
RedmineQboBonus::CreditMemoWriter.new(report).build(report.lines.first, customer: tech.customer,
email: 'tech@example.com')
end
def test_one_line_at_the_goal
memo = build(115.5)
assert_equal 1, memo.line_items.size
line = memo.line_items.first
assert_equal 'Tech Bonus 2026-46 115.5/110 hours (total/goal)', line.description
assert_equal BigDecimal('100'), line.amount
assert_equal '31', line.sales_item_line_detail.item_ref.value
assert_equal 'NON', line.sales_item_line_detail.tax_code_ref.value
assert_equal '9002', memo.customer_ref.value
assert_equal 'tech@example.com', memo.bill_email.address
end
def test_tiers_on_their_own_line
memo = build(141)
assert_equal 2, memo.line_items.size
tier = memo.line_items.last
assert_equal 'Tier Bonus 2026-46 2 x 15 hours over goal', tier.description
assert_equal BigDecimal('2'), tier.sales_item_line_detail.quantity
assert_equal BigDecimal('100'), tier.sales_item_line_detail.unit_price
assert_equal BigDecimal('200'), tier.amount
assert_includes memo.to_xml.to_s, 'Tier Bonus'
end
def test_needs_a_product_or_service
RedmineQboBonus::Settings.stubs(:item).returns(nil)
assert_raises(RedmineQboBonus::CreditMemoWriter::Error) { build(120) }
end
end
+128
View File
@@ -0,0 +1,128 @@
#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 RunnerTest < ActiveSupport::TestCase
include QboBonusTestHelper
# Stands in for QuickBooks
class FakeWriter
attr_reader :created
def initialize(fail_for: [])
@created = []
@fail_for = fail_for
end
def create(line, customer:, email: nil)
raise 'QuickBooks is down' if @fail_for.include?(line.technician.user_id)
@created << [line.technician.user_id, line.amount, email]
memo = Quickbooks::Model::CreditMemo.new
memo.id = (700 + @created.size).to_s
memo.doc_number = (1000 + @created.size).to_s
memo
end
end
def setup
User.current = User.find(1)
RedmineQboBonus::Settings.stubs(:item).returns(Struct.new(:id, :name).new('31', 'Tech Bonus'))
Customer.any_instance.stubs(:email).returns('tech@example.com')
RedmineQboBonus::CreditMemoWriter.stubs(:send_email).returns(true)
@tech = create_technician(2)
@foreman = create_technician(3, role: 'foreman')
log_time(2, 60)
log_time(3, 50)
log_time(1, 16) # 126 hours: 1 tier
end
def run_bonus(attended: [2, 3], writer: FakeWriter.new)
runner = RedmineQboBonus::Runner.new(bonus_week, attended_user_ids: attended, writer: writer)
[runner.run, writer]
end
def test_credits_and_emails_each_technician
week, writer = run_bonus
assert_equal [[2, BigDecimal('200'), 'tech@example.com'], [3, BigDecimal('300'), 'tech@example.com']],
writer.created.sort_by(&:first)
assert_equal BigDecimal('126'), week.total_hours
assert_equal 1, week.tiers
assert_equal BigDecimal('16'), week.hours_by_user[1]
credit = week.credits.find_by(user_id: 3)
assert_equal 'emailed', credit.status
assert_equal 'tech@example.com', credit.emailed_to
assert credit.credited?
assert_equal BigDecimal('500'), week.credited_total
end
def test_missed_shift_is_skipped
week, writer = run_bonus(attended: [2])
assert_equal [2], writer.created.map(&:first)
credit = week.credits.find_by(user_id: 3)
assert_equal 'skipped', credit.status
assert_equal BigDecimal('0'), credit.amount
end
def test_running_again_never_credits_twice
run_bonus(attended: [2])
week, writer = run_bonus(attended: [2, 3])
assert_equal [3], writer.created.map(&:first)
assert_equal 2, week.credits.count(&:credited?)
end
def test_a_failure_is_recorded_and_retried
week, = run_bonus(writer: FakeWriter.new(fail_for: [3]))
credit = week.credits.find_by(user_id: 3)
assert_equal 'failed', credit.status
assert_equal 'QuickBooks is down', credit.message
week, writer = run_bonus
assert_equal [3], writer.created.map(&:first)
assert_equal 'emailed', week.credits.find_by(user_id: 3).status
end
def test_no_email_keeps_it_created
Customer.any_instance.stubs(:email).returns(nil)
week, = run_bonus
credit = week.credits.find_by(user_id: 2)
assert_equal 'created', credit.status
assert_match(/no email/, credit.message)
end
def test_not_emailed_when_turned_off
RedmineQboBonus::CreditMemoWriter.expects(:send_email).never
with_bonus_settings('email_credits' => '0') do
week, = run_bonus
assert_equal %w[created created], week.credits.map(&:status)
end
end
def test_goal_not_met_records_the_week_only
TimeEntry.where(user_id: 3).delete_all # 76 hours
week, writer = run_bonus
assert_empty writer.created
assert_not week.goal_met?
assert_empty week.credits
end
def test_needs_a_product_or_service
RedmineQboBonus::Settings.stubs(:item).returns(nil)
assert_raises(RedmineQboBonus::Runner::Error) { run_bonus }
assert_equal 0, TechBonusWeek.count
end
def test_only_finished_weeks
runner = RedmineQboBonus::Runner.new(RedmineQboBonus::Week.containing(Date.current), attended_user_ids: [2],
writer: FakeWriter.new)
assert_raises(RedmineQboBonus::Runner::Error) { runner.run }
end
end
+127
View File
@@ -0,0 +1,127 @@
#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 WeekReportTest < ActiveSupport::TestCase
include QboBonusTestHelper
def report
RedmineQboBonus::WeekReport.new(bonus_week)
end
def test_everyones_hours_count_toward_the_shop_total
create_technician(2)
log_time(2, 60)
log_time(3, 40.5) # not a technician (e.g. front desk)
log_time(2, 8, spent_on: WEEK_START - 1) # Saturday before
log_time(2, 8, spent_on: WEEK_START + 7) # Sunday after
assert_equal({ 2 => BigDecimal('60'), 3 => BigDecimal('40.5') }, report.hours_by_user)
assert_equal BigDecimal('100.5'), report.total_hours
assert_not report.goal_met?
assert_equal 0, report.tiers
end
def test_sunday_and_saturday_count
log_time(2, 5, spent_on: WEEK_START)
log_time(2, 5, spent_on: WEEK_START + 6)
assert_equal BigDecimal('10'), report.total_hours
end
def test_a_tier_for_every_full_15_hours_over_the_goal
{ '110' => 0, '124.99' => 0, '125' => 1, '139.75' => 1, '140' => 2 }.each do |hours, tiers|
TimeEntry.delete_all
log_time(2, BigDecimal(hours))
assert_equal tiers, report.tiers, "#{hours} hours"
assert report.goal_met?
end
end
def test_projects_and_subprojects
log_time(2, 10, issue_id: 1) # eCookbook
log_time(2, 20, issue_id: 5) # eCookbook's subproject
log_time(2, 40, issue_id: 4) # OnlineStore
with_bonus_settings('project_ids' => ['1'], 'include_subprojects' => '1') do
assert_equal BigDecimal('30'), report.total_hours
end
with_bonus_settings('project_ids' => ['1'], 'include_subprojects' => '0') do
assert_equal BigDecimal('10'), report.total_hours
end
end
def test_excluded_trackers
Issue.find(2).update_columns(tracker_id: 3)
log_time(2, 10, issue_id: 1)
log_time(2, 20, issue_id: 2)
log_time(2, 5, issue_id: nil, project_id: 1)
with_bonus_settings('tracker_mode' => 'exclude', 'tracker_ids' => ['3']) do
assert_equal BigDecimal('15'), report.total_hours
end
with_bonus_settings('tracker_mode' => 'include', 'tracker_ids' => ['3']) do
assert_equal BigDecimal('20'), report.total_hours
end
end
def test_picked_users_and_the_technicians
create_technician(2)
log_time(2, 10)
log_time(3, 20)
log_time(1, 40)
with_bonus_settings('user_ids' => ['3']) do
assert_equal [2, 3], report.hours_by_user.keys.sort
end
end
def test_what_each_technician_earns
create_technician(2)
create_technician(3, role: 'foreman')
log_time(2, 70)
log_time(3, 72) # 142 hours: 2 tiers
lines = report.lines.index_by { |line| line.technician.user_id }
assert_equal BigDecimal('100'), lines[2].base_amount
assert_equal BigDecimal('200'), lines[2].tier_amount
assert_equal BigDecimal('150'), lines[3].base_amount
assert_equal BigDecimal('300'), lines[3].tier_amount
assert_equal BigDecimal('450'), lines[3].amount
end
def test_nothing_below_the_goal
create_technician(2)
log_time(2, 109.75)
assert_equal BigDecimal('0'), report.lines.first.amount
assert_equal BigDecimal('0.25'), report.hours_to_next_tier
end
def test_probation_and_missing_customer_are_ineligible
create_technician(2, probation_until: WEEK_START)
tech = create_technician(3)
tech.update_columns(customer_id: nil)
log_time(2, 130)
lines = report.lines.index_by { |line| line.technician.user_id }
assert_match(/probation/i, lines[2].ineligible_reason)
assert_equal BigDecimal('0'), lines[2].amount
assert_match(/customer/i, lines[3].ineligible_reason)
BonusTechnician.find_by(user_id: 2).update!(probation_until: WEEK_START - 1)
assert RedmineQboBonus::WeekReport.new(bonus_week).lines.find { |l| l.technician.user_id == 2 }.eligible?
end
def test_redmine_report_params
with_bonus_settings('project_ids' => ['2'], 'tracker_ids' => %w[3 18]) do
params = report.redmine_report_params
assert_equal %w[spent_on project_id issue.tracker_id], params[:f]
assert_equal '!', params[:op]['issue.tracker_id']
assert_equal %w[2026-11-08 2026-11-14], params[:v]['spent_on']
end
end
end
+54
View File
@@ -0,0 +1,54 @@
#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 WeekTest < ActiveSupport::TestCase
Week = RedmineQboBonus::Week
def test_weeks_run_sunday_to_saturday
week = Week.containing(Date.new(2026, 11, 18))
assert_equal Date.new(2026, 11, 15), week.start_date
assert_equal Date.new(2026, 11, 21), week.end_date
assert_equal week, Week.containing(Date.new(2026, 11, 15))
assert_equal week, Week.containing(Date.new(2026, 11, 21))
assert_equal week + 1, Week.containing(Date.new(2026, 11, 22))
end
def test_named_by_the_iso_week_of_their_monday
assert_equal '2026-47', Week.containing(Date.new(2026, 11, 18)).label
assert_equal '2026-46', Week.containing(Date.new(2026, 11, 8)).label
assert_equal '2026-53', Week.containing(Date.new(2026, 12, 31)).label
assert_equal '2027-01', Week.containing(Date.new(2027, 1, 3)).label
end
def test_last_completed_week
assert_equal Date.new(2026, 11, 8), Week.last_completed(Date.new(2026, 11, 16)).start_date
assert_equal Date.new(2026, 11, 8), Week.last_completed(Date.new(2026, 11, 15)).start_date
assert_equal Date.new(2026, 11, 15), Week.last_completed(Date.new(2026, 11, 22)).start_date
end
def test_completed_once_saturday_is_over
week = Week.containing(Date.new(2026, 11, 18))
assert_not week.completed?(Date.new(2026, 11, 21))
assert week.completed?(Date.new(2026, 11, 22))
end
def test_parse
assert_equal Date.new(2026, 11, 15), Week.parse('2026-11-18').start_date
assert_equal '2026-11-15', Week.parse('2026-11-15').to_param
assert_nil Week.parse('nope')
assert_nil Week.parse('2026-13-45')
end
def test_only_starts_on_sunday
assert_raises(ArgumentError) { Week.new(Date.new(2026, 11, 16)) }
end
end