hizbul25
hizbul25

Reputation: 3849

Add an extra field to Odoo 11.0 Account Invoice

I want to add an extra field to sales invoice which in inherited from account.invoice . More specifically i want to add a field delivery_date to each invoice line item.

from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
from datetime import date


class ReadyMixSalesInvoice(models.Model):
    _inherit = 'account.invoice'
    _name = 'account.invoice'

    delivery_date = fields.Datetime(string='Delivery Date', required=True, readonly=True, index=True,
                                    states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                                    help='Item delivery date.')

    @api.constrains('delivery_date')
    def _delivery_date_check(self):
        for record in self:
            if record.delivery_date and record.delivery_date.split(' ', 1)[0] < str(date.today()):
                raise ValidationError(_("Delivery Date must be after current date."))

and XML is:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record id="invoice_form_inherit_sale_ready_mix" model="ir.ui.view">
        <field name="name">account.invoice.form.readymix</field>
        <field name="model">account.invoice</field>
        <field name="inherit_id" ref="account.invoice_form"/>
        <field name="arch" type="xml">
            <data>
                <xpath expr="//field[@name='price_unit']" position="after">
                    <field name="delivery_date"/>
                </xpath>
                <xpath expr="//tree/field[@name='price_unit']" position="after">
                    <field name="delivery_date"/>
                </xpath>
            </data>
        </field>
    </record>

</odoo>

Upvotes: 1

Views: 1448

Answers (2)

Kenly
Kenly

Reputation: 26748

If you want to add it to invoice line the model is account.invoice.line.
In you case you do not need to specify the _name attribute because it is already inherited from the parent model..

In your python code:

class ReadyMixSalesInvoice(models.Model):
    _inherit = 'account.invoice.line'

Upvotes: 1

KbiR
KbiR

Reputation: 4174

If you want to add new field to invoice lines you need to inherit account.invoice.line. Here you inherited account.invoice, Inherit account.invoice.line and try again.

Eg:

class ReadyMixSalesInvoice(models.Model):
_inherit = 'account.invoice.line'
_name = 'account.invoice.line'

delivery_date = fields.Datetime(string='Delivery Date', required=True, readonly=True, index=True,
                                states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                                help='Item delivery date.')

@api.constrains('delivery_date')
def _delivery_date_check(self):
    for record in self:
        if record.delivery_date and record.delivery_date.split(' ', 1)[0] < str(date.today()):
            raise ValidationError(_("Delivery Date must be after current date."))

Upvotes: 2

Related Questions