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:
rickyandClaude Opus 5.5 committed 2026-11-16 18:53:25 -05:00
commit 6fcd5f9b64
44 files changed
+2661

No files matched your search

@@ -0,0 +1,66 @@
#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.
# Administration » Commission reps: who earns the monthly sales commission
class CommissionRepsController < ApplicationController
layout 'admin'
self.main_menu = false
menu_item :commission_reps
before_action :require_admin
before_action :find_rep, only: [:edit, :update, :destroy]
def index
@reps = CommissionRep.sorted.to_a
end
def new
@rep = CommissionRep.new
end
def create
@rep = CommissionRep.new(rep_params)
if @rep.save
flash[:notice] = l(:notice_successful_create)
redirect_to commission_reps_path
else
render :new, status: :unprocessable_entity
end
end
def edit; end
def update
if @rep.update(rep_params.except(:user_id))
flash[:notice] = l(:notice_successful_update)
redirect_to commission_reps_path
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@rep.destroy
flash[:notice] = l(:notice_successful_delete)
redirect_to commission_reps_path
end
private
def find_rep
@rep = CommissionRep.find(params[:id])
rescue ActiveRecord::RecordNotFound
render_404
end
def rep_params
params.require(:commission_rep).permit(:user_id, :probation_until, :active)
end
end
@@ -0,0 +1,157 @@
#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 'csv'
# Monthly sales commission (SOP-OPS-013): the months, a month's sales, pool and payouts, and approving it for payroll
class SalesCommissionsController < ApplicationController
self.main_menu = false
menu_item :sales_commissions
before_action :authorize_global
before_action :find_month, except: [:index, :current]
before_action :find_open_record, only: [:update, :add_adjustment, :remove_adjustment]
helper :sales_commissions
MONTHS_TO_PICK = 12
def index
# The month picker
month = RedmineQboCommission::Month.parse(params[:month]) if params[:month].present?
return redirect_to(sales_commission_month_path(month.to_param)) if month
@commission_months = CommissionMonth.recent.includes(:approved_by, :payouts).limit(36).to_a
@months = (0...MONTHS_TO_PICK).map { |n| RedmineQboCommission::Month.containing(Date.current) - n }
end
# The top menu opens this month, so far
def current
redirect_to sales_commission_month_path(RedmineQboCommission::Month.containing(Date.current).to_param)
end
def show
@record = CommissionMonth.for(@month)
@can_approve = RedmineQboCommission.allowed?
if @record&.approved?
@report = @record.report
else
@report = RedmineQboCommission::MonthReport.new(@month, txns: read_transactions, commission_month: @record)
@read_at = RedmineQboCommission::QboSource.read_at(@month)
end
respond_to do |format|
format.html
format.csv do
return render_403 unless @can_approve && @record&.approved?
send_data payroll_csv, type: 'text/csv; header=present',
filename: "sales-commission-#{@month.label}.csv"
end
end
end
# Saves the approver's choices (ticked deductions and reps, notes), and approves the month when asked
def update
if params[:shown].present?
@record.excluded_keys = Array(params[:shown_deductions]) - Array(params[:included])
@record.unpaid_user_ids = Array(params[:shown_reps]).map(&:to_i) - Array(params[:eligible]).map(&:to_i)
end
@record.notes = params[:notes] if params.key?(:notes)
@record.save!
if params[:approve].present?
RedmineQboCommission::Approver.new(@month).approve
flash[:notice] = l(:notice_sales_commission_approved, month: @month.name)
else
flash[:notice] = l(:notice_successful_update)
end
redirect_to sales_commission_month_path(@month.to_param)
rescue RedmineQboCommission::Approver::Error => e
flash[:error] = e.message
redirect_to sales_commission_month_path(@month.to_param)
rescue StandardError => e
Rails.logger.error "[redmine_qbo_commission] Approving #{@month.label}: #{e.message}"
flash[:error] = l(:error_sales_commission_qbo, error: e.message)
redirect_to sales_commission_month_path(@month.to_param)
end
# Reads the month from QuickBooks again now
def refresh
RedmineQboCommission::QboSource.transactions(@month, refresh: true)
redirect_to sales_commission_month_path(@month.to_param)
rescue StandardError => e
flash[:error] = l(:error_sales_commission_qbo, error: e.message)
redirect_to sales_commission_month_path(@month.to_param)
end
def reopen
record = CommissionMonth.for(@month)
return render_404 unless record&.approved?
RedmineQboCommission::Approver.reopen(record)
flash[:notice] = l(:notice_sales_commission_reopened, month: @month.name)
redirect_to sales_commission_month_path(@month.to_param)
end
def add_adjustment
adjustment = @record.adjustments.build(description: params[:description].to_s.strip, author: User.current,
amount: BigDecimal(params[:amount].to_s.delete(',$ '), exception: false))
if adjustment.save
flash[:notice] = l(:notice_successful_create)
else
flash[:error] = adjustment.errors.full_messages.join(', ')
end
redirect_to sales_commission_month_path(@month.to_param)
end
def remove_adjustment
@record.adjustments.find(params[:id]).destroy
flash[:notice] = l(:notice_successful_delete)
redirect_to sales_commission_month_path(@month.to_param)
rescue ActiveRecord::RecordNotFound
render_404
end
private
def find_month
@month = RedmineQboCommission::Month.parse(params[:month])
render_404 unless @month
end
# Only an open month can be changed
def find_open_record
@record = CommissionMonth.find_or_create_by!(month_start: @month.start_date)
return unless @record.approved?
flash[:error] = l(:error_sales_commission_already_approved)
redirect_to sales_commission_month_path(@month.to_param)
end
def read_transactions
RedmineQboCommission::QboSource.transactions(@month)
rescue StandardError => e
Rails.logger.error "[redmine_qbo_commission] Reading #{@month.label} from QuickBooks: #{e.message}"
@error = l(:error_sales_commission_qbo, error: e.message)
[]
end
def payroll_csv
CSV.generate do |csv|
csv << [l(:label_sales_commission_month), l(:field_login), l(:label_user), l(:label_sales_commission_amount),
l(:label_sales_commission_paid), l(:label_sales_commission_reason)]
@report.payouts.each do |payout|
csv << [@month.label, payout.user&.login, payout.user&.name, format('%.2f', payout.amount),
payout.eligible? ? l(:general_text_Yes) : l(:general_text_No), payout.ineligible_reason]
end
end
end
end
+30
View File
@@ -0,0 +1,30 @@
#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.
module SalesCommissionsHelper
def commission_money(amount)
RedmineQboCommission.format_money(amount)
end
def commission_rate(rate)
RedmineQboCommission.format_rate(rate)
end
# The transaction's number, linked to it in QuickBooks for those who approve
def commission_txn_link(txn, can_approve)
label = txn.doc_number.present? ? "##{txn.doc_number}" : txn.kind_name
can_approve ? link_to(label, txn.qbo_url, target: '_blank', rel: 'noopener') : label
end
# "Approved by Jane Smith on 12/03/2026 09:15 AM"
def commission_approved_text(record)
l(:text_sales_commission_approved_by, user: record.approved_by&.name.to_s, time: format_time(record.approved_at))
end
end
+19
View File
@@ -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
+21
View File
@@ -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
+84
View File
@@ -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
+17
View File
@@ -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
+36
View File
@@ -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
+15
View File
@@ -0,0 +1,15 @@
<%= error_messages_for @rep %>
<% taken = CommissionRep.where.not(id: @rep.id).pluck(:user_id) %>
<% users = User.active.sorted.where.not(id: taken).to_a %>
<% users << @rep.user if @rep.user && !users.include?(@rep.user) %>
<div class="box tabular">
<p>
<%= f.select :user_id, users.map { |user| [user.name, user.id] }, { include_blank: true, required: true, label: :field_user },
{ disabled: @rep.persisted? } %>
</p>
<p>
<%= f.date_field :probation_until, label: :field_commission_rep_probation_until %>
<em class="info"><%= l(:text_commission_rep_probation) %></em>
</p>
<p><%= f.check_box :active %></p>
</div>
+8
View File
@@ -0,0 +1,8 @@
<h2><%= link_to l(:label_commission_rep_plural), commission_reps_path %> » <%= @rep.name %></h2>
<%= labelled_form_for @rep, url: commission_rep_path(@rep), method: :patch do |f| %>
<%= render partial: 'commission_reps/form', locals: { f: f } %>
<%= submit_tag l(:button_save) %>
<% end %>
<% html_title @rep.name %>
+41
View File
@@ -0,0 +1,41 @@
<div class="contextual">
<%= link_to sprite_icon('add', l(:label_commission_rep_new)), new_commission_rep_path, class: 'icon icon-add' %>
<%= link_to sprite_icon('settings', l(:label_settings)), plugin_settings_path('redmine_qbo_commission'), class: 'icon icon-settings' %>
</div>
<h2><%= l(:label_commission_rep_plural) %></h2>
<p><em class="info"><%= l(:text_commission_reps) %></em></p>
<% if @reps.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<div class="autoscroll">
<table class="list">
<thead>
<tr>
<th><%= l(:label_user) %></th>
<th><%= l(:field_commission_rep_probation_until) %></th>
<th><%= l(:field_active) %></th>
<th></th>
</tr>
</thead>
<tbody>
<% @reps.each do |rep| %>
<tr>
<td class="name"><%= link_to_user rep.user %></td>
<td><%= format_date(rep.probation_until) %></td>
<td><%= checked_image rep.active? %></td>
<td class="buttons">
<%= link_to sprite_icon('edit', l(:button_edit)), edit_commission_rep_path(rep), class: 'icon icon-edit' %>
<%= link_to sprite_icon('del', l(:button_delete)), commission_rep_path(rep), method: :delete,
data: { confirm: l(:text_are_you_sure) }, class: 'icon icon-del' %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
<% html_title l(:label_commission_rep_plural) %>
+8
View File
@@ -0,0 +1,8 @@
<h2><%= link_to l(:label_commission_rep_plural), commission_reps_path %> » <%= l(:label_commission_rep_new) %></h2>
<%= labelled_form_for @rep, url: commission_reps_path do |f| %>
<%= render partial: 'commission_reps/form', locals: { f: f } %>
<%= submit_tag l(:button_create) %>
<% end %>
<% html_title l(:label_commission_rep_new) %>
@@ -0,0 +1,3 @@
<% content_for :header_tags do %>
<%= stylesheet_link_tag 'qbo_commission', plugin: 'redmine_qbo_commission' %>
<% end %>
@@ -0,0 +1,57 @@
<%= render partial: 'sales_commissions/styles' %>
<div class="contextual">
<% if User.current.admin? %>
<%= link_to sprite_icon('user', l(:label_commission_rep_plural)), commission_reps_path, class: 'icon icon-user' %>
<%= link_to sprite_icon('settings', l(:label_settings)), plugin_settings_path('redmine_qbo_commission'), class: 'icon icon-settings' %>
<% end %>
</div>
<h2><%= l(:label_sales_commission) %></h2>
<%= form_tag(sales_commissions_path, method: :get, id: 'sales-commission-month-picker', class: 'sales-commission-picker') do %>
<label for="sales_commission_month"><%= l(:label_sales_commission_month) %></label>
<%= select_tag 'month', options_for_select(@months.map { |month| [month.name, month.to_param] }, (@months.second || @months.first).to_param),
id: 'sales_commission_month' %>
<%= submit_tag l(:button_sales_commission_show), name: nil %>
<% end %>
<h3><%= l(:label_sales_commission_history) %></h3>
<% if @commission_months.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<div class="autoscroll">
<table class="list sales-commission-table">
<thead>
<tr>
<th><%= l(:label_sales_commission_month) %></th>
<th><%= l(:field_status) %></th>
<th><%= l(:label_sales_commission_net_sales) %></th>
<th><%= l(:label_sales_commission_pool) %></th>
<th><%= l(:label_sales_commission_payouts_total) %></th>
<th><%= l(:label_sales_commission_approved_by) %></th>
</tr>
</thead>
<tbody>
<% @commission_months.each do |record| %>
<tr>
<td class="name"><%= link_to record.month.name, sales_commission_month_path(record.month.to_param) %></td>
<td class="text">
<% if record.approved? %>
<span class="sales-commission-ok"><%= l(:label_sales_commission_status_approved) %></span>
<% else %>
<span class="sales-commission-miss"><%= l(:label_sales_commission_status_open) %></span>
<% end %>
</td>
<td><%= record.approved? ? commission_money(record.net_sales) : '' %></td>
<td><%= record.approved? ? commission_money(record.pool) : '' %></td>
<td><%= record.approved? ? commission_money(record.payouts_total) : '' %></td>
<td class="text"><%= record.approved_by ? link_to_user(record.approved_by) : '' %> <%= format_time(record.approved_at) if record.approved_at %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% end %>
<% html_title l(:label_sales_commission) %>
+296
View File
@@ -0,0 +1,296 @@
<%= render partial: 'sales_commissions/styles' %>
<% report = @report %>
<% pool = report.pool %>
<% approved = @record&.approved? %>
<% editable = @can_approve && !approved %>
<% previous_month = @month - 1 %>
<% next_month = @month + 1 %>
<div class="contextual">
<%= link_to "« #{previous_month.label}", sales_commission_month_path(previous_month.to_param) %>
<% if next_month.start_date <= Date.current %>
| <%= link_to "#{next_month.label} »", sales_commission_month_path(next_month.to_param) %>
<% end %>
<% if approved && @can_approve %>
| <%= link_to sprite_icon('download', l(:label_sales_commission_payroll_csv)),
sales_commission_month_path(@month.to_param, format: 'csv'), class: 'icon icon-download' %>
<% end %>
<% if editable %>
| <%= link_to sprite_icon('reload', l(:button_sales_commission_refresh)), refresh_sales_commission_month_path(@month.to_param),
method: :post, class: 'icon icon-reload' %>
<% end %>
</div>
<h2><%= @can_approve ? link_to(l(:label_sales_commission), sales_commissions_path) : l(:label_sales_commission) %> » <%= @month.name %></h2>
<% if @error %>
<p class="conflict"><%= @error %></p>
<% end %>
<% if approved %>
<p class="sales-commission-approved">
<%= sprite_icon('checked', commission_approved_text(@record)) %>
<% if @can_approve %>
<%= link_to l(:button_sales_commission_reopen), reopen_sales_commission_month_path(@month.to_param), method: :post,
data: { confirm: l(:text_sales_commission_reopen_confirm) } %>
<% end %>
</p>
<% elsif !@month.completed? %>
<p class="warning"><%= l(:text_sales_commission_month_in_progress, date: format_date(@month.end_date)) %></p>
<% end %>
<% if !approved && @read_at %>
<p><em class="info"><%= l(:text_sales_commission_read_at, time: format_time(@read_at)) %></em></p>
<% end %>
<div class="sales-commission-summary">
<div class="box">
<%= l(:label_sales_commission_gross_sales) %>
<span class="value"><%= commission_money(report.gross_sales) %></span>
</div>
<div class="box">
<%= l(:label_sales_commission_deductions) %>
<span class="value"><%= commission_money(report.deductions_total) %></span>
</div>
<div class="box">
<%= l(:label_sales_commission_net_sales) %>
<span class="value <%= pool.threshold_met? ? 'sales-commission-ok' : 'sales-commission-miss' %>"><%= commission_money(report.net_sales) %></span>
<% percent = pool.threshold.positive? ? [[report.net_sales / pool.threshold * 100, 0].max, 100].min : 100 %>
<div class="sales-commission-progress" title="<%= l(:label_sales_commission_threshold) %> <%= commission_money(pool.threshold) %>"><span style="width: <%= percent.to_f.round(1) %>%"></span></div>
</div>
<div class="box">
<% if pool.threshold_met? %>
<%= l(:label_sales_commission_threshold) %>
<span class="value sales-commission-ok"><%= commission_money(pool.threshold) %></span>
<%= l(:text_sales_commission_threshold_met) %>
<% else %>
<%= l(:label_sales_commission_short_of_threshold) %>
<span class="value sales-commission-miss"><%= commission_money(pool.short_of_threshold) %></span>
<%= l(:text_sales_commission_threshold_of, amount: commission_money(pool.threshold)) %>
<% end %>
</div>
<div class="box">
<%= l(:label_sales_commission_pool) %>
<span class="value"><%= commission_money(pool.amount) %></span>
<%= l(:text_sales_commission_each_rep) %>
</div>
</div>
<% if pool.parts.any? %>
<div class="autoscroll">
<table class="list sales-commission-table">
<thead>
<tr>
<th><%= l(:label_sales_commission_bracket) %></th>
<th><%= l(:label_sales_commission_rate) %></th>
<th><%= l(:label_sales_commission_bracket_sales) %></th>
<th><%= l(:label_sales_commission_amount) %></th>
</tr>
</thead>
<tbody>
<% pool.parts.each do |part| %>
<tr>
<td class="name"><%= commission_money(part.from) %> – <%= commission_money(part.to) %></td>
<td><%= commission_rate(part.rate) %></td>
<td><%= commission_money(part.sales) %></td>
<td><%= commission_money(part.amount) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr><td class="name" colspan="3"><%= l(:label_sales_commission_pool) %></td><td><%= commission_money(pool.amount) %></td></tr>
</tfoot>
</table>
</div>
<% end %>
<%= form_tag(sales_commission_month_path(@month.to_param), method: :post, id: 'sales-commission-month') do %>
<%= hidden_field_tag 'shown', '1', id: nil if editable %>
<h3><%= l(:label_sales_commission_payouts) %></h3>
<% if report.payouts.empty? %>
<p class="nodata"><%= l(:text_sales_commission_no_reps) %>
<% if User.current.admin? %><%= link_to l(:label_commission_rep_new), new_commission_rep_path %><% end %></p>
<% else %>
<div class="autoscroll">
<table class="list sales-commission-table" id="sales-commission-payouts">
<thead>
<tr>
<th><%= l(:label_user) %></th>
<% if editable %><th title="<%= l(:text_sales_commission_eligible) %>"><%= l(:label_sales_commission_paid) %></th><% end %>
<th><%= l(:label_sales_commission_amount) %></th>
<th><%= l(:label_sales_commission_reason) %></th>
</tr>
</thead>
<tbody>
<% report.payouts.each do |payout| %>
<% unticked = editable && @record&.unpaid_user_ids&.include?(payout.user&.id) %>
<tr class="<%= 'ineligible' unless payout.eligible? %>">
<td class="name"><%= payout.user ? link_to_user(payout.user) : '' %></td>
<% if editable %>
<td class="check">
<% blocked = !payout.eligible? && !unticked %>
<% unless blocked %>
<%= hidden_field_tag 'shown_reps[]', payout.user.id, id: nil %>
<%= check_box_tag 'eligible[]', payout.user.id, !unticked, id: "eligible_#{payout.user.id}" %>
<% end %>
</td>
<% end %>
<td><%= commission_money(payout.amount) %></td>
<td class="text"><%= payout.ineligible_reason %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td class="name"><%= l(:label_total) %></td>
<% if editable %><td></td><% end %>
<td><%= commission_money(report.payouts_total) %></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
<% end %>
<h3><%= l(:label_sales_commission_deductions) %></h3>
<% if report.deductions.empty? && report.adjustments.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<div class="autoscroll">
<table class="list sales-commission-table" id="sales-commission-deductions">
<thead>
<tr>
<th><%= l(:label_sales_commission_deducted) %></th>
<th><%= l(:label_date) %></th>
<th><%= l(:field_type) %></th>
<th><%= l(:label_sales_commission_number) %></th>
<th><%= l(:label_sales_commission_customer) %></th>
<th><%= l(:label_sales_commission_total) %></th>
<th><%= l(:label_sales_commission_tax) %></th>
<th><%= l(:label_sales_commission_amount) %></th>
</tr>
</thead>
<tbody>
<% report.deductions.each do |txn| %>
<% included = report.included?(txn) %>
<tr class="<%= 'excluded' unless included %>">
<td class="check">
<% if editable %>
<%= hidden_field_tag 'shown_deductions[]', txn.key, id: nil %>
<%= check_box_tag 'included[]', txn.key, included, id: "included_#{txn.key.tr(':', '_')}" %>
<% else %>
<%= checked_image included %>
<% end %>
</td>
<td class="text"><%= format_date(txn.txn_date) %></td>
<td class="text"><%= txn.kind_name %></td>
<td class="text"><%= commission_txn_link(txn, @can_approve) %></td>
<td class="text"><%= txn.customer_name %></td>
<td><%= commission_money(txn.total) %></td>
<td><%= commission_money(txn.tax) %></td>
<td><%= commission_money(txn.net) %></td>
</tr>
<% end %>
<% report.adjustments.each do |adjustment| %>
<tr class="adjustment">
<td class="check">
<% if editable %>
<%= link_to sprite_icon('del', l(:button_delete)), sales_commission_adjustment_path(@month.to_param, adjustment),
method: :delete, data: { confirm: l(:text_are_you_sure) }, class: 'icon-only icon-del' %>
<% else %>
<%= checked_image true %>
<% end %>
</td>
<td class="text"><%= format_date(adjustment.created_at) %></td>
<td class="text"><%= l(:label_sales_commission_adjustment) %></td>
<td class="text"></td>
<td class="text"><%= adjustment.description %><% if adjustment.author %> <small>(<%= adjustment.author.name %>)</small><% end %></td>
<td></td>
<td></td>
<td><%= commission_money(adjustment.amount) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr><td class="name" colspan="7"><%= l(:label_sales_commission_deductions) %></td><td><%= commission_money(report.deductions_total) %></td></tr>
</tfoot>
</table>
</div>
<% end %>
<% if editable %>
<p>
<label for="sales_commission_notes"><%= l(:field_notes) %></label><br>
<%= text_area_tag 'notes', @record&.notes, id: 'sales_commission_notes', rows: 3, cols: 80 %>
</p>
<p>
<%= submit_tag l(:button_save), name: 'save' %>
<% if @month.completed? %>
<%= submit_tag l(:button_sales_commission_approve), name: 'approve',
data: { confirm: l(:text_sales_commission_approve_confirm) } %>
<% end %>
</p>
<% elsif @record&.notes.present? %>
<h3><%= l(:field_notes) %></h3>
<div class="wiki"><%= simple_format(h(@record.notes)) %></div>
<% end %>
<% end %>
<% if editable %>
<%= form_tag(sales_commission_adjustments_path(@month.to_param), method: :post, class: 'sales-commission-adjustment',
id: 'sales-commission-adjustment') do %>
<fieldset class="box">
<legend><%= l(:label_sales_commission_add_adjustment) %></legend>
<%= text_field_tag 'description', '', placeholder: l(:field_description), required: true, maxlength: 255 %>
<%= text_field_tag 'amount', '', placeholder: l(:label_sales_commission_amount), class: 'amount', required: true,
inputmode: 'decimal' %>
<%= submit_tag l(:button_add) %>
<br><em class="info"><%= l(:text_sales_commission_adjustment) %></em>
</fieldset>
<% end %>
<% end %>
<h3><%= l(:label_sales_commission_sales) %> (<%= report.sales.size %>)</h3>
<% if report.sales.empty? %>
<p class="nodata"><%= l(:label_no_data) %></p>
<% else %>
<div class="autoscroll">
<table class="list sales-commission-table" id="sales-commission-sales">
<thead>
<tr>
<th><%= l(:label_sales_commission_paid_on) %></th>
<th><%= l(:field_type) %></th>
<th><%= l(:label_sales_commission_number) %></th>
<th><%= l(:label_sales_commission_customer) %></th>
<th><%= l(:label_sales_commission_total) %></th>
<th><%= l(:label_sales_commission_tax) %></th>
<th><%= l(:label_sales_commission_amount) %></th>
</tr>
</thead>
<tbody>
<% report.sales.each do |txn| %>
<tr>
<td class="text"><%= format_date(txn.txn_date) %></td>
<td class="text"><%= txn.kind_name %></td>
<td class="text"><%= commission_txn_link(txn, @can_approve) %></td>
<td class="text"><%= txn.customer_name %></td>
<td><%= commission_money(txn.total) %></td>
<td><%= commission_money(txn.tax) %></td>
<td><%= commission_money(txn.net) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td class="name" colspan="4"><%= l(:label_total) %></td>
<td><%= commission_money(report.sales.sum(BigDecimal('0'), &:total)) %></td>
<td><%= commission_money(report.sales.sum(BigDecimal('0')) { |txn| txn.tax.to_d }) %></td>
<td><%= commission_money(report.gross_sales) %></td>
</tr>
</tfoot>
</table>
</div>
<p><em class="info"><%= l(:text_sales_commission_sales) %></em></p>
<% end %>
<% html_title "#{l(:label_sales_commission)} #{@month.label}" %>
@@ -0,0 +1,65 @@
<%# Settings for redmine_qbo_commission (Administration » Plugins » Configure) %>
<% s = RedmineQboCommission::Settings %>
<% field = ->(key) { "settings[#{key}]" } %>
<% plain = ->(number) { number.to_d == number.to_d.to_i ? number.to_d.to_i.to_s : number.to_d.to_s('F') } %>
<% brackets = s.brackets %>
<p>
<%= link_to sprite_icon('user', l(:label_commission_rep_plural)), commission_reps_path, class: 'icon icon-user' %>
<%= link_to sprite_icon('stats', l(:label_sales_commission)), sales_commissions_path, class: 'icon icon-stats' %>
</p>
<fieldset class="box tabular">
<legend><%= l(:label_sales_commission_rates) %></legend>
<p>
<label for="qbo_commission_threshold"><%= l(:label_sales_commission_threshold) %></label>
$<%= number_field_tag field.call('threshold'), plain.call(s.threshold), id: 'qbo_commission_threshold', min: 0, step: 0.01, style: 'width: 9em' %>
<em class="info"><%= l(:text_sales_commission_setting_threshold) %></em>
</p>
<p>
<label for="qbo_commission_base_rate"><%= l(:label_sales_commission_base_rate) %></label>
<%= number_field_tag field.call('base_rate'), plain.call(brackets[0][1]), id: 'qbo_commission_base_rate', min: 0, step: 0.01, style: 'width: 5em' %>%
<%= l(:label_sales_commission_up_to) %>
$<%= number_field_tag field.call('base_limit'), plain.call(brackets[0][0]), id: 'qbo_commission_base_limit', min: 0, step: 0.01, style: 'width: 9em' %>
</p>
<p>
<label for="qbo_commission_step_rate"><%= l(:label_sales_commission_step_rate) %></label>
<%= number_field_tag field.call('step_rate'), plain.call(brackets[1][1]), id: 'qbo_commission_step_rate', min: 0, step: 0.01, style: 'width: 5em' %>%
<%= l(:label_sales_commission_up_to) %>
$<%= number_field_tag field.call('step_limit'), plain.call(brackets[1][0]), id: 'qbo_commission_step_limit', min: 0, step: 0.01, style: 'width: 9em' %>
</p>
<p>
<label for="qbo_commission_top_rate"><%= l(:label_sales_commission_top_rate) %></label>
<%= number_field_tag field.call('top_rate'), plain.call(brackets[2][1]), id: 'qbo_commission_top_rate', min: 0, step: 0.01, style: 'width: 5em' %>%
<%= l(:label_sales_commission_above) %>
</p>
<p><em class="info"><%= l(:text_sales_commission_setting_brackets) %></em></p>
</fieldset>
<fieldset class="box tabular">
<legend><%= l(:label_sales_commission_counted) %></legend>
<p>
<label for="qbo_commission_sales_receipts"><%= l(:label_sales_commission_include_sales_receipts) %></label>
<%= hidden_field_tag field.call('include_sales_receipts'), '0', id: nil %>
<%= check_box_tag field.call('include_sales_receipts'), '1', s.include_sales_receipts?, id: 'qbo_commission_sales_receipts' %>
<em class="info"><%= l(:text_sales_commission_setting_sales_receipts) %></em>
</p>
<p>
<label for="qbo_commission_refund_receipts"><%= l(:label_sales_commission_deduct_refund_receipts) %></label>
<%= hidden_field_tag field.call('deduct_refund_receipts'), '0', id: nil %>
<%= check_box_tag field.call('deduct_refund_receipts'), '1', s.deduct_refund_receipts?, id: 'qbo_commission_refund_receipts' %>
</p>
<p>
<label for="qbo_commission_credit_memos"><%= l(:label_sales_commission_deduct_credit_memos) %></label>
<%= hidden_field_tag field.call('deduct_credit_memos'), '0', id: nil %>
<%= check_box_tag field.call('deduct_credit_memos'), '1', s.deduct_credit_memos?, id: 'qbo_commission_credit_memos' %>
</p>
<p>
<label for="qbo_commission_items"><%= l(:label_sales_commission_excluded_items) %></label>
<%= hidden_field_tag "#{field.call('excluded_item_ids')}[]", '', id: nil %>
<%= select_tag "#{field.call('excluded_item_ids')}[]",
options_from_collection_for_select(Item.order(:name), :id, :name, Array(s.values['excluded_item_ids']).map(&:to_s)),
multiple: true, size: 8, id: 'qbo_commission_items' %>
<em class="info"><%= l(:text_sales_commission_setting_excluded_items) %></em>
</p>
</fieldset>