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,28 +124,11 @@ class Customer < ActiveRecord::Base
private private
# Push the updates # pull the details
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 def pull
if new_record?
@details = Quickbooks::Model::Customer.new
else
begin begin
tries ||= 3 tries ||= 3
@details = Qbo.get_base(:customer).find_by_id(self.id) @details = Qbo.get_base(:customer).find_by_id(self.id)
@@ -152,17 +136,30 @@ class Customer < ActiveRecord::Base
retry unless (tries -= 1).zero? retry unless (tries -= 1).zero?
end end
end end
end
# update's the customers name if updated # Push the updates
def update def push
begin begin
if not self.name == @detils.display_name tries ||= 3
self.name = @details.display_name Qbo.get_base(:customer).service.update(@details)
self.save rescue Exception => e
end retry unless (tries -= 1).zero?
rescue errors.add(:details, e.message)
return nil
end end
end end
# Creates a new qbo customer and aquires ID number
def create_customer
if new_record?
begin
tries ||= 3
@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
end
end end