diff --git a/README.md b/README.md index 0c90cb4..4287d1e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,9 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit * **Asset Tracking:** Adds vehicles owned by customers to the system. * **Issue Association:** Allows these vehicles to be attached directly to Redmine issues for better service tracking. -* **Invoices (redmine_qbo 2026.11.0+):** importing a work order's time or estimate fills the invoice's VIN box with its vehicle's VIN, and an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice). +* **Invoices (redmine_qbo 2026.11.0+):** 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.0+):** the VIN box suggests the customer's vehicles (one vehicle fills it in), and issues created from an estimate get the appointment's vehicle, or else the customer's vehicle whose VIN is on the estimate. +* **Customer page:** ticking a vehicle adds it to the New issue link. ## Installation diff --git a/app/views/vehicles/_list.html.erb b/app/views/vehicles/_list.html.erb index 934c103..5b8b7fc 100644 --- a/app/views/vehicles/_list.html.erb +++ b/app/views/vehicles/_list.html.erb @@ -6,7 +6,7 @@
<% if show_checkbox %> - <%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s }, class: "vehicle-checkbox appointment checkbox" %> + <%= check_box_tag "vehicle_ids[]", vehicle.id, false, onchange: "updateLink()", data: { url: vehicle_path(vehicle).html_safe, text: vehicle.to_s, new_issue_param: 'issue[vehicle_id]' }, class: "vehicle-checkbox appointment checkbox" %> <% else %>
diff --git a/lib/vehicles/hooks/billing_hook_listener.rb b/lib/vehicles/hooks/billing_hook_listener.rb index 29820a0..466690e 100644 --- a/lib/vehicles/hooks/billing_hook_listener.rb +++ b/lib/vehicles/hooks/billing_hook_listener.rb @@ -14,13 +14,34 @@ module Vehicles # Hooks for redmine_qbo's estimates and invoices class BillingHookListener < Redmine::Hook::Listener - # Each work order's VIN, so the invoice's VIN box is filled when its time or estimates are imported + # Each work order's VIN and mileage, so the invoice's VIN and mileage boxes are filled when its time or + # estimates are imported (redmine_qbo uses the highest mileage). Mileage comes from Redmine's issue + # custom field named "Mileage" (any capitalisation). def qbo_billing_issue_values(context = {}) - vins = Array(context[:issues]).each_with_object({}) do |issue, by_issue| + issues = Array(context[:issues]) + vins = issues.each_with_object({}) do |issue, by_issue| vin = issue.respond_to?(:vehicle) ? issue.vehicle&.vin : nil by_issue[issue.id] = vin if vin.present? end - { vin: vins } + { vin: vins, mileage: mileage_by_issue(issues) } + end + + # The customer's vehicles as VIN suggestions for the estimate/invoice VIN box + def qbo_billing_vin_options(context = {}) + customer = context[:customer] + return [] unless customer.respond_to?(:vehicles) + + customer.vehicles.select { |v| v.vin.present? }.map { |v| { vin: v.vin, name: v.to_s } } + end + + # Issues created from an estimate get a vehicle: the one passed in (e.g. redmine_qbo_calendar's + # appointment vehicle), or the customer's vehicle whose VIN is on the estimate + def qbo_billing_build_issue(context = {}) + issue = context[:issue] + return unless issue.respond_to?(:vehicle_id=) + + vehicle_id = context.dig(:defaults, :vehicle_id).presence || vehicle_id_from_vin(context[:estimate]) + issue.vehicle_id = vehicle_id if vehicle_id end # One vehicle per invoice (OSOR SOP): an invoice whose work orders are for more than one vehicle @@ -38,6 +59,34 @@ module Vehicles I18n.t(:error_one_vehicle_per_invoice, vehicles: details.join('; ')) end + private + + # The customer's vehicle whose VIN is on the estimate's QuickBooks VIN field, or nil + def vehicle_id_from_vin(estimate) + customer = estimate&.customer + return nil unless customer.respond_to?(:vehicles) + + company = RedmineQboBilling::Company + vin = RedmineQboBilling::EstimateWriter.custom_value(estimate.details, company.vin_field, company::VIN_NAME).to_s.strip + return nil if vin.empty? + + customer.vehicles.detect { |v| v.vin.to_s.casecmp?(vin) }&.id + rescue StandardError + nil + end + + # { issue_id => mileage } from the issue custom field named "Mileage"; empty without that field + def mileage_by_issue(issues) + issue_ids = issues.map(&:id) + field = IssueCustomField.all.detect { |f| f.name.to_s.strip.casecmp?('mileage') } + return {} if field.nil? || issue_ids.empty? + + CustomValue.where(customized_type: 'Issue', customized_id: issue_ids, custom_field_id: field.id) + .where.not(value: [nil, '']) + .pluck(:customized_id, :value) + .to_h + end + end end end