From 58e2383ed220b42c03034ed1958321d80e6dbd0d Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Mon, 5 Dec 2022 18:36:39 +0100 Subject: [PATCH] [IMP] rma_sale: Domains performance For avoiding a big list of IDs being transferred when no sales order is selected on the RMA, we have changed domains to make use of the possibility of pyjs expressions allowed in the domains. No ternary operators nor list sums are allowed in pyjs, but using a combination of allowed IDs with a controlled length of values + and/or operators to switch domains is enough for having the right performance and avoid to depend on other modules like web_domain_field. --- rma_sale/__manifest__.py | 2 +- rma_sale/models/rma.py | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rma_sale/__manifest__.py b/rma_sale/__manifest__.py index ec37b648..eec2e878 100644 --- a/rma_sale/__manifest__.py +++ b/rma_sale/__manifest__.py @@ -3,7 +3,7 @@ { "name": "Return Merchandise Authorization Management - Link with Sales", "summary": "Sale Order - Return Merchandise Authorization (RMA)", - "version": "15.0.1.0.1", + "version": "15.0.1.1.0", "development_status": "Production/Stable", "category": "RMA", "website": "https://github.com/OCA/rma", diff --git a/rma_sale/models/rma.py b/rma_sale/models/rma.py index 0b1bc5d1..9286b05b 100644 --- a/rma_sale/models/rma.py +++ b/rma_sale/models/rma.py @@ -21,7 +21,10 @@ class Rma(models.Model): comodel_name="stock.picking", compute="_compute_allowed_picking_ids", ) - picking_id = fields.Many2one(domain="[('id', 'in', allowed_picking_ids)]") + picking_id = fields.Many2one( + domain="(order_id or partner_id) and [('id', 'in', allowed_picking_ids)] or " + "[('state', '=', 'done'), ('picking_type_id.code', '=', 'outgoing')] " + ) allowed_move_ids = fields.Many2many( comodel_name="sale.order.line", compute="_compute_allowed_move_ids", @@ -34,18 +37,25 @@ class Rma(models.Model): comodel_name="product.product", compute="_compute_allowed_product_ids", ) - product_id = fields.Many2one(domain="[('id', 'in', allowed_product_ids)]") + product_id = fields.Many2one( + domain="order_id and [('id', 'in', allowed_product_ids)] or " + "[('type', 'in', ['consu', 'product'])]" + ) @api.depends("partner_id", "order_id") def _compute_allowed_picking_ids(self): domain = [("state", "=", "done"), ("picking_type_id.code", "=", "outgoing")] for rec in self: + domain2 = domain.copy() if rec.partner_id: commercial_partner = rec.partner_id.commercial_partner_id - domain.append(("partner_id", "child_of", commercial_partner.id)) + domain2.append(("partner_id", "child_of", commercial_partner.id)) if rec.order_id: - domain.append(("sale_id", "=", rec.order_id.id)) - rec.allowed_picking_ids = self.env["stock.picking"].search(domain) + domain2.append(("sale_id", "=", rec.order_id.id)) + if domain2 != domain: + rec.allowed_picking_ids = self.env["stock.picking"].search(domain2) + else: + rec.allowed_picking_ids = False # don't populate a big list @api.depends("order_id", "picking_id") def _compute_allowed_move_ids(self): @@ -67,11 +77,7 @@ class Rma(models.Model): lambda r: r.type in ["consu", "product"] ).ids else: - rec.allowed_product_ids = ( - self.env["product.product"] - .search([("type", "in", ["consu", "product"])]) - .ids - ) + rec.allowed_product_ids = False # don't populate a big list @api.onchange("partner_id") def _onchange_partner_id(self):