diff --git a/README.md b/README.md index 7be4ef8..5cfc33e 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit * **Asset Tracking:** Adds vehicles owned by customers to the system. * **Issue Association:** Allows these vehicles to be attached directly to Redmine issues for better service tracking. +* **Invoices (with [redmine_qbo_billing](https://github.com/rickbarrette/redmine_qbo_billing)):** importing a work order's time or estimate fills the invoice's VIN box with its vehicle's VIN, and an invoice whose work orders are for more than one vehicle isn't saved (one vehicle per invoice). ## Installation diff --git a/config/locales/en.yml b/config/locales/en.yml index e73aed8..eff8d06 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -52,6 +52,7 @@ en: warn_ru_sure: "Are you sure?" alert_vehicle_has_issues: "This vehicle has issues, so it can't be deleted (that would lose its service history). If it was sold, change its customer instead." + error_one_vehicle_per_invoice: "Only one vehicle per invoice: these work orders are for %{vehicles}. Make a separate invoice for each vehicle." error_vehicle_customer_mismatch: "Vehicle %{vehicle} belongs to a different customer. Pick one of this customer's vehicles." error_vehicle_not_found_for_issue: "The selected vehicle no longer exists." error_vin_status_check_failed: "Error checking VIN status." diff --git a/lib/redmine_qbo_vehicles.rb b/lib/redmine_qbo_vehicles.rb index 69d0a1a..7c5e7d1 100644 --- a/lib/redmine_qbo_vehicles.rb +++ b/lib/redmine_qbo_vehicles.rb @@ -19,6 +19,7 @@ module RedmineQboVehicles Vehicles::Hooks::PdfHookListener Vehicles::Hooks::ViewHookListener Vehicles::Hooks::CustomerAppointmentShowHookListener + Vehicles::Hooks::BillingHookListener # If the SubtaskFieldCopier plugin is installed, register our fields if defined?(SubtaskFieldCopier) diff --git a/lib/vehicles/hooks/billing_hook_listener.rb b/lib/vehicles/hooks/billing_hook_listener.rb new file mode 100644 index 0000000..cbeac70 --- /dev/null +++ b/lib/vehicles/hooks/billing_hook_listener.rb @@ -0,0 +1,43 @@ +#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 Vehicles + module Hooks + + # Hooks for redmine_qbo_billing's estimates and invoices + class BillingHookListener < Redmine::Hook::Listener + + # Each work order's VIN, so the invoice's VIN box is filled when its time or estimates are imported + def qbo_billing_issue_values(context = {}) + vins = Array(context[:issues]).each_with_object({}) do |issue, by_issue| + vin = issue.respond_to?(:vehicle) ? issue.vehicle&.vin : nil + by_issue[issue.id] = vin if vin.present? + end + { vin: vins } + end + + # One vehicle per invoice (OSOR SOP): an invoice whose work orders are for more than one vehicle + # isn't saved + def qbo_billing_validate(context = {}) + return nil unless context[:document].to_s == 'invoice' + + vehicles = Array(context[:issues]).filter_map { |issue| issue.respond_to?(:vehicle) ? issue.vehicle : nil }.uniq + return nil if vehicles.size <= 1 + + details = vehicles.map do |vehicle| + numbers = Array(context[:issues]).select { |issue| issue.vehicle == vehicle }.map { |issue| "##{issue.id}" } + "#{vehicle} (#{numbers.join(', ')})" + end + I18n.t(:error_one_vehicle_per_invoice, vehicles: details.join('; ')) + end + + end + end +end