mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-22 11:00:36 -05:00
Build issue descriptions and pick templates from the estimate's jobs
Creating issues from an estimate now groups its lines into jobs (subtotals end a job; parts go with the labor line before them, or the job's first labor line when listed first), adds the estimate's notes and the job's parts to each issue's description, preselects the template whose note has matching "Match:" phrases, and uses a named labor service's name (e.g. Standard Alignment) as the subject. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0aa95918f6
commit
7894e09f05
13 files changed
+603
-20
No files matched your search
@@ -13,7 +13,11 @@ module RedmineQboBilling
|
||||
# on the "Create issues from estimate" page (moved here from redmine_qbo_calendar).
|
||||
#
|
||||
# - Only lines with a labor product/service (LaborItems, e.g. "Labor") are proposed
|
||||
# - Subject = the line's description; estimated hours = the line's quantity
|
||||
# - Subject = the line's description; estimated hours = the line's quantity. For a named labor service
|
||||
# (e.g. "Standard Alignment", where the description is a list of what's included) the subject is the
|
||||
# product/service name and the description goes with the line's notes
|
||||
# - Each line's parts and notes (EstimateJobs) are added to its issue's description (EstimateIssueDetails),
|
||||
# and #suggested_template picks a template from the line and its parts (TemplateMatcher)
|
||||
# - 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
|
||||
@@ -34,7 +38,7 @@ module RedmineQboBilling
|
||||
class EstimateIssueBuilder
|
||||
include Redmine::I18n
|
||||
|
||||
Proposal = Struct.new(:line_id, :item_name, :description, :hours, :existing_issue, keyword_init: true) do
|
||||
Proposal = Struct.new(:line_id, :item_name, :description, :hours, :existing_issue, :parts, :notes, keyword_init: true) do
|
||||
def created?
|
||||
existing_issue.present?
|
||||
end
|
||||
@@ -110,6 +114,21 @@ module RedmineQboBilling
|
||||
@proposals ||= build_proposals
|
||||
end
|
||||
|
||||
# The estimate's lines grouped into jobs (each labor line's parts and notes)
|
||||
def jobs
|
||||
@jobs ||= EstimateJobs.new(estimate.present? ? estimate.line_items : [])
|
||||
end
|
||||
|
||||
# True when parts came first and two jobs had no subtotal between them, so a job's parts may be wrong
|
||||
def ambiguous_parts?
|
||||
jobs.ambiguous?
|
||||
end
|
||||
|
||||
# The template (an IssueTemplateSupport option) whose "Match:" phrases fit the line and its parts, or nil
|
||||
def suggested_template(proposal, templates)
|
||||
TemplateMatcher.best(templates, proposal.item_name, proposal.description, Array(proposal.parts).map(&:item_name))
|
||||
end
|
||||
|
||||
# rows: { line_id => { 'selected' => '1', 'subject' => ..., 'tracker_id' => ..., 'template' => 'global:3', 'estimated_hours' => ... } }
|
||||
# custom_field_values: { custom_field_id => value }, applied to every issue whose tracker uses the field
|
||||
# Returns the created issues, or [] with #errors filled in.
|
||||
@@ -162,8 +181,10 @@ module RedmineQboBilling
|
||||
ids = LaborItems.ids
|
||||
codes = LaborItems.codes
|
||||
links = QboEstimateLineIssue.where(estimate_id: estimate.id).includes(:issue).index_by(&:line_id)
|
||||
line_items = Array(estimate.line_items)
|
||||
@jobs = EstimateJobs.new(line_items, labor: ->(ref) { LaborItems.match?(ref, ids: ids, codes: codes) })
|
||||
|
||||
Array(estimate.line_items).each_with_index.filter_map do |line, index|
|
||||
line_items.each_with_index.filter_map do |line, index|
|
||||
detail = line.respond_to?(:sales_line_item_detail) ? line.sales_line_item_detail : nil
|
||||
next if detail.nil? || detail.item_ref.nil?
|
||||
|
||||
@@ -174,27 +195,29 @@ module RedmineQboBilling
|
||||
description = line.description.to_s.strip
|
||||
next if description.blank?
|
||||
|
||||
line_id = line_key(line, index)
|
||||
line_id = EstimateJobs.line_key(line, index)
|
||||
quantity = detail.respond_to?(:quantity) ? detail.quantity.to_f : 0.0
|
||||
job = @jobs.job(line_id)
|
||||
notes = @jobs.notes + (job ? job.notes : [])
|
||||
|
||||
# A named labor service: its name is the subject, its description (what's included) a note
|
||||
if item_name.present? && !LaborItems.name_matches?(item_name, LaborItems::DEFAULT_CODES)
|
||||
notes += [description]
|
||||
description = item_name.split(':').last.strip
|
||||
end
|
||||
|
||||
Proposal.new(
|
||||
line_id: line_id,
|
||||
item_name: item_name,
|
||||
description: description,
|
||||
hours: quantity.positive? ? quantity : nil,
|
||||
existing_issue: links[line_id]&.issue
|
||||
existing_issue: links[line_id]&.issue,
|
||||
parts: job ? job.parts : [],
|
||||
notes: notes
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# QBO line Ids are stable for the life of the estimate; fall back to line number / position
|
||||
def line_key(line, index)
|
||||
key = line.respond_to?(:id) ? line.id : nil
|
||||
key = line.line_num if key.blank? && line.respond_to?(:line_num)
|
||||
key = "pos-#{index}" if key.blank?
|
||||
key.to_s
|
||||
end
|
||||
|
||||
# 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)
|
||||
@@ -218,7 +241,8 @@ module RedmineQboBilling
|
||||
issue.subject = (row['subject'].presence || proposal.description).to_s.strip.truncate(255)
|
||||
|
||||
template = IssueTemplateSupport.find(row['template'], project)
|
||||
issue.description = template&.description.presence || default_description
|
||||
issue.description = EstimateIssueDetails.apply(template&.description.presence || default_description,
|
||||
notes: proposal.notes, parts: proposal.parts)
|
||||
|
||||
hours = row.key?('estimated_hours') ? row['estimated_hours'] : proposal.hours
|
||||
issue.estimated_hours = hours.presence
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module RedmineQboBilling
|
||||
# Adds an estimate's notes and a job's parts to a new issue's description (issues created from an estimate).
|
||||
#
|
||||
# A template can place them with {{estimate_notes}} and {{estimate_parts}}; otherwise they're added at the
|
||||
# end under "From the estimate". These are filled once, when the issue is created: later saves can't see
|
||||
# the estimate's lines (unlike redmine_qbo's issue macros such as {{estimate}}).
|
||||
module EstimateIssueDetails
|
||||
NOTES = /\{\{\s*estimate_notes\s*\}\}/i
|
||||
PARTS = /\{\{\s*estimate_parts\s*\}\}/i
|
||||
|
||||
module_function
|
||||
|
||||
def apply(text, notes:, parts:)
|
||||
text = text.to_s
|
||||
notes_text = notes_markdown(notes)
|
||||
parts_text = parts_markdown(parts)
|
||||
placed = text.match?(NOTES) || text.match?(PARTS)
|
||||
|
||||
text = text.gsub(NOTES) { notes_text }.gsub(PARTS) { parts_text }
|
||||
return text if placed
|
||||
|
||||
sections = []
|
||||
sections << "**#{I18n.t(:label_estimate_issue_notes)}**\n\n#{notes_text}" if notes_text.present?
|
||||
sections << "**#{I18n.t(:label_estimate_issue_parts)}**\n\n#{parts_text}" if parts_text.present?
|
||||
return text if sections.empty?
|
||||
|
||||
[text.rstrip, '---', "### #{I18n.t(:label_estimate_issue_details)}", *sections].reject(&:empty?).join("\n\n")
|
||||
end
|
||||
|
||||
# Each note as a quote, its lines kept
|
||||
def notes_markdown(notes)
|
||||
Array(notes).map { |note| note.to_s.strip.lines.map { |l| "> #{l.rstrip}" }.join("\n") }.reject(&:empty?).join("\n>\n")
|
||||
end
|
||||
|
||||
# "* 2 × **Item** — description; part number"
|
||||
def parts_markdown(parts)
|
||||
Array(parts).map do |part|
|
||||
description = part.description.to_s.lines.map(&:strip).reject(&:empty?).join('; ')
|
||||
line = "* #{part.quantity.presence || 1} × **#{part.item_name.presence || '—'}**"
|
||||
description.present? ? "#{line} — #{description}" : line
|
||||
end.join("\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,141 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module RedmineQboBilling
|
||||
# Groups an estimate's lines into jobs: each labor line with the parts and notes that go with it, for
|
||||
# the issues created from the estimate.
|
||||
#
|
||||
# - A subtotal line ends a job (so does the end of the estimate)
|
||||
# - A part or note belongs to the labor line before it; parts before the first labor line of a job
|
||||
# belong to that first labor line (estimates written parts first, then labor)
|
||||
# - Notes (description-only lines) before the first item are the estimate's own (e.g. the diagnosis)
|
||||
# and go with every job
|
||||
# - Parts in a job without a labor line (e.g. tires and wheels) belong to no issue
|
||||
#
|
||||
# Parts first with no subtotal between two jobs can't be told apart (the second job's parts follow the
|
||||
# first labor line); #ambiguous? says when that may have happened.
|
||||
class EstimateJobs
|
||||
Part = Struct.new(:quantity, :item_name, :description, keyword_init: true)
|
||||
Job = Struct.new(:line_id, :parts, :notes, keyword_init: true)
|
||||
|
||||
# QBO line Ids are stable for the life of the estimate; fall back to line number / position
|
||||
def self.line_key(line, index)
|
||||
key = line.respond_to?(:id) ? line.id : nil
|
||||
key = line.line_num if key.blank? && line.respond_to?(:line_num)
|
||||
key = "pos-#{index}" if key.blank?
|
||||
key.to_s
|
||||
end
|
||||
|
||||
attr_reader :notes
|
||||
|
||||
# labor: ->(item_ref) { true when the line's product/service is labor }
|
||||
def initialize(line_items, labor: ->(item_ref) { LaborItems.match?(item_ref) })
|
||||
@labor = labor
|
||||
@jobs = {}
|
||||
@notes = []
|
||||
@ambiguous = false
|
||||
group(Array(line_items))
|
||||
end
|
||||
|
||||
def job(line_id)
|
||||
@jobs[line_id.to_s]
|
||||
end
|
||||
|
||||
def ambiguous?
|
||||
@ambiguous
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def group(lines)
|
||||
started = false
|
||||
reset_job
|
||||
|
||||
lines.each_with_index do |line, index|
|
||||
case kind(line)
|
||||
when :subtotal
|
||||
reset_job
|
||||
when :note
|
||||
text = line.description.to_s.strip
|
||||
next if text.empty?
|
||||
|
||||
started ? add(:notes, text) : @notes << text
|
||||
when :labor
|
||||
started = true
|
||||
labor_line(self.class.line_key(line, index))
|
||||
when :part
|
||||
started = true
|
||||
add(:parts, part(line))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def kind(line)
|
||||
type = line.respond_to?(:detail_type) ? line.detail_type.to_s : ''
|
||||
return :subtotal if type == 'SubTotalLineDetail'
|
||||
return :note if type == 'DescriptionOnly'
|
||||
return :part if type == 'GroupLineDetail'
|
||||
|
||||
detail = line.respond_to?(:sales_line_item_detail) ? line.sales_line_item_detail : nil
|
||||
return :ignore if detail.nil? || detail.item_ref.nil?
|
||||
|
||||
@labor.call(detail.item_ref) ? :labor : :part
|
||||
end
|
||||
|
||||
def reset_job
|
||||
@current = nil
|
||||
@labor_count = 0
|
||||
@pending = { parts: [], notes: [] }
|
||||
end
|
||||
|
||||
def labor_line(line_id)
|
||||
job = Job.new(line_id: line_id, parts: [], notes: [])
|
||||
@labor_count += 1
|
||||
if @current.nil?
|
||||
job.parts.concat(@pending[:parts])
|
||||
job.notes.concat(@pending[:notes])
|
||||
elsif @pending[:parts].any?
|
||||
# Parts came first, then a second labor line with no subtotal in between
|
||||
@ambiguous = true
|
||||
end
|
||||
@current = job
|
||||
@jobs[line_id] = job
|
||||
end
|
||||
|
||||
def add(kind, item)
|
||||
if @current
|
||||
@current.send(kind) << item
|
||||
else
|
||||
@pending[kind] << item
|
||||
end
|
||||
end
|
||||
|
||||
def part(line)
|
||||
if line.respond_to?(:detail_type) && line.detail_type.to_s == 'GroupLineDetail'
|
||||
detail = line.group_line_detail
|
||||
return Part.new(quantity: quantity(detail&.quantity), item_name: detail&.group_item_ref&.name.to_s,
|
||||
description: line.description.to_s.strip)
|
||||
end
|
||||
|
||||
detail = line.sales_line_item_detail
|
||||
Part.new(quantity: quantity(detail.quantity), item_name: detail.item_ref.name.to_s.strip,
|
||||
description: line.description.to_s.strip)
|
||||
end
|
||||
|
||||
def quantity(value)
|
||||
return '' if value.nil?
|
||||
|
||||
number = BigDecimal(value.to_s)
|
||||
number == number.to_i ? number.to_i.to_s : number.to_s('F')
|
||||
rescue ArgumentError
|
||||
value.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -12,7 +12,7 @@ module RedmineQboBilling
|
||||
# Optional support for redmine_issue_templates (https://github.com/agileware-jp/redmine_issue_templates).
|
||||
# Everything here returns empty results when that plugin isn't installed or isn't enabled on the project.
|
||||
module IssueTemplateSupport
|
||||
Option = Struct.new(:key, :title, :tracker_id, :is_default, :description, :group, keyword_init: true)
|
||||
Option = Struct.new(:key, :title, :tracker_id, :is_default, :description, :group, :match_phrases, keyword_init: true)
|
||||
|
||||
module_function
|
||||
|
||||
@@ -51,7 +51,8 @@ module RedmineQboBilling
|
||||
tracker_id: template.tracker_id,
|
||||
is_default: template.is_default?,
|
||||
description: template.description,
|
||||
group: group
|
||||
group: group,
|
||||
match_phrases: TemplateMatcher.phrases(template.try(:note))
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#The MIT License (MIT)
|
||||
#
|
||||
#Copyright (c) 2026 rick barrette
|
||||
#
|
||||
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
#
|
||||
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
#
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
module RedmineQboBilling
|
||||
# Picks the issue template for an estimate line. A template opts in with a line in its note
|
||||
# (redmine_issue_templates' "Note" field), e.g.
|
||||
#
|
||||
# Match: ball joints, tf jl ball joints, jl ball joints
|
||||
#
|
||||
# The line's text and its parts' product names are searched for each phrase (whole words, ignoring
|
||||
# case, punctuation and plural "s"). The template with the longest phrase found wins, so
|
||||
# "TF JL Ball Joints" beats "ball joints"; on a tie, the first template in the list.
|
||||
module TemplateMatcher
|
||||
MATCH_LINE = /^\s*match\s*:\s*(.+)$/i
|
||||
|
||||
module_function
|
||||
|
||||
# The phrases from a template's note
|
||||
def phrases(note)
|
||||
line = note.to_s.lines.map { |l| l.match(MATCH_LINE) }.compact.first
|
||||
return [] unless line
|
||||
|
||||
line[1].split(',').map { |phrase| phrase.strip.downcase }.reject(&:empty?).uniq
|
||||
end
|
||||
|
||||
# templates: IssueTemplateSupport options (with match_phrases); texts: the line's description, part names...
|
||||
def best(templates, *texts)
|
||||
haystack = " #{normalize(texts.flatten.compact.join(' '))} "
|
||||
return nil if haystack.strip.empty?
|
||||
|
||||
best, best_length = nil, 0
|
||||
Array(templates).each do |template|
|
||||
Array(template.match_phrases).each do |phrase|
|
||||
words = normalize(phrase)
|
||||
next if words.empty? || words.length <= best_length
|
||||
next unless haystack.include?(" #{words} ")
|
||||
|
||||
best, best_length = template, words.length
|
||||
end
|
||||
end
|
||||
best
|
||||
end
|
||||
|
||||
# Lower case words without punctuation or a plural "s"
|
||||
def normalize(text)
|
||||
text.to_s.downcase.scan(/[a-z0-9]+/).map { |word| word.length > 3 ? word.sub(/s\z/, '') : word }.join(' ')
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user