Skip to content
Snippets Groups Projects
Commit 85dec37a authored by jvm-odoo's avatar jvm-odoo
Browse files

[FIX] hr_holidays: fix leaves types order


In the leaves app, you can create an allocation request where you can
select a leave type.

Before this commit:

    - The leaves type order is not correct when you have a lot of them.
      You should have your remaining leaves on the top of the list
      and for the leaves who has 0 remaining they should be at the end
      of the list.

      When you click on search more, the order is correct.

After this commit:

    - The leaves type order is correct.

Odony's comment: Leave types are reordered after each search(), based on
some context-dependent, non-stored conditions. The problem is that the
initial search() applies the given *limit*, so the "post-sort" will only
sort a limited number of random result from the initial search,
rather than all types matching the domain.

If you have more than 7 leaves types matching the domain, you have a
chance that the m2o auto-completion on the leave request form will not
suggest the correct leave types, because the post-sort will not even
receive them. So you may not see a leave type for which you have
remaining leaves!

OPW-2089593

closes odoo/odoo#39158

Signed-off-by: default avatarNicolas Martinelli (nim) <nim@odoo.com>
Co-authored-by: default avatarodony <odo@odoo.com>
parent 93411358
No related branches found
No related tags found
No related merge requests found
......@@ -272,8 +272,11 @@ class HolidaysType(models.Model):
""" Override _search to order the results, according to some employee.
The order is the following
- allocation fixed first, then allowing allocation, then free allocation
- virtual remaining leaves (higher the better, so using reverse on sorted)
- allocation fixed (with remaining leaves),
- allowing allocation (with remaining leaves),
- no allocation,
- allocation fixed (without remaining leaves),
- allowing allocation (without remaining leaves).
This override is necessary because those fields are not stored and depends
on an employee_id given in context. This sort will be done when there
......@@ -281,11 +284,18 @@ class HolidaysType(models.Model):
to the method.
"""
employee_id = self._get_contextual_employee_id()
leave_ids = super(HolidaysType, self)._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid)
if not count and not order and employee_id:
leaves = self.browse(leave_ids)
sort_key = lambda l: (l.allocation_type == 'fixed', l.allocation_type == 'fixed_allocation', l.virtual_remaining_leaves)
return leaves.sorted(key=sort_key, reverse=True).ids
post_sort = (not count and not order and employee_id)
leave_ids = super(HolidaysType, self)._search(args, offset=offset, limit=(None if post_sort else limit), order=order, count=count, access_rights_uid=access_rights_uid)
leaves = self.browse(leave_ids)
if post_sort:
sort_key = lambda l: (
l.allocation_type == 'fixed' and l.virtual_remaining_leaves > 0 and l.max_leaves > 0,
l.allocation_type == 'fixed_allocation' and l.virtual_remaining_leaves > 0 and l.max_leaves > 0,
l.allocation_type == 'no',
l.allocation_type == 'fixed',
l.allocation_type == 'fixed_allocation'
)
return leaves.sorted(key=sort_key, reverse=True).ids[:limit]
return leave_ids
@api.multi
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment