Compare commits

...

8 Commits

3 changed files with 58 additions and 63 deletions

View File

@@ -10,6 +10,9 @@
class Customer < ActiveRecord::Base class Customer < ActiveRecord::Base
include Redmine::Acts::Searchable
include Redmine::Acts::Event
has_many :issues has_many :issues
has_many :invoices has_many :invoices
has_many :estimates has_many :estimates
@@ -18,6 +21,16 @@ class Customer < ActiveRecord::Base
self.primary_key = :id self.primary_key = :id
acts_as_searchable columns: %w[name phone_number mobile_phone_number ],
scope: ->(_context) { left_joins(:project) },
date_column: :updated_at
acts_as_event :title => Proc.new {|o| "#{o}"},
:url => Proc.new {|o| { :controller => 'customers', :action => 'show', :id => o.id} },
:type => :to_s,
:description => Proc.new {|o| "#{I18n.t :label_primary_phone}: #{o.phone_number} #{I18n.t:label_mobile_phone}: #{o.mobile_phone_number}"},
:datetime => Proc.new {|o| o.updated_at || o.created_at}
# Convenience Method # Convenience Method
# returns the customer's email # returns the customer's email
def email def email
@@ -36,27 +49,6 @@ class Customer < ActiveRecord::Base
@details.email_address = s @details.email_address = s
end end
# Redmine Search Event Interface
def event_type
:customer
end
def event_title
to_s
end
def event_description
"#{I18n.t :label_primary_phone}: #{primary_phone} #{I18n.t:label_mobile_phone}: #{mobile_phone}"
end
def event_url
Rails.application.routes.url_helpers.customer_path(self)
end
def event_datetime
updated_at || created_at
end
# Convenience Method # Convenience Method
# returns the customer's primary phone # returns the customer's primary phone
def primary_phone def primary_phone
@@ -79,7 +71,8 @@ class Customer < ActiveRecord::Base
update_phone_number update_phone_number
end end
# Customers are not bound by a project. # Customers are not bound by a project
# but we need to implement this method for the Redmine::Acts::Searchable interface
def project def project
nil nil
end end
@@ -188,32 +181,13 @@ class Customer < ActiveRecord::Base
end end
end end
# Searchs the database for a customer by name or phone number with out special chars
def self.search(search) def self.search(search)
search = sanitize_sql_like(search) search = sanitize_sql_like(search)
customers = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%") customers = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
return customers.order(:name) return customers.order(:name)
end end
def self.searchable_columns # Override the defult redmine seach method to rank results by id
['name', 'phone_number', 'mobile_phone_number']
end
def self.searchable_options
{
columns: searchable_columns,
scope: self.all
}
end
def self.search_results_from_ids(ids, *args)
return [] if ids.blank?
ids = ids.map(&:to_i)
records = where(id: ids).index_by(&:id)
ids.map { |id| records[id] }.compact
end
def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {}) def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {})
return {} if tokens.blank? return {} if tokens.blank?
@@ -262,6 +236,30 @@ class Customer < ActiveRecord::Base
return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}" return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}"
end end
# Push the updates
def save_with_push
begin
qbo = Qbo.first
@details = qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Customer.new(
company_id: qbo.realm_id,
access_token: access_token
)
service.update(@details)
end
self.id = @details.id
rescue => e
errors.add(:base, e.message)
return false
end
save_without_push
end
alias_method :save_without_push, :save
alias_method :save, :save_with_push
private private
# pull the details # pull the details
@@ -278,23 +276,4 @@ class Customer < ActiveRecord::Base
end end
end end
# Push the updates
def save_with_push
begin
qbo = Qbo.first
@details = qbo.perform_authenticated_request do |access_token|
service = Quickbooks::Service::Customer.new(company_id: qbo.realm_id, access_token: access_token)
service.update(@details)
end
#raise "QBO Fault" if @details.fault?
self.id = @details.id
rescue Exception => e
errors.add(e.message)
end
save_without_push
end
alias_method :save_without_push, :save
alias_method :save, :save_with_push
end end

View File

@@ -0,0 +1,16 @@
#The MIT License (MIT)
#
#Copyright (c) 2016 - 2026 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 AddFullTextIndexToCustomers < ActiveRecord::Migration[7.0]
def change
# This creates a combined index for name and phone fields
add_index :customers, [:name, :phone_number, :mobile_phone_number], type: :fulltext, name: 'ft_search_idx'
end
end

View File

@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo do
name 'Redmine QBO plugin' name 'Redmine QBO plugin'
author 'Rick Barrette' author 'Rick Barrette'
description 'A pluging for Redmine to connect with QuickBooks Online to create Time Activity Entries for billable hours logged when an Issue is closed' description 'A pluging for Redmine to connect with QuickBooks Online to create Time Activity Entries for billable hours logged when an Issue is closed'
version '2026.2.13' version '2026.2.15'
url 'https://github.com/rickbarrette/redmine_qbo' url 'https://github.com/rickbarrette/redmine_qbo'
author_url 'https://barrettefabrication.com' author_url 'https://barrettefabrication.com'
settings default: {empty: true}, partial: 'qbo/settings' settings default: {empty: true}, partial: 'qbo/settings'