From abeb0fe0f5a1c6d8d0bf2c28c62d1b0c4f9f9ea4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 10:25:03 -0400 Subject: [PATCH] Let plugins add fields to the Create issues from estimate page View hook view_qbo_estimate_issues_form draws plugins' fields in the shared Issue fields box; values posted as issue_plugin_fields[name] override the plugin defaults and reach qbo_billing_build_issue. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- README.md | 1 + app/controllers/qbo_estimates_controller.rb | 12 +++++++++++- app/views/qbo_estimates/issues_preview.html.erb | 9 ++++++--- lib/redmine_qbo_billing/estimate_issue_builder.rb | 3 +++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c68be5b..8b91698 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ Hook|:qbo_billing_issue_values, { issues: } | Return `{ field => { issue_id => v Hook|:qbo_billing_validate, { document:, form:, issues: } | Return an error message (or several) to stop an estimate or invoice from saving, e.g. redmine_qbo_vehicles: one vehicle per invoice 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) +View|:view_qbo_estimate_issues_form, { estimate:, defaults:, project: } | Draw your plugin's fields on the Create issues from estimate page, applied to every issue. Name them `issue_plugin_fields[name]`; the posted values reach `:qbo_billing_build_issue` in `defaults` (redmine_qbo_vehicles: the Vehicle, preselected from the estimate's VIN) 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) diff --git a/app/controllers/qbo_estimates_controller.rb b/app/controllers/qbo_estimates_controller.rb index fe8e17e..8b1f8c4 100644 --- a/app/controllers/qbo_estimates_controller.rb +++ b/app/controllers/qbo_estimates_controller.rb @@ -155,7 +155,8 @@ class QboEstimatesController < ApplicationController end return render_404 unless @estimate - @defaults = issue_defaults + # Values plugins' fields posted with the form (e.g. redmine_qbo_vehicles' vehicle) override their defaults + @defaults = issue_defaults.merge(submitted_plugin_fields) @builder = RedmineQboBilling::EstimateIssueBuilder.new(@estimate, defaults: @defaults) @return_path = @defaults[:return_path].presence || edit_qbo_estimate_path(@estimate.id) @return_label = @defaults[:return_label].presence || "#{l(:label_estimate)} ##{@estimate.doc_number}" @@ -185,6 +186,15 @@ class QboEstimatesController < ApplicationController (results.detect { |r| r.is_a?(Hash) && r.any? } || {}).symbolize_keys end + # Plugins' own fields on the preview form (issue_plugin_fields[...], drawn by :view_qbo_estimate_issues_form), + # only when the form was posted: { vehicle_id: "5" } + def submitted_plugin_fields + return {} unless request.post? + + values = params[:issue_plugin_fields] + values.respond_to?(:to_unsafe_h) ? values.to_unsafe_h.to_h.transform_keys(&:to_sym).transform_values(&:to_s) : {} + end + # The shared custom field values from the preview form: { custom_field_id => value } def submitted_custom_field_values values = params.dig(:issue, :custom_field_values) diff --git a/app/views/qbo_estimates/issues_preview.html.erb b/app/views/qbo_estimates/issues_preview.html.erb index c4d9ad4..4c57e14 100644 --- a/app/views/qbo_estimates/issues_preview.html.erb +++ b/app/views/qbo_estimates/issues_preview.html.erb @@ -136,12 +136,15 @@ - <%# Custom fields for the chosen trackers, entered once and applied to every issue that uses them %> - <% if @field_set&.any? %> + <%# Fields entered once and applied to every issue: plugins' own (e.g. redmine_qbo_vehicles' vehicle, posted as + issue_plugin_fields[...]) and the custom fields for the chosen trackers %> + <% plugin_fields = call_hook(:view_qbo_estimate_issues_form, { estimate: @estimate, defaults: @defaults, project: @target_project }) %> + <% if @field_set&.any? || plugin_fields.present? %>
<%= l(:label_estimate_issue_fields) %>

<%= l(:text_estimate_issue_fields_info) %>

- <% @field_set.fields.each do |field| %> + <%= plugin_fields %> + <% Array(@field_set&.fields).each do |field| %>

diff --git a/lib/redmine_qbo_billing/estimate_issue_builder.rb b/lib/redmine_qbo_billing/estimate_issue_builder.rb index 895947b..9ccc2ec 100644 --- a/lib/redmine_qbo_billing/estimate_issue_builder.rb +++ b/lib/redmine_qbo_billing/estimate_issue_builder.rb @@ -24,6 +24,9 @@ module RedmineQboBilling # redmine_qbo_calendar's vehicle_id, when the page is opened from an appointment) # :qbo_billing_build_issue, { issue:, estimate:, defaults: } for each new issue before it's saved, to set # plugins' own fields (e.g. redmine_qbo_vehicles sets the vehicle) + # :view_qbo_estimate_issues_form, { estimate:, defaults:, project: } (view hook) draws plugins' fields on + # the page, applied to every issue; posted as issue_plugin_fields[name], they reach :qbo_billing_build_issue + # in defaults (e.g. redmine_qbo_vehicles' Vehicle, preselected from the estimate's VIN) # :qbo_billing_issues_created, { estimate:, issues:, params: } after issues are created class EstimateIssueBuilder include Redmine::I18n