-
-
Notifications
You must be signed in to change notification settings - Fork 349
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_begin_end: More extensible, use compute instead of onchange #692
Open
carmenbianca
wants to merge
5
commits into
OCA:16.0
Choose a base branch
from
coopiteasy:16.0-refactor-hr_timesheet_begin_end
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
764a1c0
[FIX] hr_timesheet_begin_end: Test uses timesheet lines now
carmenbianca 0985b0d
[REF] hr_timesheet_begin_end: Make more extensible
carmenbianca 55c67c2
[REF] hr_timesheet_begin_end: Refactor unit_amount to be a compute field
carmenbianca b601c05
[FIX] Add hr_timesheet_begin_end to rebel modules
carmenbianca 49de5a8
[IMP] simplify comment
huguesdk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Copyright 2015 Camptocamp SA - Guewen Baconnier | ||
# Copyright 2017 Tecnativa, S.L. - Luis M. Ontalba | ||
# Copyright 2024 Coop IT Easy SC - Carmen Bianca BAKKER | ||
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html | ||
|
||
from datetime import timedelta | ||
|
@@ -15,16 +16,28 @@ | |
time_start = fields.Float(string="Begin Hour") | ||
time_stop = fields.Float(string="End Hour") | ||
|
||
@api.constrains("time_start", "time_stop", "unit_amount") | ||
def _check_time_start_stop(self): | ||
for line in self: | ||
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html | ||
start = timedelta(hours=line.time_start) | ||
stop = timedelta(hours=line.time_stop) | ||
if stop < start: | ||
value_to_html(line.time_start, None) | ||
value_to_html(line.time_stop, None) | ||
# Override to be a computed field. | ||
unit_amount = fields.Float( | ||
compute="_compute_unit_amount", | ||
store=True, | ||
readonly=False, | ||
# remove the default of 0.0 to ensure it is computed when not | ||
# provided. if not computed (because project_id is False), None is | ||
# automatically converted to 0. | ||
default=None, | ||
) | ||
|
||
@api.depends("time_start", "time_stop", "project_id") | ||
def _compute_unit_amount(self): | ||
# Do not compute/adjust the unit_amount of non-timesheets. | ||
lines = self.filtered(lambda line: line.project_id) | ||
for line in lines: | ||
line.unit_amount = line.unit_amount_from_start_stop() | ||
carmenbianca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _validate_start_before_stop(self): | ||
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html | ||
for line in self: | ||
if line.time_stop < line.time_start: | ||
raise exceptions.ValidationError( | ||
_( | ||
"The beginning hour (%(html_start)s) must " | ||
|
@@ -35,7 +48,11 @@ | |
"html_stop": value_to_html(line.time_stop, None), | ||
} | ||
) | ||
hours = (stop - start).seconds / 3600 | ||
|
||
def _validate_unit_amount_equal_to_time_diff(self): | ||
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html | ||
for line in self: | ||
hours = line.unit_amount_from_start_stop() | ||
rounding = self.env.ref("uom.product_uom_hour").rounding | ||
if hours and float_compare( | ||
hours, line.unit_amount, precision_rounding=rounding | ||
|
@@ -50,16 +67,21 @@ | |
"html_hours": value_to_html(hours, None), | ||
} | ||
) | ||
# check if lines overlap | ||
others = self.search( | ||
[ | ||
("id", "!=", line.id), | ||
("employee_id", "=", line.employee_id.id), | ||
("date", "=", line.date), | ||
("time_start", "<", line.time_stop), | ||
("time_stop", ">", line.time_start), | ||
] | ||
) | ||
|
||
def _overlap_domain(self): | ||
self.ensure_one() | ||
return [ | ||
("id", "!=", self.id), | ||
("employee_id", "=", self.employee_id.id), | ||
("date", "=", self.date), | ||
("time_start", "<", self.time_stop), | ||
("time_stop", ">", self.time_start), | ||
] | ||
|
||
def _validate_no_overlap(self): | ||
value_to_html = self.env["ir.qweb.field.float_time"].value_to_html | ||
for line in self: | ||
others = self.search(line._overlap_domain()) | ||
if others: | ||
message = _("Lines can't overlap:\n") | ||
message += "\n".join( | ||
|
@@ -74,13 +96,28 @@ | |
) | ||
raise exceptions.ValidationError(message) | ||
|
||
@api.onchange("time_start", "time_stop") | ||
def onchange_hours_start_stop(self): | ||
start = timedelta(hours=self.time_start) | ||
stop = timedelta(hours=self.time_stop) | ||
@api.constrains("time_start", "time_stop", "unit_amount") | ||
def _check_time_start_stop(self): | ||
lines = self.filtered(lambda line: line.project_id) | ||
lines._validate_start_before_stop() | ||
lines._validate_unit_amount_equal_to_time_diff() | ||
lines._validate_no_overlap() | ||
|
||
@api.model | ||
def _hours_from_start_stop(self, time_start, time_stop): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if one of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this should happen because Floats default to 0 in Odoo. |
||
start = timedelta(hours=time_start) | ||
stop = timedelta(hours=time_stop) | ||
if stop < start: | ||
return | ||
self.unit_amount = (stop - start).seconds / 3600 | ||
# Invalid case, but return something sensible. | ||
return 0 | ||
return (stop - start).seconds / 3600 | ||
|
||
def unit_amount_from_start_stop(self): | ||
self.ensure_one() | ||
# Don't handle non-timesheet lines. | ||
if not self.project_id: | ||
return 0 | ||
return self._hours_from_start_stop(self.time_start, self.time_stop) | ||
|
||
def merge_timesheets(self): # pragma: no cover | ||
"""This method is needed in case hr_timesheet_sheet is installed""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed the test to use timesheet lines instead of bare analytic lines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactored the module to be more extensible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Changed ``unit_amount`` into a computed (stored, writeable) field. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn’t this have an inverse function instead of
readonly=False
? the documentation doesn’t mentionreadonly=False
(although the odoo code contains some of those) but states that to allow writing to a computed field, theinverse
parameter must be used. this would also allow to correctly computetime_stop
fromtime_start
andunit_amount
(but i don’t know whether this is actually useful).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed, i think.
compute="_method", store=True, readonly=False
is a pattern in odoo to compute whenever the requirements change, but to allow the user to override subsequently.