Compare commits

..

11 Commits

Author SHA1 Message Date
ricky cc94401a1f 2026.4.0 2026-04-09 09:32:00 -04:00
ricky 6012434de2 Display year/make/model if name is blank 2026-04-09 09:31:31 -04:00
ricky 83a5811a37 Removed InvoiceHookListener registation 2026-03-29 21:55:41 -04:00
ricky 35bd41d289 2026.3.4 2026-03-28 00:28:11 -04:00
ricky a9354026bc removed InvoiceHookListener as support has been removed from redmine_qbo 2026-03-28 00:27:59 -04:00
ricky f9102c1a5d update vehicles in batches 2026-03-26 22:10:32 -04:00
ricky 0aef1d0c2b Build vehicle name 2026-03-26 12:45:17 -04:00
ricky ba313dfd02 2026.3.3 2026-03-26 08:11:43 -04:00
ricky c38bced329 record errors and decode existing vins on migration 2026-03-26 07:51:38 -04:00
ricky 8503c48944 show all vehicles if search is blank 2026-03-26 07:21:05 -04:00
ricky 44bfb69e22 check if vin is changed to prevent loops 2026-03-26 07:20:19 -04:00
8 changed files with 90 additions and 89 deletions
+11 -1
View File
@@ -76,10 +76,20 @@ class VehiclesController < ApplicationController
end end
end end
# used by the polling JS in vehicle#show
def status def status
vehicle = Vehicle.find(params[:id]) vehicle = Vehicle.find(params[:id])
# decode the vin if it hasn't been done for some reason
unless vehicle.vin_decoded
if vehicle.error.nil?
VehicleVinDecodeJob.perform_later(vehicle)
end
end
render json: { render json: {
decoded: vehicle.vin_decoded decoded: vehicle.vin_decoded,
error: vehicle.error
} }
end end
+13 -3
View File
@@ -20,7 +20,7 @@ class VehicleVinDecodeJob < ApplicationJob
unless result.success? unless result.success?
log "Failed to decode vin" log "Failed to decode vin"
vehicle.update(vin_decoded: false) vehicle.update(vin_decoded: false, error: result.error)
return return
end end
@@ -32,13 +32,23 @@ class VehicleVinDecodeJob < ApplicationJob
model: details.model.presence || vehicle.model, model: details.model.presence || vehicle.model,
doors: details.doors.presence || vehicle.doors, doors: details.doors.presence || vehicle.doors,
trim: details.trim.presence || vehicle.trim, trim: details.trim.presence || vehicle.trim,
name: vehicle.to_s, name: build_name(vehicle, details),
vin_decoded: true vin_decoded: true,
error: nil
) )
end end
private private
def build_name(vehicle, details)
if details.year && details.make && details.model
suffix = vehicle.vin.to_s[9..]
"#{details.year} #{details.make} #{details.model} - #{suffix}"
else
vehicle.vin
end
end
def log(msg) def log(msg)
Rails.logger.info "[VehicleVinDecodeJob] #{msg}" Rails.logger.info "[VehicleVinDecodeJob] #{msg}"
end end
+8 -2
View File
@@ -17,7 +17,7 @@ class Vehicle < ApplicationRecord
validates :customer, presence: true validates :customer, presence: true
validates :vin, presence: true, uniqueness: true validates :vin, presence: true, uniqueness: true
before_validation :normalize_vin before_validation :normalize_vin
after_commit :enqueue_vin_decode after_commit :enqueue_vin_decode, if: :saved_change_to_vin?
acts_as_searchable columns: %w[vin make model year], scope: ->(_context) { all }, date_column: :updated_at acts_as_searchable columns: %w[vin make model year], scope: ->(_context) { all }, date_column: :updated_at
acts_as_event :title => Proc.new {|o| "#{o.to_s}"}, acts_as_event :title => Proc.new {|o| "#{o.to_s}"},
:url => Proc.new {|o| { :controller => 'vehicles', :action => 'show', :id => o.id} }, :url => Proc.new {|o| { :controller => 'vehicles', :action => 'show', :id => o.id} },
@@ -41,13 +41,17 @@ class Vehicle < ApplicationRecord
super(val.to_s.strip) super(val.to_s.strip)
end end
def name
val = self[:name]
val.present? ? val : to_s
end
# Redmine compatibility shim # Redmine compatibility shim
def project def project
nil nil
end end
def self.search(query) def self.search(query)
return none if query.blank?
q = "%#{sanitize_sql_like(query)}%" q = "%#{sanitize_sql_like(query)}%"
where( "vin LIKE :q OR make LIKE :q OR model LIKE :q OR year LIKE :q", q: q) where( "vin LIKE :q OR make LIKE :q OR model LIKE :q OR year LIKE :q", q: q)
end end
@@ -64,7 +68,9 @@ class Vehicle < ApplicationRecord
end end
def to_s def to_s
return self[:name] if self[:name].present?
return vin if year.blank? || make.blank? || model.blank? return vin if year.blank? || make.blank? || model.blank?
suffix = vin.to_s[9..] || vin suffix = vin.to_s[9..] || vin
"#{year} #{make} #{model} - #{suffix}" "#{year} #{make} #{model} - #{suffix}"
end end
+44 -6
View File
@@ -1,8 +1,12 @@
<h2><%=t(:field_vehicle)%> #<%=@vehicle.id%></h2> <h2><%=t(:field_vehicle)%> #<%=@vehicle.id%></h2>
<% unless @vehicle.vin_decoded? %> <% unless @vehicle.vin_decoded? %>
<div id="vin-status" class="flash notice"> <div id="vin-status" class="flash <%= @vehicle.error ? "error" : "notice" %>">
<% if @vehicle.error%>
<%= @vehicle.error %>
<% else %>
<%= t :notice_decoding_vin %> <%= t :notice_decoding_vin %>
<% end %>
</div> </div>
<% end %> <% end %>
@@ -35,9 +39,11 @@
if (alreadyDecoded) return; if (alreadyDecoded) return;
const interval = 3000; // 3 seconds const interval = 3000;
let attempts = 0; let attempts = 0;
const maxAttempts = 40; // ~2 minutes const maxAttempts = 40;
const statusEl = document.getElementById("vin-status");
const checkStatus = () => { const checkStatus = () => {
fetch(`/vehicles/${vehicleId}/status`, { fetch(`/vehicles/${vehicleId}/status`, {
@@ -45,19 +51,51 @@
}) })
.then(res => res.json()) .then(res => res.json())
.then(data => { .then(data => {
// SUCCESS → reload
if (data.decoded) { if (data.decoded) {
window.location.reload(); window.location.reload();
} else { return;
}
// ERROR → stop + show message
if (data.error) {
clearInterval(timer);
if (statusEl) {
statusEl.classList.remove("notice");
statusEl.classList.add("error");
statusEl.textContent = data.error;
}
console.warn("VIN decode failed:", data.error);
return;
}
// continue polling
attempts++; attempts++;
if (attempts >= maxAttempts) { if (attempts >= maxAttempts) {
clearInterval(timer); clearInterval(timer);
console.warn("VIN decode polling timed out");
if (statusEl) {
statusEl.classList.remove("notice");
statusEl.classList.add("warning");
statusEl.textContent = "VIN decode timed out. Please refresh or try again.";
} }
console.warn("VIN decode polling timed out");
} }
}) })
.catch(err => { .catch(err => {
console.error("Polling error:", err);
clearInterval(timer); clearInterval(timer);
if (statusEl) {
statusEl.classList.remove("notice");
statusEl.classList.add("error");
statusEl.textContent = "Error checking VIN status.";
}
console.error("Polling error:", err);
}); });
}; };
@@ -8,15 +8,22 @@
# #
#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. #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.
class AddIndexes < ActiveRecord::Migration[7.0] class AddPollingAndIndexes < ActiveRecord::Migration[7.0]
def change def change
add_column :vehicles, :vin_decoded, :boolean, default: false, null: false add_column :vehicles, :vin_decoded, :boolean, default: false, null: false
add_column :vehicles, :error, :string
add_index :vehicles, :vin_decoded add_index :vehicles, :vin_decoded
add_index :vehicles, :vin, unique: true add_index :vehicles, :vin, unique: true
add_index :vehicles, :make add_index :vehicles, :make
add_index :vehicles, :model add_index :vehicles, :model
add_index :vehicles, :year add_index :vehicles, :year
Vehicle.find_each.with_index do |vehicle, index|
VehicleVinDecodeJob
.set(wait: (index / 50).minutes)
.perform_later(vehicle.id)
end
end end
end end
+1 -1
View File
@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo_vehicles do
name 'Redmine QBO Vehicles plugin' name 'Redmine QBO Vehicles plugin'
author 'Rick Barrette' author 'Rick Barrette'
description 'This is a plugin for Redmine to intergrate with the redmine_qbo plugin to provide vehicle data tracking' description 'This is a plugin for Redmine to intergrate with the redmine_qbo plugin to provide vehicle data tracking'
version '2026.3.2' version '2026.4.0'
url 'https://github.com/rickbarrette/redmine_qbo_vehicles' url 'https://github.com/rickbarrette/redmine_qbo_vehicles'
author_url 'https://barrettefabrication.com' author_url 'https://barrettefabrication.com'
requires_redmine version_or_higher: '6.1.0' requires_redmine version_or_higher: '6.1.0'
-1
View File
@@ -14,7 +14,6 @@ module RedmineQboVehicles
Customer.prepend Vehicles::Patches::CustomerPatch Customer.prepend Vehicles::Patches::CustomerPatch
Vehicles::Hooks::CustomerShowHookListener Vehicles::Hooks::CustomerShowHookListener
Vehicles::Hooks::InvoiceHookListener
Vehicles::Hooks::IssuesFormHookListener Vehicles::Hooks::IssuesFormHookListener
Vehicles::Hooks::IssuesShowHookListener Vehicles::Hooks::IssuesShowHookListener
Vehicles::Hooks::PdfHookListener Vehicles::Hooks::PdfHookListener
@@ -1,69 +0,0 @@
#The MIT License (MIT)
#
#Copyright (c) 2016 - 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
class InvoiceHookListener < Redmine::Hook::ViewListener
include IssuesHelper
# Called by Redmine QBO Invoice
def process_invoice_custom_fields(context={})
log "Processing invoice custom fields for invoice ##{context[:invoice].id}"
issue = context[:issue]
# update the invoive custom fields with infomation from the issue if available
context[:invoice].custom_fields.each do |cf|
log "Checking invoice custom field: #{cf.name}"
# VIN from the attached vehicle
begin
if cf.name.eql? "VIN"
# Only update if blank to prevent infite loops
# TODO check cf_sync_confict flag once implemented
if cf.string_value.to_s.blank?
log "VIN was blank, updating the invoice vin in quickbooks"
vin = context[:issue].vehicle.vin
break if vin.nil?
if not cf.string_value.to_s.eql? vin
cf.string_value = vin.to_s
log "VIN has changed"
context[:is_changed] = true
end
end
end
rescue
#do nothing
log "redmine_qbo_vehicles.process_invoice_custom_fields failed, skipping"
return nil
end
end
return context if context[:is_changed]
return nil
end
private
def log(msg)
Rails.logger.info "[InvoiceHookListener] #{msg}"
end
end
end
end