From 1aa2fa9d5e92041a5fa74702d9bdcc2535297afe Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 10:52:40 -0400 Subject: [PATCH] Match the estimate's VIN with redmine_qbo's enhanced read; no VIN for new documents - Vehicles::EstimateVehicle.vin uses RedmineQboBilling::EstimateWriter.vin_for, which includes QuickBooks' newer custom fields; the local estimate's details don't, so the VIN never matched and issues created from an estimate got no vehicle. - qbo_billing_default_vin only answers for work orders (no customer-vehicle fallback), so new estimates and invoices start blank. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- README.md | 2 +- lib/vehicles/estimate_vehicle.rb | 7 ++++--- lib/vehicles/hooks/billing_hook_listener.rb | 14 +++----------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9e02583..5bbbf9e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ 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 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, and a new estimate or invoice starts with the VIN of its work orders' vehicle (or of the customer's only 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. +* **Estimates (redmine_qbo 2026.11.0+):** the VIN box suggests the customer's vehicles, 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. * **Customer page:** ticking a vehicle adds it to the New issue link. ## Installation diff --git a/lib/vehicles/estimate_vehicle.rb b/lib/vehicles/estimate_vehicle.rb index 64849bb..4613fb6 100644 --- a/lib/vehicles/estimate_vehicle.rb +++ b/lib/vehicles/estimate_vehicle.rb @@ -14,10 +14,11 @@ module Vehicles module EstimateVehicle module_function - # The VIN on the estimate, or nil + # The VIN on the estimate, or nil. Read by redmine_qbo with QuickBooks' newer custom fields: the local + # estimate's details leave them out, so a VIN field added recently in QuickBooks would never match. def vin(estimate) - company = RedmineQboBilling::Company - RedmineQboBilling::EstimateWriter.custom_value(estimate.details, company.vin_field, company::VIN_NAME).to_s.strip.presence + return nil unless estimate + RedmineQboBilling::EstimateWriter.vin_for(estimate.id) rescue StandardError nil end diff --git a/lib/vehicles/hooks/billing_hook_listener.rb b/lib/vehicles/hooks/billing_hook_listener.rb index 25e2ba1..6bb6add 100644 --- a/lib/vehicles/hooks/billing_hook_listener.rb +++ b/lib/vehicles/hooks/billing_hook_listener.rb @@ -34,20 +34,12 @@ module Vehicles customer.vehicles.select { |v| v.vin.present? }.map { |v| { vin: v.vin, name: v.to_s } } end - # The VIN a new estimate or invoice starts with: the work orders' vehicle when they share one, else the - # customer's only vehicle with a VIN. Nothing when it's ambiguous (work orders for several vehicles, or a - # customer with several and no work orders); the VIN box's suggestions still list them. + # The VIN for a document's work orders (e.g. an invoice made from an estimate without a VIN): their + # vehicle when they share one, nothing otherwise def qbo_billing_default_vin(context = {}) vins = Array(context[:issues]).filter_map { |issue| issue.respond_to?(:vehicle) ? issue.vehicle&.vin.presence : nil } .map(&:upcase).uniq - return vins.first if vins.size == 1 - return nil if vins.size > 1 - - customer = context[:customer] - return nil unless customer.respond_to?(:vehicles) - - with_vin = customer.vehicles.select { |v| v.vin.present? } - with_vin.first.vin if with_vin.size == 1 + vins.first if vins.size == 1 end # Issues created from an estimate get a vehicle: the one picked on the Create issues page (or passed in,