Comments & Sync

This commit is contained in:
2016-05-31 07:42:41 -04:00
parent e5147b3893
commit 08b539dc1e

View File

@@ -26,6 +26,7 @@ class Customer < ActiveRecord::Base
return name return name
end end
# Convenience Method
# returns the customer's email # returns the customer's email
def email def email
pull unless @details pull unless @details
@@ -36,6 +37,7 @@ class Customer < ActiveRecord::Base
end end
end end
# Convenience Method
# returns the customer's primary phone # returns the customer's primary phone
def primary_phone def primary_phone
pull unless @details pull unless @details
@@ -46,6 +48,7 @@ class Customer < ActiveRecord::Base
end end
end end
# Convenience Method
# Updates the customer's primary phone number # Updates the customer's primary phone number
def primary_phone=(n) def primary_phone=(n)
pull unless @details pull unless @details
@@ -54,6 +57,7 @@ class Customer < ActiveRecord::Base
@details.primary_phone = pn @details.primary_phone = pn
end end
# Convenience Method
# returns the customer's mobile phone # returns the customer's mobile phone
def mobile_phone def mobile_phone
pull unless @details pull unless @details
@@ -64,6 +68,7 @@ class Customer < ActiveRecord::Base
end end
end end
# Convenience Method
# Updates the custome's mobile phone number # Updates the custome's mobile phone number
def mobile_phone=(n) def mobile_phone=(n)
pull unless @details pull unless @details
@@ -72,6 +77,7 @@ class Customer < ActiveRecord::Base
@details.mobile_phone = pn @details.mobile_phone = pn
end end
# Convenience Method
# Updates Both local DB name & QBO display_name # Updates Both local DB name & QBO display_name
def name=(s) def name=(s)
pull unless @details pull unless @details
@@ -80,46 +86,52 @@ class Customer < ActiveRecord::Base
end end
# Magic Method # Magic Method
def method_missing(n, *arguments) # Maps Get/Set methods to QBO customer object
pull unless @details def method_missing(sym, *arguments)
value = arguments[0]
method_name = n.to_s
# Check to see if the method exists # Check to see if the method exists
if Quickbooks::Model::Customer.method_defined?(n) if Quickbooks::Model::Customer.method_defined?(sym)
# download details if required
pull unless @details
method_name = sym.to_s
# Setter
if method_name[-1, 1] == "=" if method_name[-1, 1] == "="
@details.method(method_name).call(value) @details.method(method_name).call(arguments[0])
# Getter
else else
return @details.method(method_name).call return @details.method(method_name).call
end end
#else
# super
end end
end end
# proforms a bruteforce sync operation # proforms a bruteforce sync operation
# This needs to be simplified # This needs to be simplified
def self.sync def self.sync
last = Qbo.first.last_sync
service = Qbo.get_base(:customer).service service = Qbo.get_base(:customer).service
query = "Select Id, DisplayName From Customer" # Sync ALL customers if the database is empty
query << " Where Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last
if count == 0 if count == 0
customers = service.all customers = service.all
else else
last = Qbo.first.last_sync
query = "Select Id, DisplayName From Customer"
query << " Where Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last
customers = service.query(query) customers = service.query(query)
end end
customers.each do |customer| customers.each do |customer|
qbo_customer = Customer.find_or_create_by(id: customer.id) qbo_customer = Customer.find_or_create_by(id: customer.id)
if customer.active?
if not qbo_customer.name.eql? customer.display_name
qbo_customer.name = customer.display_name qbo_customer.name = customer.display_name
qbo_customer.id = customer.id qbo_customer.id = customer.id
qbo_customer.save_without_push qbo_customer.save_without_push
end end
else
# remove deleted customers if not qbo_customer.new_record?
#where.not(customers.map(&:id)).destroy_all qbo_customer.delete
end
end
end
end end
# Push the updates # Push the updates
@@ -142,11 +154,9 @@ class Customer < ActiveRecord::Base
# pull the details # pull the details
def pull def pull
begin begin
#tries ||= 3
raise Exception unless self.id raise Exception unless self.id
@details = Qbo.get_base(:customer).find_by_id(self.id) @details = Qbo.get_base(:customer).find_by_id(self.id)
rescue Exception => e rescue Exception => e
#retry unless (tries -= 1).zero?
@details = Quickbooks::Model::Customer.new @details = Quickbooks::Model::Customer.new
end end
end end