Skip to Content
Menú
This question has been flagged
1 Respondre
304 Vistes

Hello,


In the quotation document there are standard fields with unit sales price and discount %, I would like to create a new field with the calculated net price with the discount already settled. Can anyone help me to handle this correctly?


I know more or less how, but I keep getting an error message.

Avatar
Descartar

Share the error message so we don't have to guess what is wrong!

Best Answer

Hii,

i dont know which version you will use but here is example i hope it is help full for solve your error 
Create/Update Python File: models/sale_order_line.py
from odoo import models, fields, api


class SaleOrderLine(models.Model):

    _inherit = 'sale.order.line'


    net_price = fields.Float(

        string='Net Price',

        compute='_compute_net_price',

        store=True,

        readonly=True

    )


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

    def _compute_net_price(self):

        for line in self:

            discount = line.discount or 0.0

            line.net_price = line.price_unit * (1 - discount / 100)

Add the Field to the XML View
In views/sale_order_form_inherit.xml

<record id="view_order_form_inherit_net_price" model="ir.ui.view">

    <field name="name">sale.order.form.inherit.net.price</field>

    <field name="model">sale.order</field>

    <field name="inherit_id" ref="sale.view_order_form"/>

    <field name="arch" type="xml">


        <xpath expr="//field[@name='order_line']/tree/field[@name='price_unit']" position="after">

            <field name="net_price" readonly="1"/>

        </xpath>


       

        <xpath expr="//field[@name='order_line']/form/field[@name='price_unit']" position="after">

            <field name="net_price" readonly="1"/>

        </xpath>

    </field>

</record>

in x path set path as per your version 
and if you can use odoo studio then no code needed

in odoo studio

Open Odoo Studio:

  • Navigate to Sales > Quotations
  • Click the Studio icon (wrench/hammer icon) in the top-right corner

Modify the Quotation Lines:

  • Click on a quotation to open the form
  • Click into the order lines section
  • In the Studio sidebar, click "Add a field"
  • Choose "Computed Field"

Configure the Computed Field:

  • Set the Label: Net Price
  • Set the Type: Float
  • Click on "Advanced Properties" or similar
  • In the Formula/Expression box, use the formula:

price_unit * (1 - (discount or 0) / 100)

Save and Close:

  • Save the field
  • Exit Studio

i hope it will work

Avatar
Descartar