Skip to content

Commit

Permalink
[IMP] sale_kit_description/
Browse files Browse the repository at this point in the history
  • Loading branch information
unaiberis authored and anajuaristi committed Oct 3, 2024
1 parent 3bc7c71 commit b67e758
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions sale_kit_description/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

@api.onchange("product_id")
def _onchange_sale_description_product_id(self):

def _set_bom_description(self):
"""Helper method to set the description based on BOM."""
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),
Expand All @@ -19,14 +17,24 @@ def _onchange_sale_description_product_id(self):
)

if bom:
self.name = self.product_id.name

bom_lines = bom.bom_line_ids

description_lines = [self.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)

description_lines = [self.product_id.name]
description_lines += [
f"- {line.product_id.name} {line.product_qty} {line.product_uom_id.name}"
for line in bom.bom_line_ids
]
self.name = "\n".join(description_lines)

@api.onchange("product_id")
def _onchange_sale_description_product_id(self):
"""Onchange method to update description when product is changed."""
self._set_bom_description()

@api.model
def create(self, vals):
"""Override create method to set the description for new sale order lines."""
sale_order_line = super().create(vals)
if "product_id" in vals:
product_id = self.env["product.product"].browse(vals["product_id"])
if product_id and product_id.bom_ids:
sale_order_line._set_bom_description()
return sale_order_line

0 comments on commit b67e758

Please sign in to comment.