diff --git a/app/models/vehicle.rb b/app/models/vehicle.rb index 2fa68f6..0bf4fb1 100644 --- a/app/models/vehicle.rb +++ b/app/models/vehicle.rb @@ -12,8 +12,6 @@ class Vehicle < ActiveRecord::Base unloadable - API_KEY = Setting.plugin_redmine_qbo['settingsEdmundsAPIKey'] - belongs_to :customer has_many :issues, :foreign_key => 'vehicles_id' @@ -21,8 +19,6 @@ class Vehicle < ActiveRecord::Base validates_presence_of :customer validates :vin, uniqueness: true - #validates :year, numericality: { only_integer: true } - before_save :decode_vin after_initialize :get_details @@ -30,7 +26,11 @@ class Vehicle < ActiveRecord::Base # returns a human readable string def to_s - return "#{year} #{make} #{model}" + if year.nil? or make.nil? or model.nil? + return "#{vin}" + else + return "#{year} #{make} #{model}" + end end # returns the raw JSON details from EMUNDS @@ -71,8 +71,9 @@ class Vehicle < ActiveRecord::Base # Force Upper Case for VIN numbers def vin=(val) - # The to_s is in case you get nil/non-string - write_attribute(:vin, val.to_s.scan(/^[A-Za-z0-9]+$/).join.upcase) + #strip VIN of all illegal chars (for barcode scanner) + val = val.to_s.upcase.gsub(/[^A-HJ-NPR-Za-hj-npr-z\d]+/,"") + write_attribute(:vin, val) end # search for a vin @@ -97,8 +98,7 @@ class Vehicle < ActiveRecord::Base # returns the Edmunds decoder service def get_decoder - #TODO API Code via Settings - return decoder = Edmunds::Vin.new(API_KEY) + return decoder = Edmunds::Vin.new(Setting.plugin_redmine_qbo['settingsEdmundsAPIKey']) end # decodes a vin and updates self diff --git a/app/views/customers/index.html.erb b/app/views/customers/index.html.erb index d3b3d95..2470aa3 100644 --- a/app/views/customers/index.html.erb +++ b/app/views/customers/index.html.erb @@ -4,6 +4,8 @@ <%= text_field_tag :search, params[:search], placeholder: "Search Customers" %> <%= submit_tag "Search" %> <% end %> +<%= button_to "New Customer", new_customer_path, method: :get%> +<%= button_to "Sync", qbo_sync_path, method: :get%>
<% if @customers.present? %>
diff --git a/lib/issues_form_hook_listener.rb b/lib/issues_form_hook_listener.rb index 1c23784..2772d29 100644 --- a/lib/issues_form_hook_listener.rb +++ b/lib/issues_form_hook_listener.rb @@ -33,7 +33,11 @@ class IssuesFormHookListener < Redmine::Hook::ViewListener customer_id = f.hidden_field :customer_id, :id => "issue_customer_id" if context[:issue].customer - vehicles = customer.vehicles.pluck(:name, :id).sort! + if customer.vehicles + vehicles = customer.vehicles.pluck(:name, :id) + else + vehicles = [nil].compact + end estimates = customer.qbo_estimates.pluck(:doc_number, :id).sort! {|x, y| y <=> x} else vehicles = [nil].compact