2019-01-13 07:05:54 +01:00
|
|
|
class PeriodicTaskGroup < ApplicationRecord
|
2013-06-21 22:04:36 +02:00
|
|
|
has_many :tasks, dependent: :destroy
|
2012-11-28 10:13:54 +01:00
|
|
|
|
|
|
|
def has_next_task?
|
2013-06-21 22:04:36 +02:00
|
|
|
return false if tasks.empty?
|
|
|
|
return false if tasks.first.due_date.nil?
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2012-11-28 10:13:54 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_next_task
|
2013-06-21 22:04:36 +02:00
|
|
|
template_task = tasks.first
|
2014-11-23 01:22:50 +01:00
|
|
|
self.next_task_date ||= template_task.due_date + period_days
|
2017-01-26 20:54:53 +01:00
|
|
|
|
2012-11-28 10:13:54 +01:00
|
|
|
next_task = template_task.dup
|
2017-01-26 20:54:53 +01:00
|
|
|
next_task.due_date = next_task_date
|
|
|
|
next_task.done = false
|
2012-11-28 10:13:54 +01:00
|
|
|
next_task.save
|
|
|
|
|
2014-11-23 01:22:50 +01:00
|
|
|
self.next_task_date += period_days
|
2012-11-28 10:13:54 +01:00
|
|
|
self.save
|
|
|
|
end
|
2016-02-24 20:47:09 +01:00
|
|
|
|
|
|
|
def create_tasks_until(create_until)
|
|
|
|
if has_next_task?
|
|
|
|
while next_task_date.nil? || next_task_date < create_until
|
|
|
|
create_next_task
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_tasks_for_upfront_days
|
|
|
|
create_until = Date.today + FoodsoftConfig[:tasks_upfront_days].to_i + 1
|
|
|
|
create_tasks_until create_until
|
|
|
|
create_until
|
|
|
|
end
|
2013-06-21 22:04:36 +02:00
|
|
|
|
|
|
|
def exclude_tasks_before(task)
|
|
|
|
tasks.where("due_date < '#{task.due_date}'").each do |t|
|
|
|
|
t.update_attribute(:periodic_task_group, nil)
|
|
|
|
end
|
|
|
|
end
|
2014-11-23 01:22:50 +01:00
|
|
|
|
2015-05-01 21:31:06 +02:00
|
|
|
def update_tasks_including(template_task, prev_due_date)
|
2015-05-01 21:14:16 +02:00
|
|
|
group_tasks = tasks + [template_task]
|
2015-05-01 21:31:06 +02:00
|
|
|
due_date_delta = template_task.due_date - prev_due_date
|
2015-05-01 21:14:16 +02:00
|
|
|
tasks.each do |task|
|
|
|
|
task.update!(name: template_task.name,
|
|
|
|
description: template_task.description,
|
|
|
|
duration: template_task.duration,
|
|
|
|
required_users: template_task.required_users,
|
2015-05-01 21:31:06 +02:00
|
|
|
workgroup: template_task.workgroup,
|
|
|
|
due_date: task.due_date + due_date_delta)
|
2015-05-01 21:14:16 +02:00
|
|
|
end
|
|
|
|
group_tasks.each do |task|
|
|
|
|
task.update_columns(periodic_task_group_id: self.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-23 01:22:50 +01:00
|
|
|
protected
|
|
|
|
|
|
|
|
# @return [Number] Number of days between two periodic tasks
|
|
|
|
def period_days
|
|
|
|
# minimum of one to avoid inifite loop when value is invalid
|
|
|
|
[FoodsoftConfig[:tasks_period_days].to_i, 1].max
|
|
|
|
end
|
2012-11-28 10:13:54 +01:00
|
|
|
end
|