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(:label_matching)%> <%= @customers.count %> <%=t(:field_customers)%>
- -<%=t(:label_no_customers)%> <%= params[:search] %>.
++ <%= text_field_tag :search, @search, class: 'customer-name', size: 40, placeholder: t(:label_search_customers), + autocomplete: 'off', data: { autocomplete_url: '/customers/autocomplete' } %> + <%= submit_tag l(:label_search), name: nil %> + <% if @search.present? %> + <%= link_to sprite_icon('reload', l(:button_clear)), customers_path, class: 'icon icon-reload' %> + <% end %> +
<% end %> -<%= @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 %> +| <%= l(:label_issue_plural) %> | + <%= sort_header_tag('updated', caption: l(:field_updated_on), default_order: 'desc') %> +||||
|---|---|---|---|---|
| <%= 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 %> | +
<%= 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,