Odoo 10:展示模特领域" sale.order"在" account.invoice"的形式视图中;

时间:2017-01-17 17:57:40

标签: openerp field relation qweb

我通常使用调试模式创建一个新的数据库结构字段,然后Edit FormView并写入例如<field name="x_delivery_date"/>。此外,我可以在以后的印刷报告中显示:

<div name="x_delivery_date" t-if="doc.x_delivery_date">
    <strong>Delivery Date:</strong>
    <p t-field="doc.x_delivery_date"/>
</div>

但是如何在另一个模型formview(commitment_date)中显示模型(sale.order)中可用的字段(account.invoice)?我想我必须使用对象关系或相关领域,但我不知道如何。我希望有人可以帮助我。非常感谢提前。

2 个答案:

答案 0 :(得分:0)

您可以使用相关字段。您必须向account.invoice添加两个字段才能执行此操作。

class AccountInvoice(models.Model):
    _inherit = "account.invoice"

    order_id = fields.Many2one('sale.order', 'Related_order')
    commitment_date = fields.Date(related='order_id.commitment_date')

然后您可以使用account.invoice表单中的commitment_date字段。 sale.order中字段的值将立即反映在表单上。但请注意,更改该字段的值也会更改sale.order上该字段的值。

修改

对于报告,只需使用该字段,因为它是account.invoice的常规字段(因此doc.commitment_date

答案 1 :(得分:0)

首先,您需要在account.invoice

中添加many2one字段
class account_invoice(osv.osv):
    _inherit = "account.invoice"

    _columns = {
        'source_id':fields.many2one('sale.order','Source')
    }

然后继承sale_order中的_prepare_invoice函数。在此功能中,您将把销售订单ID作为源ID传递给account.invoice

class sale_order(osv.osv):
    _inherit = "sale.order"

  def _prepare_invoice(self, cr, uid, order, lines, context=None):
    if context is None:
        context = {}
    journal_id = self.pool['account.invoice'].default_get(cr, uid, ['journal_id'], context=context)['journal_id']
    if not journal_id:
        raise osv.except_osv(_('Error!'),
            _('Please define sales journal for this company: "%s" (id:%d).') % (order.company_id.name, order.company_id.id))
    invoice_vals = {
        'name': order.client_order_ref or '',
        'origin': order.name,
        'type': 'out_invoice',
#Sale order id as source_id
        'source_id':order.id,
        'reference': order.client_order_ref or order.name,
        'account_id': order.partner_invoice_id.property_account_receivable.id,
        'partner_id': order.partner_invoice_id.id,
        'journal_id': journal_id,
        'invoice_line': [(6, 0, lines)],
        'currency_id': order.pricelist_id.currency_id.id,
        'comment': order.note,
        'payment_term': order.payment_term and order.payment_term.id or False,
        'fiscal_position': order.fiscal_position.id or order.partner_invoice_id.property_account_position.id,
        'date_invoice': context.get('date_invoice', False),
        'company_id': order.company_id.id,
        'user_id': order.user_id and order.user_id.id or False,
        'section_id' : order.section_id.id
    }

    invoice_vals.update(self._inv_get(cr, uid, order, context=context))
    return invoice_vals

在视图文件中添加

<record id="invoice_form" model="ir.ui.view">
            <field name="name">account.invoice.form</field>
            <field name="model">account.invoice</field>
            <field name="inherit_id" ref="account.invoice_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='date_invoice']" position="after">
                    <field name="source_id"/>
                </xpath>
             </field>
         </record>

现在将其添加到您的报告文件

<div name="x_delivery_date" t-if="doc.x_delivery_date">
    <strong>Delivery Date:</strong>
    <p t-field="doc.x_delivery_date"/>
    <p t-field="doc.source_id.commitment_date"/>
</div>