Compare commits

...

4 Commits

Author SHA1 Message Date
a4f461fd4d 2026.3.10 2026-03-15 18:01:54 -04:00
3e81d2840a fixed typo 2026-03-15 17:59:53 -04:00
c9a5dc20f9 Added manual sync links 2026-03-15 08:01:04 -04:00
db3c6021c5 2026.3.9 2026-03-14 21:54:12 -04:00
5 changed files with 119 additions and 127 deletions

View File

@@ -32,38 +32,36 @@ class CustomersController < ApplicationController
autocomplete :customer, :name, full: true, extra_data: [:id] autocomplete :customer, :name, full: true, extra_data: [:id]
def address_to_s(address)
return if address.nil?
lines = [
address.line1,
address.line2,
address.line3,
address.line4,
address.line5
].compact_blank
city_line = [
address.city,
address.country_sub_division_code,
address.postal_code
].compact_blank.join(" ")
lines << city_line unless city_line.blank?
lines.join("\n")
end
def add_customer
global_check_permission(:add_customers)
end
def allowed_params def allowed_params
params.require(:customer).permit(:name, :email, :primary_phone, :mobile_phone, :phone_number, :notes) params.require(:customer).permit(:name, :email, :primary_phone, :mobile_phone, :phone_number, :notes)
end end
# getter method for a customer's invoices
# used for customer autocomplete field / issue form
def filter_invoices_by_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 = Estimate.all.where(customer_id: params[:selected_customer])
end
# display a list of all customers
def index
if params[:search]
@customers = Customer.search(params[:search]).order(:name).paginate(page: params[:page])
if only_one_non_zero?(@customers)
redirect_to @customers.first
end
end
end
# initialize a new customer
def new
@customer = Customer.new
end
# create a new customer
def create def create
@customer = Customer.new(allowed_params) @customer = Customer.new(allowed_params)
@customer.save @customer.save
@@ -76,7 +74,79 @@ class CustomersController < ApplicationController
redirect_to new_customer_path redirect_to new_customer_path
end end
# display a specific customer def edit
@customer = Customer.find_by_id(params[:id])
return render_404 unless @customer
rescue => e
log "Failed to edit customer"
flash[:error] = e.message
render_404
end
def filter_estimates_by_customer
@filtered_estimates = Estimate.all.where(customer_id: params[:selected_customer])
end
def filter_invoices_by_customer
@filtered_invoices = Invoice.all.where(customer_id: params[:selected_customer])
end
def index
if params[:search]
@customers = Customer.search(params[:search]).order(:name).paginate(page: params[:page])
if only_one_non_zero?(@customers)
redirect_to @customers.first
end
end
end
def load_issue_data
@journals = @issue.journals.preload(:details).preload(user: :email_address).reorder(:created_on, :id).to_a
@journals.each_with_index { |j, i| j.indice = i + 1 }
@journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
Journal.preload_journals_details_custom_fields(@journals)
@journals.select! { |journal| journal.notes? || journal.visible_details.any? }
@journals.reverse! if User.current.wants_comments_in_reverse_order?
@changesets = @issue.changesets.visible.preload(:repository, :user).to_a
@changesets.reverse! if User.current.wants_comments_in_reverse_order?
@relations = @issue.relations.select { |r| r.other_issue(@issue)&.visible? }
@allowed_statuses = @issue.new_statuses_allowed_to(User.current)
@priorities = IssuePriority.active
@time_entry = TimeEntry.new(issue: @issue, project: @issue.project)
@relation = IssueRelation.new
end
def log(msg)
Rails.logger.info "[CustomersController] #{msg}"
end
def new
@customer = Customer.new
end
def only_one_non_zero?(array)
found_non_zero = false
array.each do |val|
if val != 0
return false if found_non_zero
found_non_zero = true
end
end
found_non_zero
end
def share
issue = Issue.find(params[:id])
token = issue.share_token
redirect_to view_path(token.token)
rescue ActiveRecord::RecordNotFound
flash[:error] = t(:notice_issue_not_found)
render_404
end
def show def show
@customer = Customer.find_by_id(params[:id]) @customer = Customer.find_by_id(params[:id])
return render_404 unless @customer return render_404 unless @customer
@@ -109,17 +179,11 @@ class CustomersController < ApplicationController
render_404 render_404
end end
# return an HTML form for editing a customer def sync
def edit Customer.sync
@customer = Customer.find_by_id(params[:id]) redirect_to :home, flash: { notice: I18n.t(:label_syncing) }
return render_404 unless @customer
rescue => e
log "Failed to edit customer"
flash[:error] = e.message
render_404
end end
# update a specific customer
def update def update
@customer = Customer.find_by_id(params[:id]) @customer = Customer.find_by_id(params[:id])
@customer.update(allowed_params) @customer.update(allowed_params)
@@ -131,108 +195,21 @@ class CustomersController < ApplicationController
redirect_to edit_customer_path redirect_to edit_customer_path
end end
# creates new customer view tokens, removes expired tokens & redirects to newly created customer view with new token.
def share
issue = Issue.find(params[:id])
token = issue.share_token
redirect_to view_path(token.token)
rescue ActiveRecord::RecordNotFound
flash[:error] = t(:notice_issue_not_found)
render_404
end
# displays an issue for a customer with a provided security CustomerToken
def view def view
User.current = User.anonymous User.current = User.anonymous
# Load only active, non-expired token
@token = CustomerToken.active.find_by(token: params[:token]) @token = CustomerToken.active.find_by(token: params[:token])
return render_403 unless @token return render_403 unless @token
# Load associated issue
@issue = @token.issue @issue = @token.issue
return render_403 unless @issue return render_403 unless @issue
# Optional: enforce token belongs to the issue's customer
return render_403 unless @issue.customer_id == @token.issue.customer_id return render_403 unless @issue.customer_id == @token.issue.customer_id
# Store token in session for subsequent requests if needed
session[:token] = @token.token session[:token] = @token.token
load_issue_data load_issue_data
rescue ActiveRecord::RecordNotFound rescue ActiveRecord::RecordNotFound
render_403 render_403
end end
private
def load_issue_data
@journals = @issue.journals.preload(:details).preload(user: :email_address).reorder(:created_on, :id).to_a
@journals.each_with_index { |j, i| j.indice = i + 1 }
@journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
Journal.preload_journals_details_custom_fields(@journals)
@journals.select! { |journal| journal.notes? || journal.visible_details.any? }
@journals.reverse! if User.current.wants_comments_in_reverse_order?
@changesets = @issue.changesets.visible.preload(:repository, :user).to_a
@changesets.reverse! if User.current.wants_comments_in_reverse_order?
@relations = @issue.relations.select { |r| r.other_issue(@issue)&.visible? }
@allowed_statuses = @issue.new_statuses_allowed_to(User.current)
@priorities = IssuePriority.active
@time_entry = TimeEntry.new(issue: @issue, project: @issue.project)
@relation = IssueRelation.new
end
# redmine permission - add customers
def add_customer
global_check_permission(:add_customers)
end
# redmine permission - view customers
def view_customer def view_customer
global_check_permission(:view_customers) global_check_permission(:view_customers)
end end
# checks to see if there is only one item in an array end
# @return true if array only has one item
def only_one_non_zero?( array )
found_non_zero = false
array.each do |val|
if val!=0
return false if found_non_zero
found_non_zero = true
end
end
found_non_zero
end
# format a quickbooks address to a human readable string
def address_to_s(address)
return if address.nil?
lines = [
address.line1,
address.line2,
address.line3,
address.line4,
address.line5
].compact_blank
city_line = [
address.city,
address.country_sub_division_code,
address.postal_code
].compact_blank.join(" ")
lines << city_line unless city_line.blank?
lines.join("\n")
end
def log(msg)
Rails.logger.info "[CustomersController] #{msg}"
end
end

View File

@@ -72,6 +72,13 @@ class EstimateController < ApplicationController
redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_estimate_not_found) } redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_estimate_not_found) }
end end
def sync
Estimate.sync
redirect_to :home, flash: { notice: I18n.t(:label_syncing) }
end
private
# Logs messages with a consistent prefix for easier debugging. # Logs messages with a consistent prefix for easier debugging.
def log(msg) def log(msg)
Rails.logger.info "[EstimateController] #{msg}" Rails.logger.info "[EstimateController] #{msg}"

View File

@@ -27,6 +27,11 @@ class InvoiceController < ApplicationController
redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_invoice_not_found) } redirect_back fallback_location: root_path, flash: { error: I18n.t(:notice_invoice_not_found) }
end end
def sync
Invoice.sync
redirect_to :home, flash: { notice: I18n.t(:label_syncing) }
end
private private
# Logs messages with a consistent prefix for easier debugging. # Logs messages with a consistent prefix for easier debugging.

View File

@@ -14,6 +14,8 @@ get 'qbo/oauth_callback', to: 'qbo#oauth_callback'
#manual sync #manual sync
get 'qbo/sync', to: 'qbo#sync' get 'qbo/sync', to: 'qbo#sync'
get 'invoices/sync', to: 'invoice#sync'
get 'estimates/sync', to: 'estimate#sync'
#webhook #webhook
post 'qbo/webhook', to: 'qbo#webhook' post 'qbo/webhook', to: 'qbo#webhook'
@@ -36,4 +38,5 @@ get 'filter_invoices_by_customer' => 'customers#filter_invoices_by_customer'
resources :customers do resources :customers do
get :autocomplete_customer_name, on: :collection get :autocomplete_customer_name, on: :collection
get :sync
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.3.8' version '2026.3.10'
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'