diff --git a/README.md b/README.md index 193be22..3972b36 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,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 invoiced vs. spent and spent vs. estimated. * **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. 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. 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. ### QuickBooks custom fields diff --git a/app/controllers/qbo_reports_controller.rb b/app/controllers/qbo_reports_controller.rb index 15641f7..41a2d8b 100644 --- a/app/controllers/qbo_reports_controller.rb +++ b/app/controllers/qbo_reports_controller.rb @@ -18,5 +18,7 @@ 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) + # 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 end diff --git a/app/jobs/qbo_sync_dispatcher.rb b/app/jobs/qbo_sync_dispatcher.rb index 4152c57..eb8737c 100644 --- a/app/jobs/qbo_sync_dispatcher.rb +++ b/app/jobs/qbo_sync_dispatcher.rb @@ -40,7 +40,10 @@ class QboSyncDispatcher end end - jobs.each { |job| QboSyncJob.perform_later(entity: job, full_sync: full_sync) } + jobs.each do |job| + QboSyncStatus.queued(job.to_s) + QboSyncJob.perform_later(entity: job, full_sync: full_sync) + end end def self.log(msg) diff --git a/app/jobs/qbo_sync_job.rb b/app/jobs/qbo_sync_job.rb index 813ed5e..71216cd 100644 --- a/app/jobs/qbo_sync_job.rb +++ b/app/jobs/qbo_sync_job.rb @@ -25,7 +25,13 @@ class QboSyncJob < ApplicationJob elsif doc_number.present? service.sync_by_doc_number(doc_number) else - service.sync(full_sync: full_sync) + # Shown on the business report and the settings page while it runs + QboSyncStatus.running(entity) + begin + service.sync(full_sync: full_sync) + ensure + QboSyncStatus.finished(entity) + end end end diff --git a/app/models/qbo_base_model.rb b/app/models/qbo_base_model.rb index 4931063..931f0c2 100644 --- a/app/models/qbo_base_model.rb +++ b/app/models/qbo_base_model.rb @@ -70,6 +70,7 @@ class QboBaseModel < ActiveRecord::Base # Sync all entities, typically triggered by a scheduled task or manual sync request def self.sync + QboSyncStatus.queued(model_name.name) QboSyncJob.perform_later(entity: model_name.name, full_sync: true) end diff --git a/app/models/qbo_sync_status.rb b/app/models/qbo_sync_status.rb new file mode 100644 index 0000000..1658fdf --- /dev/null +++ b/app/models/qbo_sync_status.rb @@ -0,0 +1,70 @@ +#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 sync that's queued or running, one row per entity (Invoice, Estimate...), with how many records +# it has processed so far. The row is removed when the sync finishes. Rows not touched for STALE are ignored, +# so a sync that died (e.g. Redmine restarted mid-sync) doesn't leave a notice up forever. +class QboSyncStatus < ActiveRecord::Base + STALE = 6.hours + + scope :active, -> { where('updated_at > ?', STALE.ago) } + + # A sync was asked for and is waiting for the job to start + def self.queued(entity) + record(entity) { |s| s.state = 'queued' unless s.state == 'running' } + end + + # The sync started + def self.running(entity) + record(entity) do |s| + s.state = 'running' + s.processed = 0 + s.started_at = Time.now + end + end + + # count more records were processed + def self.progress(entity, count) + where(entity: entity.to_s).update_all(['processed = processed + ?, updated_at = ?', count.to_i, Time.now]) + rescue StandardError => e + Rails.logger.warn "[QboSyncStatus] #{e.message}" + end + + def self.finished(entity) + where(entity: entity.to_s).delete_all + rescue StandardError => e + Rails.logger.warn "[QboSyncStatus] #{e.message}" + end + + # Active syncs by entity name: { "Invoice" => QboSyncStatus, ... }, optionally only for some entities + def self.by_entity(entities = nil) + scope = active + scope = scope.where(entity: Array(entities).map(&:to_s)) if entities + scope.index_by(&:entity) + rescue StandardError + {} + end + + def self.record(entity) + status = find_or_initialize_by(entity: entity.to_s) + status.state ||= 'queued' + yield status + status.updated_at = Time.now + status.save! + rescue StandardError => e + Rails.logger.warn "[QboSyncStatus] #{e.message}" + end + private_class_method :record + + def running? + state == 'running' + end +end diff --git a/app/services/sync_service_base.rb b/app/services/sync_service_base.rb index a8c698e..2aad2bb 100644 --- a/app/services/sync_service_base.rb +++ b/app/services/sync_service_base.rb @@ -38,6 +38,7 @@ class SyncServiceBase entries.each do |remote| persist(remote) end + QboSyncStatus.progress(@entity.name, entries.size) end end diff --git a/app/views/qbo/_settings.html.erb b/app/views/qbo/_settings.html.erb index c68a4d2..4b9d7f2 100644 --- a/app/views/qbo/_settings.html.erb +++ b/app/views/qbo/_settings.html.erb @@ -45,6 +45,7 @@ [l(:label_items), Item, sync_items_path, items_path], [l(:label_accounts), Account, sync_accounts_path, accounts_path] ] %> +<% sync_statuses = QboSyncStatus.by_entity %>

<%= l(:label_management_and_sync) %>

@@ -61,7 +62,16 @@ - + <% end %> @@ -88,6 +98,7 @@ table.qbo-sync-table td.updated { width: 14em; } table.qbo-sync-table td.buttons { width: 6em; } .qbo-sync-all .button { margin-left: 8px; } + .qbo-sync-busy { font-style: italic; color: #b06d00; } <%# Billing: estimates, issues from estimates and invoices %> diff --git a/app/views/qbo_reports/index.html.erb b/app/views/qbo_reports/index.html.erb index 2f2dcda..6a7b7b4 100644 --- a/app/views/qbo_reports/index.html.erb +++ b/app/views/qbo_reports/index.html.erb @@ -27,6 +27,30 @@

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

+<% if Array(@syncing).any? %> + <% sync_labels = { 'Estimate' => l(:label_estimates), 'Invoice' => l(:label_invoices), 'Payment' => l(:label_payments) } %> +
+ <%= l(:text_report_syncing) %> + +
+ <%= javascript_tag do %> + // Refresh while QuickBooks is syncing, so the figures fill in + setTimeout(function () { window.location.reload(); }, 30000); + <% end %> +<% end %> + <% if @report.unsynced_count > 0 %>

<%= l(:text_report_needs_sync, count: @report.unsynced_count) %>

<% end %> @@ -131,6 +155,7 @@ .qbo-bar-issue_hours { background: #9b7fc0; } .qbo-report-ratios { margin: 8px 4px; line-height: 1.7; } .qbo-report-range { color: #666; margin: 4px 0 10px; } + .qbo-report-syncing ul { display: inline-block; text-align: left; margin: 6px 0 0; } table.qbo-report-months td, table.qbo-report-months th { text-align: right; white-space: nowrap; } table.qbo-report-months .month { text-align: left; } table.qbo-report-months .hours-start { border-left: 2px solid #ddd; } diff --git a/config/locales/en.yml b/config/locales/en.yml index 7fe397e..0da0baa 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -328,6 +328,10 @@ en: label_report_open_balance: "Still owed on these invoices" label_report_invoiced_of_spent: "Invoiced vs. spent hours" label_report_spent_of_estimated: "Spent vs. issues' estimated hours" + text_report_syncing: "QuickBooks is still syncing, so these figures are incomplete until it finishes. This page refreshes every 30 seconds." + text_qbo_sync_running: "syncing, %{count} read so far (started %{time})" + text_qbo_sync_running_short: "Syncing… %{count} read" + 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." notice_payment_not_found: "Payment not found in QuickBooks" diff --git a/db/migrate/049_create_qbo_sync_statuses.rb b/db/migrate/049_create_qbo_sync_statuses.rb new file mode 100644 index 0000000..f3f87c9 --- /dev/null +++ b/db/migrate/049_create_qbo_sync_statuses.rb @@ -0,0 +1,31 @@ +#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. + + +# Which QuickBooks syncs are queued or running, and how far along they are (shown on the business report and +# the settings page while a long sync runs) +class CreateQboSyncStatuses < ActiveRecord::Migration[7.0] + def up + return if table_exists?(:qbo_sync_statuses) + + create_table :qbo_sync_statuses do |t| + t.string :entity, null: false + t.string :state, null: false + t.integer :processed, null: false, default: 0 + t.datetime :started_at + t.timestamps + end + add_index :qbo_sync_statuses, :entity, unique: true + end + + def down + drop_table :qbo_sync_statuses if table_exists?(:qbo_sync_statuses) + end +end
<%= list_path ? link_to(label, list_path) : label %> <%= model.count %><%= model.last_sync %> + <% status = sync_statuses[model.name] %> + <% if status&.running? %> + <%= l(:text_qbo_sync_running_short, count: number_with_delimiter(status.processed)) %> + <% elsif status %> + <%= l(:text_qbo_sync_queued) %> + <% else %> + <%= model.last_sync %> + <% end %> + <%= link_to sprite_icon('reload', l(:label_sync)), sync_path, class: 'icon icon-reload' %>