Skip to content

[ADD] new_product_type: Added sub-products to a particular product #833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 18.0
Choose a base branch
from

Conversation

hapt-odoo
Copy link

@hapt-odoo hapt-odoo commented Jul 2, 2025

In this module, added the sub-products in specific products when
boolean is true, and by using the wizard, add all the sub-products in
sale.order.line, and also added the sub-products in the invoice, and
customer preview.

task-4907378

…new fields

In the new_producttype module created in the product form to add
sub-products field in specific products and in sale.order.line added a button
in that by clicking on it, created a wizard of sub_product linked with
the main product.
In the new_producttype module, get the sub-products associated with
the product on sale.order.line using wizard and added that the sub_products
directly in sale.order.line and made the parent line ondelete=cascade
to delete sub_product_lines on deletion of parent_line
In the new_producttype module , added sub_products in invoice
When the boolean is true, and also added sub_products in
customer preview.
@robodoo
Copy link

robodoo commented Jul 2, 2025

Pull request status dashboard

@@ -0,0 +1,18 @@
{
'name': "New Product Type",
'category': '',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoud not be empty.you can either remove it.

_inherit = 'sale.order.line'

is_kit = fields.Boolean(string='Is kit', related='product_id.is_kit')
main_product_line_id = fields.Many2one("sale.order.line", ondelete="cascade")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the same quote in the file. if you are using ' then you have to apply it to all fields.

Comment on lines +15 to +41
sale_order_line = self.env["sale.order.line"].browse(sale_order_line_id)
existing_lines = self.env["sale.order.line"].search(
[("main_product_line_id", "=", sale_order_line_id)]
)
line_values = []
if existing_lines:
for sub_product in existing_lines:
curr_product = self.env["sub.product.line.kit.wizard"].create(
{
"product_id": sub_product.product_id.id,
"price": sub_product.price_unit,
"quantity": sub_product.product_uom_qty,
}
)
line_values.append(Command.link(curr_product.id))
else:
for sub_product in sale_order_line.product_id.sub_products_ids:
curr_product = self.env["sub.product.line.kit.wizard"].create(
{
"product_id": sub_product.id,
"price": sub_product.list_price,
"quantity": 1,
}
)
line_values.append(Command.link(curr_product.id))
res["sub_products_ids"] = line_values
res["sale_order_line_id"] = sale_order_line_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sale_order_line = self.env["sale.order.line"].browse(sale_order_line_id)
existing_lines = self.env["sale.order.line"].search(
[("main_product_line_id", "=", sale_order_line_id)]
)
line_values = []
if existing_lines:
for sub_product in existing_lines:
curr_product = self.env["sub.product.line.kit.wizard"].create(
{
"product_id": sub_product.product_id.id,
"price": sub_product.price_unit,
"quantity": sub_product.product_uom_qty,
}
)
line_values.append(Command.link(curr_product.id))
else:
for sub_product in sale_order_line.product_id.sub_products_ids:
curr_product = self.env["sub.product.line.kit.wizard"].create(
{
"product_id": sub_product.id,
"price": sub_product.list_price,
"quantity": 1,
}
)
line_values.append(Command.link(curr_product.id))
res["sub_products_ids"] = line_values
res["sale_order_line_id"] = sale_order_line_id
sale_order_line = self.env["sale.order.line"].browse(sale_order_line_id)
existing_lines = self.env["sale.order.line"].search([
("main_product_line_id", "=", sale_order_line_id)
])
sub_product_line_wizard = self.env["sub.product.line.kit.wizard"]
line_values = []
if existing_lines:
vals_list = [
{
"product_id": line.product_id.id,
"price": line.price_unit,
"quantity": line.product_uom_qty,
}
for line in existing_lines
]
else:
vals_list = [
{
"product_id": sub_product.id,
"price": sub_product.list_price,
"quantity": 1,
}
for sub_product in sale_order_line.product_id.sub_products_ids
]
if vals_list:
curr_products = sub_product_line_wizard.create(vals_list)
line_values = [Command.link(product.id) for product in curr_products]
res.update({
"sub_products_ids": line_values,
"sale_order_line_id": sale_order_line_id,
})

Comment on lines +56 to +58
existing_lines.product_uom_qty = rec.quantity
existing_lines.last_price = rec.price
existing_lines.price_unit = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should make a database call.You can use the code below to reduce database calls.

Suggested change
existing_lines.product_uom_qty = rec.quantity
existing_lines.last_price = rec.price
existing_lines.price_unit = 0
existing_lines.write({
"product_uom_qty": rec.quantity,
"last_price": rec.price,
"price_unit": 0,
})

Comment on lines +47 to +53
existing_lines = self.env["sale.order.line"].search(
[
("main_product_line_id", "=", self.sale_order_line_id.id),
("product_id", "=", rec.product_id.id),
("order_id", "=", self.sale_order_line_id.order_id.id)
],
limit=1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a search outside of the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants