<% html_title l(:button_create_estimate_issues) %> <%# Extra params to carry through (e.g. appointment_id from redmine_qbo_calendar) %> <% carry = request.query_parameters.merge(request.request_parameters).slice('appointment_id') %>
<%= link_to sprite_icon('angle-left', @return_label), @return_path, class: 'icon icon-angle-left' %>

<%= l(:button_create_estimate_issues) %> · <%= l(:label_estimate) %> #<%= @estimate.doc_number %> <% if @estimate.customer %>· <%= @estimate.customer.name %><% end %>

<%# Target project: changing it reloads the page with that project's trackers and templates %> <% if @available_projects.size > 1 %> <%= form_tag issues_preview_qbo_estimate_path(@estimate.id), method: :get, id: 'estimate-issues-project-form' do %> <% carry.each do |key, value| %><%= hidden_field_tag key, value, id: nil %><% end %>

<%= select_tag :project_id, options_from_collection_for_select(@available_projects, :id, :name, @target_project.id), onchange: 'this.form.submit();' %>

<% end %> <% else %>

<%= l(:field_project) %>: <%= link_to_project @target_project %>

<% end %> <% if @builder.errors.any? %>
<% end %> <% proposals = @builder.proposals %> <% if proposals.empty? %>

<%= l(:error_no_labor_lines_found) %> (<%= l(:label_estimate_item_codes) %>: <%= RedmineQboBilling::EstimateIssueBuilder.item_codes.join(', ') %>) <% if User.current.admin? %> · <%= link_to l(:label_settings), plugin_settings_path(id: 'redmine_qbo') %> <% end %>

<% else %> <% templates_enabled = RedmineQboBilling::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_qbo_estimate_path(@estimate.id), method: :post, id: 'estimate-issues-form', data: { templates: template_data.to_json } do %> <%= hidden_field_tag :project_id, @target_project.id %> <% carry.each do |key, value| %><%= hidden_field_tag key, value, id: nil %><% end %> <% if templates_enabled %> <% end %> <% proposals.each do |proposal| %> <% field = "lines[#{proposal.line_id}]" %> <% if proposal.created? %> <% 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 %> <% if templates_enabled %> <% end %> <% end %> <% end %>
<% if selectable_count > 0 %> <% end %> <%= l(:label_estimate_item) %> <%= l(:field_subject) %> <%= l(:field_tracker) %><%= l(:label_issue_template) %><%= l(:field_estimated_hours) %>
<%= proposal.item_name %> <%= proposal.description %> <%= l(:label_estimate_line_already_created) %>: <%= link_to_issue(proposal.existing_issue, truncate: 60) %>
<%= hidden_field_tag "#{field}[selected]", '0', id: nil %> <%= check_box_tag "#{field}[selected]", '1', selected, class: 'estimate-line-select', id: nil %> <%= proposal.item_name %> <%= text_field_tag "#{field}[subject]", row['subject'] || proposal.description, maxlength: 255, class: 'estimate-line-subject', id: nil %> <%= select_tag "#{field}[tracker_id]", options_from_collection_for_select(@trackers, :id, :name, tracker_id), class: 'estimate-line-tracker', id: nil %> <%# 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 %> <%= number_field_tag "#{field}[estimated_hours]", hours, step: 0.25, min: 0, size: 6, id: nil %>
<%# Custom fields for the chosen trackers, entered once and applied to every issue that uses them %> <% if @field_set&.any? %>
<%= l(:label_estimate_issue_fields) %>

<%= l(:text_estimate_issue_fields_info) %>

<% @field_set.fields.each do |field| %>

<%= custom_field_tag('issue', field.custom_value) %>

<% end %>
<% end %>

<% if templates_enabled %> <%= l(:text_estimate_issues_template_hint) %> <% else %> <%= l(:text_estimate_issues_hint) %> <% end %>

<% if selectable_count > 0 %> <%= submit_tag l(:button_create_estimate_issues_submit), data: { disable_with: l(:label_loading) } %> <% end %> <%= link_to l(:button_cancel), @return_path %>

<% 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); }); }); // 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 %> <%# Inline so the page never depends on precompiled plugin assets %>