diff --git a/app/controllers/qbo_estimates_controller.rb b/app/controllers/qbo_estimates_controller.rb
index edcbefd..b888f31 100644
--- a/app/controllers/qbo_estimates_controller.rb
+++ b/app/controllers/qbo_estimates_controller.rb
@@ -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 })
diff --git a/app/views/issues/_show_details.html.erb b/app/views/issues/_show_details.html.erb
index 0f4e732..1accba2 100644
--- a/app/views/issues/_show_details.html.erb
+++ b/app/views/issues/_show_details.html.erb
@@ -10,6 +10,12 @@
<%= 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 %>
diff --git a/app/views/qbo_estimates/issues_preview.html.erb b/app/views/qbo_estimates/issues_preview.html.erb
index 4c57e14..0ecc119 100644
--- a/app/views/qbo_estimates/issues_preview.html.erb
+++ b/app/views/qbo_estimates/issues_preview.html.erb
@@ -14,26 +14,58 @@
-<%# 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 %>
-
-
+<%# 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 %>
+
+ <%# Dates for every issue: from the appointment the page was opened from, else the parent task; blank means none %>
+
+
<%# 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';
diff --git a/config/locales/en.yml b/config/locales/en.yml
index c6b6ec6..7d4b0d6 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -11,6 +11,11 @@
# English strings go here for Rails i18n
# Usage I18n.t(:label)
en:
+ button_create_estimate_subtasks: "Create subtasks from estimate"
+ error_estimate_parent_not_found: "Parent task #%{id} wasn't found, or you can't see it."
+ error_estimate_parent_not_allowed: "You aren't allowed to add subtasks in %{project}."
+ label_estimate_parent_placeholder: "Optional"
+ text_estimate_parent_info: "The issues are created as subtasks. They get the parent's customer, and the vehicle and custom field values below start from the parent's; any left empty are filled from the parent."
notice_qbo_oauth_state_mismatch: "QuickBooks connection was not completed: the request could not be verified. Please try connecting again."
permission_bill_issues: "Bill issue time to QuickBooks"
permission_share_issues_with_customers: "Share issues with customers"
@@ -313,6 +318,7 @@ en:
label_default_project: "Default project"
label_default_tracker: "Default tracker"
label_estimate_issue_fields: "Issue fields"
+ label_estimate_issue_dates: "Dates"
label_estimate_issue_settings: "Issues from estimates"
label_estimate_item: "Item"
label_estimate_line_already_created: "Already created"
diff --git a/lib/redmine_qbo/patches/pdf_patch.rb b/lib/redmine_qbo/patches/pdf_patch.rb
index cb4e9de..19503e4 100644
--- a/lib/redmine_qbo/patches/pdf_patch.rb
+++ b/lib/redmine_qbo/patches/pdf_patch.rb
@@ -103,6 +103,9 @@ module RedmineQbo
left << [l(:field_status), issue.status]
left << [l(:field_priority), issue.priority]
left << [l(:field_customer), issue.customer&.name]
+ left << [l(:field_estimate), issue.estimate.doc_number] if issue.estimate
+ invoice_numbers = issue.invoices.map(&:doc_number).compact_blank
+ left << [invoice_numbers.size > 1 ? l(:label_invoices) : l(:field_invoice), invoice_numbers.join(', ')] if invoice_numbers.any?
left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?(:assigned_to_id)
log "Calling :pdf_left hook"
diff --git a/lib/redmine_qbo_billing/estimate_issue_builder.rb b/lib/redmine_qbo_billing/estimate_issue_builder.rb
index 9ccc2ec..dc6a08d 100644
--- a/lib/redmine_qbo_billing/estimate_issue_builder.rb
+++ b/lib/redmine_qbo_billing/estimate_issue_builder.rb
@@ -17,6 +17,9 @@ module RedmineQboBilling
# - Lines already turned into issues are shown but can't be created again
# - Creation is all-or-nothing: if any issue fails validation, none are saved
# - All issues created from the same estimate are related to each other, across batches
+ # - With a parent issue, the issues are created as its subtasks and take its customer, plus its vehicle and
+ # custom field values where the page left them empty (the same rules as the subtask_field_copier plugin,
+ # which is used when installed)
#
# Other plugins fill in more through hooks:
# :qbo_billing_issue_defaults, { estimate:, params: } => a hash of start_date, due_date, assignee,
@@ -91,13 +94,15 @@ module RedmineQboBilling
end
end
- attr_reader :estimate, :defaults, :errors
+ attr_reader :estimate, :defaults, :parent, :errors
# estimate: redmine_qbo's local Estimate (its QuickBooks details give the lines)
# defaults: start_date, due_date, assignee, description and plugins' own values (from :qbo_billing_issue_defaults)
- def initialize(estimate, defaults: {})
+ # parent: an issue to create the issues under as subtasks (optional; the caller checks the user may see it)
+ def initialize(estimate, defaults: {}, parent: nil)
@estimate = estimate
@defaults = (defaults || {}).to_h.symbolize_keys
+ @parent = parent
@errors = []
end
@@ -190,10 +195,12 @@ module RedmineQboBilling
key.to_s
end
- # Relates the new issues to each other and to every issue created earlier from the same estimate.
+ # Relates the new issues to each other and to every issue created earlier from the same estimate,
+ # except their parent and its ancestors (Redmine doesn't allow relating a subtask to its parent)
def relate_issues(new_issues)
+ skip_ids = parent ? parent.self_and_ancestors.pluck(:id) : []
earlier = QboEstimateLineIssue.where(estimate_id: estimate.id)
- .where.not(issue_id: new_issues.map(&:id))
+ .where.not(issue_id: new_issues.map(&:id) + skip_ids)
.includes(:issue)
.filter_map(&:issue)
self.class.relate_all(earlier + new_issues)
@@ -223,15 +230,45 @@ module RedmineQboBilling
assignee = defaults[:assignee]
issue.assigned_to = assignee if assignee && issue.assignable_users.include?(assignee)
- issue.customer_id = estimate.customer_id if issue.respond_to?(:customer_id=)
+ # A subtask belongs to its parent's customer; otherwise the estimate's
+ if issue.respond_to?(:customer_id=)
+ issue.customer_id = parent&.customer_id.presence || estimate.customer_id
+ end
issue.estimate_id = estimate.id if issue.respond_to?(:estimate_id=)
# Plugins set their own fields (e.g. redmine_qbo_vehicles: the vehicle)
Redmine::Hook.call_hook(:qbo_billing_build_issue, { issue: issue, estimate: estimate, defaults: defaults })
+ if parent
+ issue.parent_issue_id = parent.id
+ copy_from_parent(issue, author)
+ end
+
issue
end
+ # Fills the subtask's empty fields from the parent: custom fields (those the author can see on the parent
+ # and edit on the subtask) and plugins' registered fields (customer, vehicle). Values entered on the page win.
+ def copy_from_parent(issue, author)
+ if defined?(::SubtaskFieldCopier)
+ ::SubtaskFieldCopier.copy_from_parent(issue, parent, author, {})
+ return
+ end
+
+ parent_values = parent.visible_custom_field_values(author).index_by(&:custom_field_id)
+ copied = issue.editable_custom_field_values(author).each_with_object({}) do |custom_value, values|
+ value = parent_values[custom_value.custom_field_id]&.value
+ next if Array(value).all?(&:blank?) || Array(custom_value.value).any?(&:present?)
+
+ values[custom_value.custom_field_id.to_s] = value
+ end
+ issue.custom_field_values = copied if copied.any?
+
+ if issue.respond_to?(:vehicle_id=) && issue.vehicle_id.blank? && parent.respond_to?(:vehicle_id)
+ issue.vehicle_id = parent.vehicle_id
+ end
+ end
+
def default_description
defaults[:description].presence ||
"The purpose of this work is to bill the time spent on this issue for Estimate ##{estimate.doc_number}"