Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][REF] hr_timesheet_overtime #32

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions hr_timesheet_overtime/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging

from odoo import fields, models
from odoo import api, fields, models

_logger = logging.getLogger(__name__)

Expand All @@ -15,9 +15,11 @@ class AnalyticLine(models.Model):

_inherit = "account.analytic.line"

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)

def write(self, values):
if not self.env.context.get("create"): # sale module
Expand All @@ -29,17 +31,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)

# 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
values["unit_amount"] = unit_amount * self.rate_for_date(date)

@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
)
Loading