From ad4ce232b56bee50afc324586a44f9547014bde3 Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Sun, 28 Feb 2016 12:07:58 -0500 Subject: [PATCH] Added removal for deleted entery while syncing Added Invoices & Estimates to QBO#Index --- app/controllers/qbo_controller.rb | 5 ++--- app/models/qbo_customer.rb | 14 +++++++++++++- app/models/qbo_employee.rb | 13 ++++++++++++- app/models/qbo_estimate.rb | 13 ++++++++++++- app/models/qbo_invoice.rb | 14 +++++++++++++- app/models/qbo_item.rb | 13 ++++++++++++- app/views/qbo/index.html.erb | 15 ++++++++++++++- 7 files changed, 78 insertions(+), 9 deletions(-) diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index f114aa2..6ea94de 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -19,9 +19,8 @@ class QboController < ApplicationController @qbo_customer_count = QboCustomer.count @qbo_item_count = QboItem.count @qbo_employee_count = QboEmployee.count - @selected_customer - @selected_item - @selected_employee + @qbo_invoice_count = QboInvoice.count + @qbo_estimate_count = QboEstimate.count end # diff --git a/app/models/qbo_customer.rb b/app/models/qbo_customer.rb index e6d817d..a8a69da 100644 --- a/app/models/qbo_customer.rb +++ b/app/models/qbo_customer.rb @@ -24,12 +24,24 @@ class QboCustomer < ActiveRecord::Base end def self.update_all + + service = get_base.service + # Update the customer table - get_base.service.all.each { |customer| + service.all.each { |customer| qbo_customer = QboCustomer.find_or_create_by(id: customer.id) qbo_customer.id = customer.id qbo_customer.name = customer.display_name qbo_customer.save! } + + #remove deleted customers + all.each { |customer| + begin + service.fetch_by_id(customer.id) + rescue + delete_all(id: customer.id) + end + } end end diff --git a/app/models/qbo_employee.rb b/app/models/qbo_employee.rb index 051c8b2..69453bf 100644 --- a/app/models/qbo_employee.rb +++ b/app/models/qbo_employee.rb @@ -19,12 +19,23 @@ class QboEmployee < ActiveRecord::Base end def self.update_all + service = get_base.service + # Update the item table - get_base.service.all.each { |employee| + service.all.each { |employee| qbo_employee = QboEmployee.find_or_create_by(id: employee.id) qbo_employee.name = employee.display_name qbo_employee.id = employee.id qbo_employee.save! } + + #remove deleted employees + all.each { |employee| + begin + service.fetch_by_id(employee.id) + rescue + delete_all(id: employee.id) + end + } end end diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index 1c78c84..724e623 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -19,13 +19,24 @@ class QboEstimate < ActiveRecord::Base end def self.update_all + service = get_base.service + # Update the item table - get_base.service.all.each { |estimate| + service.all.each { |estimate| qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) qbo_estimate.doc_number = estimate.doc_number qbo_estimate.id = estimate.id qbo_estimate.save! } + + #remove deleted estimates + all.each { |estimate| + begin + service.fetch_by_id(estimate.id) + rescue + delete_all(id: estimate.id) + end + } end def self.update(id) diff --git a/app/models/qbo_invoice.rb b/app/models/qbo_invoice.rb index 2f1af1f..9fce03b 100644 --- a/app/models/qbo_invoice.rb +++ b/app/models/qbo_invoice.rb @@ -19,13 +19,25 @@ class QboInvoice < ActiveRecord::Base end def self.update_all + service = get_base.service + + # Update the item table - get_base.service.all.each { |invoice| + service.all.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 + all.each { |invoice| + begin + service.fetch_by_id(invoice.id) + rescue + delete_all(id: invoice.id) + end + } end def self.update(id) diff --git a/app/models/qbo_item.rb b/app/models/qbo_item.rb index a376859..626d7f0 100644 --- a/app/models/qbo_item.rb +++ b/app/models/qbo_item.rb @@ -19,12 +19,23 @@ class QboItem < ActiveRecord::Base end def self.update_all + service = get_base.service + # Update the item table - get_base.service.find_by(:type, "Service").each { |item| + service.find_by(:type, "Service").each { |item| qbo_item = QboItem.find_or_create_by(id: item.id) qbo_item.name = item.name qbo_item.id = item.id qbo_item.save! } + + #remove deleted items + all.each { |item| + begin + service.fetch_by_id(item.id) + rescue + delete_all(id: item.id) + end + } end end diff --git a/app/views/qbo/index.html.erb b/app/views/qbo/index.html.erb index 989210b..e9d1130 100644 --- a/app/views/qbo/index.html.erb +++ b/app/views/qbo/index.html.erb @@ -34,8 +34,21 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
<%= f.select :qbo_employee_id, QboEmployee.all.pluck(:name, :id), :selected => @selected_employee, include_blank: true %> - <% end %> + +

+ <%= f.label "Invoice Count: "+@qbo_invoice_count.to_s %> +
+ <%=f.select :qbo_invoice_id, QboInvoice.all.pluck(:doc_number, :id).sort! {|x, y| y <=> x}, :selected => @selected_invoice, include_blank: true%> +

+

+ <%= f.label "Estimate Count: "+@qbo_estimate_count.to_s %> +
+ <%=f.select :qbo_estimate_id, QboEstimate.all.pluck(:doc_number, :id).sort! {|x, y| y <=> x}, :selected => @selected_estimate, include_blank: true%> +

+ + + <% end %>

<%= link_to "Sync", qbo_sync_path %>