mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b38f850df3 | |||
| 138e55933b | |||
| 5fbc169ade | |||
| d6737a6747 | |||
| 65db8f00a8 | |||
| 0197dc2a30 | |||
| cd1caa502d | |||
| 4b45d24a75 |
@@ -9,6 +9,9 @@
|
||||
#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 Customer < ActiveRecord::Base
|
||||
|
||||
include Redmine::Acts::Searchable
|
||||
include Redmine::Acts::Event
|
||||
|
||||
has_many :issues
|
||||
has_many :invoices
|
||||
@@ -17,6 +20,16 @@ class Customer < ActiveRecord::Base
|
||||
validates_presence_of :id, :name
|
||||
|
||||
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
|
||||
# returns the customer's email
|
||||
@@ -35,27 +48,6 @@ class Customer < ActiveRecord::Base
|
||||
pull unless @details
|
||||
@details.email_address = s
|
||||
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
|
||||
# returns the customer's primary phone
|
||||
@@ -79,7 +71,8 @@ class Customer < ActiveRecord::Base
|
||||
update_phone_number
|
||||
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
|
||||
nil
|
||||
end
|
||||
@@ -187,33 +180,14 @@ class Customer < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Searchs the database for a customer by name or phone number with out special chars
|
||||
|
||||
def self.search(search)
|
||||
search = sanitize_sql_like(search)
|
||||
customers = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
|
||||
return customers.order(:name)
|
||||
end
|
||||
|
||||
def self.searchable_columns
|
||||
['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
|
||||
|
||||
# Override the defult redmine seach method to rank results by id
|
||||
def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {})
|
||||
return {} if tokens.blank?
|
||||
|
||||
@@ -261,6 +235,30 @@ class Customer < ActiveRecord::Base
|
||||
def to_s
|
||||
return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}"
|
||||
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
|
||||
|
||||
@@ -277,24 +275,5 @@ class Customer < ActiveRecord::Base
|
||||
@details = Quickbooks::Model::Customer.new
|
||||
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
|
||||
|
||||
16
db/migrate/039_add_full_text_index_to_customers.rb
Normal file
16
db/migrate/039_add_full_text_index_to_customers.rb
Normal 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
|
||||
2
init.rb
2
init.rb
@@ -14,7 +14,7 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
name 'Redmine QBO plugin'
|
||||
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'
|
||||
version '2026.2.13'
|
||||
version '2026.2.15'
|
||||
url 'https://github.com/rickbarrette/redmine_qbo'
|
||||
author_url 'https://barrettefabrication.com'
|
||||
settings default: {empty: true}, partial: 'qbo/settings'
|
||||
|
||||
Reference in New Issue
Block a user