How to Pass a New Field Value From Sale Order to Invoice in Odoo

Abid Patel
29-Oct-2024 Updated : 29-Oct-2024

Learn how to pass a new field value from Sale Order to Invoice in Odoo to enhance data integrity and streamline processes.

How to Pass a New Field Value From Sale Order to Invoice in Odoo

When you work with Odoo you find yourself in a need to pass new field value from Sale Order to Invoice. This process can maintain consistency. It can accurately reflect relevant information in both documents. In this blog, we will walk you through the steps. We'll tell you how to achieve this in Odoo.

What You Need to Know

In Odoo Sale Order and Invoice models are closely related. Sale Order usually holds all the details for customer's order. Invoice is derived from that specific order. To pass value of a new field there are typically adjustments to be done. You may find yourself working in both Sale Order and Invoice models.

Step-by-Step Guide

1. Define New Field in Sale Order:

At first you’ll desire to introduce this new field to sale.order model. This could encompass essential data. It may be a unique discount or custom note, or explicit conditions.

These are steps to introduce a new field in Sale Order model:

python

from odoo import models, fields

class SaleOrder(models.Model):
    _inherit = 'sale.order'

    custom_field = fields.Char(string='Custom Field')

2. Update Invoice Model:

Ensure that the new field is present in account.move model. This model represents an Invoice. You can make this possible by adding the same field in model of Invoice.

python

class AccountMove(models.Model):
    _inherit = 'account.move'

    custom_field = fields.Char(string='Custom Field')

3. Override Create Method:

The value needs to pass from Sale Order to Invoice. You should override create method in model of Invoice for this. Pull the value from Sale Order. Then assign it to the Invoice.

python

class AccountMove(models.Model):
    _inherit = 'account.move'

    @api.model
    def create(self, vals):
        if 'sale_id' in vals:
            sale_order = self.env['sale.order'].browse(vals['sale_id'])
            vals['custom_field'] = sale_order.custom_field
        return super(AccountMove, self).create(vals)

4. Update Invoice View:

Do not neglect including this new field in Invoice view. This way users can witness the information on the interface of Invoice. You must update XML file correlated with Invoice view.

xml

<record id="view_move_form_inherit_custom_field" model="ir.ui.view">
    <field name="name">account.move.form.inherit.custom.field</field>
    <field name="model">account.move</field>
    <field name="inherit_id" ref="account.view_move_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='invoice_line_ids']" position="after">
            <field name="custom_field" readonly="1"/>
        </xpath>
    </field>
</record>

Conclusion

The process of passing new field value from Sale Order to an Invoice in Odoo is straightforward. One must comprehend the link between the models. Defining the new field and updating both models is key. Overriding the create method is crucial. Modifying the views helps essential information to move easily from one document to the other. This process enhances data integrity. It also boosts the overall user experience in an Odoo application.

Make a Comment

Your email address will not be published. Required fields are marked *