Skip to content

Commit

Permalink
[IMP] sale_order_product_recommendation: Exclude delivery carrier pro…
Browse files Browse the repository at this point in the history
…ducts from recommendations

TT35965
  • Loading branch information
sergio-teruel committed Apr 12, 2022
1 parent 243fea9 commit cf6447a
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from odoo import api, fields, models
from odoo.tests import Form
from odoo.tools import ormcache

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)}
)
Expand Down

0 comments on commit cf6447a

Please sign in to comment.