mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-20 18:40:37 -05:00
Any logged-in user could open any estimate or invoice PDF by id. Now: - estimates: View customers or Write estimates, or an estimate attached to an issue the user can see (the issue page links to it); users without the permission don't trigger a sync of uncached estimates - invoices: View customers, Write invoices, View/Receive payments or Issue refunds, or invoices attached to issues the user can see - share-link customers are unchanged (their own issue's documents) - the sidebar's estimate search only shows to those who can use it The issue PDF merges an estimate PDF with optional content, as it already did for the issue's own PDF, instead of failing. Removed dead code: the JSON branches of CustomersController#create (Redmine refuses JSON there) and the scaffold views for qbo#authenticate, #oauth_callback and #webhook, which those actions never render. An invalid customer now shows the form with a 422. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
222 lines
7.7 KiB
Ruby
222 lines
7.7 KiB
Ruby
#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 CustomersController < ApplicationController
|
|
|
|
include AuthHelper
|
|
helper :issues
|
|
helper :journals
|
|
helper :projects
|
|
helper :custom_fields
|
|
helper :issue_relations
|
|
helper :watchers
|
|
helper :attachments
|
|
helper :queries
|
|
include QueriesHelper
|
|
helper :repositories
|
|
helper :sort
|
|
include SortHelper
|
|
helper :timelog
|
|
|
|
# This tells Redmine to check global permissions for the current controller/action
|
|
# customers#view is the public share-link page: it is authorized by its token, not by Redmine permissions
|
|
before_action :authorize_global, except: [:view]
|
|
before_action :add_customer, only: [:new, :create]
|
|
before_action :view_customer, except: [:new, :create, :view, :share, :sync]
|
|
before_action :require_admin, only: :sync
|
|
skip_before_action :verify_authenticity_token, :check_if_login_required, only: [:view]
|
|
|
|
# Used for autocomplete forms. Fuzzy matching (nicknames, typos, word order, phone digits) helps
|
|
# staff spot an existing customer before creating a duplicate. See CustomerSearchService.
|
|
def autocomplete
|
|
name_query = (params[:name] || params[:q] || params[:term]).to_s.strip
|
|
items = CustomerSearchService.call(name: name_query, phone: params[:phone])
|
|
|
|
render json: items.map { |i|
|
|
{ id: i.id, name: i.name, phone_number: i.phone_number, mobile_phone_number: i.mobile_phone_number }
|
|
}
|
|
end
|
|
|
|
def create
|
|
@customer = Customer.new(allowed_params)
|
|
|
|
if @customer.save
|
|
log "Customer ##{@customer.id} created successfully."
|
|
redirect_to @customer, notice: l(:notice_successful_create)
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
rescue => e
|
|
log "Failed to create customer: #{e.message}"
|
|
flash[:error] = e.message
|
|
redirect_to new_customer_path
|
|
end
|
|
|
|
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
|
|
|
|
# Most matches a search lists (best first)
|
|
SEARCH_LIMIT = 200
|
|
|
|
# Every customer, sortable and paged like Redmine's lists; a search narrows it to the best matches (a single
|
|
# match opens that customer). Sorting a search's results by a column replaces the best-first order.
|
|
def index
|
|
sort_init 'name', 'asc'
|
|
sort_update 'name' => "#{Customer.table_name}.name",
|
|
'phone' => "#{Customer.table_name}.phone_number",
|
|
'mobile' => "#{Customer.table_name}.mobile_phone_number",
|
|
'updated' => "#{Customer.table_name}.updated_at"
|
|
|
|
@search = params[:search].to_s.strip
|
|
if @search.present?
|
|
scope = CustomerSearchService.call(name: @search, limit: SEARCH_LIMIT)
|
|
@best_first = params[:sort].blank?
|
|
else
|
|
scope = Customer.all
|
|
end
|
|
scope = scope.reorder(sort_clause).order(:id) unless @best_first
|
|
|
|
@customer_count = scope.count
|
|
return redirect_to(scope.first) if @search.present? && @customer_count == 1
|
|
|
|
@customer_pages = Paginator.new @customer_count, per_page_option, params['page']
|
|
@customers = scope.limit(@customer_pages.per_page).offset(@customer_pages.offset).to_a
|
|
@issue_counts = Issue.where(customer_id: @customers.map(&:id)).group(:customer_id).count
|
|
end
|
|
|
|
def new
|
|
@customer = Customer.new
|
|
|
|
if request.xhr?
|
|
render partial: 'form', layout: false, locals: { hide_submit: true, hide_toolbar: true }
|
|
end
|
|
end
|
|
|
|
def share
|
|
issue = Issue.visible.find(params[:id])
|
|
token = issue.share_token
|
|
return render_403 unless token
|
|
|
|
redirect_to view_path(token.token)
|
|
rescue ActiveRecord::RecordNotFound
|
|
flash[:error] = t(:notice_issue_not_found)
|
|
render_404
|
|
end
|
|
|
|
def show
|
|
@customer = Customer.find_by_id(params[:id])
|
|
return render_404 unless @customer
|
|
|
|
@open_issues = @customer.issues
|
|
.joins(:status)
|
|
.includes(:status, :project, :tracker, :priority)
|
|
.where(issue_statuses: { is_closed: false })
|
|
.order(id: :desc)
|
|
|
|
@closed_issues = @customer.issues
|
|
.joins(:status)
|
|
.includes(:status, :project, :tracker, :priority)
|
|
.where(issue_statuses: { is_closed: true })
|
|
.order(id: :desc)
|
|
|
|
@hours = TimeEntry
|
|
.joins(:issue)
|
|
.where(issues: { id: @open_issues.select(:id) })
|
|
.sum(:hours)
|
|
|
|
@closed_hours = TimeEntry
|
|
.joins(:issue)
|
|
.where(issues: { id: @closed_issues.select(:id) })
|
|
.sum(:hours)
|
|
|
|
rescue => e
|
|
Rails.logger.error "Failed to load customer ##{params[:id]}: #{e.message}\n#{e.backtrace.join("\n")}"
|
|
flash[:error] = e.message
|
|
render_404
|
|
end
|
|
|
|
def sync
|
|
Customer.sync
|
|
redirect_back fallback_location: home_path, flash: { notice: I18n.t(:label_syncing) }
|
|
end
|
|
|
|
def update
|
|
@customer = Customer.find_by_id(params[:id])
|
|
return render_404 unless @customer
|
|
|
|
if @customer.update(allowed_params)
|
|
flash[:notice] = t :notice_customer_updated
|
|
redirect_to @customer
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
rescue => e
|
|
log "Failed to update customer: #{e.message}"
|
|
flash[:error] = e.message
|
|
redirect_to edit_customer_path
|
|
end
|
|
|
|
def view
|
|
User.current = User.anonymous
|
|
@token = CustomerToken.active.find_by(token: params[:token])
|
|
return render_403 unless @token
|
|
@issue = @token.issue
|
|
return render_403 unless @issue
|
|
session[:token] = @token.token
|
|
load_issue_data
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_403
|
|
end
|
|
|
|
private
|
|
|
|
def add_customer
|
|
global_check_permission(:add_customers)
|
|
end
|
|
|
|
def allowed_params
|
|
params.require(:customer).permit(:name, :email, :primary_phone, :mobile_phone, :phone_number, :notes)
|
|
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 view_customer
|
|
global_check_permission(:view_customers)
|
|
end
|
|
|
|
end
|