[英]Override a method in odoo
我正在嘗試向(odoo11 / hr_payroll)中的方法添加額外的代碼。 我所做的事情被復制並粘貼了整個代碼,並在其中添加了額外的代碼,但是當該方法被執行時,它被執行了兩次,這證明了我所做的是錯誤的。
我正在尋找解決方案黃油,而不是應對和粘貼整個代碼。
因此,我想在基本方法中添加以下內容:
基本方法:
if debit_account_id:
debit_line = (0, 0, {
'name': line.name,
'partner_id': line._get_partner_id(credit_account=False),
'account_id': debit_account_id,
'journal_id': slip.journal_id.id,
'date': date,
'debit': amount > 0.0 and amount or 0.0,
'credit': amount < 0.0 and -amount or 0.0,
'analytic_account_id': line.salary_rule_id.analytic_account_id.id,
'tax_line_id': line.salary_rule_id.account_tax_id.id,
})
line_ids.append(debit_line)
debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
繼承的方法:
@api.multi
def action_payslip_done(self):
res = super(PayslipBills, self).action_payslip_done()
if debit_account_id:
debit_line = (0, 0, {
'name': line.name,
'partner_id': line._get_partner_id(credit_account=False),
'account_id': debit_account_id,
'journal_id': slip.journal_id.id,
'x_account_no': x_debit_account, # extra
'x_jtag': [(6, 0, x_tags)], # extra
'x_jtag_option': [(6, 0, x_tags_option)], # extra
'date': date,
'debit': amount > 0.0 and amount or 0.0,
'credit': amount < 0.0 and -amount or 0.0,
'analytic_account_id': line.salary_rule_id.analytic_account_id.id,
'tax_line_id': line.salary_rule_id.account_tax_id.id,
})
line_ids.append(debit_line)
debit_sum += debit_line[2]['debit'] - debit_line[2]['credit']
return res
您的問題缺少一些有趣的信息。 首先,此原始方法(模塊hr_payroll)已被hr_payroll_account覆蓋。 其次,通過hr_payroll_account進行的第一次覆蓋實際上是messed_up,您無法通過嘗試更改/擴展來覆蓋。
因此,唯一的解決方案是完全重寫/覆蓋原始方法,而無需調用super。 當心兩種已經存在的方法中的事實業務邏輯! 您必須將兩種邏輯都復制到新方法中。
我不喜歡這些解決方案,但這是唯一(我知道)的解決方案。
為了避免其他可以繼承並重新定義相同模塊和方法的模塊出現問題,我將使用supper(...)保留對原始方法的調用,然后立即使用所需的額外字段和值更新這些記錄進行添加,例如,如果該記錄集的x_whatever值始終相同,並且不管是貸方還是借方行,都可以嘗試以下操作:
class PayslipBills(models.Model):
_inherit = 'hr.payslip'
(... define new fields and new methods...)
@api.multi
def action_payslip_done(self):
res = super(PayslipBills, self).action_payslip_done()
for record in res:
for lines in record.line_ids
# add values to the extra fields...
lines.write({'x_account_no': x_debit_account,
'x_jtag': [(6, 0, x_tags)],
'x_jtag_option': [(6, 0, x_tags_option)],
})
return res
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.