Add preview page for creating issues from an estimate

- "Create Issues from Estimate" now opens a preview listing each
  matching estimate line: include checkbox, subject (from the line
  description), tracker dropdown, template dropdown and estimated
  hours (from the line quantity)
- Admins set which QBO item codes to look for (e.g. Labor, Alignments)
  and a default tracker in the plugin settings
- Optional redmine_issue_templates support: templates supply the issue
  description; titles still come from the line description
- New appointment_estimate_issues table records which issue came from
  which estimate line, so a line can't be turned into an issue twice;
  deleting the issue frees the line again
- Creation is all-or-nothing; errors are shown on the preview page
- Removes the old Labor-only create_issues_from_labor_lines code

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-28 20:34:06 -04:00
1 parent 73115da2b9
commit 184c1e2464
15 files changed
+613 -92

No files matched your search

@@ -0,0 +1,198 @@
<div class="contextual">
<%= link_to customer_appointment_path(@appointment) do %>
<i class="fa-solid fa-arrow-left"></i> <%= l(:label_appointment) %>
<% end %>
</div>
<h2>
<%= l(:button_create_issues_from_estimate) %>
<% if @appointment.estimate %>
<span class="estimate-preview-subtitle">
&middot; <%= l(:label_estimate) %> #<%= @appointment.estimate.doc_number %>
&middot; <%= @appointment.subject %>
</span>
<% end %>
</h2>
<%# Target project: changing it reloads the page with that project's trackers and templates %>
<% if @available_projects.size > 1 %>
<%= form_tag issues_preview_customer_appointment_path(@appointment), method: :get, id: 'estimate-issues-project-form' do %>
<p>
<label for="project_id"><strong><%= l(:field_project) %>:</strong></label>
<%= select_tag :project_id,
options_from_collection_for_select(@available_projects, :id, :name, @target_project.id),
onchange: 'this.form.submit();' %>
<noscript><%= submit_tag l(:button_apply), name: nil %></noscript>
</p>
<% end %>
<% else %>
<p><strong><%= l(:field_project) %>:</strong> <%= link_to_project @target_project %></p>
<% end %>
<% if @builder.errors.any? %>
<div id="errorExplanation">
<ul>
<% @builder.errors.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% proposals = @builder.proposals %>
<% if @appointment.estimate.blank? %>
<p class="nodata"><%= l(:label_appointment_has_no_estimate) %></p>
<% elsif proposals.empty? %>
<p class="nodata">
<%= l(:error_no_labor_lines_found) %>
(<%= l(:label_estimate_item_codes) %>: <%= RedmineQboCalendar::EstimateIssueBuilder.item_codes.join(', ') %>)
<% if User.current.admin? %>
&middot; <%= link_to l(:label_settings), plugin_settings_path(id: 'redmine_qbo_calendar') %>
<% end %>
</p>
<% else %>
<%
templates_enabled = RedmineQboCalendar::IssueTemplateSupport.enabled_for?(@target_project)
template_data = @templates.map { |t| { key: t.key, title: t.title, tracker_id: t.tracker_id, is_default: t.is_default, group: t.group } }
default_template_for = ->(tracker_id) { @templates.detect { |t| t.tracker_id == tracker_id.to_i && t.is_default }&.key }
selectable_count = proposals.count { |p| !p.created? }
%>
<%= form_tag create_issues_customer_appointment_path(@appointment),
method: :post,
id: 'estimate-issues-form',
data: { templates: template_data.to_json } do %>
<%= hidden_field_tag :project_id, @target_project.id %>
<table class="list estimate-issues-preview">
<thead>
<tr>
<th class="checkbox">
<% if selectable_count > 0 %>
<input type="checkbox" id="estimate-issues-toggle-all" checked title="<%= l(:button_check_all) %>">
<% end %>
</th>
<th><%= l(:label_estimate_item) %></th>
<th><%= l(:field_subject) %></th>
<th><%= l(:field_tracker) %></th>
<% if templates_enabled %>
<th><%= l(:label_issue_template) %></th>
<% end %>
<th><%= l(:field_estimated_hours) %></th>
</tr>
</thead>
<tbody>
<% proposals.each do |proposal| %>
<% field = "lines[#{proposal.line_id}]" %>
<% if proposal.created? %>
<tr class="estimate-line created">
<td class="checkbox"><input type="checkbox" disabled></td>
<td><%= proposal.item_name %></td>
<td class="subject"><%= proposal.description %></td>
<td colspan="<%= templates_enabled ? 3 : 2 %>">
<%= l(:label_estimate_line_already_created) %>:
<%= link_to_issue(proposal.existing_issue, truncate: 60) %>
</td>
</tr>
<% else %>
<%
row = (@rows[proposal.line_id] || {}).to_h
selected = row.empty? || row['selected'] == '1'
tracker_id = (row['tracker_id'].presence || @default_tracker&.id).to_i
template_key = row.key?('template') ? row['template'] : default_template_for.call(tracker_id)
hours = row.key?('estimated_hours') ? row['estimated_hours'] : proposal.hours
%>
<tr class="estimate-line">
<td class="checkbox">
<%= hidden_field_tag "#{field}[selected]", '0', id: nil %>
<%= check_box_tag "#{field}[selected]", '1', selected, class: 'estimate-line-select', id: nil %>
</td>
<td><%= proposal.item_name %></td>
<td class="subject">
<%= text_field_tag "#{field}[subject]", row['subject'] || proposal.description,
maxlength: 255, class: 'estimate-line-subject', id: nil %>
</td>
<td>
<%= select_tag "#{field}[tracker_id]",
options_from_collection_for_select(@trackers, :id, :name, tracker_id),
class: 'estimate-line-tracker', id: nil %>
</td>
<% if templates_enabled %>
<td>
<%# Options are filled by the script below to match the chosen tracker %>
<%= select_tag "#{field}[template]",
options_for_select([[l(:label_no_issue_template), '']] +
@templates.select { |t| t.tracker_id == tracker_id }.map { |t| [t.title, t.key] }, template_key),
class: 'estimate-line-template', id: nil %>
</td>
<% end %>
<td>
<%= number_field_tag "#{field}[estimated_hours]", hours, step: 0.25, min: 0, size: 6, id: nil %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<p class="estimate-issues-hint">
<% if templates_enabled %>
<%= l(:text_estimate_issues_template_hint) %>
<% else %>
<%= l(:text_estimate_issues_hint) %>
<% end %>
</p>
<p>
<% if selectable_count > 0 %>
<%= submit_tag l(:button_create_issues), data: { disable_with: l(:label_loading) } %>
<% end %>
<%= link_to l(:button_cancel), customer_appointment_path(@appointment) %>
</p>
<% end %>
<%= javascript_tag do %>
(function () {
var form = document.getElementById('estimate-issues-form');
if (!form) return;
// Select / deselect all lines
var toggleAll = document.getElementById('estimate-issues-toggle-all');
if (toggleAll) {
toggleAll.addEventListener('change', function () {
form.querySelectorAll('.estimate-line-select').forEach(function (box) { box.checked = toggleAll.checked; });
});
}
// Keep each template dropdown limited to templates for the chosen tracker
var templates = [];
try { templates = JSON.parse(form.dataset.templates || '[]'); } catch (e) { templates = []; }
var noneLabel = <%= raw l(:label_no_issue_template).to_json %>;
function fillTemplates(templateSelect, trackerId, useDefault) {
var current = templateSelect.value;
var matches = templates.filter(function (t) { return String(t.tracker_id) === String(trackerId); });
templateSelect.innerHTML = '';
templateSelect.appendChild(new Option(noneLabel, ''));
matches.forEach(function (t) {
templateSelect.appendChild(new Option(t.title + (t.group === 'global' ? ' (global)' : ''), t.key));
});
var defaultTemplate = matches.find(function (t) { return t.is_default; });
if (useDefault) {
templateSelect.value = defaultTemplate ? defaultTemplate.key : '';
} else if (matches.some(function (t) { return t.key === current; })) {
templateSelect.value = current;
}
}
form.querySelectorAll('tr.estimate-line').forEach(function (row) {
var tracker = row.querySelector('.estimate-line-tracker');
var template = row.querySelector('.estimate-line-template');
if (!tracker || !template) return;
fillTemplates(template, tracker.value, false);
tracker.addEventListener('change', function () { fillTemplates(template, tracker.value, true); });
});
})();
<% end %>
<% end %>