mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-04-02 15:11:58 -04:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 83a5811a37 | |||
| 35bd41d289 | |||
| a9354026bc | |||
| f9102c1a5d | |||
| 0aef1d0c2b | |||
| ba313dfd02 | |||
| c38bced329 | |||
| 8503c48944 | |||
| 44bfb69e22 | |||
| 89dc3cd5f4 | |||
| 030340b35e | |||
| 097276e558 | |||
| ea73878c71 | |||
| 8ec60c8cd7 | |||
| 9cb38cfbb1 | |||
| 9a1678d353 | |||
| 28957c5dff | |||
| a67b75671e | |||
| 492ff000bf | |||
| e0fc07141c | |||
| 3e6c2672e0 | |||
| 71bde3a249 | |||
| 4f2ae19460 | |||
| 9ee792cbd8 | |||
| ae7ccbf4b0 | |||
| 550601b5c9 | |||
| 85cd02b16a | |||
| 4b8b3c3495 | |||
| b2c1bebea3 | |||
| 2bd13a819b | |||
| d6138843f8 | |||
| 452568e8b3 |
@@ -10,7 +10,6 @@ The goal of this project is to enable vehicle tracking for customer vehicles wit
|
|||||||
* **Parent Plugin:** [Redmine QuickBooks Online](https://github.com/rickbarrette/redmine_qbo)
|
* **Parent Plugin:** [Redmine QuickBooks Online](https://github.com/rickbarrette/redmine_qbo)
|
||||||
|
|
||||||
## Compatibility
|
## Compatibility
|
||||||
|
|
||||||
| Plugin Version | Redmine Version | Ruby Version |
|
| Plugin Version | Redmine Version | Ruby Version |
|
||||||
| :--- | :--- | :--- |
|
| :--- | :--- | :--- |
|
||||||
| 2026.1.2+ | Redmine 6.1 | 3.2+ |
|
| 2026.1.2+ | Redmine 6.1 | 3.2+ |
|
||||||
|
|||||||
@@ -60,10 +60,15 @@ class VehiclesController < ApplicationController
|
|||||||
# display a specific vehicle
|
# display a specific vehicle
|
||||||
def show
|
def show
|
||||||
begin
|
begin
|
||||||
@vehicle = Vehicle.find_by_id(params[:id])
|
@vehicle = Vehicle.includes(issues: [:estimate, :invoices]).find(params[:id])
|
||||||
@vin = @vehicle.vin.scan(/.{1,9}/) if @vehicle.vin
|
@vin = @vehicle.vin.scan(/.{1,9}/) if @vehicle.vin
|
||||||
@issues = @vehicle.issues.order(id: :desc)
|
@issues = @vehicle.issues
|
||||||
@closed_issues = (@issues - @issues.open)
|
.joins(:status)
|
||||||
|
.includes(:estimate, :invoices, :status, :project, :tracker, :priority)
|
||||||
|
.order(id: :desc)
|
||||||
|
@open_issues = @issues.select { |i| !i.status.is_closed }
|
||||||
|
@closed_issues = @issues.select { |i| i.status.is_closed }
|
||||||
|
|
||||||
flash[:error] = t :alert_no_customer if @vehicle.customer.nil?
|
flash[:error] = t :alert_no_customer if @vehicle.customer.nil?
|
||||||
rescue
|
rescue
|
||||||
flash[:error] = t :alert_vehicle_not_found
|
flash[:error] = t :alert_vehicle_not_found
|
||||||
@@ -71,6 +76,23 @@ class VehiclesController < ApplicationController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# used by the polling JS in vehicle#show
|
||||||
|
def status
|
||||||
|
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: {
|
||||||
|
decoded: vehicle.vin_decoded,
|
||||||
|
error: vehicle.error
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
# return an HTML form for editing a vehicle
|
# return an HTML form for editing a vehicle
|
||||||
def edit
|
def edit
|
||||||
begin
|
begin
|
||||||
|
|||||||
55
app/jobs/vehicle_vin_decode_job.rb
Normal file
55
app/jobs/vehicle_vin_decode_job.rb
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#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.
|
||||||
|
|
||||||
|
class VehicleVinDecodeJob < ApplicationJob
|
||||||
|
queue_as :default
|
||||||
|
retry_on StandardError, wait: 5.minutes, attempts: 5
|
||||||
|
|
||||||
|
def perform(vehicle_id)
|
||||||
|
log "Looking up VIN for vehicle ##{vehicle_id}"
|
||||||
|
vehicle = Vehicle.find_by(id: vehicle_id)
|
||||||
|
return unless vehicle&.vin.present?
|
||||||
|
result = VinDecoder.call(vehicle.vin)
|
||||||
|
|
||||||
|
unless result.success?
|
||||||
|
log "Failed to decode vin"
|
||||||
|
vehicle.update(vin_decoded: false, error: result.error)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
details = result.data
|
||||||
|
|
||||||
|
vehicle.update(
|
||||||
|
year: details.year.presence || vehicle.year,
|
||||||
|
make: details.make.presence || vehicle.make,
|
||||||
|
model: details.model.presence || vehicle.model,
|
||||||
|
doors: details.doors.presence || vehicle.doors,
|
||||||
|
trim: details.trim.presence || vehicle.trim,
|
||||||
|
name: build_name(vehicle, details),
|
||||||
|
vin_decoded: true,
|
||||||
|
error: nil
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
Rails.logger.info "[VehicleVinDecodeJob] #{msg}"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -8,99 +8,79 @@
|
|||||||
#
|
#
|
||||||
#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 Vehicle < ActiveRecord::Base
|
class Vehicle < ApplicationRecord
|
||||||
|
include Redmine::Acts::Searchable
|
||||||
|
include Redmine::Acts::Event
|
||||||
|
|
||||||
belongs_to :customer
|
belongs_to :customer
|
||||||
has_many :issues
|
has_many :issues
|
||||||
validates_presence_of :customer
|
validates :customer, presence: true
|
||||||
validates :vin, uniqueness: true
|
validates :vin, presence: true, uniqueness: true
|
||||||
before_save :decode_vin
|
before_validation :normalize_vin
|
||||||
|
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_event :title => Proc.new {|o| "#{o.to_s}"},
|
||||||
|
:url => Proc.new {|o| { :controller => 'vehicles', :action => 'show', :id => o.id} },
|
||||||
|
:type => :to_s,
|
||||||
|
:description => Proc.new {|o| "#{o.vin} - #{o.customer}"},
|
||||||
|
:datetime => Proc.new {|o| o.updated_at || o.created_at}
|
||||||
|
|
||||||
# returns a human readable string
|
|
||||||
def to_s
|
|
||||||
if year.nil? or make.nil? or model.nil?
|
|
||||||
return "#{vin}"
|
|
||||||
else
|
|
||||||
split_vin = vin.scan(/.{1,9}/)
|
|
||||||
return "#{year} #{make} #{model} - #{split_vin[1]}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns the raw JSON details from NHTSA
|
|
||||||
def details
|
|
||||||
get_details if @details.nil?
|
|
||||||
return @details
|
|
||||||
end
|
|
||||||
|
|
||||||
# Force Upper Case for make numbers
|
|
||||||
def make=(val)
|
|
||||||
# The to_s is in case you get nil/non-string
|
|
||||||
write_attribute(:make, val.to_s.titleize)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Force Upper Case for model numbers
|
|
||||||
def model=(val)
|
|
||||||
# The to_s is in case you get nil/non-string
|
|
||||||
write_attribute(:model, val.to_s.titleize)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Force Upper Case & strip VIN of all illegal chars (for barcode scanner)
|
|
||||||
def vin=(val)
|
|
||||||
val = val.to_s.upcase.gsub(/[^A-HJ-NPR-Za-hj-npr-z\d]+/,"")
|
|
||||||
write_attribute(:vin, val)
|
|
||||||
end
|
|
||||||
|
|
||||||
# search for a vehicle by vin, make, model, or year
|
|
||||||
def self.search(query)
|
|
||||||
q = sanitize_sql_like(query)
|
|
||||||
where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%")
|
|
||||||
end
|
|
||||||
|
|
||||||
# decodes a vin and updates self
|
|
||||||
def decode_vin
|
|
||||||
get_details
|
|
||||||
if @details
|
|
||||||
begin
|
|
||||||
self.year = @details.year unless @details.year.nil?
|
|
||||||
self.make = @details.make unless @details.make.nil?
|
|
||||||
self.model = @details.model unless @details.model.nil?
|
|
||||||
self.doors = @details.doors unless @details.doors.nil?
|
|
||||||
self.trim = @details.trim unless @details.trim.nil?
|
|
||||||
rescue Exception => e
|
|
||||||
errors.add(:vin, e.message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
self.name = to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
# reurns all invoices for this vehicle
|
|
||||||
def invoices
|
|
||||||
self.issues.flat_map(&:invoices).uniq.compact
|
|
||||||
end
|
|
||||||
|
|
||||||
# returns all estimates for this vehicle
|
|
||||||
def estimates
|
def estimates
|
||||||
self.issues.flat_map(&:estimate).uniq.compact
|
issues.includes(:estimate).flat_map(&:estimate).uniq.compact
|
||||||
|
end
|
||||||
|
|
||||||
|
def invoices
|
||||||
|
issues.includes(:invoices).flat_map(&:invoices).uniq.compact
|
||||||
|
end
|
||||||
|
|
||||||
|
def make=(val)
|
||||||
|
super(val.to_s.strip)
|
||||||
|
end
|
||||||
|
|
||||||
|
def model=(val)
|
||||||
|
super(val.to_s.strip)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Redmine compatibility shim
|
||||||
|
def project
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.search(query)
|
||||||
|
q = "%#{sanitize_sql_like(query)}%"
|
||||||
|
where( "vin LIKE :q OR make LIKE :q OR model LIKE :q OR year LIKE :q", q: q)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Override the defult redmine seach method to rank results by id
|
||||||
|
def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {})
|
||||||
|
return {} if tokens.blank?
|
||||||
|
scope = self.all
|
||||||
|
tokens.each do |token|
|
||||||
|
scope = scope.search(token)
|
||||||
|
end
|
||||||
|
ids = scope.distinct.limit(options[:limit] || 100).pluck(:id)
|
||||||
|
ids.index_with { |id| id }
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
return vin if year.blank? || make.blank? || model.blank?
|
||||||
|
suffix = vin.to_s[9..] || vin
|
||||||
|
"#{year} #{make} #{model} - #{suffix}"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# init method to pull JSON details from NHTSA
|
def enqueue_vin_decode
|
||||||
def get_details
|
VehicleVinDecodeJob.perform_later(id)
|
||||||
if self.vin?
|
|
||||||
#validate the vin before calling a remote server
|
|
||||||
validation = NhtsaVin.validate(self.vin)
|
|
||||||
begin
|
|
||||||
#if the vin validation failed, raise an exception and exit
|
|
||||||
raise RuntimeError, validation.error unless validation.valid?
|
|
||||||
# query NHTSA for details on the vin
|
|
||||||
query = NhtsaVin.get(self.vin)
|
|
||||||
raise RuntimeError, query.error unless query.valid?
|
|
||||||
@details = query.response
|
|
||||||
rescue Exception => e
|
|
||||||
errors.add(:vin, e.message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize_vin
|
||||||
|
return if vin.blank?
|
||||||
|
cleaned = vin.to_s.upcase.gsub(/[^A-HJ-NPR-Z\d]/, "")
|
||||||
|
self.vin = cleaned
|
||||||
|
end
|
||||||
|
|
||||||
|
def log(msg)
|
||||||
|
Rails.logger.info "[Vehicle] #{msg}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
49
app/services/vin_decoder.rb
Normal file
49
app/services/vin_decoder.rb
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#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.
|
||||||
|
|
||||||
|
class VinDecoder
|
||||||
|
Result = Struct.new(:success?, :data, :error)
|
||||||
|
|
||||||
|
def self.call(vin)
|
||||||
|
new(vin).call
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(vin)
|
||||||
|
@vin = vin
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
log "Decoding VIN"
|
||||||
|
validation = NhtsaVin.validate(@vin)
|
||||||
|
return failure(validation.error) unless validation.valid?
|
||||||
|
|
||||||
|
query = NhtsaVin.get(@vin)
|
||||||
|
return failure(query.error) unless query.valid?
|
||||||
|
|
||||||
|
success(query.response)
|
||||||
|
rescue StandardError => e
|
||||||
|
failure(e.message)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def success(data)
|
||||||
|
Result.new(true, data, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
def failure(error)
|
||||||
|
log "VIN decode failed for #{@vin}: #{error}"
|
||||||
|
Result.new(false, nil, error)
|
||||||
|
end
|
||||||
|
|
||||||
|
def log(msg)
|
||||||
|
Rails.logger.info "[VinDecoder] #{msg}"
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<h4><%=t(:field_vehicles)%>:</h4>
|
<h4><%=t(:field_vehicles)%>:</h4>
|
||||||
<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]), show_customer: false, show_checkbox: false } %>
|
<%= render partial: 'vehicles/list', locals: { vehicles: customer.vehicles.paginate(page: params[:page]), show_customer: false, show_checkbox: true } %>
|
||||||
<div style="float: right;">
|
<div style="float: right;">
|
||||||
<%= button_to t(:button_new_vehicle), new_customer_vehicle_path(customer), method: :get %>
|
<%= button_to t(:button_new_vehicle), new_customer_vehicle_path(customer), method: :get %>
|
||||||
</div>
|
</div>
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
<div class="clearfix">
|
<div class="clearfix">
|
||||||
<%=t(:field_customer)%>:
|
<%=t(:field_customer)%>:
|
||||||
<div class="input">
|
<div class="input">
|
||||||
<%= f.autocomplete_field :customer, autocomplete_customer_name_customers_path, value: @customer.name, update_elements: {id: '#customer_id', value: '#issue_customer'}, required: true %>
|
<%= f.text_field :customer, class: "customer-name", autocomplete: "off", value: @customer.name, required: true,data: { autocomplete_url: "/customers/autocomplete" } %>
|
||||||
<%= f.hidden_field :customer_id, id: "customer_id", value: @customer.id %>
|
<%= f.hidden_field :customer_id, id: "customer_id", value: @customer.id %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
<div class="label-sub">
|
<div class="label-sub">
|
||||||
<div onclick="handleCopy(event)"><%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><b><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%></b></div>
|
<div onclick="handleCopy(event)"><%= vehicle.vin.scan(/.{1,9}/)[0] if vehicle.vin %><b><%=vehicle.vin.scan(/.{1,9}/)[1] if vehicle.vin%></b></div>
|
||||||
<% if show_customer %>
|
<% if show_customer %>
|
||||||
<br/>
|
|
||||||
<%= vehicle.customer %>
|
<%= vehicle.customer %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<%= form_tag(vehicles_path, method: "get", id: "search-form") do %>
|
<%= form_tag(vehicles_path, method: "get", id: "vehicles-search-form") do %>
|
||||||
<%= text_field_tag :search, params[:search], placeholder: t(:label_search), autocomplete: "off" %>
|
<%= text_field_tag :search, params[:search], placeholder: t(:label_search_vehicles), autocomplete: "off" %>
|
||||||
<%= submit_tag t(:label_search) %>
|
<%= submit_tag t(:label_search) %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
<h2><%=t(:field_vehicle)%> #<%=@vehicle.id%></h2>
|
<h2><%=t(:field_vehicle)%> #<%=@vehicle.id%></h2>
|
||||||
|
|
||||||
|
<% unless @vehicle.vin_decoded? %>
|
||||||
|
<div id="vin-status" class="flash <%= @vehicle.error ? "error" : "notice" %>">
|
||||||
|
<% if @vehicle.error%>
|
||||||
|
<%= @vehicle.error %>
|
||||||
|
<% else %>
|
||||||
|
<%= t :notice_decoding_vin %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= render partial: 'vehicles/details', locals: {vehicle: @vehicle} %>
|
<%= render partial: 'vehicles/details', locals: {vehicle: @vehicle} %>
|
||||||
|
|
||||||
<div class="splitcontent">
|
<div class="splitcontent">
|
||||||
@@ -14,10 +24,81 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h3><%=@issues.open.count%> <%=t(:label_open_issues)%></h3>
|
<h3><%=@open_issues.count%> <%=t(:label_open_issues)%></h3>
|
||||||
|
|
||||||
<%= render partial: 'issues/list_simple', locals: {issues: @issues.open} %>
|
<%= render partial: 'issues/list_simple', locals: {issues: @open_issues} %>
|
||||||
|
|
||||||
<h3><%=@closed_issues.count%> <%=t(:label_closed_issues)%></h3>
|
<h3><%=@closed_issues.count%> <%=t(:label_closed_issues)%></h3>
|
||||||
|
|
||||||
<%= render partial: 'issues/list_simple', locals: {issues: (@closed_issues)} %>
|
<%= render partial: 'issues/list_simple', locals: {issues: (@closed_issues)} %>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
const vehicleId = <%= @vehicle.id %>;
|
||||||
|
const alreadyDecoded = <%= @vehicle.vin_decoded? ? 'true' : 'false' %>;
|
||||||
|
|
||||||
|
if (alreadyDecoded) return;
|
||||||
|
|
||||||
|
const interval = 3000;
|
||||||
|
let attempts = 0;
|
||||||
|
const maxAttempts = 40;
|
||||||
|
|
||||||
|
const statusEl = document.getElementById("vin-status");
|
||||||
|
|
||||||
|
const checkStatus = () => {
|
||||||
|
fetch(`/vehicles/${vehicleId}/status`, {
|
||||||
|
headers: { 'Accept': 'application/json' }
|
||||||
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
|
||||||
|
// SUCCESS → reload
|
||||||
|
if (data.decoded) {
|
||||||
|
window.location.reload();
|
||||||
|
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++;
|
||||||
|
if (attempts >= maxAttempts) {
|
||||||
|
clearInterval(timer);
|
||||||
|
|
||||||
|
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 => {
|
||||||
|
clearInterval(timer);
|
||||||
|
|
||||||
|
if (statusEl) {
|
||||||
|
statusEl.classList.remove("notice");
|
||||||
|
statusEl.classList.add("error");
|
||||||
|
statusEl.textContent = "Error checking VIN status.";
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error("Polling error:", err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const timer = setInterval(checkStatus, interval);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
@@ -12,6 +12,11 @@ async function handleCopy(event) {
|
|||||||
link = event.target;
|
link = event.target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the text is already "Copied!", don't do anything
|
||||||
|
if (text == "Copied!") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Write to clipboard
|
// Write to clipboard
|
||||||
await navigator.clipboard.writeText(text);
|
await navigator.clipboard.writeText(text);
|
||||||
|
|||||||
@@ -28,9 +28,10 @@ en:
|
|||||||
label_model: "Model"
|
label_model: "Model"
|
||||||
label_new_vehicle: "New Customer Vehicle"
|
label_new_vehicle: "New Customer Vehicle"
|
||||||
label_no_vehicles: "There are no vehicles containing the term(s)"
|
label_no_vehicles: "There are no vehicles containing the term(s)"
|
||||||
label_search: "Search Vehicles"
|
label_search_vehicles: "Search Vehicles"
|
||||||
label_year: "Year"
|
label_year: "Year"
|
||||||
no_customer: "Customer no longer exists"
|
no_customer: "Customer no longer exists"
|
||||||
|
notice_decoding_vin: "Decoding VIN… this page will update automatically."
|
||||||
notice_vehicle_created: "Vehicle was successfully created."
|
notice_vehicle_created: "Vehicle was successfully created."
|
||||||
notice_vehicle_deleted: "Vehicle was successfully deleted."
|
notice_vehicle_deleted: "Vehicle was successfully deleted."
|
||||||
notice_vehicle_updated: "Vehicle was successfully updated."
|
notice_vehicle_updated: "Vehicle was successfully updated."
|
||||||
@@ -15,4 +15,8 @@ resources :customers do
|
|||||||
end
|
end
|
||||||
|
|
||||||
#allow for just vehicles too
|
#allow for just vehicles too
|
||||||
resources :vehicles
|
resources :vehicles do
|
||||||
|
member do
|
||||||
|
get :status
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
17
db/migrate/003_add_timestamp.rb
Normal file
17
db/migrate/003_add_timestamp.rb
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#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.
|
||||||
|
|
||||||
|
class AddTimestamp < ActiveRecord::Migration[7.0]
|
||||||
|
|
||||||
|
def change
|
||||||
|
add_timestamps(:vehicles, null: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
29
db/migrate/004_add_polling_and_indexes.rb
Normal file
29
db/migrate/004_add_polling_and_indexes.rb
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#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.
|
||||||
|
|
||||||
|
class AddPollingAndIndexes < ActiveRecord::Migration[7.0]
|
||||||
|
|
||||||
|
def change
|
||||||
|
add_column :vehicles, :vin_decoded, :boolean, default: false, null: false
|
||||||
|
add_column :vehicles, :error, :string
|
||||||
|
add_index :vehicles, :vin_decoded
|
||||||
|
add_index :vehicles, :vin, unique: true
|
||||||
|
add_index :vehicles, :make
|
||||||
|
add_index :vehicles, :model
|
||||||
|
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
|
||||||
14
init.rb
14
init.rb
@@ -14,14 +14,14 @@ 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.2.5'
|
version '2026.3.4'
|
||||||
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'
|
||||||
|
|
||||||
# Ensure redmine_qbo is installed
|
# Ensure redmine_qbo is installed
|
||||||
begin
|
begin
|
||||||
requires_redmine_plugin :redmine_qbo, version_or_higher: '2026.1.2'
|
requires_redmine_plugin :redmine_qbo, version_or_higher: '2026.3.13'
|
||||||
rescue Redmine::PluginNotFound
|
rescue Redmine::PluginNotFound
|
||||||
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
raise 'Please install the redmine_qbo plugin (https://github.com/rickbarrette/redmine_qbo)'
|
||||||
end
|
end
|
||||||
@@ -35,12 +35,10 @@ Redmine::Plugin.register :redmine_qbo_vehicles do
|
|||||||
# Register top menu items
|
# Register top menu items
|
||||||
menu :top_menu, :vehicles, { controller: :vehicles, action: :index }, caption: :field_vehicles, if: Proc.new { User.current.logged? }
|
menu :top_menu, :vehicles, { controller: :vehicles, action: :index }, caption: :field_vehicles, if: Proc.new { User.current.logged? }
|
||||||
|
|
||||||
|
Redmine::Search.map do |search|
|
||||||
|
search.register :vehicles
|
||||||
end
|
end
|
||||||
|
|
||||||
# Dynamically load all Hooks & Patches recursively
|
|
||||||
base_dir = File.join(File.dirname(__FILE__), 'lib')
|
|
||||||
|
|
||||||
# '**' looks inside subdirectories, '*.rb' matches Ruby files
|
|
||||||
Dir.glob(File.join(base_dir, '**', '*.rb')).sort.each do |file|
|
|
||||||
require file
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
RedmineQboVehicles.setup
|
||||||
22
lib/redmine_qbo_vehicles.rb
Normal file
22
lib/redmine_qbo_vehicles.rb
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#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 RedmineQboVehicles
|
||||||
|
def self.setup
|
||||||
|
Issue.prepend Vehicles::Patches::IssuePatch
|
||||||
|
Customer.prepend Vehicles::Patches::CustomerPatch
|
||||||
|
|
||||||
|
Vehicles::Hooks::CustomerShowHookListener
|
||||||
|
Vehicles::Hooks::IssuesFormHookListener
|
||||||
|
Vehicles::Hooks::IssuesShowHookListener
|
||||||
|
Vehicles::Hooks::PdfHookListener
|
||||||
|
Vehicles::Hooks::ViewHookListener
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,63 +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={})
|
|
||||||
Rails.logger.info "redmine_qbo_vehicles.process_invoice_custom_fields"
|
|
||||||
issue = context[:issue]
|
|
||||||
|
|
||||||
# update the invoive custom fields with infomation from the issue if available
|
|
||||||
context[:invoice].custom_fields.each do |cf|
|
|
||||||
|
|
||||||
Rails.logger.info "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?
|
|
||||||
|
|
||||||
Rails.logger.info "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
|
|
||||||
Rails.logger.info "VIN has changed"
|
|
||||||
context[:is_changed] = true
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
rescue
|
|
||||||
#do nothing
|
|
||||||
Rails.logger.info "redmine_qbo_vehicles.process_invoice_custom_fields failed, skipping"
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return context if context[:is_changed]
|
|
||||||
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
@@ -14,16 +14,12 @@ module Vehicles
|
|||||||
# Patches Redmine QBO Customer dynamically.
|
# Patches Redmine QBO Customer dynamically.
|
||||||
# Adds a relationship for vehicle ownership by customers
|
# Adds a relationship for vehicle ownership by customers
|
||||||
module CustomerPatch
|
module CustomerPatch
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record) do
|
prepended do
|
||||||
|
|
||||||
Customer.class_eval do
|
|
||||||
has_many :vehicles
|
has_many :vehicles
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -8,23 +8,18 @@
|
|||||||
#
|
#
|
||||||
#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.
|
||||||
|
|
||||||
require_dependency 'issue'
|
|
||||||
|
|
||||||
module Vehicles
|
module Vehicles
|
||||||
module Patches
|
module Patches
|
||||||
|
|
||||||
# Patches Redmine's Issues dynamically.
|
# Patches Redmine's Issues dynamically.
|
||||||
# Adds a relationship for attahcing a vehilce to an issue
|
# Adds a relationship for attahcing a vehilce to an issue
|
||||||
module IssuePatch
|
module IssuePatch
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
ActiveSupport.on_load(:active_record) do
|
prepended do
|
||||||
|
|
||||||
Issue.class_eval do
|
|
||||||
belongs_to :vehicle
|
belongs_to :vehicle
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user