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? %>