在Odoo 11.0帐户发票中添加一个额外字段

时间:2017-11-12 11:01:10

标签: python openerp odoo-10 odoo-11

我想在销售发票中添加一个额外字段,该字段继承自account.invoice。更具体地说,我想在每个发票行项目中添加一个字段delivery_date

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."))

和XML是:

<?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>

2 个答案:

答案 0 :(得分:2)

如果要向发票行添加新字段,则需要继承account.invoice.line。 在这里,您继承了account.invoice,继承account.invoice.line,然后重试。

例如:

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."))

答案 1 :(得分:1)

如果要将其添加到发票行,则模型为account.invoice.line 在这种情况下,您不需要指定_name属性,因为它是 已经从父模型继承..

在你的python代码中:

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