Files
Claude 388ac915d7 Handle QuickBooks custom fields generically; no vehicle code left
redmine_qbo now works the same for any business; vehicle behaviour lives in redmine_qbo_vehicles.

- Every enabled QuickBooks text custom field gets a box on the estimate/invoice editor (the sales person
  is still filled in automatically). Forms keep custom_fields { field_id => value } instead of vin and
  mileage; Duplicate and Create invoice copy them all.
- Company: custom_field_matching(pattern) and editable_custom_fields; the VIN and Mileage name patterns
  are gone.
- Hooks are keyed by custom field id: qbo_billing_field_options (box suggestions) and
  qbo_billing_field_defaults (values for a document's work orders) replace the VIN hooks;
  qbo_billing_issue_values returns { field_id => { issue_id => value } }.
- Editor script API for plugins: window.qboEditor, the qbo:customer-changed and qbo:imported events, and
  the view_qbo_estimate_form_bottom hook to add a script. The VIN/mileage filling and the several-vehicle
  warning moved to redmine_qbo_vehicles.
- EstimateWriter.create_estimate(custom_fields:), custom_field_value and custom_field_values replace
  the vin/mileage arguments and vin_for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
2026-10-31 11:34:10 -04:00

85 lines
4.4 KiB
Ruby

#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
# The work orders (Redmine issues) behind an estimate or invoice, and the two hooks other plugins use with
# them:
#
# :qbo_billing_issue_values, { issues: [Issue, ...] }
# Return a hash of QuickBooks custom field id => { issue_id => value }, e.g. redmine_qbo_vehicles returns
# { "2" => { 116 => "1C4HJXDG5JW123456" } } for the VIN field. They're given to the editor's script with
# imported time and estimates (the qbo:imported event), and to other plugins (e.g. redmine_qbo_lineitems'
# estimates); the plugin's own editor script decides how to fill its fields.
#
# :qbo_billing_validate, { document: "invoice" or "estimate", form: EstimateForm, issues: [Issue, ...] }
# Return an error message (or a list of them) to stop the save, e.g. redmine_qbo_vehicles refuses an
# invoice whose work orders are for more than one vehicle. Return nil to allow it.
module IssueValues
# The issue a line came from: redmine_qbo writes "Install #116: some job" on time activities, and
# redmine_qbo_lineitems starts its estimates with "Added from: Install #116: some job"
ISSUE_REFERENCE = /#(\d+):/
ADDED_FROM = /\A\s*Added from:/i
module_function
def issue_reference(text)
text.to_s.lines.first.to_s[ISSUE_REFERENCE, 1]
end
# Issues behind the document: the ones its time lines and "Added from" notes name, and the ones linked
# to its estimates (an invoice's linked estimates, or the estimate itself)
def issue_ids(form)
ids = form.filled_lines.filter_map do |line|
next if line.subtotal?
issue_reference(line.description) if line.time_activity_id.present? || line.description.to_s.match?(ADDED_FROM)
end
estimate_ids = Array(form.linked_estimate_ids).dup
estimate_ids << form.id if !form.invoice? && form.persisted?
ids += Issue.where(estimate_id: estimate_ids.map(&:to_s)).pluck(:id).map(&:to_s) if estimate_ids.any?
ids.map(&:to_s).uniq
end
def issues(form)
ids = issue_ids(form)
ids.empty? ? [] : Issue.where(id: ids).to_a
end
# { "2" => { "116" => "..." } } (custom field id => values by issue) from every plugin listening
def values_for(issues)
issues = Array(issues)
return {} if issues.empty?
Redmine::Hook.call_hook(:qbo_billing_issue_values, { issues: issues }).each_with_object({}) do |result, merged|
next unless result.is_a?(Hash)
result.each do |field, by_issue|
next unless by_issue.is_a?(Hash)
(merged[field.to_s] ||= {}).merge!(by_issue.transform_keys(&:to_s).transform_values(&:to_s).reject { |_, v| v.strip.empty? })
end
end
end
# The same values by issue, for the editor: { "116" => { "2" => "..." } }
def by_issue(issues)
values_for(issues).each_with_object({}) do |(field, by_id), result|
by_id.each { |id, value| (result[id] ||= {})[field] = value }
end
end
# Error messages from plugins that want this save stopped
def plugin_errors(form)
context = { document: form.document, form: form, issues: issues(form) }
Redmine::Hook.call_hook(:qbo_billing_validate, context).flatten.compact.map(&:to_s).reject(&:empty?)
rescue StandardError => e
Rails.logger.warn "[RedmineQboBilling] A plugin's validation failed: #{e.message}"
[]
end
end
end