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:
commit
a2ecb74c1d
37 files changed
+2274
No files matched your search
@@ -0,0 +1,50 @@
|
||||
#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 BonusTechniciansTest < Redmine::IntegrationTest
|
||||
include QboBonusTestHelper
|
||||
|
||||
def test_admins_link_a_user_to_a_customer
|
||||
customer = create_customer(id: 9100, name: 'Jane Tech')
|
||||
log_user('admin', 'admin')
|
||||
get '/bonus_technicians/new'
|
||||
assert_response :success
|
||||
assert_select 'input.customer-name'
|
||||
|
||||
assert_difference 'BonusTechnician.count', 1 do
|
||||
post '/bonus_technicians', params: { bonus_technician: { user_id: '2', customer_id: customer.id.to_s,
|
||||
role: 'foreman', active: '1' } }
|
||||
end
|
||||
assert_redirected_to '/bonus_technicians'
|
||||
tech = BonusTechnician.last
|
||||
assert_equal customer, tech.customer
|
||||
assert_equal 'foreman', tech.role
|
||||
|
||||
get '/bonus_technicians'
|
||||
assert_select 'td', text: 'Jane Tech'
|
||||
|
||||
patch "/bonus_technicians/#{tech.id}", params: { bonus_technician: { probation_until: '2026-12-01' } }
|
||||
assert_equal Date.new(2026, 12, 1), tech.reload.probation_until
|
||||
end
|
||||
|
||||
def test_unknown_customer
|
||||
log_user('admin', 'admin')
|
||||
post '/bonus_technicians', params: { bonus_technician: { user_id: '2', customer_id: '424242', role: 'technician' } }
|
||||
assert_response :unprocessable_entity
|
||||
end
|
||||
|
||||
def test_admins_only
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/bonus_technicians'
|
||||
assert_response :forbidden
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,88 @@
|
||||
#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 TechBonusesTest < Redmine::IntegrationTest
|
||||
include QboBonusTestHelper
|
||||
|
||||
def setup
|
||||
Customer.any_instance.stubs(:email).returns('tech@example.com')
|
||||
RedmineQboBonus::Settings.stubs(:item).returns(Struct.new(:id, :name).new('31', 'Tech Bonus'))
|
||||
RedmineQboBonus::CreditMemoWriter.stubs(:send_email).returns(true)
|
||||
RedmineQboBonus::CreditMemoWriter.any_instance.stubs(:create).returns(saved_credit_memo)
|
||||
create_technician(2)
|
||||
log_time(2, 100)
|
||||
log_time(3, 15.5)
|
||||
end
|
||||
|
||||
def test_needs_the_permission
|
||||
log_user('jsmith', 'jsmith')
|
||||
get '/tech_bonuses'
|
||||
assert_response :forbidden
|
||||
|
||||
Role.find(1).add_permission!(:run_tech_bonuses)
|
||||
get '/tech_bonuses'
|
||||
assert_response :success
|
||||
assert_select '#top-menu a.tech-bonuses'
|
||||
end
|
||||
|
||||
def test_week_preview
|
||||
log_user('admin', 'admin')
|
||||
get '/tech_bonuses', params: { week: '2026-11-10' }
|
||||
assert_redirected_to '/tech_bonuses/2026-11-08'
|
||||
get '/tech_bonuses/2026-11-08'
|
||||
assert_response :success
|
||||
assert_select 'h2', text: /2026-46/
|
||||
assert_select '.tech-bonus-summary .value', text: '115.5'
|
||||
assert_select 'input[name=?][value="2"]', 'attended[]'
|
||||
assert_select 'td', text: '$100.00'
|
||||
assert_select 'a[href^="/time_entries/report"]'
|
||||
end
|
||||
|
||||
def test_run_week
|
||||
log_user('admin', 'admin')
|
||||
post '/tech_bonuses/2026-11-08', params: { attended: ['2'] }
|
||||
assert_redirected_to '/tech_bonuses/2026-11-08'
|
||||
credit = TechBonusCredit.find_by(user_id: 2)
|
||||
assert_equal 'emailed', credit.status
|
||||
assert_equal '1042', credit.doc_number
|
||||
follow_redirect!
|
||||
assert_select 'a[href*="creditmemo?txnId=777"]', text: '#1042'
|
||||
|
||||
get '/tech_bonuses'
|
||||
assert_select 'table.tech-bonus-weeks td', text: /115.5\/110/
|
||||
end
|
||||
|
||||
def test_pdf
|
||||
log_user('admin', 'admin')
|
||||
post '/tech_bonuses/2026-11-08', params: { attended: ['2'] }
|
||||
credit = TechBonusCredit.find_by(user_id: 2)
|
||||
RedmineQboBonus::CreditMemoWriter.expects(:pdf).with('777').returns('%PDF-1.4')
|
||||
get "/tech_bonuses/credits/#{credit.id}/pdf"
|
||||
assert_response :success
|
||||
assert_equal 'application/pdf', response.media_type
|
||||
end
|
||||
|
||||
def test_resend
|
||||
log_user('admin', 'admin')
|
||||
post '/tech_bonuses/2026-11-08', params: { attended: ['2'] }
|
||||
credit = TechBonusCredit.find_by(user_id: 2)
|
||||
RedmineQboBonus::CreditMemoWriter.expects(:send_email).with('777', 'tech@example.com').returns(true)
|
||||
post "/tech_bonuses/credits/#{credit.id}/resend"
|
||||
assert_redirected_to '/tech_bonuses/2026-11-08'
|
||||
end
|
||||
|
||||
def test_not_a_week
|
||||
log_user('admin', 'admin')
|
||||
get '/tech_bonuses/2026-02-31'
|
||||
assert_response :not_found
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
#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/test_helper', __dir__)
|
||||
|
||||
module QboBonusTestHelper
|
||||
# Sunday 2026-11-08 to Saturday 2026-11-14 (2026-46)
|
||||
WEEK_START = Date.new(2026, 11, 8)
|
||||
|
||||
def self.included(base)
|
||||
base.fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :issues, :trackers,
|
||||
:projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :enabled_modules
|
||||
base.setup do
|
||||
TechBonusCredit.delete_all
|
||||
TechBonusWeek.delete_all
|
||||
BonusTechnician.delete_all
|
||||
TimeEntry.delete_all
|
||||
Customer.where(id: 9000..9999).delete_all
|
||||
end
|
||||
end
|
||||
|
||||
def bonus_week
|
||||
RedmineQboBonus::Week.new(WEEK_START)
|
||||
end
|
||||
|
||||
# Time logged without Redmine's checks (membership, permissions)
|
||||
def log_time(user_id, hours, spent_on: WEEK_START + 1, issue_id: 1, project_id: nil)
|
||||
issue = issue_id && Issue.find(issue_id)
|
||||
entry = TimeEntry.new(user_id: user_id, author_id: user_id, hours: hours, spent_on: spent_on, issue: issue,
|
||||
project_id: project_id || issue&.project_id, activity_id: 9)
|
||||
entry.save!(validate: false)
|
||||
entry
|
||||
end
|
||||
|
||||
# A customer, without sending it to QuickBooks
|
||||
def create_customer(id: 9001, name: 'Jane Tech')
|
||||
customer = Customer.new(id: id, name: name)
|
||||
customer.skip_qbo_push = true
|
||||
customer.save!
|
||||
customer
|
||||
end
|
||||
|
||||
def create_technician(user_id, role: 'technician', customer: nil, **attributes)
|
||||
customer ||= create_customer(id: 9000 + user_id, name: "Tech #{user_id}")
|
||||
BonusTechnician.create!({ user_id: user_id, role: role, customer_id: customer.id }.merge(attributes))
|
||||
end
|
||||
|
||||
def with_bonus_settings(values)
|
||||
saved = Setting.plugin_redmine_qbo_bonus
|
||||
Setting.plugin_redmine_qbo_bonus = RedmineQboBonus::Settings::DEFAULTS.merge(values)
|
||||
yield
|
||||
ensure
|
||||
Setting.plugin_redmine_qbo_bonus = saved
|
||||
end
|
||||
|
||||
# A credit memo as QuickBooks returns it
|
||||
def saved_credit_memo(id: '777', doc_number: '1042')
|
||||
memo = Quickbooks::Model::CreditMemo.new
|
||||
memo.id = id
|
||||
memo.doc_number = doc_number
|
||||
memo
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user