diff --git a/sale_order_product_recommendation/wizards/sale_order_recommendation.py b/sale_order_product_recommendation/wizards/sale_order_recommendation.py index 5679e670797a..211e0fa3158d 100644 --- a/sale_order_product_recommendation/wizards/sale_order_recommendation.py +++ b/sale_order_product_recommendation/wizards/sale_order_recommendation.py @@ -8,6 +8,7 @@ from odoo import api, fields, models from odoo.tests import Form +from odoo.tools import ormcache _logger = logging.getLogger(__name__) @@ -75,12 +76,32 @@ def _recomendable_sale_order_lines_domain(self): ] ) ) - return [ + domain = [ ("order_id", "in", (other_sales - self.order_id).ids), ("product_id.active", "=", True), ("product_id.sale_ok", "=", True), ("qty_delivered", "!=", 0.0), ] + delivery_product_ids = self._get_delivery_products() + if delivery_product_ids: + domain.append(("product_id", "not in", delivery_product_ids)) + return domain + + @api.model + @ormcache() + def _get_delivery_products(self): + # To avoid to add delivery dependency + delivery_product_ids = [] + if "delivery.carrier" in self.env.registry: + delivery_product_ids = ( + self.env["delivery.carrier"] + .with_context(active_test=False) + .read_group([], ["product_ids:array_agg(product_id)"], [])[0][ + "product_ids" + ] + or [] + ) + return delivery_product_ids def _prepare_recommendation_line_vals(self, group_line, so_line=False): """Return the vals dictionary for creating a new recommendation line. @@ -128,8 +149,12 @@ def _generate_recommendations(self): found_dict = {l["product_id"][0]: l for l in found_lines} recommendation_lines = self.env["sale.order.recommendation.line"] existing_product_ids = set() - # Always recommend all products already present in the linked SO - for line in self.order_id.order_line: + # Always recommend all products already present in the linked SO except delivery + # carrier products + delivery_product_ids = self._get_delivery_products() + for line in self.order_id.order_line.filtered( + lambda ln: ln.product_id.id not in delivery_product_ids + ): found_line = found_dict.get( line.product_id.id, {"product_id": (line.product_id.id, False)} )