From 91917b4d76ee95170afe09d31fdaf6c2dd05def7 Mon Sep 17 00:00:00 2001 From: Unai Beristain Date: Thu, 3 Oct 2024 17:39:04 +0200 Subject: [PATCH] [IMP] sale_kit_description/ --- .../models/sale_order_line.py | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/sale_kit_description/models/sale_order_line.py b/sale_kit_description/models/sale_order_line.py index a6ad5aa5..4b62c909 100644 --- a/sale_kit_description/models/sale_order_line.py +++ b/sale_kit_description/models/sale_order_line.py @@ -6,9 +6,7 @@ class SaleOrderLine(models.Model): @api.onchange("product_id") def _onchange_sale_description_product_id(self): - if self.product_id and self.product_id.bom_ids: - bom = self.env["mrp.bom"].search( [ ("product_tmpl_id", "=", self.product_id.product_tmpl_id.id), @@ -30,3 +28,31 @@ def _onchange_sale_description_product_id(self): description_lines.append(description_line) self.name = "\n".join(description_lines) + + @api.model + def create(self, vals): + if "product_id" in vals: + product_id = self.env["product.product"].browse(vals["product_id"]) + if product_id and product_id.bom_ids: + bom = self.env["mrp.bom"].search( + [ + ("product_tmpl_id", "=", product_id.product_tmpl_id.id), + ("active", "=", True), + ], + order="sequence", + limit=1, + ) + + if bom: + name = product_id.name + bom_lines = bom.bom_line_ids + + description_lines = [name] + for line in bom_lines: + description_line = f"""- {line.product_id.name} \ +{line.product_qty} {line.product_uom_id.name}""" + description_lines.append(description_line) + + vals["name"] = "\n".join(description_lines) + + return super().create(vals)