diff --git a/Gemfile b/Gemfile
index 9924dce..baf0247 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
gem 'quickbooks-ruby'
-gem 'oauth2', '1.4.7'
+gem 'oauth2'
gem 'roxml'
gem 'nhtsa_vin'
gem 'will_paginate'
diff --git a/LICENSE b/LICENSE
index 5ecf62e..ce662ac 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)
-Copyright (c) 2016 - 2022 Rick Barrette
+Copyright (c) 2016 - 2023 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
diff --git a/README.md b/README.md
index 06e0968..51ae0a2 100644
--- a/README.md
+++ b/README.md
@@ -8,7 +8,10 @@ The goal of this project is to allow Redmine to connect with Quickbooks Online t
Note: Although the core functionality is complete, this project is still under development & the master branch may be unstable. Tags should be stable and are recommended
-Use tags Version 1.0.0+ for Redmine 4+ and Version 0.8.1 for Redine 3
+Use tags for the following Redmine Versions
+* Version 2.0.0+ for Redmine 5+
+* Version 1.0.0+ for Redmine 4+
+* Version 0.8.1 for Redine 3
#### Features
* Issues can be assigned to a Customer via drop down in the edit Issue form
diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb
index fcb7142..dae292c 100644
--- a/app/controllers/customers_controller.rb
+++ b/app/controllers/customers_controller.rb
@@ -36,7 +36,7 @@ class CustomersController < ApplicationController
autocomplete :customer, :name, :full => true, :extra_data => [:id]
def allowed_params
- params.require(:customer).permit(:name, :email, :primary_phone, :mobile_phone, :phone_number)
+ params.require(:customer).permit(:name, :email, :primary_phone, :mobile_phone, :phone_number, :notes)
end
# getter method for a customer's vehicles
@@ -93,6 +93,10 @@ class CustomersController < ApplicationController
@billing_address = address_to_s(@customer.billing_address)
@shipping_address = address_to_s(@customer.shipping_address)
@closed_issues = (@issues - @issues.open)
+ @hours = 0
+ @closed_hours = 0
+ @issues.open.each { |i| @hours+= i.total_spent_hours }
+ @closed_issues.each { |i| @closed_hours+= i.total_spent_hours }
rescue
render_404
end
@@ -111,7 +115,7 @@ class CustomersController < ApplicationController
def update
begin
@customer = Customer.find_by_id(params[:id])
- if @customer.update_attributes(allowed_params)
+ if @customer.update(allowed_params)
flash[:notice] = "Customer updated"
redirect_to @customer
else
@@ -214,14 +218,14 @@ class CustomersController < ApplicationController
# format a quickbooks address to a human readable string
def address_to_s (address)
return if address.nil?
- string = address.line1
+ string = address.line1 if address.line1
string << "\n" + address.line2 if address.line2
string << "\n" + address.line3 if address.line3
string << "\n" + address.line4 if address.line4
string << "\n" + address.line5 if address.line5
- string << " " + address.city
- string << ", " + address.country_sub_division_code
- string << " " + address.postal_code
+ string << " " + address.city if address.city
+ string << ", " + address.country_sub_division_code if address.country_sub_division_code
+ string << " " + address.postal_code if address.postal_code
return string
end
diff --git a/app/controllers/estimate_controller.rb b/app/controllers/estimate_controller.rb
index cfab872..fd37dd0 100644
--- a/app/controllers/estimate_controller.rb
+++ b/app/controllers/estimate_controller.rb
@@ -14,16 +14,21 @@ class EstimateController < ApplicationController
before_action :require_user, :unless => proc {|c| session[:token].nil? }
skip_before_action :verify_authenticity_token, :check_if_login_required, :unless => proc {|c| session[:token].nil? }
+
+ def get_estimate
+ estimate = Estimate.find_by_id(params[:id]) if params[:id]
+ estimate = Estimate.find_by_doc_number(params[:search]) if params[:search]
+ return estimate
+ end
#
# Downloads and forwards the estimate pdf
#
def show
- e = Estimate.find_by_id(params[:id]) if params[:id]
- e = Estimate.find_by_doc_number(params[:search]) if params[:search]
+ estimate = get_estimate
begin
- send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf"
+ send_data estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf"
rescue
redirect_to :back, :flash => { :error => "Estimate not found" }
end
@@ -33,11 +38,10 @@ class EstimateController < ApplicationController
# Downloads estimate by document number
#
def doc
- e = Estimate.find_by_doc_number(params[:id]) if params[:id]
- e = Estimate.find_by_doc_number(params[:search]) if params[:search]
+ estimate = get_estimate
begin
- send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf"
+ send_data estimate.pdf, filename: "estimate #{estimate.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf"
rescue
redirect_to :back, :flash => { :error => "Estimate not found" }
end
diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb
index fd9e625..b3446b7 100644
--- a/app/controllers/qbo_controller.rb
+++ b/app/controllers/qbo_controller.rb
@@ -1,6 +1,6 @@
#The MIT License (MIT)
#
-#Copyright (c) 2022 rick barrette
+#Copyright (c) 2023 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:
#
@@ -26,7 +26,7 @@ class QboController < ApplicationController
# Called when the user requests that Redmine to connect to QBO
#
def authenticate
- oauth2_client = Qbo.get_client
+ oauth2_client = Qbo.construct_oauth2_client
callback = Setting.host_name + "/qbo/oauth_callback/"
grant_url = oauth2_client.auth_code.authorize_url(redirect_uri: callback, response_type: "code", state: SecureRandom.hex(12), scope: "com.intuit.quickbooks.accounting")
redirect_to grant_url
@@ -37,7 +37,7 @@ class QboController < ApplicationController
#
def oauth_callback
if params[:state].present?
- oauth2_client = Qbo.get_client
+ oauth2_client = Qbo.construct_oauth2_client
# use the state value to retrieve from your backend any information you need to identify the customer in your system
redirect_uri = Setting.host_name + "/qbo/oauth_callback/"
if resp = oauth2_client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
@@ -47,15 +47,11 @@ class QboController < ApplicationController
# Save the authentication information
qbo = Qbo.new
- qbo.company_id = params[:realmId]
-
- # Generate Access Token & Serialize it into the database
- access_token = OAuth2::AccessToken.new(oauth2_client, resp.token, refresh_token: resp.refresh_token)
- qbo.token = access_token.to_hash
- qbo.expire = 1.hour.from_now.utc
+ qbo.update(oauth2_access_token: resp.token, oauth2_refresh_token: resp.refresh_token, realm_id: params[:realmId])
+ qbo.refresh_token!
if qbo.save!
- redirect_to sync_path, :flash => { :notice => "Successfully connected to Quickbooks" }
+ redirect_to qbo_sync_path, :flash => { :notice => "Successfully connected to Quickbooks" }
else
redirect_to plugin_settings_path(:redmine_qbo), :flash => { :error => "Error" }
end
@@ -145,7 +141,6 @@ class QboController < ApplicationController
if Qbo.exists?
Customer.sync
Invoice.sync
- QboItem.sync
Employee.sync
Estimate.sync
@@ -155,6 +150,6 @@ class QboController < ApplicationController
ActiveRecord::Base.connection.close
end
- redirect_to :home, :flash => { :notice => "Successfully synced to Quickbooks" }
+ redirect_to :home, :flash => { :notice => "Syncing Quickbooks" }
end
end
diff --git a/app/helpers/auth_helper.rb b/app/helpers/auth_helper.rb
index 42b81a2..7959fec 100644
--- a/app/helpers/auth_helper.rb
+++ b/app/helpers/auth_helper.rb
@@ -13,7 +13,7 @@ module AuthHelper
def require_user
return unless session[:token].nil?
if !User.current.logged?
- render :file => "public/401.html.erb", :status => :unauthorized, :layout =>true
+ render_403
end
end
@@ -27,14 +27,14 @@ module AuthHelper
def check_permission(permission)
if !allowed_to?(permission)
- render :file => "public/401.html.erb", :status => :unauthorized, :layout =>true
+ render_403
end
end
def global_check_permission(permission)
if !globaly_allowed_to?(permission)
- render :file => "public/401.html.erb", :status => :unauthorized, :layout =>true
+ render_403
end
end
diff --git a/app/models/concerns/quickbooks_oauth.rb b/app/models/concerns/quickbooks_oauth.rb
new file mode 100644
index 0000000..9e03a82
--- /dev/null
+++ b/app/models/concerns/quickbooks_oauth.rb
@@ -0,0 +1,80 @@
+#The MIT License (MIT)
+#
+#Copyright (c) 2023 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.
+
+module QuickbooksOauth
+ extend ActiveSupport::Concern
+
+ OAUTH_CONSUMER_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
+ OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret']
+
+ #== Instance Methods
+
+ def perform_authenticated_request(&block)
+ attempts = 0
+ begin
+ yield oauth_access_token
+ rescue OAuth2::Error, Quickbooks::AuthorizationFailure => ex
+ Rails.logger.info("QuickbooksOauth.perform: #{ex.message}")
+
+ # to prevent an infinite loop here keep a counter and bail out after N times...
+ attempts += 1
+
+ raise "QuickbooksOauth:ExceededAuthAttempts" if attempts >= 3
+
+ # check if its an invalid_grant first, but assume it is for now
+ refresh_token!
+
+ retry
+ end
+ end
+
+ def refresh_token!
+ t = oauth_access_token
+ refreshed = t.refresh!
+
+ if refreshed.params['x_refresh_token_expires_in'].to_i > 0
+ oauth2_refresh_token_expires_at = Time.now + refreshed.params['x_refresh_token_expires_in'].to_i.seconds
+ else
+ oauth2_refresh_token_expires_at = 100.days.from_now
+ end
+
+ update!(
+ oauth2_access_token: refreshed.token,
+ oauth2_access_token_expires_at: Time.at(refreshed.expires_at),
+ oauth2_refresh_token: refreshed.refresh_token,
+ oauth2_refresh_token_expires_at: oauth2_refresh_token_expires_at
+ )
+ end
+
+ def oauth_client
+ self.class.construct_oauth2_client
+ end
+
+ def oauth_access_token
+ OAuth2::AccessToken.new(oauth_client, oauth2_access_token, refresh_token: oauth2_refresh_token)
+ end
+
+ def consumer
+ oauth_access_token
+ end
+
+ module ClassMethods
+
+ def construct_oauth2_client
+ options = {
+ site: "https://appcenter.intuit.com/connect/oauth2",
+ authorize_url: "https://appcenter.intuit.com/connect/oauth2",
+ token_url: "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
+ }
+ OAuth2::Client.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, options)
+ end
+
+ end
+end
diff --git a/app/models/customer.rb b/app/models/customer.rb
index ce9ba02..1314de8 100644
--- a/app/models/customer.rb
+++ b/app/models/customer.rb
@@ -23,7 +23,7 @@ class Customer < ActiveRecord::Base
# returns a human readable string
def to_s
- return name
+ return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}"
end
# Convenience Method
@@ -87,6 +87,13 @@ class Customer < ActiveRecord::Base
#update our locally stored number too
update_mobile_phone_number
end
+
+ # Convenience Method
+ # Sets the notes
+ def notes=(s)
+ pull unless @details
+ @details.notes = s
+ end
# update the localy stored phone number as a plain string with no special chars
def update_phone_number
@@ -147,16 +154,17 @@ class Customer < ActiveRecord::Base
# customers = service.query(query)
#end
- customers.each do |customer|
- customer = Customer.find_or_create_by(id: customer.id)
- if customer.active?
- if not customer.name.eql? customer.display_name
- customer.name = customer.display_name
- customer.id = customer.id
+ customers.each do |c|
+ logger.info "Processing customer #{c.id}"
+ customer = Customer.find_or_create_by(id: c.id)
+ if c.active?
+ if not customer.name.eql? c.display_name
+ customer.name = c.display_name
+ customer.id = c.id
customer.save_without_push
end
else
- if not customer.new_record?
+ if not c.new_record?
customer.delete
end
end
diff --git a/app/models/employee.rb b/app/models/employee.rb
index efc4a1f..3319558 100644
--- a/app/models/employee.rb
+++ b/app/models/employee.rb
@@ -1,6 +1,6 @@
#The MIT License (MIT)
#
-#Copyright (c) 2022 rick barrette
+#Copyright (c) 2023 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:
#
@@ -22,10 +22,11 @@ class Employee < ActiveRecord::Base
transaction do
# Update the item table
- employees.each { |employee|
- employee = find_or_create_by(id: employee.id)
- employee.name = employee.display_name
- employee.id = employee.id
+ employees.each { |e|
+ logger.info "Processing employee #{e.id}"
+ employee = find_or_create_by(id: e.id)
+ employee.name = e.display_name
+ employee.id = e.id
employee.save!
}
end
diff --git a/app/models/estimate.rb b/app/models/estimate.rb
index 4d9bf82..541f4bd 100644
--- a/app/models/estimate.rb
+++ b/app/models/estimate.rb
@@ -50,13 +50,13 @@ class Estimate < ActiveRecord::Base
end
# process an estimate into the database
- def self.process_estimate(estimate)
- logger.info "Processing estimate #{estimate.id}"
- estimate = find_or_create_by(id: estimate.id)
- estimate.doc_number = estimate.doc_number
- estimate.customer_id = estimate.customer_ref.value
- estimate.id = estimate.id
- estimate.txn_date = estimate.txn_date
+ def self.process_estimate(qbo_estimate)
+ logger.info "Processing estimate #{qbo_estimate.id}"
+ estimate = find_or_create_by(id: qbo_estimate.id)
+ estimate.doc_number = qbo_estimate.doc_number
+ estimate.customer_id = qbo_estimate.customer_ref.value
+ estimate.id = qbo_estimate.id
+ estimate.txn_date = qbo_estimate.txn_date
estimate.save!
end
diff --git a/app/models/invoice.rb b/app/models/invoice.rb
index e007354..8f72b32 100644
--- a/app/models/invoice.rb
+++ b/app/models/invoice.rb
@@ -70,26 +70,26 @@ class Invoice < ActiveRecord::Base
end
# processes the invoice into the database
- def self.process_invoice(invoice)
- logger.info "Processing invoice #{invoice.id}"
+ def self.process_invoice(i)
+ logger.info "Processing invoice #{i.id}"
# Load the invoice into the database
- invoice = Invoice.find_or_create_by(id: invoice.id)
- invoice.doc_number = invoice.doc_number
- invoice.id = invoice.id
- invoice.customer_id = invoice.customer_ref
- invoice.txn_date = invoice.txn_date
+ invoice = Invoice.find_or_create_by(id: i.id)
+ invoice.doc_number = i.doc_number
+ invoice.id = i.id
+ invoice.customer_id = i.customer_ref
+ invoice.txn_date = i.txn_date
invoice.save!
# Scan the private notes for hashtags and attach to the applicable issues
- if not invoice.private_note.nil?
- invoice.private_note.scan(/#(\w+)/).flatten.each { |issue|
+ if not i.private_note.nil?
+ i.private_note.scan(/#(\w+)/).flatten.each { |issue|
attach_to_issue(Issue.find_by_id(issue.to_i), invoice)
}
end
# Scan the line items for hashtags and attach to the applicable issues
- invoice.line_items.each { |line|
+ i.line_items.each { |line|
if line.description
line.description.scan(/#(\w+)/).flatten.each { |issue|
attach_to_issue(Issue.find_by_id(issue.to_i), invoice)
diff --git a/app/models/qbo.rb b/app/models/qbo.rb
index 09f3515..66c9984 100644
--- a/app/models/qbo.rb
+++ b/app/models/qbo.rb
@@ -10,85 +10,43 @@
class Qbo < ActiveRecord::Base
unloadable
- validates_presence_of :token, :company_id, :expire
- serialize :token
-
- OAUTH_CONSUMER_KEY = Setting.plugin_redmine_qbo['settingsOAuthConsumerKey']
- OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret']
-
- #
- # Getter for quickbooks OAuth2 client
- #
- def self.get_client
- oauth_params = {
- site: "https://appcenter.intuit.com/connect/oauth2",
- authorize_url: "https://appcenter.intuit.com/connect/oauth2",
- token_url: "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
- }
- return OAuth2::Client.new(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, oauth_params)
- end
-
- #
- # Getter for oauth consumer
- #
- def self.get_oauth_consumer
- # Quickbooks Config Info
- return $qb_oauth_consumer
- end
+
+ include QuickbooksOauth
#
# Get a quickbooks base service object for type
# @params type of base
#
def self.get_base(type)
- # lets getnourbold access token from the database
- oauth2_client = get_client
qbo = self.first
- access_token = OAuth2::AccessToken.from_hash(oauth2_client, qbo.token)
- # check to see if we need to refresh the acesstoken
- if qbo.expire.to_time.utc.past?
- puts "Updating access token"
- new_access_token_object = access_token.refresh!
- qbo.token = new_access_token_object.to_hash
- qbo.expire = 1.hour.from_now.utc
- qbo.save!
- access_token = new_access_token_object
- else
- puts "Using current token"
+ qbo.perform_authenticated_request do |access_token|
+ # build the reqiested service
+ case type
+ when :time_activity
+ return Quickbooks::Service::TimeActivity.new(:company_id => qbo.realm_id, :access_token => access_token)
+ when :customer
+ return Quickbooks::Service::Customer.new(:company_id => qbo.realm_id, :access_token => access_token)
+ when :invoice
+ return Quickbooks::Service::Invoice.new(:company_id => qbo.realm_id, :access_token => access_token)
+ when :estimate
+ return Quickbooks::Service::Estimate.new(:company_id => qbo.realm_id, :access_token => access_token)
+ when :employee
+ return Quickbooks::Service::Employee.new(:company_id => qbo.realm_id, :access_token => access_token)
+ when :item
+ return Quickbooks::Service::Item.new(:company_id => qbo.realm_id, :access_token => access_token)
+ else
+ return nil
+ end
end
-
- # build the reqiested service
- case type
- when :item
- return Quickbooks::Service::Item.new(:company_id => qbo.company_id, :access_token => access_token)
- when :time_activity
- return Quickbooks::Service::TimeActivity.new(:company_id => qbo.company_id, :access_token => access_token)
- when :customer
- return Quickbooks::Service::Customer.new(:company_id => qbo.company_id, :access_token => access_token)
- when :invoice
- return Quickbooks::Service::Invoice.new(:company_id => qbo.company_id, :access_token => access_token)
- when :estimate
- return Quickbooks::Service::Estimate.new(:company_id => qbo.company_id, :access_token => access_token)
- when :account
- return Quickbooks::Service::Account.new(:company_id => qbo.company_id, :access_token => access_token)
- when :employee
- return Quickbooks::Service::Employee.new(:company_id => qbo.company_id, :access_token => access_token)
- else
- return access_token
- end
-
- end
-
- # Get the QBO account
- def self.get_account
- first
end
# Updates last sync time stamp
def self.update_time_stamp
+ date = DateTime.now
+ logger.info "Updating QBO timestamp to #{date}"
qbo = Qbo.first
- qbo.last_sync = DateTime.now
+ qbo.last_sync = date
qbo.save
end
diff --git a/app/views/customers/show.html.erb b/app/views/customers/show.html.erb
index 2a7689f..af5d5b7 100644
--- a/app/views/customers/show.html.erb
+++ b/app/views/customers/show.html.erb
@@ -35,8 +35,8 @@
-