From 72f3e40a0cfc70097c654037a49eca33f62f6328 Mon Sep 17 00:00:00 2001 From: "Xavier Bol (xbo)" Date: Wed, 17 Jan 2024 12:36:50 +0100 Subject: [PATCH] [FIX] sale{_timesheet}: avoid using allowed_so_line_ids Before this commit, the `allowed_so_line_ids` has been introduced to always use `_default_sale_line_domain` method in the domain of `so_line` field and so instead of easing the domain in the field definition we will be able to override the domain by overriding `_default_sale_line_domain` method. However, since in `sale_timesheet` module, the new domain will contain a dynamic value in the right part of a leaf (`('order_partner_id', 'child_of', commercial_partner_id)`), the `allowed_so_line_ids` is instead use in the domain of `so_line` field to easily add that leaf in the domain returned `_default_sale_line_domain` in the compute method of that field. The problem is the number of SOLs fetched could be really huge and caused a performance issues. This commit reverts the commit b7491b5b94a1c411fce55565c2bdbddb99b32de5 by - deprecating the `allowed_so_line_ids`, that is, that field will now always get an empty recordset. - deprecating the `_default_sale_line_domain` method, that is the method will no longer be used - manually adding the domain instead of calling `_default_sale_line_domain` - altering the domain when the `sale_timesheet` module is installed to get the same domain used in the `sale_line_id` of `project.task` model closes odoo/odoo#149747 Signed-off-by: Xavier Bol (xbo) --- addons/sale/models/analytic.py | 11 +++++------ addons/sale_timesheet/models/account.py | 10 +++++++++- addons/sale_timesheet/views/hr_timesheet_views.xml | 2 -- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/addons/sale/models/analytic.py b/addons/sale/models/analytic.py index 06ae2a552cb..5888e183ef9 100644 --- a/addons/sale/models/analytic.py +++ b/addons/sale/models/analytic.py @@ -7,21 +7,20 @@ from odoo import fields, models class AccountAnalyticLine(models.Model): _inherit = "account.analytic.line" + # [XBO] TODO: remove me in master allowed_so_line_ids = fields.Many2many('sale.order.line', compute='_compute_allowed_so_line_ids') - so_line = fields.Many2one('sale.order.line', string='Sales Order Item', domain="[('id', 'in', allowed_so_line_ids)]") + so_line = fields.Many2one('sale.order.line', string='Sales Order Item', domain=[('qty_delivered_method', '=', 'analytic')]) def _default_sale_line_domain(self): """ This is only used for delivered quantity of SO line based on analytic line, and timesheet (see sale_timesheet). This can be override to allow further customization. + [XBO] TODO: remove me in master """ - self.ensure_one() return [('qty_delivered_method', '=', 'analytic')] def _compute_allowed_so_line_ids(self): - for timesheet in self: - domain = timesheet._default_sale_line_domain() - timesheet.allowed_so_line_ids = self.env['sale.order.line'].search(domain) - + # [XBO] TODO: remove me in master + self.allowed_so_line_ids = False class AccountAnalyticApplicability(models.Model): _inherit = 'account.analytic.applicability' diff --git a/addons/sale_timesheet/models/account.py b/addons/sale_timesheet/models/account.py index 3e3444f3567..5a5f0fabafb 100644 --- a/addons/sale_timesheet/models/account.py +++ b/addons/sale_timesheet/models/account.py @@ -26,6 +26,13 @@ class AccountAnalyticLine(models.Model): commercial_partner_id = fields.Many2one('res.partner', compute="_compute_commercial_partner") timesheet_invoice_id = fields.Many2one('account.move', string="Invoice", readonly=True, copy=False, help="Invoice created from the timesheet", index='btree_not_null') so_line = fields.Many2one(compute="_compute_so_line", store=True, readonly=False, + domain="""[ + ('qty_delivered_method', 'in', ['analytic', 'timesheet']), + ('order_partner_id', '=', commercial_partner_id), + ('is_service', '=', True), + ('is_expense', '=', False), + ('state', '=', 'sale') + ]""", help="Sales order item to which the time spent will be added in order to be invoiced to your customer. Remove the sales order item for the timesheet entry to be non-billable.") # we needed to store it only in order to be able to groupby in the portal order_id = fields.Many2one(related='so_line.order_id', store=True, readonly=True, index=True) @@ -33,6 +40,7 @@ class AccountAnalyticLine(models.Model): allow_billable = fields.Boolean(related="project_id.allow_billable") def _default_sale_line_domain(self): + # [XBO] TODO: remove me in master return expression.OR([[ ('is_service', '=', True), ('is_expense', '=', False), @@ -40,8 +48,8 @@ class AccountAnalyticLine(models.Model): ('order_partner_id', 'child_of', self.sudo().commercial_partner_id.ids) ], super()._default_sale_line_domain()]) - @api.depends('commercial_partner_id') def _compute_allowed_so_line_ids(self): + # [XBO] TODO: remove me in master super()._compute_allowed_so_line_ids() @api.depends('project_id.partner_id.commercial_partner_id', 'task_id.partner_id.commercial_partner_id') diff --git a/addons/sale_timesheet/views/hr_timesheet_views.xml b/addons/sale_timesheet/views/hr_timesheet_views.xml index 35f41fa9766..09adfca6649 100644 --- a/addons/sale_timesheet/views/hr_timesheet_views.xml +++ b/addons/sale_timesheet/views/hr_timesheet_views.xml @@ -42,7 +42,6 @@ - @@ -57,7 +56,6 @@ -