diff --git a/hr_timesheet_overtime/README.rst b/hr_timesheet_overtime/README.rst index c5b5d5f..b369862 100644 --- a/hr_timesheet_overtime/README.rst +++ b/hr_timesheet_overtime/README.rst @@ -29,6 +29,32 @@ Computes overtime hours according to employee's contracts. .. contents:: :local: +Known issues / Roadmap +====================== + +There is a problem with the way this module handles rates for overtime. If the +rate ever changes, things will start to break. + +At time of writing (2024-06-28), the way a rate is computed for a date is by +looking _exclusively_ at the corresponding day of the week. This should be more +robust. + +Furthermore, when inserting hours worked, the actual hours worked get lost. You +(try to) write a value to ``unit_amount``, but an augmented value gets written +to the field instead. This is rather ugly. + +We can improve this by relying on the computation of ``unit_amount`` in Odoo +≥16: create a new field ``hours_worked``, which contains the actual hours worked +sans rate. Then, compute ``unit_amount`` from ``hours_worked`` (in a more robust +fashion than is presently the case). In the interface, show ``hours_worked`` +more prominently than ``unit_amount`` as the main editable field. + +To make this module subsequently compatible with ``hr_timesheet_begin_end``, +``hours_worked`` must be computed from ``time_stop`` and ``time_start``, and +``unit_amount`` must use this module's computation method instead of +``hr_timesheet_begin_end``'s. The compatibility layer should go into its own +module. + Bug Tracker =========== diff --git a/hr_timesheet_overtime/models/account_analytic_line.py b/hr_timesheet_overtime/models/account_analytic_line.py index b4308c9..b4667b9 100644 --- a/hr_timesheet_overtime/models/account_analytic_line.py +++ b/hr_timesheet_overtime/models/account_analytic_line.py @@ -15,10 +15,11 @@ class AnalyticLine(models.Model): _inherit = "account.analytic.line" - @api.model - def create(self, values): - self._update_values(values) - return super().create(values) + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + self._update_values(vals) + return super().create(vals_list) @api.multi def write(self, values): @@ -32,17 +33,19 @@ def _update_values(self, values): Update values if date or unit_amount fields have changed """ if "date" in values or "unit_amount" in values: + # TODO: self.date and self.unit_amount do not exist when called from + # create(). date = values.get("date", self.date) unit_amount = values.get("unit_amount", self.unit_amount) + values["unit_amount"] = unit_amount * self.rate_for_date(date) - # rate management - weekday = fields.Date.from_string(date).weekday() - rate = ( - self.env["resource.overtime.rate"] - .search([("dayofweek", "=", weekday)], limit=1) - .rate - or 1.0 - ) - - # update - values["unit_amount"] = unit_amount * rate + @api.model + def rate_for_date(self, date): + # n.b. from_string also works on date objects, returning itself. + weekday = fields.Date.from_string(date).weekday() + return ( + self.env["resource.overtime.rate"] + .search([("dayofweek", "=", weekday)], limit=1) + .rate + or 1.0 + ) diff --git a/hr_timesheet_overtime/readme/ROADMAP.rst b/hr_timesheet_overtime/readme/ROADMAP.rst new file mode 100644 index 0000000..08e5a7f --- /dev/null +++ b/hr_timesheet_overtime/readme/ROADMAP.rst @@ -0,0 +1,22 @@ +There is a problem with the way this module handles rates for overtime. If the +rate ever changes, things will start to break. + +At time of writing (2024-06-28), the way a rate is computed for a date is by +looking _exclusively_ at the corresponding day of the week. This should be more +robust. + +Furthermore, when inserting hours worked, the actual hours worked get lost. You +(try to) write a value to ``unit_amount``, but an augmented value gets written +to the field instead. This is rather ugly. + +We can improve this by relying on the computation of ``unit_amount`` in Odoo +≥16: create a new field ``hours_worked``, which contains the actual hours worked +sans rate. Then, compute ``unit_amount`` from ``hours_worked`` (in a more robust +fashion than is presently the case). In the interface, show ``hours_worked`` +more prominently than ``unit_amount`` as the main editable field. + +To make this module subsequently compatible with ``hr_timesheet_begin_end``, +``hours_worked`` must be computed from ``time_stop`` and ``time_start``, and +``unit_amount`` must use this module's computation method instead of +``hr_timesheet_begin_end``'s. The compatibility layer should go into its own +module.