Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
32 changes: 32 additions & 0 deletions project_timesheet_time_control/models/account_analytic_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tools.sql import SQL


class AccountAnalyticLine(models.Model):
Expand All @@ -22,6 +23,7 @@ class AccountAnalyticLine(models.Model):
string="End Time",
compute="_compute_date_time_end",
inverse="_inverse_date_time_end",
search="_search_date_time_end",
)
show_time_control = fields.Selection(
selection=[("resume", "Resume"), ("stop", "Stop")],
Expand All @@ -32,6 +34,7 @@ class AccountAnalyticLine(models.Model):
@api.depends("date_time", "unit_amount", "product_uom_id")
def _compute_date_time_end(self):
hour_uom = self.env.ref("uom.product_uom_hour")
day_uom = self.env.ref("uom.product_uom_day")
for record in self:
if (
record.product_uom_id == hour_uom
Expand All @@ -41,6 +44,15 @@ def _compute_date_time_end(self):
record.date_time_end = record.date_time + relativedelta(
hours=record.unit_amount
)
elif (
record.product_uom_id == day_uom
and day_uom.factor == 1
and record.date_time
and record.unit_amount
):
record.date_time_end = record.date_time + relativedelta(
hours=record.unit_amount * hour_uom.factor
)
else:
record.date_time_end = record.date_time_end

Expand Down Expand Up @@ -125,3 +137,23 @@ def button_end_work(self):
)
line.unit_amount = line._duration(line.date_time, end)
return True

@api.model
def _search_date_time_end(self, operator, value):
# reference value is 1 day == 8 hours
hour_uom = self.env.ref("uom.product_uom_hour")

return [
(
"date_time",
operator,
SQL(
"%(start_time)s - account_analytic_line.unit_amount * "
"(select 1 / factor * %(day_factor)s "
"from uom_uom where id = account_analytic_line.product_uom_id) * "
"interval '1 hour'",
start_time=datetime.strptime(value, "%Y-%m-%d %H:%M:%S"),
day_factor=hour_uom.factor,
),
)
]
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,27 @@ def test_non_timesheet_analytic_line(self):
)
line.unit_amount = 500.0
self.assertFalse(line.date_time_end)

def test_search(self):
line = (
self.env["account.analytic.line"]
.with_user(self.user)
.create(
{
"date": date(2023, 1, 10),
"date_time": datetime(2023, 1, 10, 8, 0, 0),
"unit_amount": 2.0,
"project_id": self.project.id,
"name": "Test line",
}
)
)

result = self.env["account.analytic.line"].search(
[
("date_time_end", ">=", "2023-01-10 9:00:00"),
("date_time_end", "<=", "2023-01-10 11:00:00"),
]
)

self.assertIn(line, result)