From 14f04093953229bab1c1239e40aa6cf1dfddece7 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 00:00:36 -0400 Subject: [PATCH] Add a customer payments page Payments on the customer page lists the customer's QuickBooks payments, newest first, with the invoices each was applied to, memo, amount, unapplied credit and totals; each links to the payment page and its PDF. A new View payments permission allows the list, payment page and PDF; Receive payments includes it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- README.md | 4 +- app/controllers/qbo_payments_controller.rb | 16 ++++- app/views/customers/show.html.erb | 3 + app/views/qbo_payments/index.html.erb | 82 ++++++++++++++++++++++ app/views/qbo_payments/show.html.erb | 5 +- config/locales/en.yml | 4 ++ config/routes.rb | 4 +- init.rb | 3 +- lib/redmine_qbo_billing.rb | 5 ++ lib/redmine_qbo_billing/payment_writer.rb | 14 ++++ 10 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 app/views/qbo_payments/index.html.erb diff --git a/README.md b/README.md index c9b6cf4..30831df 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ Customer records are automatically updated in the local database when changes oc * With none ticked, the payment is applied to the customer's open invoices **oldest first**, like QuickBooks does; the page shows how it will be split as the amount is typed, and it's worked out again from QuickBooks when saving. Anything more than the open balances stays as a credit. * Tick **Deposit** to keep the whole payment as a credit on the customer's account without paying any invoice (also what happens when there are no open invoices). * The saved payment's page shows where it was applied and has **Print**, QuickBooks' payment PDF. +* **Payments** on the customer page lists all of the customer's payments from QuickBooks, newest first: date, method, reference no., the invoices each was applied to (or Deposit), memo, amount and what's still unapplied, with totals. Each date opens the payment's page and **Print** opens its PDF. ### QuickBooks custom fields @@ -165,7 +166,8 @@ Administration → Items lists the QuickBooks product/service list (synced from * **Write estimates**: create and edit estimates * **Email estimates**: email estimates to customers (Save and send, and the Email link) * **Create issues from estimates**: the Create issues page (was redmine_qbo_calendar's "Create issues from appointment estimates") - * **Receive payments**: the Receive payment page, the payment page and its PDF + * **View payments**: a customer's Payments list, the payment page and its PDF + * **Receive payments**: the Receive payment page, plus everything View payments allows * **Write invoices**: create and edit invoices, and import time and estimates into them * **Email invoices**: email invoices to customers diff --git a/app/controllers/qbo_payments_controller.rb b/app/controllers/qbo_payments_controller.rb index 93dc1d4..c43f760 100644 --- a/app/controllers/qbo_payments_controller.rb +++ b/app/controllers/qbo_payments_controller.rb @@ -9,13 +9,27 @@ #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. # Receives customer payments in QuickBooks: payment type and amount, applied to open invoices oldest first, or -# saved as a deposit. A saved payment can be printed (QuickBooks' payment PDF). +# saved as a deposit. A saved payment can be printed (QuickBooks' payment PDF). A customer's payments are +# listed on their payments page. class QboPaymentsController < ApplicationController before_action :authorize_global QBO_ERRORS = QboEstimatesController::QBO_ERRORS + # The customer's payments, newest first, read from QuickBooks + def index + @customer = Customer.find_by(id: params[:customer_id]) + return render_404 unless @customer + + @payments = RedmineQboBilling::PaymentWriter.customer_payments(@customer) + @totals = RedmineQboBilling::PaymentWriter.payment_totals(@payments) + rescue *QBO_ERRORS => e + @payments = [] + @totals = RedmineQboBilling::PaymentWriter.payment_totals([]) + flash.now[:error] = qbo_message(e) + end + def new customer = Customer.find_by(id: params[:customer_id]) if params[:customer_id].present? @form = RedmineQboBilling::PaymentWriter.new_form(customer: customer, user: User.current) diff --git a/app/views/customers/show.html.erb b/app/views/customers/show.html.erb index 6d44275..3ac91f6 100644 --- a/app/views/customers/show.html.erb +++ b/app/views/customers/show.html.erb @@ -14,6 +14,9 @@ <% if RedmineQboBilling.invoice_writer? %> <%= render partial: 'qbo_invoices/customer_link', locals: { customer: @customer } %> <% end %> + <% if RedmineQboBilling.payment_viewer? %> + <%= link_to sprite_icon('list', l(:label_payments)), qbo_payments_path(customer_id: @customer.id), class: 'icon icon-list' %> + <% end %> <% if RedmineQboBilling.payment_receiver? %> <%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %> <% else %> diff --git a/app/views/qbo_payments/index.html.erb b/app/views/qbo_payments/index.html.erb new file mode 100644 index 0000000..0759fdc --- /dev/null +++ b/app/views/qbo_payments/index.html.erb @@ -0,0 +1,82 @@ +<% html_title @customer.name, l(:label_payments) %> + +
+ <% if RedmineQboBilling.payment_receiver? %> + <%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %> + <% end %> + <%= link_to sprite_icon('link', l(:button_open_in_quickbooks)), + "https://#{RedmineQbo.sandbox_mode? ? 'sandbox' : 'app'}.qbo.intuit.com/app/customerdetail?nameId=#{@customer.id}", + class: 'icon icon-link', target: :_blank, rel: 'noopener' %> +
+ +

+ <%= link_to l(:label_customers), customers_path %> » + <%= link_to @customer.name, customer_path(@customer) %> » + <%= l(:label_payments) %> +

+ +<% if @payments.empty? %> +

<%= l(:label_no_payments) %>

+<% else %> +
+ + + + + + + + + + + + + + + <% @payments.each do |payment| %> + <% applied = RedmineQboBilling::PaymentWriter.applied_invoices(payment) %> + <% unapplied = BigDecimal(payment.unapplied_amount.to_s.presence || '0') %> + + + + + + + + + + + <% end %> + + + + + + + + + +
<%= l(:label_payment_date) %><%= l(:label_payment_method) %><%= l(:label_payment_reference) %><%= l(:label_payment_applies_to) %><%= l(:label_memo) %><%= l(:label_amount) %><%= l(:label_payment_unapplied) %>
<%= link_to format_date(payment.txn_date), qbo_payment_path(payment.id) %><%= RedmineQboBilling::PaymentWriter.payment_method_name(payment) %><%= payment.payment_ref_number %> + <% if applied.empty? %> + <%= l(:label_payment_deposit) %> + <% else %> + <% applied.each do |invoice_id, number, amount| %> +
+ <%= link_to "##{number}", RedmineQboBilling.invoice_writer? ? edit_qbo_invoice_path(invoice_id) : invoice_path(invoice_id) %> + <%= number_to_currency(amount) %> +
+ <% end %> + <% end %> +
<%= truncate(payment.private_note.to_s, length: 60) %><%= number_to_currency(payment.total) %><%= unapplied > 0 ? number_to_currency(unapplied) : '' %> + <%= link_to sprite_icon('application-pdf', l(:button_print_payment)), pdf_qbo_payment_path(payment.id), + class: 'icon icon-pdf', target: :_blank %> +
<%= l(:label_payment_totals) %> (<%= @payments.size %>)<%= number_to_currency(@totals[:received]) %><%= @totals[:unapplied] > 0 ? number_to_currency(@totals[:unapplied]) : '' %>
+
+<% end %> + + diff --git a/app/views/qbo_payments/show.html.erb b/app/views/qbo_payments/show.html.erb index fabab24..5aed2ef 100644 --- a/app/views/qbo_payments/show.html.erb +++ b/app/views/qbo_payments/show.html.erb @@ -3,7 +3,7 @@
<%= link_to sprite_icon('application-pdf', l(:button_print_payment)), pdf_qbo_payment_path(@payment.id), class: 'icon icon-pdf', target: :_blank %> - <% if @customer %> + <% if @customer && RedmineQboBilling.payment_receiver? %> <%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %> <% end %> <%= link_to sprite_icon('link', l(:button_open_in_quickbooks)), @@ -13,7 +13,8 @@

<%= link_to l(:label_customers), customers_path %> » - <% if @customer %><%= link_to @customer.name, customer_path(@customer) %> »<% end %> + <% if @customer %><%= link_to @customer.name, customer_path(@customer) %> » + <%= link_to l(:label_payments), qbo_payments_path(customer_id: @customer.id) %> »<% end %> <%= l(:label_payment) %> <%= number_to_currency(@payment.total) %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index 46f5c1a..9fa101e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -308,6 +308,10 @@ en: label_payment_method: "Payment method" label_payment_reference: "Reference no." label_payment_unapplied: "Unapplied (credit)" + label_payments: "Payments" + label_no_payments: "No payments from this customer in QuickBooks" + label_payment_totals: "Total" + permission_view_payments: "View payments" 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/config/routes.rb b/config/routes.rb index 6e190cd..0ba7fbd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -68,8 +68,8 @@ resources :qbo_invoices, only: [:new, :create, :edit, :update] do end end -# Receive payment pages -resources :qbo_payments, only: [:new, :create, :show] do +# Receive payment pages, and a customer's payments (/qbo_payments?customer_id=) +resources :qbo_payments, only: [:index, :new, :create, :show] do member do get :pdf end diff --git a/init.rb b/init.rb index 722e363..3b4232b 100644 --- a/init.rb +++ b/init.rb @@ -49,7 +49,8 @@ Redmine::Plugin.register :redmine_qbo do permission :write_invoices, { qbo_invoices: [:new, :create, :edit, :update, :import_options], qbo_estimates: [:customer_info, :items] }, global: true permission :send_invoices, { qbo_invoices: [:send_email] }, global: true - permission :receive_payments, { qbo_payments: [:new, :create, :show, :pdf, :open_invoices], + permission :view_payments, { qbo_payments: [:index, :show, :pdf] }, global: true + permission :receive_payments, { qbo_payments: [:index, :new, :create, :show, :pdf, :open_invoices], qbo_estimates: [:customer_info] }, global: true # The product/service list, accounts and their syncs are admin-only (see ItemsController, AccountsController) diff --git a/lib/redmine_qbo_billing.rb b/lib/redmine_qbo_billing.rb index 6b2bba8..21e7e13 100644 --- a/lib/redmine_qbo_billing.rb +++ b/lib/redmine_qbo_billing.rb @@ -43,6 +43,11 @@ module RedmineQboBilling user.allowed_to?(:receive_payments, nil, global: true) end + # True when the user may see customer payments (receiving them includes seeing them) + def self.payment_viewer?(user = User.current) + user.allowed_to?(:view_payments, nil, global: true) || payment_receiver?(user) + end + # True when the user may write invoices def self.invoice_writer?(user = User.current) user.allowed_to?(:write_invoices, nil, global: true) diff --git a/lib/redmine_qbo_billing/payment_writer.rb b/lib/redmine_qbo_billing/payment_writer.rb index eb0c8f7..0367613 100644 --- a/lib/redmine_qbo_billing/payment_writer.rb +++ b/lib/redmine_qbo_billing/payment_writer.rb @@ -105,6 +105,20 @@ module RedmineQboBilling saved end + # Every payment the customer has made, newest first (by date, then QuickBooks id), read from QuickBooks + def customer_payments(customer) + payments = with_service('Payment') do |service| + service.all("SELECT * FROM Payment WHERE CustomerRef = '#{customer.id.to_i}' ORDERBY TxnDate DESC") + end + Array(payments).sort_by { |p| [p.txn_date || Date.new(1900), p.id.to_i] }.reverse + end + + # Totals for the customer's payment list: amount received and what's still unapplied (credit) + def payment_totals(payments) + sum = ->(values) { values.sum(BigDecimal('0')) { |v| BigDecimal(v.to_s.presence || '0') } } + { received: sum.call(payments.map(&:total)), unapplied: sum.call(payments.map(&:unapplied_amount)) } + end + def load(id) payment = with_service('Payment') { |service| service.fetch_by_id(id) } raise Error, I18n.t(:notice_payment_not_found) unless payment