Fixed various issues

This commit is contained in:
2016-05-29 10:30:09 -04:00
parent ec1063f80f
commit d829c2f83a
4 changed files with 39 additions and 32 deletions

View File

@@ -66,11 +66,11 @@ class QboController < ApplicationController
# #
def sync def sync
if Qbo.exists? if Qbo.exists?
Customer.delay.sync Customer.sync
QboItem.delay.sync QboItem.sync
QboEmployee.delay.sync QboEmployee.sync
QboEstimate.delay.sync QboEstimate.sync
QboInvoice.delay.sync QboInvoice.sync
#QboPurchase.sync #QboPurchase.sync
# Record the last sync time # Record the last sync time

View File

@@ -21,12 +21,11 @@ class Customer < ActiveRecord::Base
after_initialize :pull after_initialize :pull
self.primary_key = :id self.primary_key = :id
def all def all
without_callback(:initialize, :after, :pull) do without_callback(:initialize, :after, :pull) do
Customer.all.sort super
end end
end end
@@ -79,7 +78,7 @@ class Customer < ActiveRecord::Base
# Updates Both local DB name & QBO display_name # Updates Both local DB name & QBO display_name
def name=(s) def name=(s)
display_name = s display_name = s
self.name = s super
end end
# Magic Method # Magic Method
@@ -104,11 +103,15 @@ class Customer < ActiveRecord::Base
# This needs to be simplified # This needs to be simplified
def self.sync def self.sync
last = Qbo.first.last_sync last = Qbo.first.last_sync
service = Qbo.get_base(:customer).service
query = "Select Id, DisplayName From Customer" query = "Select Id, DisplayName From Customer"
query << " Where Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last query << " Where Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last
Qbo.get_base(:customer).service.query(query).each do |customer| customers = service.query(query)
customers = service.all if count == 0
customers.each do |customer|
qbo_customer = Customer.find_or_create_by(id: customer.id) qbo_customer = Customer.find_or_create_by(id: customer.id)
qbo_customer.name = customer.display_name qbo_customer.name = customer.display_name
qbo_customer.id = customer.id qbo_customer.id = customer.id

View File

@@ -25,19 +25,19 @@ class QboInvoice < ActiveRecord::Base
last = Qbo.first.last_sync last = Qbo.first.last_sync
query = "SELECT Id, DocNumber FROM Invoice" query = "SELECT Id, DocNumber FROM Invoice"
query << " WHERE Metadata.LastUpdatedTime>'#{last}' " if last query << " WHERE Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last
invoices = get_base.service.query() invoices = get_base.service.query()
invoices = get_base.service.all if count == 0
# Update the invoice table # Update the invoice table
transaction do
invoices.each { | invoice | invoices.each { | invoice |
qbo_invoice = find_or_create_by(id: invoice.id) qbo_invoice = find_or_create_by(id: invoice.id)
qbo_invoice.doc_number = invoice.doc_number qbo_invoice.doc_number = invoice.doc_number
qbo_invoice.id = invoice.id qbo_invoice.id = invoice.id
qbo_invoice.save! qbo_invoice.save!
} }
end
#remove deleted invoices #remove deleted invoices
#where.not(invoices.map(&:id)).destroy_all #where.not(invoices.map(&:id)).destroy_all

View File

@@ -14,6 +14,8 @@ class QboItem < ActiveRecord::Base
attr_accessible :name attr_accessible :name
validates_presence_of :id, :name validates_presence_of :id, :name
self.primary_key = :id
def self.get_base def self.get_base
Qbo.get_base(:item) Qbo.get_base(:item)
end end
@@ -22,20 +24,22 @@ class QboItem < ActiveRecord::Base
last = Qbo.first.last_sync last = Qbo.first.last_sync
query = "SELECT Id, Name FROM Item WHERE Type = 'Service' " query = "SELECT Id, Name FROM Item WHERE Type = 'Service' "
query << " AND Metadata.LastUpdatedTime > '#{last}' " if last query << " AND Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last
items = get_base.service.query(query) items = get_base.service.query(query)
transaction do
# Update the item table items = get_base.service.all if count == 0
items.each { |item|
qbo_item = QboItem.find_or_create_by(id: item.id) unless items.count = 0
qbo_item.name = item.name items.find_by(:type, "Service").each { |i|
qbo_item.id = item.id qbo_item = QboItem.find_or_create_by(id: i.id)
qbo_item.name = i.name
qbo_item.id = i.id
qbo_item.save qbo_item.save
} }
QboItem.where.not(items.map(&:id)).destroy_all
end end
# QboItem.where.not(items.map(&:id)).destroy_all
end end
end end