Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] hr_timesheet: avoid setting an archived employee to a timesheet in the form view #94308

Conversation

mafo-odoo
Copy link
Contributor

@mafo-odoo mafo-odoo commented Jun 22, 2022

Steps to reproduce:

  • Install timesheet
  • Archive an employee
  • Create a timesheet

Current behavior:
You can select the archived employee

Expected behavior:
You can not select the archived employee

Explanation:
In commit 17b2b07 the employee_id
field of the timesheet was set to have context active_test to false.
To fix this issue we reset the context for this field in every
timesheet form.

A task exist to fix the issue on master id 2884736

opw-2887727
opw-2870739

@robodoo
Copy link
Contributor

robodoo commented Jun 22, 2022

@mafo-odoo
Copy link
Contributor Author

linked on enterprise to https://github.com/odoo/enterprise/pull/28749

@mafo-odoo mafo-odoo force-pushed the 15.0-opw-2870739-archived-employee-in-timesheet-mafo branch from 3a2a7f9 to 1708faa Compare June 22, 2022 13:00
@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Jun 22, 2022
@mafo-odoo mafo-odoo marked this pull request as ready for review June 22, 2022 13:25
@C3POdoo C3POdoo requested a review from a team June 22, 2022 13:27
Copy link
Contributor

@xavierbol xavierbol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, it seems good to me. However, I don't understand why we will block only in master, the creation of Timesheet with an archived employee on it. 🤔
Do you know why we don't do this in stable too? 🤔

addons/hr_timesheet/views/hr_timesheet_views.xml Outdated Show resolved Hide resolved
@mafo-odoo
Copy link
Contributor Author

However, I don't understand why we will block only in master, the creation of Timesheet with an archived employee on it. Do you know why we don't do this in stable too?

That's a good question I don't have the answer for, maybe @auon-odoo or @aju-odoo will be able to answer you as they created the other task.
Maybe it was to avoid breaking stuff in stable with a risky modification.

@mafo-odoo mafo-odoo changed the title [FIX] hr_timesheet: limit timesheet form to active employees [FIX] hr_timesheet: void setting an archived employee to a timesheet in the form view Jun 23, 2022
@mafo-odoo mafo-odoo changed the title [FIX] hr_timesheet: void setting an archived employee to a timesheet in the form view [FIX] hr_timesheet: avoid setting an archived employee to a timesheet in the form view Jun 23, 2022
@mafo-odoo mafo-odoo force-pushed the 15.0-opw-2870739-archived-employee-in-timesheet-mafo branch 2 times, most recently from 2e59384 to 7e154d1 Compare June 23, 2022 08:41
@mafo-odoo
Copy link
Contributor Author

@xavierbol

@xavierbol
Copy link
Contributor

? 🤔

Also it seems you have some changes unrelated to the issue described on this PR.

@mafo-odoo
Copy link
Contributor Author

Also it seems you have some changes unrelated to the issue described on this PR.

Which changes do you speak of?

? 🤔

Does the current PR need some other changes?

@xavierbol
Copy link
Contributor

Strange I saw other changes unrelated, but it is no longer the case. 🤔

Ah, you didn't ask to @aju-odoo to know that? 🤯

@mafo-odoo
Copy link
Contributor Author

Ah, you didn't ask to @aju-odoo to know that? exploding_head

Seeing the changes of @auon-odoo in https://github.com/odoo/odoo/pull/93823/files I just guessed they were to big for stable.

@xavierbol
Copy link
Contributor

I didn't ask to copy/paste the work of @auon-odoo 😅
Moreover, his work is not finished. 🤔

You could just check if the employee who will be assigned to the timesheet is archived or not, for instance...

@xavierbol
Copy link
Contributor

xavierbol commented Jun 27, 2022

So, you asked to @aju-odoo if you have to manage it and after that you decide to not do it because the code of @auon-odoo is too complex to do it in stable? 🤔

@mafo-odoo
Copy link
Contributor Author

I didn't ask to copy/paste the work of @auon-odoo 😅

And yet I feel like it is what you want when you say

You could just check if the employee who will be assigned to the timesheet is archived or not, for instance...

As it is what he is doing but anyway you were right, after discussion with @aju-odoo, she will just ask @auon-odoo to retarget stable with his modifications.

@mafo-odoo
Copy link
Contributor Author

Will be fixed by @auon-odoo with task 2884736

@mafo-odoo mafo-odoo closed this Jun 27, 2022
@xavierbol
Copy link
Contributor

I think @auon-odoo wants to refactor it and also manages others cases since the HR Team added the possibility to the user to easily create an employee and link it to a user. Indeed, his task has the same purpose than for fix but in master, he could refactor the code to try to improve the maintainability of our code. So, I agree it is not a good idea to use his code in stable for the moment but it is not a reason to do nothing to fix it, that's why I asked you if you had discussed with @aju-odoo about that since you pinged without any context.

It is a bit pity that you decide to do nothing or cancel your fix without discussing with us (in private or here) to know what you have to do if really you don't understand what you have to do. Actually, you just had to add a check to prevent the user setting an archived employees instead of copy/paste the work from @auon-odoo.

So, I guess this additional diff was sufficient for your bug fix:

diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py
index ceb9d35dc74..d77a6e86fad 100644
--- a/addons/hr_timesheet/models/hr_timesheet.py
+++ b/addons/hr_timesheet/models/hr_timesheet.py
@@ -129,6 +129,7 @@ class AccountAnalyticLine(models.Model):
         for employee in employees:
             employee_for_user_company[employee.user_id.id][employee.company_id.id] = employee.id
 
+        employee_ids = set()
         for vals in vals_list:
             # compute employee only for timesheet lines, makes no sense for other lines
             if not vals.get('employee_id') and vals.get('project_id'):
@@ -137,6 +138,11 @@ class AccountAnalyticLine(models.Model):
                     continue
                 company_id = list(employee_for_company)[0] if len(employee_for_company) == 1 else vals.get('company_id', self.env.company.id)
                 vals['employee_id'] = employee_for_company.get(company_id, False)
+            elif vals.get('employee_id'):
+                employee_ids.add(vals['employee_id'])
+
+        if any(not emp.active for emp in self.env['hr.employee'].browse(list(employee_ids))):
+            raise UserError(_('Timesheets must be created with an active employee.'))
 
         lines = super(AccountAnalyticLine, self).create(vals_list)
         for line, values in zip(lines, vals_list):
@@ -150,6 +156,10 @@ class AccountAnalyticLine(models.Model):
             raise AccessError(_("You cannot access timesheets that are not yours."))
 
         values = self._timesheet_preprocess(values)
+        if values.get('employee_id'):
+            employee = self.env['hr.employee'].browse(values['employee_id'])
+            if not employee.active:
+                raise UserError(_('You cannot set an archived employee to the existing timesheets.'))
         if 'name' in values and not values.get('name'):
             values['name'] = '/'
         result = super(AccountAnalyticLine, self).write(values)

(I didn't test this code, it is just a suggestion and this diff is done in master)

We could indeed waiting the @auon-odoo if you prefer but he is off this week so your customer will not have a fix before the next week or next 2 weeks.

cc @aju-odoo @IT-Ideas

@mafo-odoo mafo-odoo reopened this Jun 27, 2022
@mafo-odoo mafo-odoo force-pushed the 15.0-opw-2870739-archived-employee-in-timesheet-mafo branch from 7e154d1 to df6f2ed Compare June 28, 2022 07:12
… in the form view

Steps to reproduce:
- Install timesheet
- Archive an employee
- Create a timesheet

Current behavior:
You can select the archived employee

Expected behavior:
You can not select the archived employee

Explanation:
In commit 17b2b07 the employee_id
field of the timesheet was set to have context active_test to false.
To fix this issue we reset the context for this field in every
timesheet form and we add user errors if the employee is not active
while the timesheet is created or edited.

opw-2887727
opw-2870739
@mafo-odoo mafo-odoo force-pushed the 15.0-opw-2870739-archived-employee-in-timesheet-mafo branch from df6f2ed to fce748b Compare June 29, 2022 06:46
@mafo-odoo
Copy link
Contributor Author

Hi @xavierbol! I think it should be ok now :)

Copy link
Contributor

@xavierbol xavierbol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, I have just one comment. After that, it will look good to me. I let @IT-Ideas doing the final review. 🙂

Comment on lines +423 to +424
''' the timesheet can be created or edited only with an active employee
'''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
''' the timesheet can be created or edited only with an active employee
'''
""" test cannot create/update timesheet with archived employee """

Copy link
Contributor

@IT-Ideas IT-Ideas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix 🙂
@robodoo r+

@fw-bot
Copy link
Contributor

fw-bot commented Jul 4, 2022

This pull request has forward-port PRs awaiting action (not merged or closed): #95011, #95016

@fw-bot
Copy link
Contributor

fw-bot commented Jul 5, 2022

This pull request has forward-port PRs awaiting action (not merged or closed): #95016

1 similar comment
@fw-bot
Copy link
Contributor

fw-bot commented Jul 6, 2022

This pull request has forward-port PRs awaiting action (not merged or closed): #95016

@fw-bot
Copy link
Contributor

fw-bot commented Jul 7, 2022

This pull request has forward-port PRs awaiting action (not merged or closed): #95442, #95453

@fw-bot
Copy link
Contributor

fw-bot commented Jul 8, 2022

This pull request has forward-port PRs awaiting action (not merged or closed): #95453

@fw-bot fw-bot deleted the 15.0-opw-2870739-archived-employee-in-timesheet-mafo branch July 14, 2022 17:46
@xavierbol xavierbol mentioned this pull request Aug 18, 2022
nboulif pushed a commit to odoo-dev/odoo that referenced this pull request Aug 18, 2022
… in the form view

Steps to reproduce:
- Install timesheet
- Archive an employee
- Create a timesheet

Current behavior:
You can select the archived employee

Expected behavior:
You can not select the archived employee

Explanation:
In commit 17b2b07 the employee_id
field of the timesheet was set to have context active_test to false.
To fix this issue we reset the context for this field in every
timesheet form and we add user errors if the employee is not active
while the timesheet is created or edited.

opw-2887727
opw-2870739

closes odoo#94308

Related: odoo/enterprise#28749
Signed-off-by: Laurent Stukkens (ltu) <ltu@odoo.com>
nboulif pushed a commit to odoo-dev/odoo that referenced this pull request Aug 26, 2022
… in the form view

Steps to reproduce:
- Install timesheet
- Archive an employee
- Create a timesheet

Current behavior:
You can select the archived employee

Expected behavior:
You can not select the archived employee

Explanation:
In commit 17b2b07 the employee_id
field of the timesheet was set to have context active_test to false.
To fix this issue we reset the context for this field in every
timesheet form and we add user errors if the employee is not active
while the timesheet is created or edited.

backport of odoo#94308

opw-2887727
opw-2870739
robodoo pushed a commit that referenced this pull request Aug 29, 2022
… in the form view

Steps to reproduce:
- Install timesheet
- Archive an employee
- Create a timesheet

Current behavior:
You can select the archived employee

Expected behavior:
You can not select the archived employee

Explanation:
In commit 17b2b07 the employee_id
field of the timesheet was set to have context active_test to false.
To fix this issue we reset the context for this field in every
timesheet form and we add user errors if the employee is not active
while the timesheet is created or edited.

backport of #94308

opw-2887727
opw-2870739

closes #97760

Related: odoo/enterprise#30271
Signed-off-by: Nasreddin Boulif (bon) <bon@odoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants