diff --git a/README.md b/README.md index 5c6a985..4ea6090 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,7 @@ Estimates and invoices keep their number, date, customer, total and **man-hours* * **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. +* **Projects**: pick one or more projects (Ctrl/Cmd-click) 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. diff --git a/app/controllers/qbo_reports_controller.rb b/app/controllers/qbo_reports_controller.rb index bd17c0d..cca7d98 100644 --- a/app/controllers/qbo_reports_controller.rb +++ b/app/controllers/qbo_reports_controller.rb @@ -15,13 +15,48 @@ class QboReportsController < ApplicationController before_action :authorize_global + # The user's preference holding the projects the report opens with + PROJECTS_PREF = :qbo_report_project_ids + def index + # Projects for the issues and spent time: the projects picker can choose some + @projects = Project.visible.to_a + @project_ids = report_project_ids @report = RedmineQboBilling::BusinessReport.new(period: params[:period].presence || 'this_year', from: params[:from], to: params[:to], today: User.current.today, - week_start: RedmineQboBilling::BusinessReport.week_start_from_setting(Setting.start_of_week)) + week_start: RedmineQboBilling::BusinessReport.week_start_from_setting(Setting.start_of_week), + project_ids: @project_ids) # 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 + + private + + # The form's projects (none picked is every project), or the user's saved ones when the address has none. + # "Remember these projects" saves the form's choice. Only projects the user can see are kept. + def report_project_ids + visible = @projects.map(&:id) + if params.key?(:project_ids) + ids = Array(params[:project_ids]).map(&:to_i) & visible + save_project_ids(ids) if params[:save_projects] == '1' + else + ids = saved_project_ids & visible + end + @saved_project_ids = saved_project_ids & visible + ids + end + + def saved_project_ids + return [] unless User.current.logged? + Array(User.current.pref[PROJECTS_PREF]).map(&:to_i) + end + + def save_project_ids(ids) + return unless User.current.logged? + pref = User.current.pref + pref[PROJECTS_PREF] = ids + pref.save + end end diff --git a/app/views/qbo_reports/index.html.erb b/app/views/qbo_reports/index.html.erb index e8ff6dc..c521de4 100644 --- a/app/views/qbo_reports/index.html.erb +++ b/app/views/qbo_reports/index.html.erb @@ -64,10 +64,36 @@ <%= check_box_tag 'compare', '1', @compare, id: 'qbo-report-compare' %> <%= l(:label_report_compare_last_year) %> + <%# Projects for the issues and spent time; the blank value sends project_ids when none are picked (every project) %> +
+ + <%= hidden_field_tag 'project_ids[]', '', id: nil %> + <%= select_tag 'project_ids[]', + project_tree_options_for_select(@projects, selected: @projects.select { |p| @project_ids.include?(p.id) }), + multiple: true, size: [[@projects.size, 3].max, 8].min, id: 'qbo-report-project-ids' %> + + <%= l(:text_report_projects) %> + + <% if @saved_project_ids.any? %> + <%= l(:text_report_saved_projects, projects: @projects.select { |p| @saved_project_ids.include?(p.id) }.map(&:name).join(', ')) %> + <% end %> + +
<% end %> -

<%= format_date(@report.from) %> – <%= format_date(@report.to) %>

+

+ <%= format_date(@report.from) %> – <%= format_date(@report.to) %> + <% if @report.projects_filtered? %> + · <%= l(:label_project_plural) %>: <%= @projects.select { |p| @project_ids.include?(p.id) }.map(&:name).join(', ') %> + <% end %> +

+<% if @report.projects_filtered? %> +

<%= l(:text_report_projects_filtered) %>

+<% end %> <% if Array(@syncing).any? %> <% sync_labels = { 'Estimate' => l(:label_estimates), 'Invoice' => l(:label_invoices), 'Payment' => l(:label_payments) } %> @@ -230,6 +256,10 @@ table.qbo-report-months tfoot td { border-top: 2px solid #ccc; } #qbo-report-form select, #qbo-report-form input { vertical-align: middle; } #qbo-report-form .qbo-report-compare { margin-left: 12px; white-space: nowrap; } + #qbo-report-form .qbo-report-projects { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 6px 10px; margin-top: 8px; } + #qbo-report-form .qbo-report-projects select { min-width: 220px; max-width: 100%; } + #qbo-report-form .qbo-report-projects-options { display: flex; flex-direction: column; gap: 4px; } + #qbo-report-form .qbo-report-projects-options label { white-space: nowrap; } .qbo-report-chart { position: relative; height: 260px; margin: 4px 4px 16px; } diff --git a/config/locales/en.yml b/config/locales/en.yml index b2b242d..87f95a6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -343,6 +343,10 @@ en: text_qbo_sync_queued: "waiting to sync" text_report_needs_sync: "%{count} estimates or invoices in this period don't have their total or hours yet. Run a full sync from QuickBooks to fill them in." text_report_notes: "Estimates, invoices and payments count by their QuickBooks date; their hours are the quantities of the labor products/services. Issues' estimated time counts issues created in the period, without subtasks. Spent time counts time logged in the period." + label_report_remember_projects: "Remember these projects" + text_report_projects: "For issues' estimated time and spent time. None picked: every project. Ctrl/Cmd-click to pick several." + text_report_saved_projects: "Opens with: %{projects}" + text_report_projects_filtered: "Only issues' estimated time and spent time are limited to these projects. Estimates, invoices and payments come from QuickBooks, which has no projects, so they cover the whole business (and so does the CPI's invoiced hours)." notice_payment_not_found: "Payment not found in QuickBooks" notice_payment_saved: "Payment of %{amount} saved in QuickBooks" text_no_payment_methods: "QuickBooks' payment methods couldn't be read. Check the QuickBooks connection." diff --git a/lib/redmine_qbo_billing/business_report.rb b/lib/redmine_qbo_billing/business_report.rb index 8c022bb..8649517 100644 --- a/lib/redmine_qbo_billing/business_report.rb +++ b/lib/redmine_qbo_billing/business_report.rb @@ -16,7 +16,8 @@ module RedmineQboBilling # 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 - # spent time by the day it was spent. + # spent time by the day it was spent. project_ids limits the issues and spent time to those projects; the + # QuickBooks figures have no project, so they always cover the whole business. class BusinessReport PERIODS = %w[this_week last_week this_month last_month this_quarter last_quarter this_year last_year custom].freeze WEEK_DAYS = { 1 => :monday, 6 => :saturday, 7 => :sunday }.freeze @@ -26,7 +27,7 @@ module RedmineQboBilling MONEY = %i[estimates invoices payments].freeze HOURS = %i[estimate_hours issue_hours invoice_hours spent_hours].freeze - attr_reader :from, :to, :period + attr_reader :from, :to, :period, :project_ids # The dates for a period, from today: [from, to]. "custom" uses the given dates (either can be blank). def self.range_for(period, today: Date.current, from: nil, to: nil, week_start: :monday) @@ -57,14 +58,21 @@ module RedmineQboBilling # payments: [[date, total]], issues: [[date, estimated_hours]], time_entries: [[date, hours]] } # week_start: the first day of a week (:monday, :saturday or :sunday); see week_start_from_setting. # previous_data (for tests): last year's data, like data. + # project_ids: the projects whose issues and spent time count; blank for every project. def initialize(period: 'this_year', from: nil, to: nil, today: Date.current, data: nil, week_start: :monday, - previous_data: nil) + previous_data: nil, project_ids: nil) @period = PERIODS.include?(period.to_s) ? period.to_s : 'this_year' @week_start = week_start @from, @to = self.class.range_for(@period, today: today, from: from, to: to, week_start: week_start) @today = today @data = data @previous_data = previous_data + @project_ids = Array(project_ids).map(&:to_i).select(&:positive?).uniq + end + + # Whether the issues and spent time are limited to some projects + def projects_filtered? + project_ids.any? end # Redmine's "Start calendars on" setting (1 Monday, 6 Saturday, 7 Sunday); Monday when it follows the language @@ -251,13 +259,18 @@ module RedmineQboBilling dates = from..to times = from.in_time_zone.beginning_of_day..to.in_time_zone.end_of_day leaf = "#{Issue.table_name}.rgt IS NULL OR #{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1" + issues = Issue.where(created_on: times).where(leaf).where.not(estimated_hours: nil) + time_entries = TimeEntry.where(spent_on: dates) + if projects_filtered? + issues = issues.where(project_id: project_ids) + time_entries = time_entries.where(project_id: project_ids) + end { estimates: Estimate.where(txn_date: dates).pluck(:txn_date, :total_amount, :hours), invoices: Invoice.where(txn_date: dates).pluck(:txn_date, :total_amount, :hours, :balance), payments: Payment.where(txn_date: dates).pluck(:txn_date, :total_amount), - issues: Issue.where(created_on: times).where(leaf).where.not(estimated_hours: nil) - .pluck(:created_on, :estimated_hours).map { |t, h| [t.in_time_zone.to_date, h] }, - time_entries: TimeEntry.where(spent_on: dates).pluck(:spent_on, :hours) + issues: issues.pluck(:created_on, :estimated_hours).map { |t, h| [t.in_time_zone.to_date, h] }, + time_entries: time_entries.pluck(:spent_on, :hours) } end