diff --git a/project_timesheet_time_control/models/account_analytic_line.py b/project_timesheet_time_control/models/account_analytic_line.py index a90d73c773..89e359b775 100644 --- a/project_timesheet_time_control/models/account_analytic_line.py +++ b/project_timesheet_time_control/models/account_analytic_line.py @@ -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): @@ -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")], @@ -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 @@ -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 @@ -166,3 +178,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, + ), + ) + ] diff --git a/project_timesheet_time_control/tests/test_project_timesheet_time_control.py b/project_timesheet_time_control/tests/test_project_timesheet_time_control.py index cbd2d3b5ce..929348f58d 100644 --- a/project_timesheet_time_control/tests/test_project_timesheet_time_control.py +++ b/project_timesheet_time_control/tests/test_project_timesheet_time_control.py @@ -305,6 +305,30 @@ 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) + def test_onchange_date_time_with_hour_uom_and_dates(self): hour_uom = self.env.ref("uom.product_uom_hour") form = Form(