|
| 1 | +# Copyright 2024 Janik von Rotz <[email protected]> |
| 2 | +# Copyright 2024 Camptocamp |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
1 | 4 | import logging
|
2 | 5 |
|
3 | 6 | from odoo import _, fields, models
|
| 7 | +from odoo.tools.float_utils import float_round |
4 | 8 |
|
5 |
| -_logger = logging.getLogger(__name__) |
6 |
| -from datetime import datetime |
| 9 | +from odoo.addons.resource.models.utils import HOURS_PER_DAY |
7 | 10 |
|
8 |
| -from odoo.tools.float_utils import float_round |
| 11 | +_logger = logging.getLogger(__name__) |
9 | 12 |
|
10 | 13 |
|
11 | 14 | class HolidaysAllocation(models.Model):
|
@@ -36,72 +39,49 @@ def _get_number_of_days_and_hours(self, date_from, date_to, employee_id):
|
36 | 39 | )[employee.id]
|
37 | 40 |
|
38 | 41 | def _compute_remaining_leaves(self):
|
39 |
| - now = fields.Datetime.now() |
40 |
| - now = datetime.combine(now, datetime.min.time()) |
41 |
| - |
| 42 | + all_consumed_leaves = self.employee_id._get_consumed_leaves( |
| 43 | + self.holiday_status_id |
| 44 | + )[0] |
| 45 | + all_consumed_leaves_current = self.employee_id._get_consumed_leaves( |
| 46 | + self.holiday_status_id, ignore_future=True |
| 47 | + )[0] |
42 | 48 | for allocation in self:
|
43 |
| - # Get all validated leaves filtered by employee and leave type |
44 |
| - leaves = self.env["hr.leave"].search( |
45 |
| - [ |
46 |
| - ("employee_id", "=", allocation.employee_id.id), |
47 |
| - ("state", "=", "validate"), |
48 |
| - ("holiday_status_id", "=", allocation.holiday_status_id.id), |
49 |
| - "|", |
50 |
| - ("holiday_allocation_id", "=", allocation.id), |
51 |
| - ("holiday_allocation_id", "=", False), |
52 |
| - ] |
| 49 | + consumes_allo = all_consumed_leaves[allocation.employee_id][ |
| 50 | + allocation.holiday_status_id |
| 51 | + ][allocation] |
| 52 | + consumes_allo_current = all_consumed_leaves_current[allocation.employee_id][ |
| 53 | + allocation.holiday_status_id |
| 54 | + ][allocation] |
| 55 | + allocation_calendar = ( |
| 56 | + allocation.holiday_status_id.company_id.resource_calendar_id |
53 | 57 | )
|
54 |
| - |
55 |
| - # Set the remaining leaves |
56 |
| - allocation.remaining_leaves_hours = ( |
57 |
| - allocation.number_of_hours_display |
58 |
| - - sum(leaves.mapped("number_of_hours_display")) |
59 |
| - ) |
60 |
| - allocation.remaining_leaves_days = allocation.number_of_days - sum( |
61 |
| - leaves.mapped("number_of_days") |
62 |
| - ) |
63 |
| - |
64 |
| - # Get past leaves |
65 |
| - past_leaves = leaves.filtered(lambda l: l.date_to < now) |
66 |
| - past_leave_hours = sum(past_leaves.mapped("number_of_hours_display")) |
67 |
| - past_leave_days = sum(past_leaves.mapped("number_of_days")) |
68 |
| - |
69 |
| - # Check for leaves that are active and calculate the exact number of hours and days |
70 |
| - active_leave_hours = 0 |
71 |
| - active_leave_days = 0 |
72 |
| - for leave in leaves.filtered(lambda l: l.date_from < now < l.date_to): |
73 |
| - result = self._get_number_of_days_and_hours( |
74 |
| - leave.date_from, now, leave.employee_id.id |
75 |
| - ) |
76 |
| - active_leave_days = result["days"] |
77 |
| - active_leave_hours = result["hours"] |
78 |
| - |
79 |
| - allocation.remaining_leaves_current_hours = ( |
80 |
| - allocation.number_of_hours_display |
81 |
| - - past_leave_hours |
82 |
| - - active_leave_hours |
83 |
| - ) |
84 |
| - allocation.remaining_leaves_current_days = ( |
85 |
| - allocation.number_of_days - past_leave_days - active_leave_days |
| 58 | + if allocation.holiday_type == "employee" and allocation.employee_id: |
| 59 | + allocation_calendar = allocation.employee_id.sudo().resource_calendar_id |
| 60 | + allocation.remaining_leaves_days = consumes_allo["remaining_leaves"] |
| 61 | + allocation.remaining_leaves_hours = consumes_allo["remaining_leaves"] * ( |
| 62 | + allocation_calendar.hours_per_day or HOURS_PER_DAY |
86 | 63 | )
|
| 64 | + allocation.remaining_leaves_current_days = consumes_allo_current[ |
| 65 | + "remaining_leaves" |
| 66 | + ] |
| 67 | + allocation.remaining_leaves_current_hours = consumes_allo_current[ |
| 68 | + "remaining_leaves" |
| 69 | + ] * (allocation_calendar.hours_per_day or HOURS_PER_DAY) |
87 | 70 |
|
88 | 71 | def _compute_remaining_leaves_display(self):
|
89 | 72 | for allocation in self:
|
90 |
| - allocation.remaining_leaves_display = "%g %s" % ( |
91 |
| - ( |
92 |
| - float_round(allocation.remaining_leaves_hours, precision_digits=2) |
93 |
| - if allocation.type_request_unit == "hour" |
94 |
| - else float_round( |
95 |
| - allocation.remaining_leaves_days, precision_digits=2 |
96 |
| - ) |
97 |
| - ), |
| 73 | + allocation.remaining_leaves_display = "{} {}".format( |
| 74 | + float_round(allocation.remaining_leaves_hours, precision_digits=2) |
| 75 | + if allocation.type_request_unit == "hour" |
| 76 | + else float_round(allocation.remaining_leaves_days, precision_digits=2), |
98 | 77 | _("hours") if allocation.type_request_unit == "hour" else _("days"),
|
99 | 78 | )
|
100 | 79 |
|
101 |
| - allocation.remaining_leaves_current_display = "%g %s" % ( |
| 80 | + allocation.remaining_leaves_current_display = "{} {}".format( |
102 | 81 | (
|
103 | 82 | float_round(
|
104 |
| - allocation.remaining_leaves_current_hours, precision_digits=2 |
| 83 | + allocation.remaining_leaves_current_hours, |
| 84 | + precision_digits=2, |
105 | 85 | )
|
106 | 86 | if allocation.type_request_unit == "hour"
|
107 | 87 | else float_round(
|
|
0 commit comments