Update customer.rb

This commit is contained in:
2016-05-11 12:50:04 -04:00
parent 74c3535335
commit ad1bd78afe

View File

@@ -20,7 +20,8 @@ class Customer < ActiveRecord::Base
validates_presence_of :id, :name validates_presence_of :id, :name
after_initialize :pull after_initialize :pull
before_save :push before_save :create_customer
after_save :push
self.primary_key = :id self.primary_key = :id
@@ -123,46 +124,42 @@ class Customer < ActiveRecord::Base
private private
# pull the details
def pull
if new_record?
@details = Quickbooks::Model::Customer.new
else
begin
tries ||= 3
@details = Qbo.get_base(:customer).find_by_id(self.id)
rescue
retry unless (tries -= 1).zero?
end
end
end
# Push the updates # Push the updates
def push def push
#begin
# tries ||= 3
Qbo.get_base(:customer).service.update(@details)
#rescue Exception => e
# retry unless (tries -= 1).zero?
# errors.add(:details, e.message)
#end
end
def qbo
if new_record?
customer = Quickbooks::Model::Customer.new
customer.display_name = self.name
@details = Qbo.get_base(:customer).service.create(customer)
self.id = @details.id
end
end
# pull details
def pull
begin begin
tries ||= 3 tries ||= 3
@details = Qbo.get_base(:customer).find_by_id(self.id) Qbo.get_base(:customer).service.update(@details)
rescue rescue Exception => e
retry unless (tries -= 1).zero? retry unless (tries -= 1).zero?
errors.add(:details, e.message)
end end
end end
# update's the customers name if updated # Creates a new qbo customer and aquires ID number
def update def create_customer
begin if new_record?
if not self.name == @detils.display_name begin
self.name = @details.display_name tries ||= 3
self.save @details = Qbo.get_base(:customer).service.create(@details)
self.id = @details.id
rescue Exception => e
retry unless (tries -= 1).zero?
errors.add(:id, e.message)
end end
rescue
return nil
end end
end end
end end