diff --git a/README.md b/README.md index 22b084d..0dab9d0 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit * **QuickBooks VIN and Mileage fields (redmine_qbo 2026.11.2+):** redmine_qbo shows every QuickBooks text custom field on its estimate/invoice editor; this plugin fills the one named like **VIN** and, if there is one, the one named like **Mileage** or **Odometer**. VINs are shown in capitals. * **Invoices (redmine_qbo 2026.11.2+):** importing a work order's time or estimate fills the invoice's VIN box with its vehicle's VIN and the mileage box with the highest value of the work orders' **Mileage** issue custom field (Redmine's own custom field named "Mileage"), and an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice). * **Estimates (redmine_qbo 2026.11.2+):** the VIN box suggests the customer's vehicles, an invoice made from an estimate without a VIN gets its work orders' vehicle, and the Create issues from estimate page has a **Vehicle** for all the issues: preselected with the appointment's vehicle, or else the customer's vehicle whose VIN is on the estimate; when none matches it says so, so the team can pick one before creating the issues. +* **Vehicles page:** every vehicle in a Redmine list (vehicle, VIN, customer, last serviced date and mileage, last update), sortable by column and paged with Redmine's pagination and per-page choices. Search narrows it to the vehicles matching every word (e.g. "2018 jeep wrangler"; a single match opens it). A customer's vehicles (`/customers/:id/vehicles`) are the same list for that customer, with New Vehicle. * **Customer page:** ticking a vehicle adds it to the New issue link. * **Appointments (redmine_qbo_calendar):** an appointment added from an estimate gets the customer's vehicle with the estimate's VIN. On an appointment without a vehicle, **Add Vehicle** creates one for the customer, sets it on the appointment and returns to it (for users who may edit appointments). * **For other plugins:** creating a vehicle calls the `:controller_vehicles_new_after_save` hook (context: `params`, `vehicle`), and the new vehicle form returns to `back_url` when given, so a plugin can link to `new_vehicle_path(customer_id:, back_url:, ...)` and act on the vehicle it gets back. diff --git a/app/controllers/vehicles_controller.rb b/app/controllers/vehicles_controller.rb index 7de3cc7..de3f7ed 100644 --- a/app/controllers/vehicles_controller.rb +++ b/app/controllers/vehicles_controller.rb @@ -10,6 +10,9 @@ class VehiclesController < ApplicationController + helper :sort + include SortHelper + # Checks the vehicle permissions declared in init.rb (also requires login) before_action :authorize_global before_action :find_vehicle, only: [:show, :status, :edit, :update, :destroy] @@ -21,20 +24,28 @@ class VehiclesController < ApplicationController params.require(:vehicle).permit(:year, :make, :model, :customer_id, :notes, :vin) end - # Vehicles for one customer, or search results + # Every vehicle (or one customer's), sortable and paged like Redmine's lists; a search narrows it to the + # vehicles matching every word (a single match opens that vehicle) def index if params[:customer_id].present? @customer = Customer.find_by(id: params[:customer_id]) return render_404 unless @customer - - paginate_vehicles(@customer.vehicles.order(:year, :make, :model, :id)) - return end - if params[:search].present? - paginate_vehicles(Vehicle.search_all(params[:search]).order(:id)) - redirect_to @vehicles.first if @vehicle_count == 1 - end + sort_init 'vehicle', 'asc' + sort_update 'vehicle' => %w[year make model].map { |c| "#{Vehicle.table_name}.#{c}" }, + 'vin' => "#{Vehicle.table_name}.vin", + 'customer' => "#{Customer.table_name}.name", + 'updated' => "#{Vehicle.table_name}.updated_at" + + @search = params[:search].to_s.strip + scope = @customer ? @customer.vehicles : Vehicle.all + # Searched before joining customers, whose name column would make Vehicle.search's ambiguous + scope = scope.search_all(@search) if @search.present? + paginate_vehicles(scope.left_joins(:customer).preload(:customer).reorder(sort_clause).order(:id)) + return redirect_to(@vehicles.first) if @search.present? && @vehicle_count == 1 + + @last_services = Vehicle.last_service_infos(@vehicles.map(&:id)) end # Form for a new vehicle, pre-filled with the customer when coming from a customer or appointment diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index b5f1f1e..96e2ff9 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -51,7 +51,7 @@ class Vehicle < ApplicationRecord .joins(:custom_values) .where(custom_values: { custom_field_id: field.id }) .where.not(custom_values: { value: ['', nil] }) - .order(created_on: :desc) + .order(created_on: :desc, id: :desc) .first end @@ -65,13 +65,27 @@ class Vehicle < ApplicationRecord .joins(:custom_values) .where(custom_values: { custom_field_id: field.id }) .where.not(custom_values: { value: ['', nil] }) - .order(created_on: :desc) + .order(created_on: :desc, id: :desc) .pick(:created_on, 'custom_values.value') end @last_service_info = row && { date: row[0]&.to_date, mileage: row[1].presence } end + # last_service_info for many vehicles in one query (a list's Last serviced column): + # { vehicle id => { date:, mileage: } }, leaving out vehicles without a mileage recorded + def self.last_service_infos(ids) + field = mileage_custom_field + return {} unless field && ids.any? + + Issue.joins(:custom_values) + .where(vehicle_id: ids, custom_values: { custom_field_id: field.id }) + .where.not(custom_values: { value: ['', nil] }) + .order(created_on: :desc, id: :desc) + .pluck(:vehicle_id, :created_on, 'custom_values.value') + .each_with_object({}) { |(id, on, value), infos| infos[id] ||= { date: on&.to_date, mileage: value.presence } } + end + def last_service_date last_service_info&.dig(:date) end diff --git a/app/views/vehicles/_list.html.erb b/app/views/vehicles/_list.html.erb index 8a93f9d..a5668ab 100644 --- a/app/views/vehicles/_list.html.erb +++ b/app/views/vehicles/_list.html.erb @@ -48,5 +48,5 @@ <%=t(:label_matching)%> <%= local_assigns[:count] || vehicles.size %> <%=t(:field_vehicles) %> <% else %> -

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

+

<%= l(:label_no_data) %>

<% end %> diff --git a/app/views/vehicles/_search.html.erb b/app/views/vehicles/_search.html.erb deleted file mode 100644 index 1688c0b..0000000 --- a/app/views/vehicles/_search.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -<%= form_tag(vehicles_path, method: "get", id: "vehicles-search-form") do %> - <%= text_field_tag :search, params[:search], placeholder: t(:label_search_vehicles), autocomplete: "off" %> - <%= submit_tag t(:label_search) %> -<% end %> diff --git a/app/views/vehicles/index.html.erb b/app/views/vehicles/index.html.erb index b94eaad..4020baa 100644 --- a/app/views/vehicles/index.html.erb +++ b/app/views/vehicles/index.html.erb @@ -1,4 +1,72 @@ -

<%=t(:label_customer_vehicles)%> <%= render partial: 'vehicles/search' %>

-
+<% html_title(*[@customer&.to_s, l(:field_vehicles)].compact) %> -<%= render partial: 'vehicles/list', locals: {vehicles: @vehicles, pages: @vehicle_pages, count: @vehicle_count, show_customer: true, show_checkbox: false} %> +
+ <% if @customer && User.current.allowed_to?(:add_vehicles, nil, global: true) %> + <%= link_to sprite_icon('add', l(:button_new_vehicle)), new_customer_vehicle_path(@customer), class: 'icon icon-add' %> + <% end %> +
+ +

+ <% if @customer %> + <%= link_to l(:label_customers), customers_path %> » <%= link_to @customer.to_s, customer_path(@customer) %> » + <% end %> + <%= l(:field_vehicles) %> +

+ +<%= form_tag(@customer ? customer_vehicles_path(@customer) : vehicles_path, method: :get, id: 'vehicles-search-form') do %> + +<% end %> + +<% if @vehicles.empty? %> +

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

+<% else %> +
+ + + + <%= sort_header_tag('vehicle', caption: l(:field_vehicle)) %> + <%= sort_header_tag('vin', caption: l(:field_vin)) %> + <% unless @customer %> + <%= sort_header_tag('customer', caption: l(:field_customer)) %> + <% end %> + + <%= sort_header_tag('updated', caption: l(:field_updated_on), default_order: 'desc') %> + + + + <% @vehicles.each do |vehicle| %> + <% service = @last_services[vehicle.id] %> + + + + <% unless @customer %> + + <% end %> + + + + <% end %> + +
<%= l(:label_last_serviced) %>
<%= link_to vehicle.to_s, vehicle_path(vehicle) %><%= render partial: 'vehicles/vin', locals: { vin: vehicle.vin } %><%= link_to vehicle.customer.to_s, customer_path(vehicle.customer) if vehicle.customer %> + <% if service %> + <%= format_date(service[:date]) %> + <% if service[:mileage] %>· <%= number_with_delimiter(service[:mileage]) %> <%= l(:label_miles) %><% end %> + <% end %> + <%= format_time(vehicle.updated_at) if vehicle.updated_at %>
+
+ <%= pagination_links_full @vehicle_pages, @vehicle_count %> +<% end %> + + diff --git a/config/locales/en.yml b/config/locales/en.yml index f527d01..90451c9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -30,7 +30,8 @@ en: label_miles: "miles" label_model: "Model" label_new_vehicle: "New Customer Vehicle" - label_no_vehicles: "There are no vehicles containing the term(s)" + label_no_vehicles: "No vehicles match \"%{search}\"." + label_last_serviced: "Last serviced" label_search_vehicles: "Search Vehicles" label_unknown: "Unknown" label_vehicle: "Vehicle"