diff --git a/app/models/qbo.rb b/app/models/qbo.rb index 0bfbbc9..8b0f5f6 100644 --- a/app/models/qbo.rb +++ b/app/models/qbo.rb @@ -10,11 +10,12 @@ class Qbo < ActiveRecord::Base unloadable - validates_presence_of :token, :secret, :realmId, :token_expires_at, :reconnect_token_at + validates_presence_of :qb_token, :qb_secret, :company_id, :token_expires_at, :reconnect_token_at QB_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey'] QB_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] + # Quickbooks Config Info $qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, { :site => "https://oauth.intuit.com", :request_token_path => "/oauth/v1/get_request_token", @@ -22,16 +23,15 @@ class Qbo < ActiveRecord::Base :access_token_path => "/oauth/v1/get_access_token" }) - def self.get_auth_token - qbo = first - return OAuth::AccessToken.new($qb_oauth_consumer, qbo.token, qbo.secret) - end - - def self.get_oauth_consumer - return $qb_oauth_consumer - end - - def self.get_realm_id - return first.realmId + # Configure quickbooks-ruby-base to access our database + Quickbooks::Base.configure do |c| + c.persistent_token = 'qb_token' + c.persistent_secret = 'qb_secret' + c.persistent_company_id = 'company_id' + end + + # Get the QBO account + def self.get_account + first end end diff --git a/app/models/qbo_customers.rb b/app/models/qbo_customers.rb index 294aeeb..6c0ba50 100644 --- a/app/models/qbo_customers.rb +++ b/app/models/qbo_customers.rb @@ -13,13 +13,18 @@ class QboCustomers < ActiveRecord::Base has_many :issues attr_accessible :name validates_presence_of :id, :name + + def self.get_base + Quickbooks::Base.new(Qbo.get_account, :employee) + end + + def self.get_customer (id) + get_base.service.find_by_id(id) + end def self.update_all - qbo = Qbo.first - service = Quickbooks::Service::Customer.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token) - # Update the customer table - service.all.each { |customer| + get_base.service.all.each { |customer| qbo_customer = QboCustomers.find_or_create_by(id: customer.id) qbo_customer.id = customer.id qbo_customer.name = customer.display_name diff --git a/app/models/qbo_employee.rb b/app/models/qbo_employee.rb index 8ddfad2..d03311e 100644 --- a/app/models/qbo_employee.rb +++ b/app/models/qbo_employee.rb @@ -14,12 +14,13 @@ class QboEmployee < ActiveRecord::Base attr_accessible :name validates_presence_of :id, :name + def self.get_base + Quickbooks::Base.new(Qbo.get_account, :employee) + end + def self.update_all - qbo = Qbo.first - service = Quickbooks::Service::Employee.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token) - # Update the item table - service.all.each { |employee| + get_base.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 diff --git a/app/models/qbo_estimate.rb b/app/models/qbo_estimate.rb new file mode 100644 index 0000000..8da46cc --- /dev/null +++ b/app/models/qbo_estimate.rb @@ -0,0 +1,30 @@ +#The MIT License (MIT) +# +#Copyright (c) 2016 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: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +class QboEstimate < ActiveRecord::Base + unloadable + has_many :issues + attr_accessible :doc_number + validates_presence_of :id, :doc_number + + def self.get_base + Quickbooks::Base.new(Qbo.get_account, :estimate) + end + + def self.update_all + # Update the item table + get_base.service.all.each { |estimate| + qbo_estimate = QboItem.find_or_create_by(id: estimate.id) + qbo_estimate.name = estimate.doc_number + qbo_estimate.id = estimate.id + qbo_estimate.save! + } + end +end \ No newline at end of file diff --git a/app/models/qbo_item.rb b/app/models/qbo_item.rb index e5b2406..054325f 100644 --- a/app/models/qbo_item.rb +++ b/app/models/qbo_item.rb @@ -14,12 +14,13 @@ class QboItem < ActiveRecord::Base attr_accessible :name validates_presence_of :id, :name + def self.get_base + Quickbooks::Base.new(Qbo.get_account, :item) + end + def self.update_all - qbo = Qbo.first - service = Quickbooks::Service::Item.new(:company_id => qbo.realmId, :access_token => Qbo.get_auth_token) - # Update the item table - service.find_by(:type, "Service").each { |item| + get_base.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 diff --git a/lib/issues_save_hook_listener.rb b/lib/issues_save_hook_listener.rb index 9c743b1..3e148ae 100644 --- a/lib/issues_save_hook_listener.rb +++ b/lib/issues_save_hook_listener.rb @@ -21,8 +21,8 @@ class IssuesSaveHookListener < Redmine::Hook::ViewListener # if this is a quote, lets create a new estimate based off estimated hours if issue.tracker.name = "Quote" and issue.status.name = "New" and issue.custom_field_value(CustomField.find_by_name("Estimate").id).empty? then - item_service = Quickbooks::Service::Item.new(:company_id => Qbo.get_realm_id, :access_token => Qbo.get_auth_token) - + item_service = QboItem.get_base.service + estimate = Quickbooks::Model::Estimate .new estimate .customer_id = issue.qbo_customer_id estimate .txn_date = Date.today