From e6095f5f1b2e8390e920030d96889797b302bf8e Mon Sep 17 00:00:00 2001 From: Elouan Le Bars Date: Tue, 15 Oct 2019 18:12:08 +0200 Subject: [PATCH] [ADD] b_shift : wizard for attendance sheet generation --- beesdoo_shift/__openerp__.py | 1 + beesdoo_shift/models/attendance_sheet.py | 5 ++ beesdoo_shift/wizard/__init__.py | 3 +- .../wizard/generate_attendance_sheet.py | 78 +++++++++++++++++++ .../wizard/generate_attendance_sheet.xml | 37 +++++++++ beesdoo_shift/wizard/subscribe.xml | 2 +- 6 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 beesdoo_shift/wizard/generate_attendance_sheet.py create mode 100644 beesdoo_shift/wizard/generate_attendance_sheet.xml diff --git a/beesdoo_shift/__openerp__.py b/beesdoo_shift/__openerp__.py index 9e479de..ab92d23 100644 --- a/beesdoo_shift/__openerp__.py +++ b/beesdoo_shift/__openerp__.py @@ -35,6 +35,7 @@ "wizard/assign_super_coop.xml", "wizard/subscribe.xml", "wizard/extension.xml", + "wizard/generate_attendance_sheet.xml", "wizard/holiday.xml", "wizard/temporary_exemption.xml", ], diff --git a/beesdoo_shift/models/attendance_sheet.py b/beesdoo_shift/models/attendance_sheet.py index f61dc9c..a9c4986 100644 --- a/beesdoo_shift/models/attendance_sheet.py +++ b/beesdoo_shift/models/attendance_sheet.py @@ -1,4 +1,7 @@ # -*- coding: utf-8 -*- + +from lxml import etree + from openerp import models, exceptions, fields, api from openerp.exceptions import UserError, ValidationError @@ -140,6 +143,8 @@ class AttendanceSheetShiftAdded(models.Model): self.regular_task_type = False + + class AttendanceSheet(models.Model): _name = "beesdoo.shift.sheet" _inherit = ["mail.thread"] diff --git a/beesdoo_shift/wizard/__init__.py b/beesdoo_shift/wizard/__init__.py index 59863f5..7f92f15 100644 --- a/beesdoo_shift/wizard/__init__.py +++ b/beesdoo_shift/wizard/__init__.py @@ -1,7 +1,8 @@ import instanciate_planning +import generate_attendance_sheet import batch_template import assign_super_coop import subscribe import extension import holiday -import temporary_exemption \ No newline at end of file +import temporary_exemption diff --git a/beesdoo_shift/wizard/generate_attendance_sheet.py b/beesdoo_shift/wizard/generate_attendance_sheet.py new file mode 100644 index 0000000..2fe5dcb --- /dev/null +++ b/beesdoo_shift/wizard/generate_attendance_sheet.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +from openerp import models, fields, api, exceptions, _ + +from datetime import date, datetime, timedelta + + +class GenerateAttendanceSheet(models.TransientModel): + _name = "beesdoo.shift.sheet.generate" + _description = "Use a selected time range to generate the corresponding attendance sheet." + + time_range = fields.Selection("_get_time_ranges", string="Hours") + + def _get_time_ranges(self): + time_ranges = set() + tasks = self.env["beesdoo.shift.shift"] + sheets = self.env["beesdoo.shift.sheet"] + current_time = datetime.now() + allowed_time_range = timedelta(minutes=45) + + tasks = tasks.search( + [ + ("start_time", ">", str(current_time - allowed_time_range),), + ("start_time", "<", str(current_time + allowed_time_range),), + ] + ) + + for task in tasks: + start_time = task.start_time + end_time = task.end_time + sheets = sheets.search( + [("start_time", "=", start_time), ("end_time", "=", end_time),] + ) + + if len(sheets) == 0: + start_time_dt = fields.Datetime.from_string(start_time) + start_time_dt = fields.Datetime.context_timestamp( + self, start_time_dt + ) + end_time_dt = fields.Datetime.from_string(end_time) + end_time_dt = fields.Datetime.context_timestamp( + self, end_time_dt + ) + + # We display contextualized time range + # but we save it according to UTC timezone + time_ranges.add( + ( + start_time + "~" + end_time, + start_time_dt.strftime("%H:%M") + + " - " + + end_time_dt.strftime("%H:%M"), + ) + ) + return list(time_ranges) + + @api.multi + def button_generate(self): + self.ensure_one() + tasks = self.env["beesdoo.shift.shift"] + sheets = self.env["beesdoo.shift.sheet"] + if not self.time_range: + raise exceptions.UserError( + "Please select a time time_range to generate the sheet." + ) + time_range = self.time_range.split("~") + if len(time_range) != 2: + raise exceptions.ValidationError("Selection key has wrong format.") + sheet = sheets.create( + {"start_time": time_range[0], "end_time": time_range[1]} + ) + + return { + "type": "ir.actions.act_window", + "res_model": "beesdoo.shift.sheet", + "res_id": sheet.id, + "view_type": "form", + "view_mode": "form", + } diff --git a/beesdoo_shift/wizard/generate_attendance_sheet.xml b/beesdoo_shift/wizard/generate_attendance_sheet.xml new file mode 100644 index 0000000..7e0d43e --- /dev/null +++ b/beesdoo_shift/wizard/generate_attendance_sheet.xml @@ -0,0 +1,37 @@ + + + Generate an attendance sheet + beesdoo.shift.sheet.generate + +
+ + + +
+
+
+
+
+ + + + + + + + + +
diff --git a/beesdoo_shift/wizard/subscribe.xml b/beesdoo_shift/wizard/subscribe.xml index 97b0333..476c661 100644 --- a/beesdoo_shift/wizard/subscribe.xml +++ b/beesdoo_shift/wizard/subscribe.xml @@ -1,6 +1,6 @@ - Subscribre Cooperator + Subscribe Cooperator beesdoo.shift.subscribe