#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. class BonusTechnician < ApplicationRecord # The SOP's roles, by their bonus amounts (RedmineQboBonus::Settings.amount) ROLES = %w[technician foreman].freeze belongs_to :user belongs_to :customer, optional: true has_many :credits, class_name: 'TechBonusCredit', foreign_key: :user_id, primary_key: :user_id validates :user_id, presence: true, uniqueness: true validates :role, inclusion: { in: ROLES } validate :customer_exists scope :active, -> { where(active: true) } scope :sorted, -> { includes(:user).order(Arel.sql('users.firstname, users.lastname')).references(:users) } def self.role_options ROLES.map { |role| [I18n.t("label_bonus_role_#{role}"), role] } end def role_name I18n.t("label_bonus_role_#{role}") end def name user&.name.to_s end # In the 90-day probation during the week (no bonus that week) def on_probation?(week) probation_until.present? && probation_until >= week.start_date end # Why the technician can't get a bonus for the week (attendance aside), or nil def ineligible_reason(week) return I18n.t(:text_tech_bonus_inactive) unless active? return I18n.t(:text_tech_bonus_probation, date: I18n.l(probation_until)) if on_probation?(week) return I18n.t(:text_tech_bonus_no_customer) if customer.nil? nil end def base_amount RedmineQboBonus::Settings.amount(role) end def tier_amount RedmineQboBonus::Settings.tier_amount(role) end private def customer_exists return if customer_id.blank? errors.add(:customer_id, :invalid) unless Customer.exists?(id: customer_id) end end