diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 9dada65..6052483 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -48,13 +48,13 @@ class CustomersController < ApplicationController # getter method for a customer's invoices # used for customer autocomplete field / issue form def filter_invoices_by_customer - @filtered_invoices = QboInvoice.all.where(customer_id: params[:selected_customer]) + @filtered_invoices = Invoice.all.where(customer_id: params[:selected_customer]) end # getter method for a customer's estimates # used for customer autocomplete field / issue form def filter_estimates_by_customer - @filtered_estimates = QboEstimate.all.where(customer_id: params[:selected_customer]) + @filtered_estimates = Estimate.all.where(customer_id: params[:selected_customer]) end # display a list of all customers diff --git a/app/controllers/estimate_controller.rb b/app/controllers/estimate_controller.rb index 11c5e8d..e88bcb8 100644 --- a/app/controllers/estimate_controller.rb +++ b/app/controllers/estimate_controller.rb @@ -18,8 +18,8 @@ class EstimateController < ApplicationController # Downloads and forwards the estimate pdf # def show - e = QboEstimate.find_by_id(params[:id]) if params[:id] - e = QboEstimate.find_by_doc_number(params[:search]) if params[:search] + e = Estimate.find_by_id(params[:id]) if params[:id] + e = Estimate.find_by_doc_number(params[:search]) if params[:search] begin send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" @@ -32,8 +32,8 @@ class EstimateController < ApplicationController # Downloads estimate by document number # def doc - e = QboEstimate.find_by_doc_number(params[:id]) if params[:id] - e = QboEstimate.find_by_doc_number(params[:search]) if params[:search] + e = Estimate.find_by_doc_number(params[:id]) if params[:id] + e = Estimate.find_by_doc_number(params[:search]) if params[:search] begin send_data e.pdf, filename: "estimate #{e.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" diff --git a/app/controllers/invoice_controller.rb b/app/controllers/invoice_controller.rb index 203f1a4..f0a7a58 100644 --- a/app/controllers/invoice_controller.rb +++ b/app/controllers/invoice_controller.rb @@ -19,9 +19,13 @@ class InvoiceController < ApplicationController # Downloads and forwards the invoice pdf # def show - base = QboInvoice.get_base - invoice = base.fetch_by_id(params[:id]) - @pdf = base.pdf(invoice) - send_data @pdf, filename: "invoice #{invoice.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" + begin + base = Invoice.get_base + invoice = base.fetch_by_id(params[:id]) + @pdf = base.pdf(invoice) + send_data @pdf, filename: "invoice #{invoice.doc_number}.pdf", :disposition => 'inline', :type => "application/pdf" + rescue + redirect_to :back, :flash => { :error => "Invoice not found" } + end end end diff --git a/app/controllers/qbo_controller.rb b/app/controllers/qbo_controller.rb index ec06c99..b56fad9 100644 --- a/app/controllers/qbo_controller.rb +++ b/app/controllers/qbo_controller.rb @@ -15,8 +15,8 @@ class QboController < ApplicationController include AuthHelper - before_action :require_user, :except => :qbo_webhook - skip_before_action :verify_authenticity_token, :check_if_login_required, :only => [:qbo_webhook] + before_action :require_user, :except => :webhook + skip_before_action :verify_authenticity_token, :check_if_login_required, :only => [:webhook] def allowed_params params.permit(:code, :state, :realmId, :id) @@ -28,10 +28,10 @@ class QboController < ApplicationController def index @qbo = Qbo.first @customer_count = Customer.count - @qbo_item_count = QboItem.count - @qbo_employee_count = QboEmployee.count - @qbo_invoice_count = QboInvoice.count - @qbo_estimate_count = QboEstimate.count + @item_count = QboItem.count + @employee_count = Employee.count + @invoice_count = Invoice.count + @estimate_count = Estimate.count end # @@ -40,7 +40,6 @@ class QboController < ApplicationController def authenticate oauth2_client = Qbo.get_client callback = Setting.host_name + "/qbo/oauth_callback/" - #callback = qbo_oauth_callback_url 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 end @@ -52,7 +51,6 @@ class QboController < ApplicationController if params[:state].present? oauth2_client = Qbo.get_client # use the state value to retrieve from your backend any information you need to identify the customer in your system - #redirect_uri = qbo_oauth_callback_url redirect_uri = Setting.host_name + "/qbo/oauth_callback/" if resp = oauth2_client.auth_code.get_token(params[:code], redirect_uri: redirect_uri) @@ -69,7 +67,7 @@ class QboController < ApplicationController qbo.expire = 1.hour.from_now.utc if qbo.save! - redirect_to qbo_sync_path, :flash => { :notice => "Successfully connected to Quickbooks" } + redirect_to sync_path, :flash => { :notice => "Successfully connected to Quickbooks" } else redirect_to plugin_settings_path(:redmine_qbo), :flash => { :error => "Error" } end @@ -90,7 +88,7 @@ class QboController < ApplicationController end # Quickbooks Webhook Callback - def qbo_webhook + def webhook logger.info "Quickbooks is calling webhook" @@ -161,10 +159,10 @@ class QboController < ApplicationController Thread.new do if Qbo.exists? Customer.sync - QboInvoice.sync + Invoice.sync QboItem.sync - QboEmployee.sync - QboEstimate.sync + Employee.sync + Estimate.sync # Record the last sync time Qbo.update_time_stamp diff --git a/app/models/customer.rb b/app/models/customer.rb index 1466fb0..e9ec640 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -12,9 +12,9 @@ class Customer < ActiveRecord::Base unloadable has_many :issues - has_many :qbo_purchases - has_many :qbo_invoices - has_many :qbo_estimates + has_many :purchases + has_many :invoices + has_many :estimates has_many :vehicles #attr_accessible :name, :notes, :email, :primary_phone, :mobile_phone, :phone_number @@ -149,16 +149,16 @@ class Customer < ActiveRecord::Base #end customers.each do |customer| - qbo_customer = Customer.find_or_create_by(id: customer.id) + customer = Customer.find_or_create_by(id: customer.id) if customer.active? - if not qbo_customer.name.eql? customer.display_name - qbo_customer.name = customer.display_name - qbo_customer.id = customer.id - qbo_customer.save_without_push + if not customer.name.eql? customer.display_name + customer.name = customer.display_name + customer.id = customer.id + customer.save_without_push end else - if not qbo_customer.new_record? - qbo_customer.delete + if not customer.new_record? + customer.delete end end end @@ -176,16 +176,16 @@ class Customer < ActiveRecord::Base service = Qbo.get_base(:customer) customer = service.fetch_by_id(id) - qbo_customer = Customer.find_or_create_by(id: customer.id) + customer = Customer.find_or_create_by(id: customer.id) if customer.active? - if not qbo_customer.name.eql? customer.display_name - qbo_customer.name = customer.display_name - qbo_customer.id = customer.id - qbo_customer.save_without_push + if not customer.name.eql? customer.display_name + customer.name = customer.display_name + customer.id = customer.id + customer.save_without_push end else - if not qbo_customer.new_record? - qbo_customer.delete + if not customer.new_record? + customer.delete end end end diff --git a/app/models/customer_token.rb b/app/models/customer_token.rb index d8e7cdb..62c28ab 100644 --- a/app/models/customer_token.rb +++ b/app/models/customer_token.rb @@ -14,7 +14,7 @@ class CustomerToken < ActiveRecord::Base validates_presence_of :expires_at, :issue_id before_create :generate_token - OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] || 'CONFIGURE_QBO__' + SecureRandom.uuid + OAUTH_CONSUMER_SECRET = Setting.plugin_redmine_qbo['settingsOAuthConsumerSecret'] || 'CONFIGURE__' + SecureRandom.uuid def generate_token self.token = SecureRandom.base64(15).tr('+/=lIO0', OAUTH_CONSUMER_SECRET) diff --git a/app/models/qbo_employee.rb b/app/models/employee.rb similarity index 79% rename from app/models/qbo_employee.rb rename to app/models/employee.rb index 651c511..75e6188 100644 --- a/app/models/qbo_employee.rb +++ b/app/models/employee.rb @@ -8,7 +8,7 @@ # #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 QboEmployee < ActiveRecord::Base +class Employee < ActiveRecord::Base unloadable has_many :users #attr_accessible :name @@ -24,19 +24,19 @@ class QboEmployee < ActiveRecord::Base transaction do # Update the item table employees.each { |employee| - qbo_employee = find_or_create_by(id: employee.id) - qbo_employee.name = employee.display_name - qbo_employee.id = employee.id - qbo_employee.save! + employee = find_or_create_by(id: employee.id) + employee.name = employee.display_name + employee.id = employee.id + employee.save! } end end def self.sync_by_id(id) employee = get_base.fetch_by_id(id) - qbo_employee = find_or_create_by(id: employee.id) - qbo_employee.name = employee.display_name - qbo_employee.id = employee.id - qbo_employee.save! + employee = find_or_create_by(id: employee.id) + employee.name = employee.display_name + employee.id = employee.id + employee.save! end end diff --git a/app/models/qbo_estimate.rb b/app/models/estimate.rb similarity index 85% rename from app/models/qbo_estimate.rb rename to app/models/estimate.rb index 0070272..99c9618 100644 --- a/app/models/qbo_estimate.rb +++ b/app/models/estimate.rb @@ -8,7 +8,7 @@ # #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 +class Estimate < ActiveRecord::Base unloadable has_and_belongs_to_many :issues @@ -44,26 +44,26 @@ class QboEstimate < ActiveRecord::Base 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.txn_date = estimate.txn_date - qbo_estimate.save! + estimate = find_or_create_by(id: id) + estimate.doc_number = estimate.doc_number + estimate.txn_date = estimate.txn_date + estimate.save! end # process an estimate into the database def self.process_estimate(estimate) logger.info "Processing estimate #{estimate.id}" - 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.txn_date = estimate.txn_date - qbo_estimate.save! + 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 + estimate.save! end # download the pdf from quickbooks def pdf - base = QboEstimate.get_base + base = Estimate.get_base estimate = base.fetch_by_id(id) return base.pdf(estimate) end diff --git a/app/models/qbo_invoice.rb b/app/models/invoice.rb similarity index 91% rename from app/models/qbo_invoice.rb rename to app/models/invoice.rb index 74e5321..fb9e2bd 100644 --- a/app/models/qbo_invoice.rb +++ b/app/models/invoice.rb @@ -8,7 +8,7 @@ # #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 QboInvoice < ActiveRecord::Base +class Invoice < ActiveRecord::Base unloadable has_and_belongs_to_many :issues belongs_to :customer @@ -59,10 +59,10 @@ class QboInvoice < ActiveRecord::Base logger.debug "Attaching invoice #{invoice.id} to issue #{issue.id}" - qbo_invoice = QboInvoice.find_or_create_by(id: invoice.id) + invoice = Invoice.find_or_create_by(id: invoice.id) - unless issue.qbo_invoices.include?(qbo_invoice) - issue.qbo_invoices << qbo_invoice + unless issue.invoices.include?(invoice) + issue.invoices << invoice issue.save! end @@ -74,12 +74,12 @@ class QboInvoice < ActiveRecord::Base logger.info "Processing invoice #{invoice.id}" # Load the invoice into the database - qbo_invoice = QboInvoice.find_or_create_by(id: invoice.id) - qbo_invoice.doc_number = invoice.doc_number - qbo_invoice.id = invoice.id - qbo_invoice.customer_id = invoice.customer_ref - qbo_invoice.txn_date = invoice.txn_date - qbo_invoice.save! + 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.save! # Scan the private notes for hashtags and attach to the applicable issues if not invoice.private_note.nil? @@ -105,7 +105,7 @@ class QboInvoice < ActiveRecord::Base # TODO maybe add a cf_sync_confict flag to invoices def self.compare_custom_fields(issue, invoice) logger.debug "Comparing custom fields" - # TODO break if QboInvoice.find(invoice.id).cf_sync_confict + # TODO break if Invoice.find(invoice.id).cf_sync_confict is_changed = false # update the invoive custom fields with infomation from the issue if available @@ -160,7 +160,7 @@ class QboInvoice < ActiveRecord::Base # Do nothing, probaly custome field sync confict on the invoice. # This is a problem with how it's billed # TODO Add notes in memo area - # TODO flag QboInvoice.cf_sync_confict here + # TODO flag Invoice.cf_sync_confict here logger.error "Failed to update invoice" end end diff --git a/app/models/qbo.rb b/app/models/qbo.rb index 55b90da..8156beb 100644 --- a/app/models/qbo.rb +++ b/app/models/qbo.rb @@ -1,6 +1,6 @@ #The MIT License (MIT) # -#Copyright (c) 2020 rick barrette +#Copyright (c) 2022 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,7 +10,6 @@ class Qbo < ActiveRecord::Base unloadable - #validates_presence_of :qb_token, :qb_secret, :company_id, :token_expires_at, :reconnect_token_at validates_presence_of :token, :company_id, :expire serialize :token diff --git a/app/views/customers/filter_estimates_by_customer.js.erb b/app/views/customers/filter_estimates_by_customer.js.erb index b31b665..bbabde0 100644 --- a/app/views/customers/filter_estimates_by_customer.js.erb +++ b/app/views/customers/filter_estimates_by_customer.js.erb @@ -1 +1 @@ -$('select#issue_qbo_estimate_id').html('<%= j content_tag(:option,'',:value=>"")+options_from_collection_for_select(@filtered_estimates, :id, :doc_number) %>'); +$('select#issue_estimate_id').html('<%= j content_tag(:option,'',:value=>"")+options_from_collection_for_select(@filtered_estimates, :id, :doc_number) %>'); diff --git a/app/views/estimates/_list.html.erb b/app/views/estimates/_list.html.erb index 47219e1..334fd84 100644 --- a/app/views/estimates/_list.html.erb +++ b/app/views/estimates/_list.html.erb @@ -1,6 +1,6 @@ <% if @customer.present? %> - <% @customer.qbo_estimates.order(doc_number: :desc).each do |estimate| %> + <% @customer.estimates.order(doc_number: :desc).each do |estimate| %>
| <%=t(:label_client_id)%> | - + |
|---|---|
| <%=t(:label_client_secret)%> | - + |
| <%=t(:label_webhook_token)%> | - + |