From 159420fac145d285faa34c855eb09500e2bcaab4 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Tue, 3 Nov 2026 21:53:13 -0500 Subject: [PATCH] Show estimate, invoice and payment activity on the activity page Each type appears only on the overall activity page, and only for users who may see it: estimates need View customers or Write estimates, invoices View customers or Write invoices, and payments View or Receive payments. Co-Authored-By: Claude Opus 5.5 --- app/models/concerns/qbo_activity.rb | 71 +++++++++++++++++++ app/models/estimate.rb | 6 ++ app/models/invoice.rb | 15 ++++ app/models/payment.rb | 13 ++++ assets/images/icons.svg | 26 +++++++ assets/stylesheets/autocomplete.css | 5 ++ config/locales/en.yml | 10 +++ init.rb | 7 ++ lib/redmine_qbo.rb | 3 + .../patches/activity_fetcher_patch.rb | 29 ++++++++ 10 files changed, 185 insertions(+) create mode 100644 app/models/concerns/qbo_activity.rb create mode 100644 assets/images/icons.svg create mode 100644 lib/redmine_qbo/patches/activity_fetcher_patch.rb diff --git a/app/models/concerns/qbo_activity.rb b/app/models/concerns/qbo_activity.rb new file mode 100644 index 0000000..f404287 --- /dev/null +++ b/app/models/concerns/qbo_activity.rb @@ -0,0 +1,71 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 - 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. + +# Estimates, invoices and payments on Redmine's activity page. They come from QuickBooks and belong to no +# project, so they show on the overall activity page only (not a project's, nor a user's), and each kind only to +# users with one of its global permissions. +module QboActivity + extend ActiveSupport::Concern + + class_methods do + # event_type: the activity type registered in init.rb (e.g. 'qbo_estimates'); icon: the event's CSS class + # and icon in this plugin's sprite (assets/images/icons.svg); permissions: any one of these shows it + def qbo_activity(event_type, icon:, permissions:, title:, description:, url:) + # These models can load before Redmine adds acts_as_event to every model + include Redmine::Acts::Event unless respond_to?(:acts_as_event) + acts_as_event title: title, description: description, url: url, type: icon, + datetime: :activity_datetime, author: nil + class_attribute :qbo_activity_permissions, instance_writer: false, default: permissions + class_attribute :qbo_activity_event_type, instance_writer: false, default: event_type + end + + def qbo_activity_visible_to?(user) + qbo_activity_permissions.any? { |permission| user.allowed_to?(permission, nil, global: true) } + end + + # What Redmine::Activity::Fetcher reads for a project's activity page; these never show there + def activity_provider_options + { qbo_activity_event_type => { permission: qbo_activity_permissions.first } } + end + + # Documents dated from..to (the page's days, to not included), newest first. From the QuickBooks date, so + # a sync bringing in old documents doesn't pile them onto the day it ran. + def find_events(event_type, user, from, to, options) + return [] if options[:project] || options[:author] || !qbo_activity_visible_to?(user) + + scope = includes(:customer).where.not(txn_date: nil) + scope = scope.where('txn_date >= ?', from.to_date) if from + scope = scope.where('txn_date < ?', to.to_date) if to + scope = scope.order(txn_date: :desc, id: :desc).limit(options[:limit]) if options[:limit] + scope.to_a + end + end + + # When it happened: the time it reached Redmine when that was its QuickBooks date (created or changed in + # QuickBooks and sent by the webhook moments later), else midday on its QuickBooks date (e.g. a full sync) + def activity_datetime + return created_at if created_at && created_at.getlocal.to_date == txn_date + + Time.local(txn_date.year, txn_date.month, txn_date.day, 12) + end + + # Activity events have a project; these don't + def project + nil + end + + def activity_money(amount) + ActiveSupport::NumberHelper.number_to_currency(amount) if amount + end + + def activity_customer + customer&.name.presence || I18n.t(:label_none) + end +end diff --git a/app/models/estimate.rb b/app/models/estimate.rb index c9f7d6b..634329c 100644 --- a/app/models/estimate.rb +++ b/app/models/estimate.rb @@ -17,6 +17,12 @@ class Estimate < QboBaseModel self.primary_key = :id qbo_sync push: false + include QboActivity + qbo_activity 'qbo_estimates', icon: 'qbo-estimate', permissions: %i[view_customers write_estimates], + title: ->(e) { I18n.t(:label_activity_estimate, number: e.doc_number, customer: e.activity_customer) }, + description: ->(e) { [e.activity_money(e[:total_amount]), e[:txn_status]].compact.join(' · ') }, + url: ->(e) { { controller: 'estimates', action: 'show', id: e.id } } + def name to_s end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 35e2d34..0ff3ea6 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -17,11 +17,26 @@ class Invoice < QboBaseModel self.primary_key = :id qbo_sync push: false + include QboActivity + qbo_activity 'qbo_invoices', icon: 'qbo-invoice', permissions: %i[view_customers write_invoices], + title: ->(i) { I18n.t(:label_activity_invoice, number: i.doc_number, customer: i.activity_customer) }, + description: ->(i) { i.activity_description }, + url: ->(i) { { controller: 'invoices', action: 'show', id: i.id } } + # Return the invoice's document number as its string representation def to_s doc_number end + # Amount and where it stands, e.g. "$1,250.00 · Partially paid ($400.00 owed)" + def activity_description + state = status + owed = activity_money(self[:balance]) if state && self[:balance].to_d.positive? + [activity_money(self[:total_amount]), + (I18n.t(:"label_invoice_status_#{state}", default: state.to_s.humanize) if state), + (I18n.t(:text_activity_invoice_owed, amount: owed) if owed && state != :open && state != :overdue)].compact.join(' · ') + end + # Paid, partially paid, overdue, open or voided, from the cached total, balance and due date # (RedmineQboBilling::InvoiceStatus); nil when the balance isn't cached def status(today: User.current.today) diff --git a/app/models/payment.rb b/app/models/payment.rb index bbee059..920bb2f 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -18,6 +18,19 @@ class Payment < QboBaseModel self.primary_key = :id qbo_sync push: false + include QboActivity + qbo_activity 'qbo_payments', icon: 'qbo-payment', permissions: %i[view_payments receive_payments], + title: ->(p) { I18n.t(:label_activity_payment, customer: p.activity_customer, amount: p.activity_money(p.total_amount)) }, + description: ->(p) { p.activity_description }, + url: ->(p) { { controller: 'qbo_payments', action: 'show', id: p.id } } + + # Money not yet applied to an invoice, when there is some + def activity_description + return '' unless unapplied_amount.to_d.positive? + + I18n.t(:text_activity_payment_unapplied, amount: activity_money(unapplied_amount)) + end + def to_s "#{txn_date} #{total_amount}" end diff --git a/assets/images/icons.svg b/assets/images/icons.svg new file mode 100644 index 0000000..58651bb --- /dev/null +++ b/assets/images/icons.svg @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/assets/stylesheets/autocomplete.css b/assets/stylesheets/autocomplete.css index 2a7e50b..4f4fec1 100644 --- a/assets/stylesheets/autocomplete.css +++ b/assets/stylesheets/autocomplete.css @@ -12,3 +12,8 @@ margin: -4px 0 10px; padding-left: 0; } + +/* Estimates, invoices and payments on the activity page belong to no project: no " - " after an empty name */ +div#activity span.project:empty:after { + content: none; +} diff --git a/config/locales/en.yml b/config/locales/en.yml index 4af930c..dd1fabc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -358,3 +358,13 @@ en: text_payment_pick_invoices: "Tick invoices to fill in the amount and pay just those. With none ticked, the payment goes on the oldest invoices first." text_payment_reference: "e.g. the check number" text_payment_unapplied_deposit: "Not applied to any invoice: it's a credit on the customer's account." + + # Activity page + label_qbo_estimate_plural: "Estimates" + label_qbo_invoice_plural: "Invoices" + label_qbo_payment_plural: "Payments" + label_activity_estimate: "Estimate #%{number}: %{customer}" + label_activity_invoice: "Invoice #%{number}: %{customer}" + label_activity_payment: "Payment from %{customer}: %{amount}" + text_activity_invoice_owed: "%{amount} owed" + text_activity_payment_unapplied: "%{amount} not applied to an invoice" diff --git a/init.rb b/init.rb index 09094f3..c0d0446 100644 --- a/init.rb +++ b/init.rb @@ -52,6 +52,13 @@ Redmine::Plugin.register :redmine_qbo do permission :view_business_reports, { qbo_reports: [:index] }, global: true # The product/service list, accounts and their syncs are admin-only (see ItemsController, AccountsController) + # Estimates, invoices and payments on the activity page (QboActivity): the overall page only, for users with + # View customers or Write estimates / invoices, and View or Receive payments + # (plugin: picks their icons from this plugin's sprite, assets/images/icons.svg) + activity_provider :qbo_estimates, class_name: 'Estimate', plugin: :redmine_qbo + activity_provider :qbo_invoices, class_name: 'Invoice', plugin: :redmine_qbo + activity_provider :qbo_payments, class_name: 'Payment', plugin: :redmine_qbo + # Register top menu items menu :top_menu, :customers, { controller: :customers, action: :index }, caption: :label_customers, if: Proc.new {User.current.allowed_to?(:view_customers, nil, global: true)} menu :top_menu, :qbo_reports, { controller: :qbo_reports, action: :index }, caption: :label_business_report, diff --git a/lib/redmine_qbo.rb b/lib/redmine_qbo.rb index 0a50ce2..c42cd1c 100644 --- a/lib/redmine_qbo.rb +++ b/lib/redmine_qbo.rb @@ -18,6 +18,9 @@ module RedmineQbo IssueQuery.prepend RedmineQbo::Patches::QueryPatch TimeEntryQuery.prepend RedmineQbo::Patches::TimeEntryQueryPatch + # === Activity: estimates, invoices and payments, for users who may see them === + Redmine::Activity::Fetcher.prepend RedmineQbo::Patches::ActivityFetcherPatch + # === Controllers === RedmineQbo::Patches::IssuesControllerPatch.apply RedmineQbo::Patches::AttachmentsControllerPatch.apply diff --git a/lib/redmine_qbo/patches/activity_fetcher_patch.rb b/lib/redmine_qbo/patches/activity_fetcher_patch.rb new file mode 100644 index 0000000..d1dc18c --- /dev/null +++ b/lib/redmine_qbo/patches/activity_fetcher_patch.rb @@ -0,0 +1,29 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 - 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. + +module RedmineQbo + module Patches + # The activity page lists every activity type for the whole site, and a project's page those its members may + # see. QuickBooks estimates, invoices and payments (QboActivity) belong to no project: they're kept to the + # overall page, and to users with one of their permissions, so nobody gets a checkbox for activity they can't see. + module ActivityFetcherPatch + def event_types + return @event_types if @qbo_activity_checked + + @qbo_activity_checked = true + @event_types = super.select do |event_type| + providers = Redmine::Activity.providers[event_type].map(&:constantize) + qbo = providers.select { |provider| provider.respond_to?(:qbo_activity_visible_to?) } + qbo.empty? || (project.nil? && qbo.any? { |provider| provider.qbo_activity_visible_to?(user) }) + end + end + end + end +end