如何在 odoo11 中将采购订单值复制到 account.invoice

How to copy purchase order value to account.invoice in odoo11

我在采购订单中创建了一个自定义字段,并在 account.invoice 中创建了另一个具有相同名称的自定义字段 budget_id,当我创建采购订单并根据该订单创建账单时,我想将采购订单中budget_id的值复制到账单中的budget_id。

我已经覆盖了 purchase.order 中的智能按钮方法“action_view_invoice”并添加了我的代码,但没有任何反应。还有其他方法吗?

提前致谢!

我的代码 @api.multi def action_view_invoice(自我):

        action = self.env.ref('account.action_invoice_tree2')
        result = action.read()[0]

        result['context'] = {'type': 'in_invoice', 'default_purchase_id': self.id}

        if not self.invoice_ids:
            # Choose a default account journal in the same currency in case a new invoice is created
            journal_domain = [
                ('type', '=', 'purchase'),
                ('company_id', '=', self.company_id.id),
                ('currency_id', '=', self.currency_id.id),
            ]
            default_journal_id = self.env['account.journal'].search(journal_domain, limit=1)
            if default_journal_id:
                result['context']['default_journal_id'] = default_journal_id.id
        else:
            # Use the same account journal than a previous invoice
            result['context']['default_journal_id'] = self.invoice_ids[0].journal_id.id

        # choose the view_mode accordingly
        if len(self.invoice_ids) != 1:
            result['domain'] = "[('id', 'in', " + str(self.invoice_ids.ids) + ")]"
        elif len(self.invoice_ids) == 1:
            res = self.env.ref('account.invoice_supplier_form', False)
            result['views'] = [(res and res.id or False, 'form')]
            result['res_id'] = self.invoice_ids.id
        result['context']['default_origin'] = self.name
        result['context']['default_reference'] = self.partner_ref
        result['context']['default_budget_id'] = self.budget_id.id # my code here

        return result

account.invoice 对象具有字段 purchase_id,它是 link 和 purchase.order。 因此,在 account.invoice 中,您可以通过任何方式(如在供应商账单创建最佳方法中)将 采购订单 budget_id 字段值转换为 account.invoice budget_id 字段.

代码 link - Purchase link In Invoice