diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index e7d6545..5dfe293 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -66,11 +66,11 @@ class QboController < ApplicationController # def sync if Qbo.exists? - Customer.delay.sync - QboItem.delay.sync - QboEmployee.delay.sync - QboEstimate.delay.sync - QboInvoice.delay.sync + Customer.sync + QboItem.sync + QboEmployee.sync + QboEstimate.sync + QboInvoice.sync #QboPurchase.sync # Record the last sync time diff --git a/app/models/customer.rb b/app/models/customer.rb index 96ea08a..71942c3 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -21,12 +21,11 @@ class Customer < ActiveRecord::Base after_initialize :pull - self.primary_key = :id def all without_callback(:initialize, :after, :pull) do - Customer.all.sort + super end end @@ -79,7 +78,7 @@ class Customer < ActiveRecord::Base # Updates Both local DB name & QBO display_name def name=(s) display_name = s - self.name = s + super end # Magic Method @@ -104,11 +103,15 @@ class Customer < ActiveRecord::Base # This needs to be simplified def self.sync last = Qbo.first.last_sync - + service = Qbo.get_base(:customer).service + query = "Select Id, DisplayName From Customer" 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.name = customer.display_name qbo_customer.id = customer.id diff --git a/app/models/qbo_invoice.rb b/app/models/qbo_invoice.rb index 5be68fe..c3c1fb3 100644 --- a/app/models/qbo_invoice.rb +++ b/app/models/qbo_invoice.rb @@ -25,19 +25,19 @@ class QboInvoice < ActiveRecord::Base last = Qbo.first.last_sync 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.all if count == 0 + # Update the invoice table - transaction do - invoices.each { | invoice | - qbo_invoice = find_or_create_by(id: invoice.id) - qbo_invoice.doc_number = invoice.doc_number - qbo_invoice.id = invoice.id - qbo_invoice.save! - } - end + invoices.each { | invoice | + qbo_invoice = find_or_create_by(id: invoice.id) + qbo_invoice.doc_number = invoice.doc_number + qbo_invoice.id = invoice.id + qbo_invoice.save! + } #remove deleted invoices #where.not(invoices.map(&:id)).destroy_all diff --git a/app/models/qbo_item.rb b/app/models/qbo_item.rb index 8a565c9..b8a47b1 100644 --- a/app/models/qbo_item.rb +++ b/app/models/qbo_item.rb @@ -13,29 +13,33 @@ class QboItem < ActiveRecord::Base has_many :issues attr_accessible :name validates_presence_of :id, :name - + + self.primary_key = :id + def self.get_base Qbo.get_base(:item) end def self.sync last = Qbo.first.last_sync - + query = "SELECT Id, Name FROM Item WHERE Type = 'Service' " - query << " AND Metadata.LastUpdatedTime > '#{last}' " if last - - items = get_base.service.query(query) - transaction do - # Update the item table - items.each { |item| - qbo_item = QboItem.find_or_create_by(id: item.id) - qbo_item.name = item.name - qbo_item.id = item.id + query << " AND Metadata.LastUpdatedTime >= '#{last.iso8601}' " if last + + items = get_base.service.query(query) + + items = get_base.service.all if count == 0 + + unless items.count = 0 + items.find_by(:type, "Service").each { |i| + qbo_item = QboItem.find_or_create_by(id: i.id) + qbo_item.name = i.name + qbo_item.id = i.id qbo_item.save } - - QboItem.where.not(items.map(&:id)).destroy_all end + +# QboItem.where.not(items.map(&:id)).destroy_all end end