Skip to content

Commit

Permalink
[FIX] hr_timesheet_overtime_rate: Populate hours_worked on installation
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Jul 11, 2024
1 parent 907d466 commit 90d017e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions hr_timesheet_overtime_rate/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions hr_timesheet_overtime_rate/data/data.xml
Original file line number Diff line number Diff line change
@@ -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>
19 changes: 0 additions & 19 deletions hr_timesheet_overtime_rate/migrations/16.0.1.0.0/post-upgrade.py

This file was deleted.

23 changes: 23 additions & 0 deletions hr_timesheet_overtime_rate/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

0 comments on commit 90d017e

Please sign in to comment.