From ec01c4b08975c30f632f72b0b89a0361e58dffce Mon Sep 17 00:00:00 2001 From: "Denis Vermylen (dve)" Date: Fri, 5 Aug 2016 17:30:02 +0200 Subject: [PATCH] [REF] hr_timesheet_sheet: replace workflow by python code Note that to copy the workflow, just after creating the state is updated to draft. The 'New' state is not used anymore in the flow. It could probably be cleaned. However in this task we only remove the workflows without changing their behavior to avoid unwanted side effects. --- .../models/hr_timesheet_sheet.py | 4 +- .../tests/test_hr_timesheet_sheet.py | 4 +- addons/hr_timesheet_sheet/__openerp__.py | 1 - .../data/hr_timesheet_workflow.xml | 62 ------------------- .../models/hr_timesheet_sheet.py | 31 +++++++--- .../views/hr_timesheet_sheet_views.xml | 8 +-- 6 files changed, 30 insertions(+), 80 deletions(-) delete mode 100644 addons/hr_timesheet_sheet/data/hr_timesheet_workflow.xml diff --git a/addons/hr_timesheet_attendance/models/hr_timesheet_sheet.py b/addons/hr_timesheet_attendance/models/hr_timesheet_sheet.py index 653f19d8ce0..acdd32820f4 100644 --- a/addons/hr_timesheet_attendance/models/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_attendance/models/hr_timesheet_sheet.py @@ -66,12 +66,12 @@ class HrTimesheetSheet(models.Model): } @api.multi - def button_confirm(self): + def action_timesheet_confirm(self): for sheet in self: sheet.check_employee_attendance_state() di = sheet.user_id.company_id.timesheet_max_difference if (abs(sheet.total_difference) <= di) or not di: - return super(HrTimesheetSheet, self).button_confirm() + return super(HrTimesheetSheet, self).action_timesheet_confirm() else: raise UserError(_('Please verify that the total difference of the sheet is lower than %.2f.') % (di,)) diff --git a/addons/hr_timesheet_attendance/tests/test_hr_timesheet_sheet.py b/addons/hr_timesheet_attendance/tests/test_hr_timesheet_sheet.py index 44c26482bd3..443c9d9339e 100644 --- a/addons/hr_timesheet_attendance/tests/test_hr_timesheet_sheet.py +++ b/addons/hr_timesheet_attendance/tests/test_hr_timesheet_sheet.py @@ -48,7 +48,7 @@ class TestHrTimesheetSheet(TransactionCase): # I confirm Gilles' timesheet with over 1 hour difference # in attendance and actual worked hours try: - self.test_timesheet_sheet.button_confirm() + self.test_timesheet_sheet.action_timesheet_confirm() except Exception: pass @@ -67,7 +67,7 @@ class TestHrTimesheetSheet(TransactionCase): # I confirm Gilles' timesheet with less than 1 hour difference # in attendance and actual worked hours - self.test_timesheet_sheet.button_confirm() + self.test_timesheet_sheet.action_timesheet_confirm() # I check the state is confirmed assert self.test_timesheet_sheet.state == 'confirm' diff --git a/addons/hr_timesheet_sheet/__openerp__.py b/addons/hr_timesheet_sheet/__openerp__.py index 5144083be7c..b49e849c4bc 100644 --- a/addons/hr_timesheet_sheet/__openerp__.py +++ b/addons/hr_timesheet_sheet/__openerp__.py @@ -30,7 +30,6 @@ The validation can be configured in the company: 'data': [ 'security/ir.model.access.csv', 'security/hr_timesheet_sheet_security.xml', - 'data/hr_timesheet_workflow.xml', 'data/hr_timesheet_sheet_data.xml', 'views/hr_timesheet_sheet_templates.xml', 'views/hr_timesheet_sheet_views.xml', diff --git a/addons/hr_timesheet_sheet/data/hr_timesheet_workflow.xml b/addons/hr_timesheet_sheet/data/hr_timesheet_workflow.xml deleted file mode 100644 index 0f2e61f012a..00000000000 --- a/addons/hr_timesheet_sheet/data/hr_timesheet_workflow.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - hr_timesheet_sheet.sheet - hr_timesheet_sheet.sheet - True - - - - - new - function - write({'state':'new'}) - True - - - - draft - function - write({'state':'draft'}) - - - - confirm - function - write({'state':'confirm'}) - - - - done - function - write({'state':'done'}) - True - - - - - - - - - - - confirm - - - - - - done - - - - - - cancel - - - diff --git a/addons/hr_timesheet_sheet/models/hr_timesheet_sheet.py b/addons/hr_timesheet_sheet/models/hr_timesheet_sheet.py index caf3dc2d8bf..af694054355 100644 --- a/addons/hr_timesheet_sheet/models/hr_timesheet_sheet.py +++ b/addons/hr_timesheet_sheet/models/hr_timesheet_sheet.py @@ -56,6 +56,8 @@ class HrTimesheetSheet(models.Model): readonly=True, states={ 'draft': [('readonly', False)], 'new': [('readonly', False)]}) + # state is created in 'new', automatically goes to 'draft' when created. Then 'new' is never used again ... + # (=> 'new' is completely useless) state = fields.Selection([ ('new', 'New'), ('draft', 'Open'), @@ -102,7 +104,9 @@ class HrTimesheetSheet(models.Model): if 'employee_id' in vals: if not self.env['hr.employee'].browse(vals['employee_id']).user_id: raise UserError(_('In order to create a timesheet for this employee, you must link him/her to a user.')) - return super(HrTimesheetSheet, self).create(vals) + res = super(HrTimesheetSheet, self).create(vals) + res.write({'state': 'draft'}) + return res @api.multi def write(self, vals): @@ -114,19 +118,28 @@ class HrTimesheetSheet(models.Model): return super(HrTimesheetSheet, self).write(vals) @api.multi - def button_confirm(self): - for sheet in self: - if sheet.employee_id and sheet.employee_id.parent_id and sheet.employee_id.parent_id.user_id: - self.message_subscribe_users(user_ids=[sheet.employee_id.parent_id.user_id.id]) - sheet.signal_workflow('confirm') + def action_timesheet_draft(self): + if not self.env.user.has_group('base.group_hr_user'): + raise UserError(_('Only an HR Officer or Manager can refuse timesheets or reset them to draft.')) + self.write({'state': 'draft'}) return True @api.multi - def action_set_to_draft(self): - self.write({'state': 'draft'}) - self.create_workflow() + def action_timesheet_confirm(self): + for sheet in self: + if sheet.employee_id and sheet.employee_id.parent_id and sheet.employee_id.parent_id.user_id: + self.message_subscribe_users(user_ids=[sheet.employee_id.parent_id.user_id.id]) + self.write({'state': 'confirm'}) return True + @api.multi + def action_timesheet_done(self): + if not self.env.user.has_group('base.group_hr_user'): + raise UserError(_('Only an HR Officer or Manager can approve timesheets.')) + if self.filtered(lambda sheet: sheet.state != 'confirm'): + raise UserError(_("Cannot approve a non-submitted timesheet.")) + self.write({'state': 'done'}) + @api.multi def name_get(self): # week number according to ISO 8601 Calendar diff --git a/addons/hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml b/addons/hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml index 3c67c4113b3..c308e1ccaa5 100644 --- a/addons/hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml +++ b/addons/hr_timesheet_sheet/views/hr_timesheet_sheet_views.xml @@ -57,10 +57,10 @@
-