diff --git a/app/controllers/customer_appointments_controller.rb b/app/controllers/customer_appointments_controller.rb index 91ea919..03380e8 100644 --- a/app/controllers/customer_appointments_controller.rb +++ b/app/controllers/customer_appointments_controller.rb @@ -16,6 +16,7 @@ class CustomerAppointmentsController < ApplicationController before_action :authorize_global helper :projects + helper :custom_fields def customer_options customer = Customer.find_by(id: params[:customer_id]) @@ -38,12 +39,17 @@ class CustomerAppointmentsController < ApplicationController return if performed? @rows = params[:lines].respond_to?(:to_unsafe_h) ? params[:lines].to_unsafe_h : {} - created_issues = @builder.create_issues(project: @target_project, rows: @rows) + @custom_field_values = submitted_custom_field_values + created_issues = @builder.create_issues(project: @target_project, rows: @rows, + custom_field_values: @custom_field_values) if created_issues.any? flash[:notice] = l(:notice_appointment_issues_created, count: created_issues.size) redirect_to customer_appointment_path(@appointment) else + # Rebuild the fields so the form keeps what was entered + @field_set = RedmineQboCalendar::IssueFieldSet.new(project: @target_project, trackers: @trackers, + values: @custom_field_values) render :issues_preview, status: :unprocessable_entity end end @@ -168,9 +174,16 @@ class CustomerAppointmentsController < ApplicationController @trackers = Issue.allowed_target_trackers(@target_project, User.current).to_a @default_tracker = @trackers.detect { |t| t.id == RedmineQboCalendar::EstimateIssueBuilder.default_tracker_id } || @trackers.first @templates = RedmineQboCalendar::IssueTemplateSupport.templates_for(@target_project) + @field_set = RedmineQboCalendar::IssueFieldSet.new(project: @target_project, trackers: @trackers) @rows ||= {} 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) + values.respond_to?(:to_unsafe_h) ? values.to_unsafe_h : {} + end + def find_project @project = Project.find(params[:project_id]) if params[:project_id].present? end diff --git a/app/views/customer_appointments/issues_preview.html.erb b/app/views/customer_appointments/issues_preview.html.erb index 9c6a730..10e0d47 100644 --- a/app/views/customer_appointments/issues_preview.html.erb +++ b/app/views/customer_appointments/issues_preview.html.erb @@ -136,6 +136,25 @@ + <%# Custom fields for the chosen trackers, entered once and applied to every issue that uses them %> + <% if @field_set&.any? %> +
+ <% end %> +<% if templates_enabled %> <%= l(:text_estimate_issues_template_hint) %> @@ -193,6 +212,40 @@ fillTemplates(template, tracker.value, false); tracker.addEventListener('change', function () { fillTemplates(template, tracker.value, true); }); }); + + // Show only the custom fields used by the trackers of the selected lines, and star the required ones + var fieldBox = document.getElementById('estimate-issue-fields'); + + function idList(value) { + return (value || '').split(',').filter(function (v) { return v !== ''; }); + } + + function updateFields() { + if (!fieldBox) return; + var trackerIds = []; + form.querySelectorAll('tr.estimate-line').forEach(function (row) { + var box = row.querySelector('.estimate-line-select'); + var tracker = row.querySelector('.estimate-line-tracker'); + if (box && tracker && box.checked && trackerIds.indexOf(tracker.value) < 0) trackerIds.push(tracker.value); + }); + + var visibleCount = 0; + fieldBox.querySelectorAll('.estimate-issue-field').forEach(function (field) { + var used = idList(field.dataset.trackerIds).some(function (id) { return trackerIds.indexOf(id) >= 0; }); + var required = idList(field.dataset.requiredTrackerIds).some(function (id) { return trackerIds.indexOf(id) >= 0; }); + field.style.display = used ? '' : 'none'; + var star = field.querySelector('.estimate-field-required'); + if (star) star.style.display = required ? '' : 'none'; + if (used) visibleCount++; + }); + fieldBox.style.display = visibleCount > 0 ? '' : 'none'; + } + + form.querySelectorAll('.estimate-line-select, .estimate-line-tracker').forEach(function (el) { + el.addEventListener('change', updateFields); + }); + if (toggleAll) toggleAll.addEventListener('change', updateFields); + updateFields(); })(); <% end %> <% end %> diff --git a/assets/stylesheets/customer_appointments.css b/assets/stylesheets/customer_appointments.css index 21ef32d..0f5a1be 100644 --- a/assets/stylesheets/customer_appointments.css +++ b/assets/stylesheets/customer_appointments.css @@ -642,3 +642,6 @@ table.estimate-issues-preview tr.created td { color: #777; font-size: 0.9em; } +.estimate-issue-fields .estimate-issue-fields-info { + padding-left: 0; +} diff --git a/config/locales/en.yml b/config/locales/en.yml index 448f0e5..1eeea38 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -62,6 +62,7 @@ en: label_estimated_hours: Estimated Time label_default_tracker: "Default tracker" label_estimate_issue_settings: "Issues from estimates" + label_estimate_issue_fields: "Issue fields" label_estimate_item: "Item" label_estimate_item_codes: "Estimate item codes" label_estimate_line_already_created: "Already created" @@ -102,6 +103,7 @@ en: permission_manage_customer_appointments: Manage Customer Appointments permission_view_customer_appointments: "View customer appointments" text_default_tracker_info: "Pre-selected for each proposed issue. Leave blank to use the project's first tracker." + text_estimate_issue_fields_info: "Entered once and applied to every created issue whose tracker uses the field. Fields marked * are required by the selected trackers." text_estimate_issues_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. Edit anything before creating." text_estimate_issues_template_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. A template supplies the issue's description." text_estimate_item_codes_info: "QBO item names that can become issues, one per line (e.g. Labor, Alignments). Matches the full item name or any part of a Category:Item name, ignoring case." diff --git a/lib/redmine_qbo_calendar/estimate_issue_builder.rb b/lib/redmine_qbo_calendar/estimate_issue_builder.rb index 4296a96..c1549c5 100644 --- a/lib/redmine_qbo_calendar/estimate_issue_builder.rb +++ b/lib/redmine_qbo_calendar/estimate_issue_builder.rb @@ -68,8 +68,9 @@ module RedmineQboCalendar end # rows: { line_id => { 'selected' => '1', 'subject' => ..., 'tracker_id' => ..., 'template' => 'global:3', 'estimated_hours' => ... } } + # custom_field_values: { custom_field_id => value }, applied to every issue whose tracker uses the field # Returns the created issues, or [] with #errors filled in. - def create_issues(project:, rows:, author: User.current) + def create_issues(project:, rows:, custom_field_values: {}, author: User.current) @errors = [] rows = (rows || {}).to_h.transform_keys(&:to_s) @@ -82,7 +83,7 @@ module RedmineQboCalendar allowed_trackers = Issue.allowed_target_trackers(project, author).to_a built = selected.map do |proposal| - [proposal, build_issue(proposal, rows[proposal.line_id] || {}, project, author, allowed_trackers)] + [proposal, build_issue(proposal, rows[proposal.line_id] || {}, project, author, allowed_trackers, custom_field_values)] end Issue.transaction do @@ -153,9 +154,15 @@ module RedmineQboCalendar key.to_s end - def build_issue(proposal, row, project, author, allowed_trackers) + def build_issue(proposal, row, project, author, allowed_trackers, custom_field_values) issue = Issue.new(project: project, author: author) issue.tracker = allowed_trackers.detect { |t| t.id == row['tracker_id'].to_i } + + # Shared custom field values. Going through safe_attributes means fields the tracker doesn't + # use, or that this user can't edit (hidden / read-only by workflow), are ignored. + if custom_field_values.present? && issue.tracker + issue.send(:safe_attributes=, { 'custom_field_values' => custom_field_values.to_h }, author) + end issue.subject = (row['subject'].presence || proposal.description).to_s.strip.truncate(255) template = IssueTemplateSupport.find(row['template'], project) diff --git a/lib/redmine_qbo_calendar/issue_field_set.rb b/lib/redmine_qbo_calendar/issue_field_set.rb new file mode 100644 index 0000000..ca0265e --- /dev/null +++ b/lib/redmine_qbo_calendar/issue_field_set.rb @@ -0,0 +1,72 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +module RedmineQboCalendar + # The issue custom fields shown on the "Create Issues from Estimate" preview page. + # + # For every tracker the user may pick, a sample new issue is built in the target project to find + # which custom fields the user can edit, and which are required (either the field's own + # "Required" flag or a workflow rule for that tracker / role). Values are entered once on the page + # and applied to every created issue whose tracker uses that field. + class IssueFieldSet + # File uploads can't be handled by this shared form + UNSUPPORTED_FORMATS = %w[attachment].freeze + + Field = Struct.new(:custom_field, :custom_value, :tracker_ids, :required_tracker_ids, keyword_init: true) do + def id + custom_field.id + end + + def required_for_any? + required_tracker_ids.any? + end + end + + attr_reader :fields + + # values: the submitted { custom_field_id => value } hash, used to refill the form after errors + def initialize(project:, trackers:, user: User.current, values: {}) + @fields = [] + by_id = {} + + Array(trackers).each do |tracker| + issue = Issue.new(project: project, author: user) + issue.tracker = tracker + + issue.editable_custom_field_values(user).each do |custom_value| + custom_field = custom_value.custom_field + next if UNSUPPORTED_FORMATS.include?(custom_field.field_format) + + field = by_id[custom_field.id] ||= Field.new( + custom_field: custom_field, + custom_value: custom_value, + tracker_ids: [], + required_tracker_ids: [] + ) + field.tracker_ids << tracker.id + if custom_field.is_required? || issue.required_attribute?(custom_field.id.to_s, user) + field.required_tracker_ids << tracker.id + end + end + end + + @fields = by_id.values.sort_by { |f| [f.required_for_any? ? 0 : 1, f.custom_field.position.to_i] } + + values = (values || {}).to_h.transform_keys(&:to_s) + @fields.each do |field| + field.custom_value.value = values[field.id.to_s] if values.key?(field.id.to_s) + end + end + + def any? + @fields.any? + end + end +end