mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-17 18:20:36 -05:00
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:
1 parent
42461e7899
commit
8a947483db
6 files changed
+190
-21
No files matched your search
@@ -101,7 +101,8 @@ class QboEstimatesController < ApplicationController
|
|||||||
def create_issues
|
def create_issues
|
||||||
@rows = params[:lines].respond_to?(:to_unsafe_h) ? params[:lines].to_unsafe_h : {}
|
@rows = params[:lines].respond_to?(:to_unsafe_h) ? params[:lines].to_unsafe_h : {}
|
||||||
@custom_field_values = submitted_custom_field_values
|
@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?
|
if created.any?
|
||||||
Redmine::Hook.call_hook(:qbo_billing_issues_created, { estimate: @estimate, issues: created, params: params })
|
Redmine::Hook.call_hook(:qbo_billing_issues_created, { estimate: @estimate, issues: created, params: params })
|
||||||
@@ -174,31 +175,97 @@ class QboEstimatesController < ApplicationController
|
|||||||
end
|
end
|
||||||
return render_404 unless @estimate
|
return render_404 unless @estimate
|
||||||
|
|
||||||
# Values plugins' fields posted with the form (e.g. redmine_qbo_vehicles' vehicle) override their defaults
|
load_parent_issue
|
||||||
@defaults = issue_defaults.merge(submitted_plugin_fields)
|
|
||||||
@builder = RedmineQboBilling::EstimateIssueBuilder.new(@estimate, defaults: @defaults)
|
@defaults = build_defaults
|
||||||
@return_path = @defaults[:return_path].presence || edit_qbo_estimate_path(@estimate.id)
|
@return_path = @defaults[:return_path].presence || edit_qbo_estimate_path(@estimate.id)
|
||||||
@return_label = @defaults[:return_label].presence || "#{l(:label_estimate)} ##{@estimate.doc_number}"
|
@return_label = @defaults[:return_label].presence || "#{l(:label_estimate)} ##{@estimate.doc_number}"
|
||||||
@available_projects = Project.allowed_to(User.current, :add_issues).sorted.to_a
|
@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
|
# A project chosen on the page wins, then the parent issue's, then the one from another plugin (e.g. the
|
||||||
# default project from the settings, then the first project the user can add issues to
|
# 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?
|
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])
|
from_hook = @defaults[:project] if @available_projects.include?(@defaults[:project])
|
||||||
default_project = @available_projects.detect { |p| p.id == RedmineQboBilling::EstimateIssueBuilder.default_project_id }
|
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
|
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
|
@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
|
@default_tracker = @trackers.detect { |t| t.id == RedmineQboBilling::EstimateIssueBuilder.default_tracker_id } || @trackers.first
|
||||||
@templates = RedmineQboBilling::IssueTemplateSupport.templates_for(@target_project)
|
@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 ||= {}
|
@rows ||= {}
|
||||||
rescue *QBO_ERRORS => e
|
rescue *QBO_ERRORS => e
|
||||||
flash[:error] = qbo_message(e)
|
flash[:error] = qbo_message(e)
|
||||||
redirect_back_or_default home_path
|
redirect_back_or_default home_path
|
||||||
end
|
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
|
# The first non-empty hash a plugin returns for :qbo_billing_issue_defaults
|
||||||
def issue_defaults
|
def issue_defaults
|
||||||
results = Redmine::Hook.call_hook(:qbo_billing_issue_defaults, { estimate: @estimate, params: params })
|
results = Redmine::Hook.call_hook(:qbo_billing_issue_defaults, { estimate: @estimate, params: params })
|
||||||
|
|||||||
@@ -10,6 +10,12 @@
|
|||||||
<div class="value">
|
<div class="value">
|
||||||
<%= estimate_link %>
|
<%= estimate_link %>
|
||||||
<%= render partial: 'qbo_estimates/estimate_actions', locals: { estimate: issue.estimate } if issue.estimate && RedmineQboBilling.writer? %>
|
<%= 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>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -14,26 +14,58 @@
|
|||||||
</span>
|
</span>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
<%# Target project: changing it reloads the page with that project's trackers and templates %>
|
<%# Target project and parent task: changing either reloads the page (the project's trackers and templates;
|
||||||
<% if @available_projects.size > 1 %>
|
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 %>
|
<%= 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 %>
|
<% carry.each do |key, value| %><%= hidden_field_tag key, value, id: nil %><% end %>
|
||||||
<p>
|
<p>
|
||||||
<label for="project_id"><strong><%= l(:field_project) %>:</strong></label>
|
<label for="project_id"><strong><%= l(:field_project) %>:</strong></label>
|
||||||
|
<% if @available_projects.size > 1 %>
|
||||||
<%= select_tag :project_id,
|
<%= select_tag :project_id,
|
||||||
options_from_collection_for_select(@available_projects, :id, :name, @target_project.id),
|
options_from_collection_for_select(@available_projects, :id, :name, @target_project.id),
|
||||||
onchange: 'this.form.submit();' %>
|
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>
|
</p>
|
||||||
<% end %>
|
<% end %>
|
||||||
<% else %>
|
<noscript><p><%= submit_tag l(:button_apply), name: nil %></p></noscript>
|
||||||
<p><strong><%= l(:field_project) %>:</strong> <%= link_to_project @target_project %></p>
|
|
||||||
<% end %>
|
<% 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">
|
<div id="errorExplanation">
|
||||||
<ul>
|
<ul>
|
||||||
<% @builder.errors.each do |message| %>
|
<% errors.each do |message| %>
|
||||||
<li><%= message %></li>
|
<li><%= message %></li>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</ul>
|
||||||
@@ -63,6 +95,7 @@
|
|||||||
id: 'estimate-issues-form',
|
id: 'estimate-issues-form',
|
||||||
data: { templates: template_data.to_json } do %>
|
data: { templates: template_data.to_json } do %>
|
||||||
<%= hidden_field_tag :project_id, @target_project.id %>
|
<%= 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 %>
|
<% carry.each do |key, value| %><%= hidden_field_tag key, value, id: nil %><% end %>
|
||||||
|
|
||||||
<table class="list estimate-issues-preview">
|
<table class="list estimate-issues-preview">
|
||||||
@@ -136,6 +169,21 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</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
|
<%# 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 %>
|
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 }) %>
|
<% plugin_fields = call_hook(:view_qbo_estimate_issues_form, { estimate: @estimate, defaults: @defaults, project: @target_project }) %>
|
||||||
@@ -234,6 +282,8 @@
|
|||||||
|
|
||||||
var visibleCount = 0;
|
var visibleCount = 0;
|
||||||
fieldBox.querySelectorAll('.estimate-issue-field').forEach(function (field) {
|
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 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; });
|
var required = idList(field.dataset.requiredTrackerIds).some(function (id) { return trackerIds.indexOf(id) >= 0; });
|
||||||
field.style.display = used ? '' : 'none';
|
field.style.display = used ? '' : 'none';
|
||||||
|
|||||||
@@ -11,6 +11,11 @@
|
|||||||
# English strings go here for Rails i18n
|
# English strings go here for Rails i18n
|
||||||
# Usage I18n.t(:label)
|
# Usage I18n.t(:label)
|
||||||
en:
|
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."
|
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_bill_issues: "Bill issue time to QuickBooks"
|
||||||
permission_share_issues_with_customers: "Share issues with customers"
|
permission_share_issues_with_customers: "Share issues with customers"
|
||||||
@@ -313,6 +318,7 @@ en:
|
|||||||
label_default_project: "Default project"
|
label_default_project: "Default project"
|
||||||
label_default_tracker: "Default tracker"
|
label_default_tracker: "Default tracker"
|
||||||
label_estimate_issue_fields: "Issue fields"
|
label_estimate_issue_fields: "Issue fields"
|
||||||
|
label_estimate_issue_dates: "Dates"
|
||||||
label_estimate_issue_settings: "Issues from estimates"
|
label_estimate_issue_settings: "Issues from estimates"
|
||||||
label_estimate_item: "Item"
|
label_estimate_item: "Item"
|
||||||
label_estimate_line_already_created: "Already created"
|
label_estimate_line_already_created: "Already created"
|
||||||
|
|||||||
@@ -103,6 +103,9 @@ module RedmineQbo
|
|||||||
left << [l(:field_status), issue.status]
|
left << [l(:field_status), issue.status]
|
||||||
left << [l(:field_priority), issue.priority]
|
left << [l(:field_priority), issue.priority]
|
||||||
left << [l(:field_customer), issue.customer&.name]
|
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)
|
left << [l(:field_assigned_to), issue.assigned_to] unless issue.disabled_core_fields.include?(:assigned_to_id)
|
||||||
|
|
||||||
log "Calling :pdf_left hook"
|
log "Calling :pdf_left hook"
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ module RedmineQboBilling
|
|||||||
# - Lines already turned into issues are shown but can't be created again
|
# - 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
|
# - 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
|
# - 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:
|
# Other plugins fill in more through hooks:
|
||||||
# :qbo_billing_issue_defaults, { estimate:, params: } => a hash of start_date, due_date, assignee,
|
# :qbo_billing_issue_defaults, { estimate:, params: } => a hash of start_date, due_date, assignee,
|
||||||
@@ -91,13 +94,15 @@ module RedmineQboBilling
|
|||||||
end
|
end
|
||||||
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)
|
# 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)
|
# 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
|
@estimate = estimate
|
||||||
@defaults = (defaults || {}).to_h.symbolize_keys
|
@defaults = (defaults || {}).to_h.symbolize_keys
|
||||||
|
@parent = parent
|
||||||
@errors = []
|
@errors = []
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -190,10 +195,12 @@ module RedmineQboBilling
|
|||||||
key.to_s
|
key.to_s
|
||||||
end
|
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)
|
def relate_issues(new_issues)
|
||||||
|
skip_ids = parent ? parent.self_and_ancestors.pluck(:id) : []
|
||||||
earlier = QboEstimateLineIssue.where(estimate_id: estimate.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)
|
.includes(:issue)
|
||||||
.filter_map(&:issue)
|
.filter_map(&:issue)
|
||||||
self.class.relate_all(earlier + new_issues)
|
self.class.relate_all(earlier + new_issues)
|
||||||
@@ -223,15 +230,45 @@ module RedmineQboBilling
|
|||||||
assignee = defaults[:assignee]
|
assignee = defaults[:assignee]
|
||||||
issue.assigned_to = assignee if assignee && issue.assignable_users.include?(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=)
|
issue.estimate_id = estimate.id if issue.respond_to?(:estimate_id=)
|
||||||
|
|
||||||
# Plugins set their own fields (e.g. redmine_qbo_vehicles: the vehicle)
|
# 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 })
|
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
|
issue
|
||||||
end
|
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
|
def default_description
|
||||||
defaults[:description].presence ||
|
defaults[:description].presence ||
|
||||||
"The purpose of this work is to bill the time spent on this issue for Estimate ##{estimate.doc_number}"
|
"The purpose of this work is to bill the time spent on this issue for Estimate ##{estimate.doc_number}"
|
||||||
|
|||||||
Reference in new issue
Block a user