diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb index 1985a5b..d974e37 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/qbo_estimate.rb @@ -1,6 +1,6 @@ #The MIT License (MIT) # -#Copyright (c) 2016 rick barrette +#Copyright (c) 2017 rick barrette # #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # @@ -10,47 +10,49 @@ class QboEstimate < ActiveRecord::Base unloadable - has_many :issues - belongs_to :customer - attr_accessible :doc_number - validates_presence_of :id, :doc_number, :customer_id + has_and_belongs_to_many :issues + belongs_to :customer + attr_accessible :doc_number, :id + validates_presence_of :doc_number, :id + self.primary_key = :id + + # return the QBO Estimate service def self.get_base - Qbo.get_base(:estimate) + Qbo.get_base(:estimate).service end + # sync all estimates def self.sync - estimates = get_base.service.all - - # Update the item table - transaction do - estimates.each { |estimate| - qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) - qbo_estimate.doc_number = estimate.doc_number - qbo_estimate.customer_id = estimate.customer_ref.value - qbo_estimate.id = estimate.id - qbo_estimate.save! - } - end - + estimates = get_base.all + estimates.each { |estimate| + process_estimate(estimate) + } + #remove deleted estimates where.not(estimates.map(&:id)).destroy_all end + # sync only one estimate def self.sync_by_id(id) - estimate = get_base.service.fetch_by_id(id) - qbo_estimate = QboEstimate.find_or_create_by(id: estimate.id) + process_estimate(get_base.fetch_by_id(id)) + end + + # update an estimate + def self.update(id) + # Update the item table + estimate = get_base.fetch_by_id(id) + qbo_estimate = find_or_create_by(id: id) + qbo_estimate.doc_number = estimate.doc_number + qbo_estimate.save! + end + + # process an estimate into the database + def process_estimate(estimate) + qbo_estimate = find_or_create_by(id: estimate.id) qbo_estimate.doc_number = estimate.doc_number qbo_estimate.customer_id = estimate.customer_ref.value qbo_estimate.id = estimate.id qbo_estimate.save! end - - def self.update(id) - # Update the item table - estimate = get_base.service.fetch_by_id(id) - qbo_estimate = QboEstimate.find_or_create_by(id: id) - qbo_estimate.doc_number = estimate.doc_number - qbo_estimate.save! - end end