Files
redmine_qbo_commission/app/views/sales_commissions/show.html.erb
T
rickyandClaude Opus 5.5 6fcd5f9b64 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>
2026-11-16 18:53:25 -05:00

297 lines
12 KiB
ERB
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<%= 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}" %>