diff --git a/README.md b/README.md index 3972b36..c68be5b 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/redmine_qbo_billing.rb b/lib/redmine_qbo_billing.rb index 39e9644..30d934a 100644 --- a/lib/redmine_qbo_billing.rb +++ b/lib/redmine_qbo_billing.rb @@ -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) diff --git a/lib/redmine_qbo_billing/estimate_writer.rb b/lib/redmine_qbo_billing/estimate_writer.rb index 4e85024..7ea9b8f 100644 --- a/lib/redmine_qbo_billing/estimate_writer.rb +++ b/lib/redmine_qbo_billing/estimate_writer.rb @@ -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, diff --git a/lib/redmine_qbo_billing/invoice_writer.rb b/lib/redmine_qbo_billing/invoice_writer.rb index e7c158e..d71e447 100644 --- a/lib/redmine_qbo_billing/invoice_writer.rb +++ b/lib/redmine_qbo_billing/invoice_writer.rb @@ -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