问题描述
大家好,我向 account.invoice
添加了一个新字段,并将该字段添加到供应商账单视图中以获取所有预算。选择特定预算后,我只想使用 onchange 在帐单部分显示与此预算相关的分析帐户。
class custom_accounting_invoice(models.Model):
_inherit = 'account.invoice'
creating_user_id = fields.Many2one('res.users','Project Manager',default=lambda self: self.env.user)
email = fields.Char('Email',related='creating_user_id.login')
budget_id = fields.Many2one('crossovered.budget',string='Budget')
@api.onchange('budget_id')
def onchange_analytic_account_budget(self):
return {'domain': {'invoice_line_ids.account_analytic_id': [
('id','in',self.mapped('budget_id.crossovered_budget_line.analytic_account_id.id')
)]
}
}
解决方法
您可以覆盖发票行中现有的 onchange 方法以更改 analytic_account_id
域。
在以下示例中,当 Unit of Measure
更改且仅当设置了 budget_id
字段时,才会设置域:
class BillsEdit(models.Model):
_inherit = 'account.invoice.line'
account_analytic_id = fields.Many2one('account.analytic.account',string='Budget line')
@api.onchange('uom_id')
def _onchange_uom_id(self):
res = super(BillsEdit,self)._onchange_uom_id()
account_ids = self.mapped('invoice_id.budget_id.crossovered_budget_line.analytic_account_id.id')
new_domain = {}
if account_ids:
new_domain['account_analytic_id'] = [('id','in',account_ids)]
if 'domain' in res:
res['domain'].update(new_domain)
else:
res['domain'] = new_domain
return res