Allow configuration of task periods

This commit is contained in:
wvengen 2014-11-23 01:22:50 +01:00
parent 6e990fed4c
commit f6c008c79c
7 changed files with 43 additions and 10 deletions

View File

@ -1,8 +1,6 @@
class PeriodicTaskGroup < ActiveRecord::Base
has_many :tasks, dependent: :destroy
PeriodDays = 7
def has_next_task?
return false if tasks.empty?
return false if tasks.first.due_date.nil?
@ -11,13 +9,13 @@ class PeriodicTaskGroup < ActiveRecord::Base
def create_next_task
template_task = tasks.first
self.next_task_date ||= template_task.due_date + PeriodDays
self.next_task_date ||= template_task.due_date + period_days
next_task = template_task.dup
next_task.due_date = next_task_date
next_task.save
self.next_task_date += PeriodDays
self.next_task_date += period_days
self.save
end
@ -26,4 +24,12 @@ class PeriodicTaskGroup < ActiveRecord::Base
t.update_attribute(:periodic_task_group, nil)
end
end
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
end

View File

@ -36,8 +36,8 @@ class Task < ActiveRecord::Base
end
# find all tasks in the next week (or another number of days)
def self.next_assigned_tasks_for(user, number = 7)
# find all tasks in the period (or another number of days)
def self.next_assigned_tasks_for(user, number = FoodsoftConfig[:tasks_period_days].to_i)
user.tasks.undone.where(assignments: {accepted: true}).
where(["tasks.due_date >= ? AND tasks.due_date <= ?", Time.now, number.days.from_now])
end

View File

@ -1,3 +1,15 @@
-#= config_use_heading form, :use_tasks do
%fieldset
%label
%h4= t '.periodic_title'
= config_input form, :tasks_period_days do
.input-append
= config_input_field form, :tasks_period_days, as: :numeric, class: 'input-mini'
%span.add-on days
= config_input form, :tasks_upfront_days do
.input-append
= config_input_field form, :tasks_upfront_days, as: :numeric, class: 'input-mini'
%span.add-on days
= config_use_heading form, :use_apple_points do
= config_input form, :stop_ordering_under, as: :numeric, input_html: {class: 'input-small'}

View File

@ -55,14 +55,19 @@ default: &defaults
# Ordergroups, which have less than 75 apples should not be allowed to make new orders
# Comment out this option to activate this restriction
# stop_ordering_under: 75
#stop_ordering_under: 75
# Comment out to completely hide apple points (be sure to comment stop_ordering_under)
# use_apple_points: false
#use_apple_points: false
# ordergroups can only order when their balance is higher than or equal to this
# not fully enforced right now, since the check is only client-side
# minimum_balance: 0
#minimum_balance: 0
# how many days there are between two periodic tasks
#tasks_period_days: 7
# how many days upfront periodic tasks are created
#tasks_upfront_days: 49
# When use_nick is enabled, there will be a nickname field in the user form,
# and the option to show a nickname instead of full name to foodcoop members.

View File

@ -237,6 +237,8 @@ en:
pdf_title: PDF documents
tab_messages:
emails_title: Sending email
tab_tasks:
periodic_title: Periodic tasks
tabs:
title: Configuration
update:
@ -467,6 +469,8 @@ en:
price_markup: Percentage that is added to the gross price for foodcoop members.
stop_ordering_under: Members can only order when they have at least this many apple points.
tax_default: Default VAT percentage for new articles.
tasks_period_days: Number of days between two instances of a periodic task.
tasks_upfront_days: How many days upfront to schedule tasks from a periodic task.
tolerance_is_costly: Order as much of the member tolerance as possible (compared to only as much needed to fill the last box). Enabling this also includes the tolerance in the total price of the open member order.
use_apple_points: When the apple point system is enabled, members are required to do some tasks to be able to keep ordering.
use_messages: Allow members to communicate with each other within Foodsoft.
@ -502,6 +506,8 @@ en:
pdf_page_size: Page size
price_markup: Foodcoop margin
stop_ordering_under: Minimum apple points
tasks_period_days: Period
tasks_upfront_days: Create upfront
tax_default: Default VAT
time_zone: Time zone
tolerance_is_costly: Tolerance is costly

View File

@ -226,6 +226,8 @@ class FoodsoftConfig
currency_space: true,
foodsoft_url: 'https://github.com/foodcoops/foodsoft',
contact: {}, # avoid errors when undefined
tasks_period_days: 7,
tasks_upfront_days: 49,
# The following keys cannot, by default, be set by foodcoops themselves.
protected: {
multi_coop_install: true,

View File

@ -40,7 +40,9 @@ namespace :foodsoft do
task :create_upcoming_periodic_tasks => :environment do
for tg in PeriodicTaskGroup.all
if tg.has_next_task?
while tg.next_task_date.nil? or tg.next_task_date < Date.today + 50
create_until = Date.today + FoodsoftConfig[:tasks_upfront_days].to_i + 1
rake_say "creating until #{create_until}"
while tg.next_task_date.nil? or tg.next_task_date < create_until
tg.create_next_task
end
end