2023-11-24 15:05:38 +00:00
|
|
|
class TimeBudget < ActiveRecord::Base
|
|
|
|
validates :hours,
|
|
|
|
presence: true
|
|
|
|
|
2023-11-30 17:37:54 +00:00
|
|
|
validates :category_id,
|
|
|
|
inclusion: { in: TimeBudgetCategory.pluck(:id) },
|
2023-11-24 16:51:15 +00:00
|
|
|
allow_nil: true
|
|
|
|
|
2023-11-30 17:37:54 +00:00
|
|
|
validates_uniqueness_of :job_id, scope: :category_id, message: "Only one time budget can exist for each category"
|
2023-11-24 15:05:38 +00:00
|
|
|
|
|
|
|
belongs_to :job
|
2023-11-30 17:37:54 +00:00
|
|
|
belongs_to :category, class_name: "TimeBudgetCategory"
|
2023-11-30 19:50:40 +00:00
|
|
|
|
|
|
|
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
|
2023-11-24 15:05:38 +00:00
|
|
|
end
|