Estimate/invoice numbers on issue PDF; create estimate issues as subtasks

- Issue PDF: show the issue's estimate number and invoice number(s)
- Create issues from estimate: optional parent task. The issues are created as
  its subtasks, take its customer, and start from its project, vehicle and
  custom field values (subtask_field_copier's rules when installed)
- Issue page: shortcut to create the estimate's issues as subtasks of the issue
- Create issues page: editable start and due dates, pre-filled from the
  appointment or the parent task
- Always show plugins' fields (e.g. the vehicle) on the Create issues page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-09 20:17:38 -05:00
1 parent 42461e7899
commit 8a947483db
6 files changed
+193 -24

No files matched your search

+75 -8
View File
@@ -101,7 +101,8 @@ class QboEstimatesController < ApplicationController
def create_issues
@rows = params[:lines].respond_to?(:to_unsafe_h) ? params[:lines].to_unsafe_h : {}
@custom_field_values = submitted_custom_field_values
created = @builder.create_issues(project: @target_project, rows: @rows, custom_field_values: @custom_field_values)
# A parent was entered but can't be used: create nothing rather than top-level issues
created = @parent_issue_error ? [] : @builder.create_issues(project: @target_project, rows: @rows, custom_field_values: @custom_field_values)
if created.any?
Redmine::Hook.call_hook(:qbo_billing_issues_created, { estimate: @estimate, issues: created, params: params })
@@ -174,31 +175,97 @@ class QboEstimatesController < ApplicationController
end
return render_404 unless @estimate
# 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)
load_parent_issue
@defaults = build_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}"
@available_projects = Project.allowed_to(User.current, :add_issues).sorted.to_a
# A project chosen on the page wins, then the one from another plugin (e.g. the appointment's), then the
# default project from the settings, then the first project the user can add issues to
# A project chosen on the page wins, then the parent issue's, then the one from another plugin (e.g. the
# appointment's), then the default project from the settings, then the first project the user can add issues to
chosen = @available_projects.detect { |p| p.id == params[:project_id].to_i } if params[:project_id].present?
from_parent = @parent_issue.project if @parent_issue && @available_projects.include?(@parent_issue.project)
from_hook = @defaults[:project] if @available_projects.include?(@defaults[:project])
default_project = @available_projects.detect { |p| p.id == RedmineQboBilling::EstimateIssueBuilder.default_project_id }
@target_project = chosen || from_hook || default_project || @available_projects.first
@target_project = chosen || from_parent || from_hook || default_project || @available_projects.first
return deny_access unless @target_project
# Subtasks need the "Manage subtasks" permission in the project they're created in
@can_add_subtasks = User.current.allowed_to?(:manage_subtasks, @target_project)
if @parent_issue && !@can_add_subtasks
@parent_issue_error = l(:error_estimate_parent_not_allowed, project: @target_project.name)
@parent_issue = nil
@defaults = build_defaults
end
@builder = RedmineQboBilling::EstimateIssueBuilder.new(@estimate, defaults: @defaults, parent: @parent_issue)
@trackers = Issue.allowed_target_trackers(@target_project, User.current).to_a
@default_tracker = @trackers.detect { |t| t.id == RedmineQboBilling::EstimateIssueBuilder.default_tracker_id } || @trackers.first
@templates = RedmineQboBilling::IssueTemplateSupport.templates_for(@target_project)
@field_set = RedmineQboBilling::IssueFieldSet.new(project: @target_project, trackers: @trackers)
# With a parent issue, the shared fields start out with the parent's values
@field_set = RedmineQboBilling::IssueFieldSet.new(project: @target_project, trackers: @trackers,
values: parent_custom_field_values)
@rows ||= {}
rescue *QBO_ERRORS => e
flash[:error] = qbo_message(e)
redirect_back_or_default home_path
end
# The optional parent issue (parent_issue_id, "#123" or "123"): the new issues become its subtasks.
# Only an issue the user can see; an unknown one is reported and nothing is created.
def load_parent_issue
@parent_issue_id = params[:parent_issue_id].to_s.delete('#').strip
return if @parent_issue_id.blank?
@parent_issue = Issue.visible.find_by(id: @parent_issue_id.to_i) if @parent_issue_id.match?(/\A\d+\z/)
@parent_issue_error = l(:error_estimate_parent_not_found, id: @parent_issue_id) unless @parent_issue
end
# What the new issues start from:
# - dates: another plugin's (e.g. the appointment's), else the parent issue's
# - vehicle: the parent issue's, else another plugin's (e.g. the appointment's)
# Values posted with the form (the dates, plugins' fields such as redmine_qbo_vehicles' vehicle) win.
def build_defaults
parent_dates = {}
parent_vehicle = {}
if @parent_issue
parent_dates = { start_date: @parent_issue.start_date, due_date: @parent_issue.due_date }.compact
if @parent_issue.respond_to?(:vehicle_id) && @parent_issue.vehicle_id.present?
parent_vehicle = { vehicle_id: @parent_issue.vehicle_id.to_s }
end
end
parent_dates.merge(issue_defaults.compact).merge(parent_vehicle).merge(submitted_plugin_fields).merge(submitted_dates)
end
# The Start date / Due date entered on the page, only when the form was posted. A blank one means no date.
def submitted_dates
return {} unless request.post? && params[:issue_dates].respond_to?(:to_unsafe_h)
values = params[:issue_dates].to_unsafe_h
%w[start_date due_date].each_with_object({}) do |key, dates|
dates[key.to_sym] = parse_date(values[key]) if values.key?(key)
end
end
def parse_date(value)
Date.iso8601(value.to_s.strip) if value.present?
rescue ArgumentError
nil
end
# The parent issue's custom field values the user can see, to start the shared fields from: { id => value }
def parent_custom_field_values
return {} unless @parent_issue
@parent_issue.visible_custom_field_values.each_with_object({}) do |custom_value, values|
next if Array(custom_value.value).all?(&:blank?)
values[custom_value.custom_field_id.to_s] = custom_value.value
end
end
# The first non-empty hash a plugin returns for :qbo_billing_issue_defaults
def issue_defaults
results = Redmine::Hook.call_hook(:qbo_billing_issue_defaults, { estimate: @estimate, params: params })
+6
View File
@@ -10,6 +10,12 @@
<div class="value">
<%= estimate_link %>
<%= render partial: 'qbo_estimates/estimate_actions', locals: { estimate: issue.estimate } if issue.estimate && RedmineQboBilling.writer? %>
<%# Create the estimate's remaining labor lines as subtasks of this issue %>
<% if issue.estimate && User.current.allowed_to?(:create_estimate_issues, nil, global: true) && User.current.allowed_to?(:manage_subtasks, issue.project) %>
<%= link_to sprite_icon('add', l(:button_create_estimate_subtasks)),
issues_preview_qbo_estimate_path(issue.estimate.id, parent_issue_id: issue.id),
class: 'icon-only icon-add', title: l(:button_create_estimate_subtasks) %>
<% end %>
</div>
</div>
+61 -11
View File
@@ -14,26 +14,58 @@
</span>
</h2>
<%# 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 %>
<p>
<label for="project_id"><strong><%= l(:field_project) %>:</strong></label>
<%# Target project and parent task: changing either reloads the page (the project's trackers and templates;
the parent's project, vehicle and custom field values) %>
<%= 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 %>
<p>
<label for="project_id"><strong><%= l(:field_project) %>:</strong></label>
<% if @available_projects.size > 1 %>
<%= 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>
<% else %>
<%= link_to_project @target_project %>
<%= hidden_field_tag :project_id, @target_project.id, id: nil %>
<% end %>
</p>
<% if @can_add_subtasks || @parent_issue_id.present? %>
<p class="estimate-issues-parent">
<label for="parent_issue_id"><strong><%= l(:field_parent_issue) %>:</strong></label>
<%= text_field_tag :parent_issue_id, @parent_issue&.id || @parent_issue_id, size: 10,
placeholder: l(:label_estimate_parent_placeholder) %>
<% if @parent_issue %>
<%= link_to_issue(@parent_issue, truncate: 60) %>
<%= link_to sprite_icon('del', l(:button_clear)),
issues_preview_qbo_estimate_path(@estimate.id, carry.merge(project_id: @target_project.id)),
class: 'icon-only icon-del', title: l(:button_clear) %>
<br><em class="info"><%= l(:text_estimate_parent_info) %></em>
<% end %>
</p>
<% end %>
<% else %>
<p><strong><%= l(:field_project) %>:</strong> <%= link_to_project @target_project %></p>
<noscript><p><%= submit_tag l(:button_apply), name: nil %></p></noscript>
<% end %>
<% if @builder.errors.any? %>
<% if @can_add_subtasks || @parent_issue_id.present? %>
<%= javascript_tag do %>
(function () {
var field = document.getElementById('parent_issue_id');
if (!field) return;
var submitted = false;
function reload() { if (!submitted) { submitted = true; field.form.submit(); } }
observeAutocompleteField('parent_issue_id',
<%= raw auto_complete_issues_path(project_id: @target_project, scope: Setting.cross_project_subtasks, status: 'o').to_json %>,
{ select: function (event, ui) { field.value = ui.item.value; reload(); } });
field.addEventListener('change', reload);
})();
<% end %>
<% end %>
<% errors = [@parent_issue_error, *@builder.errors].compact %>
<% if errors.any? %>
<div id="errorExplanation">
<ul>
<% @builder.errors.each do |message| %>
<% errors.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
@@ -63,6 +95,7 @@
id: 'estimate-issues-form',
data: { templates: template_data.to_json } do %>
<%= hidden_field_tag :project_id, @target_project.id %>
<%= hidden_field_tag :parent_issue_id, @parent_issue&.id || @parent_issue_id, id: nil %>
<% carry.each do |key, value| %><%= hidden_field_tag key, value, id: nil %><% end %>
<table class="list estimate-issues-preview">
@@ -136,6 +169,21 @@
</tbody>
</table>
<%# Dates for every issue: from the appointment the page was opened from, else the parent task; blank means none %>
<fieldset class="box tabular estimate-issue-dates">
<legend><%= l(:label_estimate_issue_dates) %></legend>
<p>
<label for="estimate_issue_start_date"><%= l(:field_start_date) %></label>
<%= date_field_tag 'issue_dates[start_date]', @defaults[:start_date], id: 'estimate_issue_start_date', size: 10 %>
<%= calendar_for('estimate_issue_start_date') %>
</p>
<p>
<label for="estimate_issue_due_date"><%= l(:field_due_date) %></label>
<%= date_field_tag 'issue_dates[due_date]', @defaults[:due_date], id: 'estimate_issue_due_date', size: 10 %>
<%= calendar_for('estimate_issue_due_date') %>
</p>
</fieldset>
<%# 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 }) %>
@@ -234,6 +282,8 @@
var visibleCount = 0;
fieldBox.querySelectorAll('.estimate-issue-field').forEach(function (field) {
// Plugins' fields (e.g. the vehicle) apply to every tracker
if (!field.hasAttribute('data-tracker-ids')) { visibleCount++; return; }
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';