mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2025-11-08 08:54:23 -05:00
Merge branch 'vehicles' of github.com:rickbarrette/redmine_qbo into vehicles
This commit is contained in:
@@ -10,26 +10,67 @@
|
||||
|
||||
class QboCustomer < ActiveRecord::Base
|
||||
unloadable
|
||||
|
||||
has_many :issues
|
||||
has_many :qbo_purchases
|
||||
has_many :vehicles
|
||||
|
||||
attr_accessible :name
|
||||
validates_presence_of :id, :name
|
||||
|
||||
self.primary_key = "id"
|
||||
after_initialize :get_details
|
||||
|
||||
self.primary_key = :id
|
||||
|
||||
# returns true if the customer is active
|
||||
def active?
|
||||
@details.active? if @details
|
||||
end
|
||||
|
||||
# returns a human readable string
|
||||
def to_s
|
||||
name
|
||||
end
|
||||
|
||||
def get_base
|
||||
# returns the customer's email
|
||||
def email
|
||||
@details.primary_email_address if @details
|
||||
end
|
||||
|
||||
# returns the customer's primary phone
|
||||
def primary_phone
|
||||
@details.primary_phone if @details
|
||||
end
|
||||
|
||||
# returns the customer's mobile phone
|
||||
def mobile_phone
|
||||
@detail.mobile_phone if @details
|
||||
end
|
||||
|
||||
# returns the customer's notes
|
||||
def notes
|
||||
@details.notes if @details
|
||||
end
|
||||
|
||||
# updates the customer's notes in QBO
|
||||
def notes (s)
|
||||
customer = get_customer(self.id)
|
||||
customer.notes = s
|
||||
get_base.update(customer)
|
||||
end
|
||||
|
||||
# returns the bases QBO service for customers
|
||||
def get_base
|
||||
Qbo.get_base(:customer)
|
||||
end
|
||||
|
||||
|
||||
# returns the QBO customer
|
||||
def get_customer (id)
|
||||
get_base.service.find_by_id(id)
|
||||
end
|
||||
|
||||
# proforms a bruteforce sync operation
|
||||
# This needs to be simplified
|
||||
def update_all
|
||||
customers = get_base.service.all
|
||||
|
||||
@@ -37,13 +78,33 @@ class QboCustomer < ActiveRecord::Base
|
||||
# Update the customer table
|
||||
customers.each { |customer|
|
||||
qbo_customer = QboCustomer.find_or_create_by(id: customer.id)
|
||||
qbo_customer.name = customer.display_name
|
||||
qbo_customer.id = customer.id
|
||||
qbo_customer.save!
|
||||
# only update if diffrent
|
||||
if not qbo_customer.name == customer.display_name
|
||||
qbo_customer.name = customer.display_name
|
||||
qbo_customer.id = customer.id
|
||||
qbo_customer.save!
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
#remove deleted customers
|
||||
# remove deleted customers
|
||||
where.not(customers.map(&:id)).destroy_all
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# init details
|
||||
def get_details
|
||||
if self.id
|
||||
@details = get_customer(self.id)
|
||||
update
|
||||
end
|
||||
end
|
||||
|
||||
# update's the customers name
|
||||
def update
|
||||
self.name = @details.display_name
|
||||
self.save
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -19,14 +19,17 @@ class Vehicle < ActiveRecord::Base
|
||||
before_validation :decode_vin
|
||||
after_initialize :get_details
|
||||
|
||||
# returns a human readable string
|
||||
def to_s
|
||||
return "#{self.year} #{self.make} #{self.model}"
|
||||
end
|
||||
|
||||
# returns the raw JSON details from EMUNDS
|
||||
def details
|
||||
return @details
|
||||
end
|
||||
|
||||
# returns the style of the vehicle
|
||||
def style
|
||||
begin
|
||||
return @details['years'][0]['styles'][0]['name'] if @details
|
||||
@@ -35,27 +38,32 @@ class Vehicle < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
# returns the drive of the vehicle i.e. 2 wheel, 4 wheel, ect.
|
||||
def drive
|
||||
return @details['drivenWheels'] if @details
|
||||
end
|
||||
|
||||
# returns the number of doors of the vehicle
|
||||
def doors
|
||||
@details['numOfDoors'] if @details
|
||||
return @details['numOfDoors'] if @details
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# init method to pull JSON details from Edmunds
|
||||
def get_details
|
||||
if self.vin?
|
||||
@details = JSON.parse get_decoder.full(self.vin)
|
||||
end
|
||||
end
|
||||
|
||||
# returns the Edmunds decoder service
|
||||
def get_decoder
|
||||
#TODO API Code via Settings
|
||||
return decoder = Edmunds::Vin.new('2dheutzvhxs28dzukx5tgu47')
|
||||
end
|
||||
|
||||
# decodes a vin and updates self
|
||||
def decode_vin
|
||||
get_details
|
||||
if self.vin?
|
||||
|
||||
@@ -1,16 +1,36 @@
|
||||
<div style="width: 100%; display: table;">
|
||||
<div style="display: table-row">
|
||||
<div style="width: 600px; display: table-cell;">
|
||||
|
||||
<strong>
|
||||
<%= @customer%>
|
||||
</strong>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div style="display: table-cell;">
|
||||
<%= button_to "Edit", edit_customer_path(@customer), method: :get%>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Customer</th>
|
||||
<td><%= @customer.name %></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th>Email</th>
|
||||
<td><%= @customer.email %></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th>Primary Phone</th>
|
||||
<td><%= @customer.primary_phone %></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th>Mobile Phone</th>
|
||||
<td><%= @customer.mobile_phone %></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<th>Notes</th>
|
||||
<td><%= @customer.notes %></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td/>
|
||||
<td>
|
||||
<%= button_to "Edit", edit_vehicle_path(@customer), method: :get%>
|
||||
<%= button_to "Delete", @customer, method: :delete, data: {confirm: "You sure?"} %>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<h1>Customer Detail</h1>
|
||||
<br/>
|
||||
<%= render :partial => 'customers/detail' %>
|
||||
<%= render :partial => 'customers/details' %>
|
||||
<br/>
|
||||
<%= render :partial => 'vehicles/list' %>
|
||||
|
||||
Reference in New Issue
Block a user