diff --git a/README.md b/README.md index 31bf0bd..87fd565 100644 --- a/README.md +++ b/README.md @@ -159,12 +159,13 @@ Refunds for canceled orders and returned parts, following SOP-OPS-014, so they d **Business report** in the top menu compares, for a period (this or last week, month, quarter or year, or custom dates; weeks start on the day set in Redmine's *Start calendars on* setting, Monday when it follows the language): * **Sales**: estimates written, invoices written, invoices paid in full (on the day each was paid in full) and payments received (totals and counts), invoiced vs. estimated, collected vs. invoiced, and what's still owed on those invoices. Estimates and invoices are without sales tax (QuickBooks' tax on each is cached with it); payments are as received, so collected vs. invoiced compares them with the invoices' totals with tax. +* **Sales tax**: **tax invoiced**, the sales tax on invoices dated in the period, paid or not (accrual), and **tax collected**, the sales tax on invoices paid in full in the period, counted on the day each was paid in full (cash), each with how many invoices carried tax, a line chart and a month-by-month table. A partly paid invoice's tax counts as collected once it's paid in full. Only invoices are counted: sales receipts, refund receipts and credit memos aren't kept in Redmine, so file sales tax from QuickBooks' *Sales Tax Liability* report. * **Hours**: estimated hours on estimates, issues' estimated time, invoiced hours on invoices and time spent, with spent vs. estimated, and the **cost performance index** (CPI = invoiced hours รท spent hours): green at 1.00 or more, yellow below 1, red below 0.80. Each month shows its CPI too. * **Charts**: above each set of bars, a line chart of the same figures in the same colors (Chart.js 4.5.1, MIT licensed, bundled with the plugin in assets/javascripts so it doesn't clash with other plugins' copies such as redmine_indicator's Chart.js 2), with a point per day for a week, per week for up to about three months, and per month beyond. Hovering a point shows its figures. **Compare with last year** adds last year's lines, dashed and faded, without reloading: days and weeks go back 52 weeks so weekdays line up, months go back to the same month. Clicking a figure in the legend hides it (and its last-year line); clicking *last year* stops comparing. Today's line stops at today. -* **By month**: every figure month by month, with totals. +* **By month**: every sales and hours figure month by month, with totals (the sales tax has its own table). * **Projects**: pick one or more open projects (Ctrl/Cmd-click; closed and archived projects aren't listed) to count only their issues' estimated time and spent time, in the totals, charts and months; none picked counts every project. Estimates, invoices and payments have no project in QuickBooks, so they always cover the whole business (as do the CPI's invoiced hours). **Remember these projects** saves the choice in your Redmine preferences, and the report opens with it until you save another (saving none goes back to every project). -Estimates, invoices and payments count by their QuickBooks date. Issues' estimated time counts issues created in the period, leaving out parents so subtasks aren't counted twice. Spent time counts time logged in the period. While estimates, invoices or payments are syncing, the report says so (with how many have been read) and refreshes every 30 seconds; the settings page's Synchronization table shows the same per type. Estimates and invoices synced before these figures were added have no total or hours until a full sync (the report says how many); run one again after changing the labor products/services. +Estimates, invoices and payments count by their QuickBooks date; invoices paid in full and tax collected by the day each invoice was paid in full. Issues' estimated time counts issues created in the period, leaving out parents so subtasks aren't counted twice. Spent time counts time logged in the period. While estimates, invoices or payments are syncing, the report says so (with how many have been read) and refreshes every 30 seconds; the settings page's Synchronization table shows the same per type. Estimates and invoices synced before these figures were added have no total, hours or sales tax, and paid invoices no paid-in-full day, until a full sync (the report says how many); run one again after changing the labor products/services. ### QuickBooks custom fields diff --git a/app/views/qbo_reports/index.html.erb b/app/views/qbo_reports/index.html.erb index a822702..7a6f9dc 100644 --- a/app/views/qbo_reports/index.html.erb +++ b/app/views/qbo_reports/index.html.erb @@ -42,6 +42,10 @@ [:invoices, l(:label_report_invoices_written), '#e8a33d'], [:paid, l(:label_report_invoices_paid), '#c0504d'], [:payments, l(:label_report_payments_received), '#5aa05a']]) %> +<% taxes = @report.taxes %> +<% max_tax = taxes.values.map { |t| t[:total] }.max.to_f %> +<% tax_series = chart_series.call([[:tax_invoiced, l(:label_report_tax_invoiced), '#e8a33d'], + [:tax_collected, l(:label_report_tax_collected), '#c0504d']]) %> <% hours_series = chart_series.call([[:estimate_hours, l(:label_report_estimate_hours), '#628db6'], [:issue_hours, l(:label_report_issue_hours), '#9b7fc0'], [:invoice_hours, l(:label_report_invoice_hours), '#e8a33d'], @@ -182,6 +186,54 @@ +

<%= l(:label_report_taxes) %>

+
+
+
+ +
+ + + <% [[:tax_invoiced, l(:label_report_tax_invoiced)], [:tax_collected, l(:label_report_tax_collected)]].each do |key, label| %> + + + + + + <% end %> + +
<%= label %> (<%= l(:label_report_taxed_invoices, count: taxes[key][:count]) %>)
<%= number_to_currency(taxes[key][:total]) %>
+

<%= l(:text_report_taxes) %>

+
+
+ + + + + + + + + + <% @report.months.each do |row| %> + + + + + + <% end %> + + + + + + + + +
<%= l(:label_month) %><%= l(:label_report_tax_invoiced) %><%= l(:label_report_tax_collected) %>
<%= ::I18n.t('date.abbr_month_names')[row[:month].month] %> <%= row[:month].year %><%= number_to_currency(row[:tax_invoiced]) %><%= number_to_currency(row[:tax_collected]) %>
<%= l(:label_total) %><%= number_to_currency(taxes[:tax_invoiced][:total]) %><%= number_to_currency(taxes[:tax_collected][:total]) %>
+
+
+

<%= l(:label_report_by_month) %>

@@ -242,7 +294,8 @@ .qbo-bar { height: 14px; border-radius: 2px; min-width: 1px; background: #628db6; } .qbo-bar-invoices, .qbo-bar-invoice_hours { background: #e8a33d; } .qbo-bar-payments, .qbo-bar-spent_hours { background: #5aa05a; } - .qbo-bar-paid { background: #c0504d; } + .qbo-bar-paid, .qbo-bar-tax_collected { background: #c0504d; } + .qbo-bar-tax_invoiced { background: #e8a33d; } .qbo-bar-issue_hours { background: #9b7fc0; } .qbo-report-ratios { margin: 8px 4px; line-height: 1.7; } .qbo-report-cpi { margin: 10px 4px 4px; } diff --git a/config/locales/en.yml b/config/locales/en.yml index e962292..8b6e9c4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -385,6 +385,13 @@ en: label_report_estimates_written: "Estimates written" label_report_invoices_written: "Invoices written" label_report_invoices_paid: "Invoices paid in full" + label_report_taxes: "Sales tax" + label_report_tax_invoiced: "Tax invoiced" + label_report_tax_collected: "Tax collected" + label_report_taxed_invoices: + one: "1 invoice" + other: "%{count} invoices" + text_report_taxes: "Tax invoiced is the sales tax on invoices dated in the period (accrual); tax collected is the sales tax on invoices paid in full in the period (cash), counted on the day each was paid in full. Sales receipts and refunds aren't included: file from QuickBooks' Sales Tax Liability report." text_report_without_tax: "Estimates and invoices are without sales tax; invoices paid in full count on the day they were paid in full. Payments are as received, so collected vs. invoiced compares them with the invoices' totals with tax." label_report_payments_received: "Payments received" label_report_estimate_hours: "Estimated (estimates)" diff --git a/lib/redmine_qbo_billing/business_report.rb b/lib/redmine_qbo_billing/business_report.rb index 4ba84da..e190086 100644 --- a/lib/redmine_qbo_billing/business_report.rb +++ b/lib/redmine_qbo_billing/business_report.rb @@ -14,6 +14,7 @@ module RedmineQboBilling # Redmine's issues and spent time: # money: estimates written, invoices written, invoices paid in full, payments received (totals and counts); # estimates and invoices without sales tax (it isn't the shop's revenue), payments as received + # taxes: sales tax on invoices written (by their date) and on invoices paid in full (by that day) # hours: man-hours on estimates, issues' estimated time, man-hours invoiced, time spent # with a month-by-month breakdown. Estimates, invoices and payments count by their QuickBooks date, issues by # the day they were created (only issues without subtasks, so parents' totals aren't counted twice), and @@ -27,6 +28,7 @@ module RedmineQboBilling WEEKLY_UP_TO = 93 MONEY = %i[estimates invoices paid payments].freeze HOURS = %i[estimate_hours issue_hours invoice_hours spent_hours].freeze + TAXES = %i[tax_invoiced tax_collected].freeze attr_reader :from, :to, :period, :project_ids @@ -161,6 +163,12 @@ module RedmineQboBilling payments: [:payments, ->(r) { to_money(r[1]) }] }.freeze + # Sales tax on invoices written (by their date) and on invoices paid in full (by that day) + TAX_ROWS = { + tax_invoiced: [:invoices, ->(r) { to_money(r[4]) }], + tax_collected: [:paid, ->(r) { to_money(r[2]) }] + }.freeze + def self.to_money(value) BigDecimal(value.to_s.presence || '0') rescue ArgumentError @@ -179,6 +187,14 @@ module RedmineQboBilling end end + # { tax_invoiced: { total:, count: }, tax_collected: { total:, count: } }: count is the invoices that had tax + def taxes + @taxes ||= TAX_ROWS.to_h do |key, (source, value)| + amounts = Array(data[source]).map { |r| value.call(r) } + [key, { total: amounts.sum(BigDecimal('0')), count: amounts.count(&:positive?) }] + end + end + # { estimate_hours:, issue_hours:, invoice_hours:, spent_hours: } def hours @hours ||= { @@ -229,7 +245,7 @@ module RedmineQboBilling last = [to, @today].min last = from if last < from while month <= last - rows << { month: month }.merge((MONEY + HOURS).index_with { |_| 0 }) + rows << { month: month }.merge((MONEY + HOURS + TAXES).index_with { |_| 0 }) month = month.next_month end index = rows.index_by { |r| [r[:month].year, r[:month].month] } @@ -239,7 +255,7 @@ module RedmineQboBilling r[key] += kind == :money ? to_d(row[col]) : row[col].to_f end end - MONEY_ROWS.each do |key, (source, value)| + MONEY_ROWS.merge(TAX_ROWS).each do |key, (source, value)| Array(data[source]).each do |row| r = index[[row[0].year, row[0].month]] or next r[key] += value.call(row) @@ -264,11 +280,11 @@ module RedmineQboBilling # { money key or hours key => [sum per bucket] } for the data, in buckets ([first day, last day] pairs) def sums_by_bucket(rows, bucket_list) - sums = (MONEY + HOURS).index_with { |_| Array.new(bucket_list.size, 0.0) } + sums = (MONEY + HOURS + TAXES).index_with { |_| Array.new(bucket_list.size, 0.0) } find = lambda do |date| bucket_list.index { |a, b| date >= a && date <= b } end - MONEY_ROWS.each do |key, (source, value)| + MONEY_ROWS.merge(TAX_ROWS).each do |key, (source, value)| Array(rows[source]).each do |row| i = find.call(row[0]) or next sums[key][i] += value.call(row).to_f diff --git a/test/unit/business_report_test.rb b/test/unit/business_report_test.rb index 7d8eabf..8838c22 100644 --- a/test/unit/business_report_test.rb +++ b/test/unit/business_report_test.rb @@ -53,4 +53,16 @@ class BusinessReportTest < ActiveSupport::TestCase assert_match(/\APaid \S+/, RedmineQboBilling::InvoiceStatus.label(:paid, paid_on: Date.new(2026, 11, 3))) assert_equal 'Open', RedmineQboBilling::InvoiceStatus.label(:open, paid_on: Date.new(2026, 11, 3)) end + + def test_taxes + r = report(invoices: [[NOV + 3, '1070', 5, '0', '70'], [NOV + 4, '200', 1, '200', '0'], [NOV + 6, '535', 2, '0', '35']], + paid: [[NOV + 9, '1070', '70'], [Date.new(2026, 11, 10), '500', nil]]) + assert_equal({ total: BigDecimal('105'), count: 2 }, r.taxes[:tax_invoiced]) + assert_equal({ total: BigDecimal('70'), count: 1 }, r.taxes[:tax_collected]) + november = r.months.find { |m| m[:month] == NOV } + assert_equal BigDecimal('105'), november[:tax_invoiced] + assert_equal BigDecimal('70'), november[:tax_collected] + assert_equal 105.0, r.chart[:current][:tax_invoiced].compact.sum + assert_equal 70.0, r.chart[:current][:tax_collected].compact.sum + end end