Fill the VIN on new estimates and invoices through a hook

New hook qbo_billing_default_vin, { customer:, issues: }: a blank estimate or invoice starts with the VIN
a plugin gives for the customer, and an invoice made from an estimate without a VIN gets the one for the
estimate's work orders. The editor only filled the VIN when the customer was changed or work orders were
imported, so pages opened with the customer already set started blank.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-31 10:17:25 -04:00
1 parent 28538d4c7f
commit b84f39afce
4 files changed
+21 -1

No files matched your search

+1
View File
@@ -223,6 +223,7 @@ Hook|:qbo_billing_validate, { document:, form:, issues: } | Return an error mess
Hook|:qbo_billing_issue_defaults, { estimate:, params: } | Return `start_date`, `due_date`, `assignee`, `project`, `description`, `return_path`, `return_label`, plus any values your own fields need (e.g. redmine_qbo_calendar's `vehicle_id`), for issues created from an estimate (redmine_qbo_calendar, when opened from an appointment)
Hook|:qbo_billing_build_issue, { issue:, estimate:, defaults: } | Set your plugin's own fields on each issue created from an estimate, before it's saved (redmine_qbo_vehicles sets the vehicle: the one in `defaults`, or the customer's vehicle whose VIN is on the estimate)
Hook|:qbo_billing_issues_created, { estimate:, issues:, params: } | Called after issues are created from an estimate
Hook|:qbo_billing_default_vin, { customer:, issues: } | Return the VIN a new estimate or invoice starts with (an invoice made from an estimate keeps the estimate's VIN; `issues` are that estimate's work orders). redmine_qbo_vehicles: the work orders' vehicle when they share one, else the customer's only vehicle
Hook|:qbo_billing_vin_options, { customer: } | Return `[{ vin:, name: }, ...]` to suggest in the estimate/invoice VIN box; one suggestion fills an empty box (redmine_qbo_vehicles: the customer's vehicles)
View|checkboxes with `data-new-issue-param="issue[field]"` on the customer page | Ticking one adds `issue[field]=value` to the New issue link, one per field (redmine_qbo's estimates, redmine_qbo_vehicles' vehicles)
View Hook|:view_qbo_estimate_contextual, { estimate_id:, customer_id:, vin:, doc_number: } | Adds links to an estimate page's action area (e.g. redmine_qbo_calendar's Add appointment)
+14
View File
@@ -43,6 +43,20 @@ module RedmineQboBilling
[]
end
# The VIN a new estimate or invoice starts with, from plugins answering
# :qbo_billing_default_vin, { customer:, issues: } => a VIN or nil (e.g. redmine_qbo_vehicles: the work
# orders' vehicle when they share one, else the customer's only vehicle)
# issues are the work orders behind the document (e.g. the issues linked to the estimate it's made from).
def self.default_vin(customer, issues: [])
return nil unless customer
Redmine::Hook.call_hook(:qbo_billing_default_vin, { customer: customer, issues: Array(issues) })
.flatten.compact.map { |vin| vin.to_s.strip }.reject(&:empty?).first
rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] Default VIN failed: #{e.message}"
nil
end
# True when the user may write estimates
def self.writer?(user = User.current)
user.allowed_to?(:write_estimates, nil, global: true)
@@ -36,6 +36,7 @@ module RedmineQboBilling
customer_name: customer&.name,
email: customer_email(customer),
billing_address: customer_address(customer),
vin: RedmineQboBilling.default_vin(customer),
txn_date: user.today.iso8601,
expiration_date: default_expiration_date(user.today)&.iso8601,
sales_person: user.name,
+5 -1
View File
@@ -47,6 +47,7 @@ module RedmineQboBilling
customer_name: customer&.name,
email: writer.customer_email(customer),
billing_address: writer.customer_address(customer),
vin: RedmineQboBilling.default_vin(customer),
txn_date: user.today.iso8601,
sales_person: user.name,
customer_memo: Company.invoice_message,
@@ -70,7 +71,10 @@ module RedmineQboBilling
form.customer_id = source.customer_id
form.email = source.email.presence || form.email
form.billing_address = source.billing_address.presence || form.billing_address
form.vin = source.vin
# The estimate's VIN, or else a plugin's for the estimate's work orders (e.g. their vehicle)
form.vin = source.vin.presence ||
RedmineQboBilling.default_vin(customer, issues: Issue.where(estimate_id: source.id.to_s).to_a) ||
form.vin
form.mileage = source.mileage
# The estimate's writer sells the job; with no one on the estimate, whoever writes the invoice
form.sales_person = source.sales_person.presence || user.name