Skip to content

Commit

Permalink
[FIX] sale{_timesheet}: avoid using allowed_so_line_ids
Browse files Browse the repository at this point in the history
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 b7491b5 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#149747

Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
  • Loading branch information
xavierbol committed Jan 18, 2024
1 parent 04a894c commit 72f3e40
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
11 changes: 5 additions & 6 deletions addons/sale/models/analytic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
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'
Expand Down
10 changes: 9 additions & 1 deletion addons/sale_timesheet/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,30 @@ 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)
is_so_line_edited = fields.Boolean("Is Sales Order Item Manually Edited")
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),
('state', '=', 'sale'),
('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')
Expand Down
2 changes: 0 additions & 2 deletions addons/sale_timesheet/views/hr_timesheet_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<field name="commercial_partner_id" column_invisible="True" groups="sales_team.group_sale_salesman"/>
<field name="is_so_line_edited" column_invisible="True" groups="sales_team.group_sale_salesman"/>
<field name="allow_billable" column_invisible="True" groups="sales_team.group_sale_salesman"/>
<field name="allowed_so_line_ids" column_invisible="1"/>
<field name="so_line" widget="so_line_field" optional="show" options="{'no_create': True, 'no_open': True}" context="{'create': False, 'edit': False, 'delete': False}" invisible="not allow_billable" readonly="readonly_timesheet" placeholder="Non-billable" groups="sales_team.group_sale_salesman"/>
</xpath>
</field>
Expand All @@ -57,7 +56,6 @@
<field name="commercial_partner_id" invisible="1" groups="sales_team.group_sale_salesman"/>
<field name="is_so_line_edited" invisible="1" groups="sales_team.group_sale_salesman"/>
<field name="allow_billable" invisible="1" groups="sales_team.group_sale_salesman"/>
<field name="allowed_so_line_ids" invisible="1"/>
<field name="so_line" widget="so_line_field" options='{"no_create": True}' context="{'create': False, 'edit': False, 'delete': False, 'with_price_unit': True}" invisible="not allow_billable" readonly="readonly_timesheet" placeholder="Non-billable" groups="sales_team.group_sale_salesman"/>
</xpath>
<xpath expr="//group" position="before">
Expand Down

0 comments on commit 72f3e40

Please sign in to comment.