From 489edd97faf8b5d1e38af61a8aa4fff6cc36c5b9 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sun, 1 Nov 2026 17:47:19 -0500 Subject: [PATCH] Customers page lists every customer, paged by Redmine instead of will_paginate - The Customers page shows all customers without a search: a Redmine list with name, phone, mobile, issue count and last update, sortable by column, with Redmine's pagination and per-page choices - Search works as before (best matches first, a single match opens the customer); clicking a column sorts the matches by it instead, and Clear goes back to everyone - New Customer and Sync move to the page's action links - will_paginate removed from the Gemfile and init.rb; redmine_qbo_vehicles 2026.11.3 no longer needs it Co-Authored-By: Claude Opus 5.5 --- Gemfile | 1 - README.md | 4 +- app/controllers/customers_controller.rb | 41 ++++++++----- app/views/customers/index.html.erb | 80 +++++++++++++++++++------ config/locales/en.yml | 3 +- init.rb | 3 - 6 files changed, 91 insertions(+), 41 deletions(-) diff --git a/Gemfile b/Gemfile index 4663123..776e4f8 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,6 @@ source 'https://rubygems.org' gem 'quickbooks-ruby' gem 'oauth2' gem 'roxml' -gem 'will_paginate' gem 'rexml' gem 'combine_pdf' diff --git a/README.md b/README.md index fb11ecc..347cf96 100644 --- a/README.md +++ b/README.md @@ -69,8 +69,10 @@ This ensures Time Activities are recorded under the correct employee in QuickBoo The plugin provides basic Customer management: * Create Customers directly from Redmine + +* Browse every Customer on the **Customers** page: a Redmine list with phone numbers, issue counts and last update, sortable by column and paged with Redmine's own pagination (and its per-page choices) -* Search Customers by **name or phone number** +* Search Customers by **name or phone number**: the best matches come first (click a column to sort them instead), and a single match opens that Customer * View and edit Customer information diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 9f85932..1393ccf 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -117,13 +117,33 @@ class CustomersController < ApplicationController @filtered_invoices = Invoice.all.where(customer_id: params[:selected_customer]) end + # Most matches a search lists (best first) + SEARCH_LIMIT = 200 + + # Every customer, sortable and paged like Redmine's lists; a search narrows it to the best matches (a single + # match opens that customer). Sorting a search's results by a column replaces the best-first order. def index - if params[:search] - @customers = CustomerSearchService.call(name: params[:search], limit: 200).paginate(page: params[:page]) - if only_one_non_zero?(@customers) - redirect_to @customers.first - end + sort_init 'name', 'asc' + sort_update 'name' => "#{Customer.table_name}.name", + 'phone' => "#{Customer.table_name}.phone_number", + 'mobile' => "#{Customer.table_name}.mobile_phone_number", + 'updated' => "#{Customer.table_name}.updated_at" + + @search = params[:search].to_s.strip + if @search.present? + scope = CustomerSearchService.call(name: @search, limit: SEARCH_LIMIT) + @best_first = params[:sort].blank? + else + scope = Customer.all end + scope = scope.reorder(sort_clause).order(:id) unless @best_first + + @customer_count = scope.count + return redirect_to(scope.first) if @search.present? && @customer_count == 1 + + @customer_pages = Paginator.new @customer_count, per_page_option, params['page'] + @customers = scope.limit(@customer_pages.per_page).offset(@customer_pages.offset).to_a + @issue_counts = Issue.where(customer_id: @customers.map(&:id)).group(:customer_id).count end def load_issue_data @@ -157,17 +177,6 @@ class CustomersController < ApplicationController end end - def only_one_non_zero?(array) - found_non_zero = false - array.each do |val| - if val != 0 - return false if found_non_zero - found_non_zero = true - end - end - found_non_zero - end - def share issue = Issue.visible.find(params[:id]) token = issue.share_token diff --git a/app/views/customers/index.html.erb b/app/views/customers/index.html.erb index 651756c..1f5054e 100644 --- a/app/views/customers/index.html.erb +++ b/app/views/customers/index.html.erb @@ -1,24 +1,66 @@ -

<%=t(:field_customers)%> <%= render partial: 'customers/search' %>

-<% if @customers.present? %> -
- <% @customers.each do |c| %> -
-
- <%= link_to c, customer_path(c.id) %> -
-
+<% html_title l(:label_customers) %> +<% phone = ->(digits) { digits.present? ? number_to_phone(digits.gsub(/\D/, '').to_i, area_code: true) : '' } %> + +
+ <% if User.current.allowed_to?(:add_customers, nil, global: true) %> + <%= link_to sprite_icon('add', l(:label_new_customer)), new_customer_path, class: 'icon icon-add' %> + <% end %> + <% if User.current.admin? %> + <%= link_to sprite_icon('reload', l(:label_sync)), qbo_sync_path, class: 'icon icon-reload' %> <% end %> - -

<%=t(:label_matching)%> <%= @customers.count %> <%=t(:field_customers)%>

- -
- <%= will_paginate @customers %>
-<% else %> -

<%=t(:label_no_customers)%> <%= params[:search] %>.

+

<%= l(:label_customers) %>

+ +<%= form_tag(customers_path, method: :get, id: 'customer-search-form') do %> + <% end %> -
- <%= render partial: 'qbo/stats' %> -
+<% if @customers.empty? %> +

<%= @search.present? ? l(:label_no_customers, search: @search) : l(:label_no_data) %>

+<% else %> + <% if @best_first %> +

<%= l(:text_customers_best_match, count: @customer_count) %>

+ <% end %> +
+ + + + <%= sort_header_tag('name', caption: l(:field_name)) %> + <%= sort_header_tag('phone', caption: l(:label_primary_phone)) %> + <%= sort_header_tag('mobile', caption: l(:label_mobile_phone)) %> + + <%= sort_header_tag('updated', caption: l(:field_updated_on), default_order: 'desc') %> + + + + <% @customers.each do |customer| %> + + + + + + + + <% end %> + +
<%= l(:label_issue_plural) %>
<%= link_to customer.name, customer_path(customer.id) %><%= phone.call(customer.phone_number) %><%= phone.call(customer.mobile_phone_number) %><%= @issue_counts[customer.id] || 0 %><%= format_time(customer.updated_at) if customer.updated_at %>
+
+ <%= pagination_links_full @customer_pages, @customer_count %> +<% end %> + +

<%= render partial: 'qbo/stats' %>

+ + diff --git a/config/locales/en.yml b/config/locales/en.yml index 87f95a6..a67692f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -65,7 +65,8 @@ en: label_new_customer: "New Customer" label_new_issue: "New Issue" label_qbo_never_synced: "Never Synced" - label_no_customers: "There are no customers matching the search term(s)." + label_no_customers: "No customers match \"%{search}\"." + text_customers_best_match: "%{count} best matches, closest first. Click a column to sort them by it instead." label_no_estimates: "No Estimates" label_no_invoices: "No Invoices" label_oauth2_refresh_token_expires_at: "Refresh Token Expires At" diff --git a/init.rb b/init.rb index 55425b5..7bdbfad 100644 --- a/init.rb +++ b/init.rb @@ -27,9 +27,6 @@ Redmine::Plugin.register :redmine_qbo do User.safe_attributes :employee_id TimeEntry.safe_attributes :billed - # set per_page globally - WillPaginate.per_page = 20 - # Global Permissions # autocomplete and the filter_* actions feed the customer, estimate and invoice pickers on issue forms permission :view_customers, { customers: [:index, :show, :autocomplete,