From 90d017efd76d87646535ad711ae440c1b799416a Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER <carmen@coopiteasy.be> Date: Thu, 11 Jul 2024 13:18:46 +0200 Subject: [PATCH] [FIX] hr_timesheet_overtime_rate: Populate hours_worked on installation Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be> --- hr_timesheet_overtime_rate/__manifest__.py | 1 + hr_timesheet_overtime_rate/data/data.xml | 13 +++++++++++ .../migrations/16.0.1.0.0/post-upgrade.py | 19 --------------- .../models/account_analytic_line.py | 23 +++++++++++++++++++ 4 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 hr_timesheet_overtime_rate/data/data.xml delete mode 100644 hr_timesheet_overtime_rate/migrations/16.0.1.0.0/post-upgrade.py diff --git a/hr_timesheet_overtime_rate/__manifest__.py b/hr_timesheet_overtime_rate/__manifest__.py index b8c3f79..d5c5e84 100644 --- a/hr_timesheet_overtime_rate/__manifest__.py +++ b/hr_timesheet_overtime_rate/__manifest__.py @@ -19,6 +19,7 @@ "excludes": [], "data": [ "security/ir.model.access.csv", + "data/data.xml", "views/resource_views.xml", "views/account_analytic_line_views.xml", "views/hr_timesheet_sheet_views.xml", diff --git a/hr_timesheet_overtime_rate/data/data.xml b/hr_timesheet_overtime_rate/data/data.xml new file mode 100644 index 0000000..202225e --- /dev/null +++ b/hr_timesheet_overtime_rate/data/data.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8" ?> +<!-- +SPDX-FileCopyrightText: 2024 Coop IT Easy SC + +SPDX-License-Identifier: AGPL-3.0-or-later +--> +<odoo noupdate="1"> + + <function model="account.analytic.line" name="_init_hours_worked"> + <function model="account.analytic.line" name="search" eval="[[]]" /> + </function> + +</odoo> diff --git a/hr_timesheet_overtime_rate/migrations/16.0.1.0.0/post-upgrade.py b/hr_timesheet_overtime_rate/migrations/16.0.1.0.0/post-upgrade.py deleted file mode 100644 index 9cc5489..0000000 --- a/hr_timesheet_overtime_rate/migrations/16.0.1.0.0/post-upgrade.py +++ /dev/null @@ -1,19 +0,0 @@ -# SPDX-FileCopyrightText: 2024 Coop IT Easy SC -# -# SPDX-License-Identifier: AGPL-3.0-or-later - - -def migrate(cr, version): - # This is not strictly true, but it's the best way to populate the field - # with sensible data. - # - # TODO: Does this run upon module installation? This needs to be run on - # module installation. - cr.execute( - # Perfect symmetry is joyous - """ - UPDATE account_analytic_line - SET hours_worked=unit_amount - WHERE project_id IS NOT NULL - """ - ) diff --git a/hr_timesheet_overtime_rate/models/account_analytic_line.py b/hr_timesheet_overtime_rate/models/account_analytic_line.py index d27e8a0..6371fb2 100644 --- a/hr_timesheet_overtime_rate/models/account_analytic_line.py +++ b/hr_timesheet_overtime_rate/models/account_analytic_line.py @@ -46,3 +46,26 @@ def rate_for_date(self, date): .rate or 1.0 ) + + def _init_hours_worked(self): + """Upon module installation (or manually any other time, if you really + want), this method is called to populate hours_worked with something + sensible, derived from unit_amount and the rate for that day. + """ + # We use an SQL query because we do not want to trigger computation upon + # writing to hours_worked. + query = """ + UPDATE account_analytic_line + SET hours_worked=CASE + """ + params = [] + + for line in self.filtered(lambda item: item.project_id): + rate = self.rate_for_date(line.date) + hours_worked = line.unit_amount / rate + query += "WHEN id=%s THEN %s " + params.extend([line.id, hours_worked]) + query += "END WHERE id IN %s" + + if params: + self.env.cr.execute(query, (*params, tuple(line.id for line in self)))