#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. # A commission month: the approver's choices while it's open, and once it's approved, what was approved for payroll class CommissionMonth < ApplicationRecord STATUSES = %w[open approved].freeze belongs_to :approved_by, class_name: 'User', optional: true has_many :lines, -> { order(:txn_date, :id) }, class_name: 'CommissionLine', dependent: :delete_all has_many :adjustments, -> { order(:id) }, class_name: 'CommissionAdjustment', dependent: :delete_all has_many :payouts, class_name: 'CommissionPayout', dependent: :delete_all validates :month_start, presence: true, uniqueness: true validates :status, inclusion: { in: STATUSES } scope :recent, -> { order(month_start: :desc) } def self.for(month) find_by(month_start: month.start_date) end def month RedmineQboCommission::Month.new(month_start) end def approved? status == 'approved' end # The deductions the approver unticked (Txn#key) def excluded_keys json_list(:excluded_keys) end def excluded_keys=(keys) self[:excluded_keys] = Array(keys).map(&:to_s).uniq.sort.to_json end # The reps the approver unticked def unpaid_user_ids json_list(:unpaid_user_ids).map(&:to_i) end def unpaid_user_ids=(ids) self[:unpaid_user_ids] = Array(ids).map(&:to_i).uniq.sort.to_json end # The brackets the month was approved with, as [[limit or nil, rate]] def brackets JSON.parse(self[:brackets].presence || '[]').map { |limit, rate| [limit && BigDecimal(limit.to_s), BigDecimal(rate.to_s)] } rescue JSON::ParserError [] end def brackets=(brackets) self[:brackets] = brackets.map { |limit, rate| [limit&.to_d&.to_s('F'), rate.to_d.to_s('F')] }.to_json end # An approved month's report, from what was recorded def report txns = lines.map(&:to_txn) RedmineQboCommission::ApprovedReport.new(month, txns: txns, commission_month: self, pool_options: { threshold: threshold, brackets: brackets }) end def payouts_total payouts.sum(&:amount) end private def json_list(attribute) Array(JSON.parse(self[attribute].presence || '[]')) rescue JSON::ParserError [] end end