@@ -106,12 +106,60 @@ def _generate_next_planning(self):
106
106
_logger .error ("Could not generate next planning: no task template defined." )
107
107
return
108
108
109
- planning .task_template_ids ._generate_task_day ()
109
+ planning .task_template_ids .generate_task_day ()
110
110
111
111
next_date = planning ._get_next_planning_date (date )
112
112
config .set_param ("last_planning_seq" , planning .sequence )
113
113
config .set_param ("next_planning_date" , next_date )
114
114
115
+ @api .model
116
+ def get_future_shifts (self , end_date ):
117
+ """
118
+ Calculates shifts between now and end_date without
119
+ storing them in the database
120
+ Uses a list of shifts instead of a recordset because
121
+ of issues occuring when copying records
122
+ :param end_date: Datetime
123
+ :return: beesdoo.shift.shift list
124
+ """
125
+ start_date = datetime .now ()
126
+
127
+ shift_list = list (
128
+ self .env ["beesdoo.shift.shift" ]
129
+ .sudo ()
130
+ .search (
131
+ [("start_time" , ">" , start_date .strftime ("%Y-%m-%d %H:%M:%S" ))],
132
+ order = "start_time, task_template_id, task_type_id" ,
133
+ )
134
+ )
135
+
136
+ last_sequence = int (
137
+ self .env ["ir.config_parameter" ].sudo ().get_param ("last_planning_seq" )
138
+ )
139
+
140
+ next_planning = self ._get_next_planning (last_sequence )
141
+
142
+ next_planning_date = fields .Datetime .from_string (
143
+ self .env ["ir.config_parameter" ].sudo ().get_param ("next_planning_date" , 0 )
144
+ )
145
+
146
+ next_planning = next_planning .with_context (visualize_date = next_planning_date )
147
+
148
+ while next_planning_date < end_date :
149
+ for shift in next_planning .task_template_ids .get_task_day ():
150
+ if shift .start_time > start_date :
151
+ shift_list .append (shift )
152
+ next_planning_date = next_planning ._get_next_planning_date (
153
+ next_planning_date
154
+ )
155
+ last_sequence = next_planning .sequence
156
+ next_planning = self ._get_next_planning (last_sequence )
157
+ next_planning = next_planning .with_context (
158
+ visualize_date = next_planning_date
159
+ )
160
+
161
+ return shift_list
162
+
115
163
116
164
class TaskTemplate (models .Model ):
117
165
_name = "beesdoo.shift.template"
@@ -144,7 +192,7 @@ class TaskTemplate(models.Model):
144
192
domain = [("is_worker" , "=" , True )],
145
193
)
146
194
remaining_worker = fields .Integer (
147
- compute = "_compute_remaining" , store = True , string = "Remaining Spot "
195
+ compute = "_compute_remaining" , store = True , string = "Remaining Place "
148
196
)
149
197
active = fields .Boolean (default = True )
150
198
# For Kanban View Only
@@ -212,8 +260,12 @@ def _set_duration(self):
212
260
if self .start_time :
213
261
self .end_time = self .start_time + self .duration
214
262
215
- def _generate_task_day (self ):
216
- tasks = self .env ["beesdoo.shift.shift" ]
263
+ def _prepare_task_day (self ):
264
+ """
265
+ Generates a list of dict objects containing the informations
266
+ for the shifts to generate based on the template data
267
+ """
268
+ tasks = []
217
269
for rec in self :
218
270
for i in range (0 , rec .worker_nb ):
219
271
worker_id = rec .worker_ids [i ] if len (rec .worker_ids ) > i else False
@@ -234,7 +286,7 @@ def _generate_task_day(self):
234
286
and status .temporary_exempt_end_date >= rec .end_date .date ()
235
287
):
236
288
worker_id = False
237
- tasks |= tasks . create (
289
+ tasks . append (
238
290
{
239
291
"name" : "[%s] %s %s (%s - %s) [%s]"
240
292
% (
@@ -258,6 +310,34 @@ def _generate_task_day(self):
258
310
259
311
return tasks
260
312
313
+ @api .multi
314
+ def get_task_day (self ):
315
+ """
316
+ Creates the shifts according to the template without saving
317
+ them into the database.
318
+ To adapt the behaviour, function _prepare_task_day()
319
+ should be overwritten.
320
+ """
321
+ tasks = self .env ["beesdoo.shift.shift" ]
322
+ task_list = self ._prepare_task_day ()
323
+ for task in task_list :
324
+ tasks |= tasks .new (task )
325
+ return tasks
326
+
327
+ @api .multi
328
+ def generate_task_day (self ):
329
+ """
330
+ Creates the shifts according to the template and saves
331
+ them into the database.
332
+ To adapt the behaviour, function _prepare_task_day()
333
+ should be overwritten.
334
+ """
335
+ tasks = self .env ["beesdoo.shift.shift" ]
336
+ task_list = self ._prepare_task_day ()
337
+ for task in task_list :
338
+ tasks |= tasks .create (task )
339
+ return tasks
340
+
261
341
@api .onchange ("worker_ids" )
262
342
def check_for_multiple_shifts (self ):
263
343
original_ids = {worker .id for worker in self ._origin .worker_ids }
0 commit comments