覆盖odoo中的方法

问题描述 投票:1回答:2

我正在尝试向(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
python odoo odoo-11
2个回答
0
投票

您的问题遗漏了一些有趣的信息。首先,这个原始方法(模块hr_payroll)已被hr_payroll_account覆盖。其次,hr_payroll_account的第一次覆盖实际上是messed_up,您无法通过尝试更改/扩展来覆盖。

因此唯一的解决方案是完全重写/覆盖原始方法而不调用super。在两个已有的方法中注意事实的业务逻辑!您必须将两种逻辑都复制到新方法中。

我不喜欢那些解决方案,但它是唯一可能的解决方案(我知道)。


0
投票

为了避免可以继承和重新定义相同模块和方法的其他模块的问题,我将使用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
© www.soinside.com 2019 - 2024. All rights reserved.