diff --git a/README.md b/README.md index 8f8a52b..5c6a985 100644 --- a/README.md +++ b/README.md @@ -140,10 +140,11 @@ Estimates and invoices keep their number, date, customer, total and **man-hours* ### Business report -**Business report** in the top menu compares, for a period (this or last month, quarter or year, or custom dates): +**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 and payments received (totals and counts), invoiced vs. estimated, collected vs. invoiced, and what's still owed on those invoices. * **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. 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. diff --git a/app/controllers/qbo_reports_controller.rb b/app/controllers/qbo_reports_controller.rb index 41a2d8b..bd17c0d 100644 --- a/app/controllers/qbo_reports_controller.rb +++ b/app/controllers/qbo_reports_controller.rb @@ -17,7 +17,10 @@ class QboReportsController < ApplicationController def index @report = RedmineQboBilling::BusinessReport.new(period: params[:period].presence || 'this_year', - from: params[:from], to: params[:to], today: User.current.today) + from: params[:from], to: params[:to], today: User.current.today, + week_start: RedmineQboBilling::BusinessReport.week_start_from_setting(Setting.start_of_week)) + # Last year's lines on the charts + @compare = params[:compare] == '1' # Syncs still running for what the report counts: its figures are incomplete until they finish @syncing = QboSyncStatus.by_entity(%w[Estimate Invoice Payment]).values.sort_by(&:entity) end diff --git a/app/views/qbo_reports/index.html.erb b/app/views/qbo_reports/index.html.erb index dabf276..e8ff6dc 100644 --- a/app/views/qbo_reports/index.html.erb +++ b/app/views/qbo_reports/index.html.erb @@ -7,6 +7,44 @@ <% bar = ->(value, max) { max > 0 ? (value.to_f / max * 100).round(1) : 0 } %> <% fmt_hours = ->(h) { format('%.1f', h.to_f) } %> <% pct = ->(v) { v.nil? ? '–' : "#{v}%" } %> +<%# Line charts: a label and a tooltip title per point, from the chart's granularity %> +<% chart = @report.chart %> +<% months_abbr = ::I18n.t('date.abbr_month_names') %> +<% days_abbr = ::I18n.t('date.abbr_day_names') %> +<% one_year = @report.from.year == @report.to.year %> +<% point_label = lambda do |date| + case chart[:granularity] + when :day then "#{days_abbr[date.wday]} #{date.day}" + when :week then "#{months_abbr[date.month]} #{date.day}" + else one_year ? months_abbr[date.month] : "#{months_abbr[date.month]} #{date.year}" + end + end %> +<% point_title = lambda do |first, last| + case chart[:granularity] + when :day then format_date(first) + when :week then "#{format_date(first)} – #{format_date(last)}" + else "#{::I18n.t('date.month_names')[first.month]} #{first.year}" + end + end %> +<% previous_ends = chart[:previous_points].each_with_index.map { |p, i| Date.parse(p) + (Date.parse(chart[:ends][i]) - Date.parse(chart[:points][i])) } %> +<% chart_points = { + labels: chart[:points].map { |p| point_label.call(Date.parse(p)) }, + titles: chart[:points].each_with_index.map { |p, i| point_title.call(Date.parse(p), Date.parse(chart[:ends][i])) }, + previous_titles: chart[:previous_points].each_with_index.map { |p, i| point_title.call(Date.parse(p), previous_ends[i]) }, + last_year: l(:label_report_last_year) + } %> +<% chart_series = lambda do |keys| + keys.map do |key, label, color| + { label: label, color: color, current: chart[:current][key], previous: chart[:previous][key] } + end + end %> +<% sales_series = chart_series.call([[:estimates, l(:label_report_estimates_written), '#628db6'], + [:invoices, l(:label_report_invoices_written), '#e8a33d'], + [:payments, l(:label_report_payments_received), '#5aa05a']]) %> +<% 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'], + [:spent_hours, l(:label_report_spent_hours), '#5aa05a']]) %>