Files
redmine_qbo_bonus/app/controllers/tech_bonuses_controller.rb
T
rickyandClaude Opus 5.5 07e79a3c00 View tech bonuses permission, for the Foreman
A global "View tech bonuses" permission shows the weeks run and each
week's hours, tiers and bonuses, read-only: no attendance boxes, run
button or Email again, and no customer accounts, emailed-to addresses,
or QuickBooks/PDF links. "Run tech bonuses" is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:45:57 -05:00

99 lines
4.0 KiB
Ruby

#The MIT License (MIT)
#
#Copyright (c) 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.
# Weekly billable hours bonus (SOP-OPS-012): the weeks run so far, a week's hours and bonuses, and running it
class TechBonusesController < ApplicationController
self.main_menu = false
menu_item :tech_bonuses
before_action :authorize_global
before_action :find_week, only: [:show, :create]
helper :tech_bonuses
WEEKS_TO_PICK = 12
def index
# The week picker
week = RedmineQboBonus::Week.parse(params[:week]) if params[:week].present?
return redirect_to(tech_bonus_week_path(week.to_param)) if week
@bonus_weeks = TechBonusWeek.recent.includes(:credits, :run_by).limit(52).to_a
@weeks = (0...WEEKS_TO_PICK).map { |n| RedmineQboBonus::Week.last_completed - n }
@week = RedmineQboBonus::Week.last_completed
end
def show
@report = RedmineQboBonus::WeekReport.new(@week)
@bonus_week = TechBonusWeek.for(@week)
@credits = @bonus_week ? @bonus_week.credits.index_by(&:user_id) : {}
@can_run = RedmineQboBonus.allowed?
end
def create
runner = RedmineQboBonus::Runner.new(@week, attended_user_ids: Array(params[:attended]))
bonus_week = runner.run
credits = bonus_week.credits.to_a
if !bonus_week.goal_met?
flash[:warning] = l(:notice_tech_bonus_goal_not_met, week: @week.label)
elsif credits.any? { |credit| credit.status == 'failed' }
flash[:error] = l(:notice_tech_bonus_some_failed)
else
flash[:notice] = l(:notice_tech_bonus_run, count: credits.count(&:credited?), week: @week.label)
end
redirect_to tech_bonus_week_path(@week.to_param)
rescue RedmineQboBonus::Runner::Error => e
flash[:error] = e.message
redirect_to tech_bonus_week_path(@week.to_param)
end
# Emails a credit memo again (e.g. after fixing the technician's email in QuickBooks)
def resend
credit = TechBonusCredit.find(params[:id])
week = credit.tech_bonus_week.week
unless credit.credited?
render_404
return
end
Rails.cache.delete(credit.customer.details_cache_key) if credit.customer
email = RedmineQboBonus::Runner.customer_email(credit.customer)
if RedmineQboBonus::Runner.email(credit, email)
flash[:notice] = l(:notice_tech_bonus_emailed, email: email)
else
flash[:error] = credit.message
end
redirect_to tech_bonus_week_path(week.to_param)
rescue ActiveRecord::RecordNotFound
render_404
end
# The credit memo as QuickBooks prints it
def pdf
credit = TechBonusCredit.find(params[:id])
return render_404 unless credit.credited?
pdf = RedmineQboBonus::CreditMemoWriter.pdf(credit.credit_memo_id)
send_data pdf, filename: "credit-memo-#{credit.doc_number.presence || credit.credit_memo_id}.pdf",
type: 'application/pdf', disposition: 'inline'
rescue ActiveRecord::RecordNotFound
render_404
rescue StandardError => e
flash[:error] = e.message
redirect_to tech_bonuses_path
end
private
def find_week
@week = RedmineQboBonus::Week.parse(params[:week])
render_404 unless @week
end
end