[WIP]allow_negative_leave_and_allocation

This commit is contained in:
2025-10-24 10:26:34 +02:00
parent f7c7496492
commit d340a931de
6 changed files with 133 additions and 13 deletions

View File

@@ -10,7 +10,7 @@ class HrLeave(models.Model):
@api.constrains('state', 'number_of_days', 'holiday_status_id')
def _check_holidays(self):
# On ne garde que les congés qui ne permettent pas le négatif
# Keep only leaves that do not allow negative balances
to_check = self.filtered(lambda h: not h.holiday_status_id.allows_negative)
if to_check:
super(HrLeave, to_check)._check_holidays()

View File

@@ -2,7 +2,12 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from datetime import time, timedelta
from odoo import api, fields, models
from odoo.tools.translate import _
from odoo.tools.float_utils import float_round
class HolidaysType(models.Model):
@@ -12,6 +17,23 @@ class HolidaysType(models.Model):
allows_negative = fields.Boolean(string='Allow Negative Leaves',
help="If checked, users request can exceed the allocated days and balance can go in negative.")
remaining_leaves_allowing_negative = fields.Float(
string="Remaining Leaves when Negative Allowed",
compute='_compute_remaining_leaves_allowing_negative',
)
def _compute_remaining_leaves_allowing_negative(self):
for holiday_type in self:
if holiday_type.allows_negative:
# if left != usable : remaining_leaves_allowing_negative = left + usable
if (holiday_type.max_leaves - holiday_type.virtual_leaves_taken) != holiday_type.virtual_remaining_leaves:
holiday_type.remaining_leaves_allowing_negative = holiday_type.max_leaves - holiday_type.virtual_leaves_taken + holiday_type.virtual_remaining_leaves
else:
# else : remaining_leaves_allowing_negative = left
holiday_type.remaining_leaves_allowing_negative = holiday_type.max_leaves - holiday_type.virtual_leaves_taken
else:
holiday_type.remaining_leaves_allowing_negative = None
@api.depends('requires_allocation')
def _compute_valid(self):
res = super()._compute_valid()
@@ -21,5 +43,39 @@ class HolidaysType(models.Model):
def _get_days_request(self):
res = super()._get_days_request()
print('res=', res)
res[1]['allows_negative'] = self.allows_negative
res[1]['remaining_leaves_negative'] = ('%.2f' % (self.remaining_leaves_allowing_negative)).rstrip('0').rstrip('.'),
# if left != usable : recalculate leaves_approved, virtual_remaining_leaves and usable_remaining_leaves
if self.allows_negative and ((self.max_leaves - self.virtual_leaves_taken) != self.virtual_remaining_leaves):
res[1]['virtual_remaining_leaves'] = ('%.2f' % (self.remaining_leaves_allowing_negative)).rstrip('0').rstrip('.'),
res[1]['usable_remaining_leaves'] = ('%.2f' % (self.remaining_leaves_allowing_negative)).rstrip('0').rstrip('.'),
res[1]['leaves_approved'] = ('%.2f' % (abs(self.virtual_remaining_leaves) - abs(self.leaves_taken))).rstrip('0').rstrip('.'),
return res
def name_get(self):
'''Override name_get to display remaining leaves in the selection list.'''
if not self.requested_name_get():
return super().name_get()
res = []
for record in self:
name = record.name
if record.requires_allocation == "yes" and not self._context.get('from_manager_leave_form'):
if record.allows_negative:
name = "%(name)s (%(count)s)" % {
'name': name,
'count': _('%g remaining out of %g') % (
float_round(record.remaining_leaves_allowing_negative, precision_digits=2) or 0.0,
float_round(record.max_leaves, precision_digits=2) or 0.0,
) + (_(' hours') if record.request_unit == 'hour' else _(' days'))
}
else :
name = "%(name)s (%(count)s)" % {
'name': name,
'count': _('%g remaining out of %g') % (
float_round(record.virtual_remaining_leaves, precision_digits=2) or 0.0,
float_round(record.max_leaves, precision_digits=2) or 0.0,
) + (_(' hours') if record.request_unit == 'hour' else _(' days'))
}
res.append((record.id, name))
return res