Auto-assign logged time to budgets

Now that users have a "budget category" assigned to them, we can show
time logged against the correct budget.

Refs #2165
This commit is contained in:
Trevor Vallender 2023-11-30 19:50:40 +00:00
parent a697bbf7be
commit 3e04193d16
3 changed files with 19 additions and 8 deletions

View File

@ -3,10 +3,13 @@ module JobsHelper
progress_bar(job.done_ratio,
legend: "#{job.done_ratio}%
(#{l_hours_short(job.total_time_logged)}/#{l_hours_short(job.total_time_budget)})",
class: "progress")
class: "progress")
end
def progress_bar_for(budget)
l_hours_short(budget.hours)
progress_bar(budget.done_ratio,
legend: "#{budget.done_ratio}%
(#{l_hours_short(budget.total_time_logged)}/#{l_hours_short(budget.hours)})",
class: "progress")
end
end

View File

@ -35,12 +35,6 @@ class Job < ActiveRecord::Base
time_budgets.sum(&:hours)
end
def time_budget_for(category)
return 0 if category.nil? || time_budgets.find_by(category_id: category.id).nil?
time_budgets.find_by(category_id: category.id).hours
end
def total_time_logged
TimeEntry.where(job_id: id)
.sum(:hours)

View File

@ -10,4 +10,18 @@ class TimeBudget < ActiveRecord::Base
belongs_to :job
belongs_to :category, class_name: "TimeBudgetCategory"
def done_ratio
return 0 if hours.zero?
(total_time_logged / hours * 100).to_i
end
def total_time_logged
TimeEntry.joins(:user)
.where(
job_id: job_id,
users: { time_budget_category_id: category_id }
).sum(:hours)
end
end