Compare commits

..

8 Commits

7 changed files with 122 additions and 36 deletions

View File

@@ -30,8 +30,6 @@ class CustomersController < ApplicationController
before_action :view_customer, except: [:new, :view] before_action :view_customer, except: [:new, :view]
skip_before_action :verify_authenticity_token, :check_if_login_required, only: [:view] skip_before_action :verify_authenticity_token, :check_if_login_required, only: [:view]
default_search_scope :names
autocomplete :customer, :name, full: true, extra_data: [:id] autocomplete :customer, :name, full: true, extra_data: [:id]
def allowed_params def allowed_params

View File

@@ -18,11 +18,6 @@ class Customer < ActiveRecord::Base
self.primary_key = :id self.primary_key = :id
# returns a human readable string
def to_s
return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}"
end
# Convenience Method # Convenience Method
# returns the customer's email # returns the customer's email
def email def email
@@ -41,6 +36,27 @@ 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
@@ -62,7 +78,12 @@ class Customer < ActiveRecord::Base
#update our locally stored number too #update our locally stored number too
update_phone_number update_phone_number
end end
# Customers are not bound by a project.
def project
nil
end
# Convenience Method # Convenience Method
# returns the customer's mobile phone # returns the customer's mobile phone
def mobile_phone def mobile_phone
@@ -169,9 +190,45 @@ class Customer < ActiveRecord::Base
# Searchs the database for a customer by name or phone number with out special chars # 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)
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
['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 = {})
return {} if tokens.blank?
scope = self.all
tokens.each do |token|
q = "%#{sanitize_sql_like(token)}%"
scope = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%")
end
ids = scope.distinct.limit(options[:limit] || 100).pluck(:id)
# Assign simple uniform ranking
ids.each_with_object({}) { |id, h| h[id] = id }
end
# proforms a bruteforce sync operation # proforms a bruteforce sync operation
# This needs to be simplified # This needs to be simplified
@@ -200,25 +257,11 @@ class Customer < ActiveRecord::Base
end end
end end
# Push the updates # returns a human readable string
def save_with_push def to_s
begin return "#{self[:name]} - #{phone_number.split(//).last(4).join unless phone_number.nil?}"
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 end
alias_method :save_without_push, :save
alias_method :save, :save_with_push
private private
# pull the details # pull the details
@@ -234,5 +277,24 @@ class Customer < ActiveRecord::Base
@details = Quickbooks::Model::Customer.new @details = Quickbooks::Model::Customer.new
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

@@ -1,4 +1,4 @@
<%= form_tag(customers_path, method: "get", id: "search-form") do %> <%= form_tag(customers_path, method: "get", id: "customer-search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: t(:label_search_customers), autocomplete: "off" %> <%= text_field_tag :search, params[:search], placeholder: t(:label_search_customers), autocomplete: "off" %>
<%= submit_tag t(:label_search) %> <%= submit_tag t(:label_search) %>
<% end %> <% end %>

View File

@@ -1,4 +1,4 @@
<%= form_tag(estimate_doc_path, method: "get") do %> <%= form_tag(estimate_doc_path, method: "get", id: "estimate-search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: t(:label_search_estimates), autocomplete: "off" %> <%= text_field_tag :search, params[:search], placeholder: t(:label_search_estimates), autocomplete: "off" %>
<%= submit_tag t(:label_search), formtarget: "_blank" %> <%= submit_tag t(:label_search), formtarget: "_blank" %>
<% end %> <% end %>

View File

@@ -0,0 +1,15 @@
#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 AddCustomersTimestamp < ActiveRecord::Migration[5.1]
def change
add_timestamps(:customers, null: true)
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.10' version '2026.2.13'
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'
@@ -37,6 +37,10 @@ Redmine::Plugin.register :redmine_qbo do
# Register top menu items # Register top menu items
menu :top_menu, :customers, { controller: :customers, action: :index }, caption: :label_customers, if: Proc.new {User.current.logged?} menu :top_menu, :customers, { controller: :customers, action: :index }, caption: :label_customers, if: Proc.new {User.current.logged?}
Redmine::Search.map do |search|
search.register :customers
end
end end
# Dynamically load all Hooks & Patches recursively # Dynamically load all Hooks & Patches recursively

View File

@@ -43,14 +43,18 @@ module RedmineQbo
# Create billable time entries # Create billable time entries
def bill_time def bill_time
logger.debug "QBO: Billing time for issue ##{id}" logger.debug "QBO: Billing time for issue ##{self.id}"
return false if assigned_to.nil? logger.debug "Issue is closed? #{self.closed?}"
return false if self.assigned_to.nil?
return false unless Qbo.first return false unless Qbo.first
return false unless customer return false unless self.customer
Thread.new do Thread.new do
spent_time = time_entries.where(billed: [false, nil]) spent_time = self.time_entries.where(billed: [false, nil])
spent_hours ||= spent_time.sum(:hours) || 0 spent_hours ||= spent_time.sum(:hours) || 0
logger.debug "Issue has spent hours: #{spent_hours}"
if spent_hours > 0 then if spent_hours > 0 then
@@ -73,20 +77,23 @@ module RedmineQbo
# Now letes upload our totals for each activity as their own billable time entry # Now letes upload our totals for each activity as their own billable time entry
h.each do |key, val| h.each do |key, val|
logger.debug "Processing activity '#{key}' with #{val.to_i} hours for issue ##{self.id}"
# Convert float spent time to hours and minutes # Convert float spent time to hours and minutes
hours = val.to_i hours = val.to_i
minutesDecimal = (( val - hours) * 60) minutesDecimal = (( val - hours) * 60)
minutes = minutesDecimal.to_i minutes = minutesDecimal.to_i
logger.debug "Converted #{val.to_i} hours to #{hours} hours and #{minutes} minutes"
# Lets match the activity to an qbo item # Lets match the activity to an qbo item
item = item_service.query("SELECT * FROM Item WHERE Name = '#{key}' ").first item = item_service.query("SELECT * FROM Item WHERE Name = '#{key}' ").first
next if item.nil? next if item.nil?
# Create the new billable time entry and upload it # Create the new billable time entry and upload it
time_entry.description = "#{tracker} ##{id}: #{subject} #{"(Partial @ #{done_ratio}%)" if not closed?}" time_entry.description = "#{self.tracker} ##{self.id}: #{self.subject} #{"(Partial @ #{self.done_ratio}%)" unless self.closed?}"
time_entry.employee_id = assigned_to.employee_id time_entry.employee_id = self.assigned_to.employee_id
time_entry.customer_id = customer_id time_entry.customer_id = self.customer_id
time_entry.billable_status = "Billable" time_entry.billable_status = "Billable"
time_entry.hours = hours time_entry.hours = hours
time_entry.minutes = minutes time_entry.minutes = minutes