Skip to Content
Menu
This question has been flagged
2 Replies
2140 Views

Hello everyone,


I'm working on a project in Odoo and would like to add a specific feature to the sales order system. I need to create a subsection within each product line in the sales order, where I can add related products or additional services. Additionally, I would like the total of the main section to include the total of the subsections automatically.


Has anyone done something similar or have any suggestions on how I can implement this feature? I'm particularly interested in knowing how I can set up the subsections and how to ensure that the total of the main section is updated correctly based on the totals of the subsections.


Thank you in advance for any guidance or suggestions you can offer!

Avatar
Discard
Best Answer

here i give you example you can try this way:
from odoo import models, fields, api


class SaleOrderLineSubsection(models.Model):

    _name = 'sale.order.line.subsection'


    name = fields.Char(string='Name')

    product_id = fields.Many2one('product.product', string='Product')

    quantity = fields.Float(string='Quantity')

    price_unit = fields.Float(string='Unit Price')

    subtotal = fields.Float(string='Subtotal', compute='_compute_subtotal')


    @api.depends('quantity', 'price_unit')

    def _compute_subtotal(self):

        for subsection in self:

            subsection.subtotal = subsection.quantity * subsection.price_unit


class SaleOrderLine(models.Model):

    _inherit = 'sale.order.line'


    subsection_ids = fields.One2many('sale.order.line.subsection', 'order_line_id', string='Subsections')

    total_subsections = fields.Float(string='Total Subsections', compute='_compute_total_subsections')


    @api.depends('subsection_ids.subtotal')

    def _compute_total_subsections(self):

        for line in self:

            line.total_subsections = sum(sub.subtotal for sub in line.subsection_ids)


    @api.onchange('subsection_ids')

    def _onchange_subsections(self):

        self.price_subtotal = sum(sub.subtotal for sub in self.subsection_ids)


Avatar
Discard
Author Best Answer

Thank you!

Avatar
Discard
Related Posts Replies Views Activity
3
Feb 25
1596
1
Dec 24
1806
2
Mar 24
2674
0
Sep 23
1318
1
Sep 23
1337