New plugin: monthly sales commission (SOP-OPS-013)
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>
This commit is contained in:
commit
6fcd5f9b64
44 files changed
+2661
No files matched your search
@@ -0,0 +1,19 @@
|
||||
#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 deduction the approver adds by hand (e.g. a return refunded by company check), or with a negative amount,
|
||||
# sales added back
|
||||
class CommissionAdjustment < ApplicationRecord
|
||||
belongs_to :commission_month
|
||||
belongs_to :author, class_name: 'User', optional: true
|
||||
|
||||
validates :description, presence: true, length: { maximum: 255 }
|
||||
validates :amount, presence: true, numericality: { other_than: 0 }
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
#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 QuickBooks transaction as it counted toward an approved month
|
||||
class CommissionLine < ApplicationRecord
|
||||
belongs_to :commission_month
|
||||
|
||||
validates :kind, inclusion: { in: RedmineQboCommission::Txn::KINDS }
|
||||
|
||||
def to_txn
|
||||
RedmineQboCommission::Txn.new(kind: kind, txn_id: txn_id, doc_number: doc_number, customer_name: customer_name,
|
||||
txn_date: txn_date, total: total.to_d, tax: tax.to_d)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,84 @@
|
||||
#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
|
||||
@@ -0,0 +1,17 @@
|
||||
#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 sales rep's commission for an approved month, or why they weren't paid
|
||||
class CommissionPayout < ApplicationRecord
|
||||
belongs_to :commission_month
|
||||
belongs_to :user
|
||||
|
||||
validates :user_id, uniqueness: { scope: :commission_month_id }
|
||||
end
|
||||
@@ -0,0 +1,36 @@
|
||||
#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 sales rep who earns the monthly commission (SOP-OPS-013 §2)
|
||||
class CommissionRep < ApplicationRecord
|
||||
belongs_to :user
|
||||
|
||||
validates :user_id, presence: true, uniqueness: true
|
||||
|
||||
scope :active, -> { where(active: true) }
|
||||
scope :sorted, -> { includes(:user).order(Arel.sql('users.firstname, users.lastname')).references(:users) }
|
||||
|
||||
def name
|
||||
user&.name.to_s
|
||||
end
|
||||
|
||||
# In the 90-day probation on any day of the month (no commission that month)
|
||||
def on_probation?(month)
|
||||
probation_until.present? && probation_until >= month.start_date
|
||||
end
|
||||
|
||||
# Why the rep isn't paid for the month, or nil
|
||||
def ineligible_reason(month)
|
||||
return I18n.t(:text_sales_commission_inactive) unless active?
|
||||
return I18n.t(:text_sales_commission_probation, date: I18n.l(probation_until)) if on_probation?(month)
|
||||
|
||||
nil
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user