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:
rickyandClaude Opus 5.5 committed 2026-11-21 21:11:00 -05:00
1 parent 0aa95918f6
commit 7894e09f05
13 files changed
+603 -20

No files matched your search

@@ -247,4 +247,35 @@ class QboEstimatesControllerTest < Redmine::IntegrationTest
post '/qbo_estimates/598/create_issues', params: { project_id: 1, lines: {} }
assert_response :unprocessable_entity
end
def test_issues_preview_suggests_templates_and_lists_parts
Estimate.where(id: 598).delete_all
Estimate.create!(id: 598, doc_number: '1068', customer_id: 9101)
Estimate.any_instance.stubs(:line_items).returns([
qbo_sales_line(2, 31, 'Sales', 1, 315, description: 'Running boards'),
qbo_sales_line(3, 10, 'Labor', 2, 190, description: 'Install side steps'),
qbo_sales_line(4, 32, 'Sales', 1, 99, description: 'Light bar'),
qbo_sales_line(5, 10, 'Labor', 1, 190, description: 'Install light bar')
])
Setting.stubs(:plugin_redmine_qbo).returns('issue_default_project_id' => '1', 'issue_default_tracker_id' => '1')
option = RedmineQboBilling::IssueTemplateSupport::Option
RedmineQboBilling::IssueTemplateSupport.stubs(:enabled_for?).returns(true)
RedmineQboBilling::IssueTemplateSupport.stubs(:templates_for).returns([
option.new(key: 'project:1', title: 'Default', tracker_id: 1, is_default: true, group: 'project', match_phrases: []),
option.new(key: 'project:2', title: 'Side Steps', tracker_id: 2, is_default: false, group: 'project',
match_phrases: ['side steps', 'running boards'])
])
login_with(:create_estimate_issues, :add_issues)
get '/qbo_estimates/598/issues_preview'
assert_response :success
# Install side steps: matched template and its tracker
assert_select 'select[name="lines[3][tracker_id]"] option[selected][value="2"]'
assert_select 'select[name="lines[3][template]"] option[selected][value="project:2"]'
# Install light bar: no match, the tracker's default template
assert_select 'select[name="lines[5][template]"] option[selected][value="project:1"]'
assert_select '.estimate-line-parts', text: /1 × Sales/
# Parts first, then two labor lines with no subtotal between them
assert_select '.estimate-parts-ambiguous'
end
end
+7
View File
@@ -167,6 +167,13 @@ module QboFakeServiceHelper
line
end
def qbo_subtotal_line(id = nil)
line = Quickbooks::Model::InvoiceLineItem.new
line.id = id.to_s if id
line.detail_type = 'SubTotalLineDetail'
line
end
def qbo_discount_line(amount, percent: nil, account: nil)
line = Quickbooks::Model::InvoiceLineItem.new
line.amount = BigDecimal(amount.to_s)
+89 -1
View File
@@ -97,7 +97,10 @@ class EstimateIssueBuilderTest < ActiveSupport::TestCase
builder = Builder.new(@estimate, defaults: { description: 'From the appointment' })
created = builder.create_issues(project: @project, rows: rows(1, subject: 'Custom subject'), author: @author)
assert_equal 'Custom Subject', created.first.subject
assert_equal 'From the appointment', created.first.description
# The default description, then the line's parts (the brake pads follow the lift kit labor line)
description = created.first.description.gsub("\r\n", "\n")
assert description.start_with?("From the appointment\n")
assert_match '4 × **Brake pads** — Pads', description
end
def test_unassignable_user_is_left_off
@@ -130,4 +133,89 @@ class EstimateIssueBuilderTest < ActiveSupport::TestCase
assert_equal 0, Builder.relate_all(issues)
assert_equal 0, Builder.relate_all([issues.first])
end
# ---- jobs: parts, notes and templates ----
def estimate_with_jobs
subtotal = qbo_subtotal_line
@estimate.stubs(:line_items).returns([
qbo_note_line(20, "Severe play in ball joints\nRecommend ball joints"),
qbo_sales_line(21, 10, 'Labor', 4, 190, description: 'Install Ball Joints'),
qbo_sales_line(22, 30, 'TF JL Ball Joints', 1, 300, description: "Ball joint kit\n3452010"),
subtotal,
qbo_sales_line(23, 31, 'Sales', 1, 315, description: 'Running boards'),
qbo_sales_line(24, 10, 'Labor', 2, 190, description: 'Install side steps')
])
end
def template(key, description, note: '')
RedmineQboBilling::IssueTemplateSupport::Option.new(
key: key, title: key, tracker_id: 1, description: description,
match_phrases: RedmineQboBilling::TemplateMatcher.phrases(note)
)
end
def test_proposals_carry_their_job
estimate_with_jobs
builder = Builder.new(@estimate)
proposal = builder.proposals.first
assert_equal ['TF JL Ball Joints'], proposal.parts.map(&:item_name)
assert_equal ['Running boards'], builder.proposals.last.parts.map(&:description)
assert_not builder.ambiguous_parts?
end
def test_the_description_gets_the_estimate_notes_and_parts
estimate_with_jobs
RedmineQboBilling::IssueTemplateSupport.stubs(:find).returns(template('jl', 'Template body IAW Estimate #{{estimate}}'))
created = Builder.new(@estimate).create_issues(project: @project, rows: rows(21, template: 'jl'), author: @author)
description = created.first.reload.description
assert description.start_with?('Template body IAW Estimate #1068')
assert_match "Severe play in ball joints", description
assert_match "1 × **TF JL Ball Joints** — Ball joint kit; 3452010", description
assert_operator description.index('Template body'), :<, description.index('TF JL Ball Joints')
end
def test_a_template_can_place_the_notes_and_parts
estimate_with_jobs
body = "Findings:\n{{estimate_notes}}\n\nParts:\n{{estimate_parts}}\n\nEnd"
RedmineQboBilling::IssueTemplateSupport.stubs(:find).returns(template('jl', body))
description = Builder.new(@estimate).create_issues(project: @project, rows: rows(21, template: 'jl'), author: @author)
.first.reload.description.gsub("\r\n", "\n")
assert_match(/\AFindings:\n.*Severe play.*\n\nParts:\n.*TF JL Ball Joints.*\n\nEnd\z/m, description)
assert_no_match(/From the estimate/, description)
end
def test_the_default_description_gets_them_too
estimate_with_jobs
description = Builder.new(@estimate).create_issues(project: @project, rows: rows(24), author: @author).first.reload.description
assert_match 'Estimate #1068', description
assert_match 'Running boards', description
end
def test_nothing_is_added_when_a_job_has_no_notes_or_parts
created = Builder.new(@estimate).create_issues(project: @project, rows: rows(3), author: @author)
assert_no_match(/From the estimate/, created.first.reload.description)
end
def test_suggested_template_uses_the_line_and_its_parts
estimate_with_jobs
templates = [template('jk', 'JK', note: 'Match: ball joints, tf jk ball joints'),
template('jl', 'JL', note: 'Match: ball joints, tf jl ball joints'),
template('steps', 'Steps', note: 'Match: side steps, running boards')]
builder = Builder.new(@estimate)
assert_equal %w[jl steps], builder.proposals.map { |p| builder.suggested_template(p, templates)&.key }
end
def test_a_named_labor_service_uses_its_name
Setting.stubs(:plugin_redmine_qbo).returns('labor_item_ids' => %w[10 40])
@estimate.stubs(:line_items).returns([
qbo_sales_line(30, 40, 'Standard Alignment', 1, 200, description: "• Load vehicle onto machine\n• Print out alignment")
])
builder = Builder.new(@estimate)
proposal = builder.proposals.first
assert_equal 'Standard Alignment', proposal.description
assert_equal ["• Load vehicle onto machine\n• Print out alignment"], proposal.notes
templates = [template('align', 'Align', note: 'Match: standard alignment')]
assert_equal 'align', builder.suggested_template(proposal, templates).key
end
end
+96
View File
@@ -0,0 +1,96 @@
#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.
require File.expand_path('../test_helper', __dir__)
# Grouping an estimate's lines into jobs: each labor line with its parts and notes (RedmineQboBilling::EstimateJobs)
class EstimateJobsTest < ActiveSupport::TestCase
include QboFakeServiceHelper
Jobs = RedmineQboBilling::EstimateJobs
def subtotal
qbo_subtotal_line
end
def labor(id, text, hours = 1)
qbo_sales_line(id, 10, 'Labor', hours, 190, description: text)
end
def part(id, name, text, quantity = 1)
qbo_sales_line(id, 20 + id, name, quantity, 50, description: text)
end
def jobs_for(lines)
Jobs.new(lines, labor: ->(ref) { ref.name == 'Labor' })
end
def parts_of(jobs, line_id)
jobs.job(line_id).parts.map(&:item_name)
end
def test_labor_then_parts_then_subtotal
jobs = jobs_for([labor(1, 'Replace Ball Joints', 4), part(2, 'TF JK Ball Joints', "Ball joint kit\n3442002"), subtotal,
labor(3, 'Replace Front Axle Seals'), part(4, 'JK Front Axle Seal', 'Seal 710863', 2),
part(5, '80W-90 Gear Oil', 'Gear oil - quart', 2), subtotal])
assert_equal ['TF JK Ball Joints'], parts_of(jobs, '1')
assert_equal ['JK Front Axle Seal', '80W-90 Gear Oil'], parts_of(jobs, '3')
seal = jobs.job('3').parts.first
assert_equal ['2', 'Seal 710863'], [seal.quantity, seal.description]
assert_not jobs.ambiguous?
end
def test_parts_before_the_labor_line_belong_to_it
jobs = jobs_for([part(1, 'Sales', 'Running boards'), labor(2, 'Install side steps', 2)])
assert_equal ['Sales'], parts_of(jobs, '2')
assert_not jobs.ambiguous?
end
def test_parts_first_jobs_split_by_subtotals
jobs = jobs_for([part(1, 'Sales', 'Coilovers'), labor(2, 'Install coilovers'), subtotal,
part(3, 'Sales', 'Steering kit'), labor(4, 'Install steering')])
assert_equal ['Sales'], parts_of(jobs, '2')
assert_equal ['Steering kit'], jobs.job('4').parts.map(&:description)
end
def test_a_second_labor_line_takes_the_parts_after_it
jobs = jobs_for([labor(1, 'Install Ball Joints', 4), part(2, 'TF JL Ball Joints', 'Kit 3452010'),
labor(3, 'Standard Alignment'), part(4, 'TPMS Sensor', 'Sensor', 4)])
assert_equal ['TF JL Ball Joints'], parts_of(jobs, '1')
assert_equal ['TPMS Sensor'], parts_of(jobs, '3')
assert_not jobs.ambiguous?
end
def test_parts_first_with_no_subtotal_between_jobs_is_ambiguous
jobs = jobs_for([part(1, 'Sales', 'Coilovers'), labor(2, 'Install coilovers'),
part(3, 'Sales', 'Steering kit'), labor(4, 'Install steering')])
assert jobs.ambiguous?
end
def test_notes_at_the_top_are_for_every_job_and_notes_in_a_job_are_its_own
jobs = jobs_for([qbo_note_line(1, '2015 JKU'), qbo_note_line(2, "Severe play in ball joints\nRecommend ball joints"),
labor(3, 'Replace Ball Joints'), part(4, 'Sales', 'Tires'), qbo_note_line(5, 'Take offs, customer provided'),
subtotal, labor(6, 'Standard Alignment')])
assert_equal ['2015 JKU', "Severe play in ball joints\nRecommend ball joints"], jobs.notes
assert_equal ['Take offs, customer provided'], jobs.job('3').notes
assert_equal [], jobs.job('6').notes
end
def test_parts_with_no_labor_line_are_left_out
jobs = jobs_for([part(1, 'Sales', 'Wheels', 4), part(2, 'Sales', 'Tires', 4), subtotal, labor(3, 'Replace Steering')])
assert_equal [], parts_of(jobs, '3')
assert_nil jobs.job('99')
end
def test_whole_number_quantities_drop_the_decimals
jobs = jobs_for([labor(1, 'Install'), part(2, 'Sales', 'Bolts', 6), part(3, 'Sales', 'Wire', 2.5)])
assert_equal %w[6 2.5], jobs.job('1').parts.map(&:quantity)
end
end
+61
View File
@@ -0,0 +1,61 @@
#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.
require File.expand_path('../test_helper', __dir__)
# Picking an issue template from an estimate line's text and part names (RedmineQboBilling::TemplateMatcher)
class TemplateMatcherTest < ActiveSupport::TestCase
Matcher = RedmineQboBilling::TemplateMatcher
Option = RedmineQboBilling::IssueTemplateSupport::Option
def option(key, note)
Option.new(key: key, title: key, tracker_id: 1, match_phrases: Matcher.phrases(note))
end
def setup
@templates = [
option('jk', "Match: ball joints, tf jk ball joints, jk ball joints"),
option('jl', "Match: ball joints, tf jl ball joints, jl ball joints, jt ball joints"),
option('align', "Used for alignments\nmatch: standard alignment, alignment"),
option('brakes', 'Match: front brakes, brake pads'),
option('plain', 'Just a memo, no match line')
]
end
def test_phrases_come_from_a_match_line_in_the_note
assert_equal ['standard alignment', 'alignment'], Matcher.phrases("Used for alignments\nMATCH: Standard Alignment, alignment ,")
assert_equal [], Matcher.phrases('No match line here')
assert_equal [], Matcher.phrases(nil)
end
def test_the_longest_matching_phrase_wins
assert_equal 'jl', Matcher.best(@templates, 'Install Ball Joints', 'TF JL Ball Joints').key
assert_equal 'jk', Matcher.best(@templates, 'Replace Ball Joints', 'TF JK Ball Joints').key
end
def test_ties_go_to_the_first_template
assert_equal 'jk', Matcher.best(@templates, 'Replace Ball Joints').key
end
def test_plurals_and_punctuation_still_match
assert_equal 'brakes', Matcher.best(@templates, 'Replace front brake').key
assert_equal 'brakes', Matcher.best(@templates, 'Brake-pads (front)').key
assert_equal 'align', Matcher.best(@templates, 'STANDARD ALIGNMENT').key
end
def test_whole_words_only
assert_nil Matcher.best(@templates, 'Misalignmentx')
end
def test_no_match
assert_nil Matcher.best(@templates, 'Replace fuel filler')
assert_nil Matcher.best([], 'Standard alignment')
end
end