简体   繁体   English

Odoo 10:如何根据工资规则调用函数?

[英]Odoo 10 : How to make a call to a function on a salary rule?

I need to make a call function from the object payslip , named salaryfunction :我需要从对象拨打电话功能payslip ,名为salaryfunction

class HrNicPaySlip(models.Model):
    _inherit = 'hr.payslip'

    @api.multi
    def salaryfunction(self):
       return 100.00

I need to call this function from the salary rule, like this:我需要从工资规则中调用这个函数,如下所示:

result = payslip.salaryfunction()

But I get the following error when calculating the pay sheep:但是在计算工资羊时出现以下错误:

Wrong python code defined for salary rule为工资规则定义了错误的 Python 代码

In the logs the error is:在日志中,错误是:

ValueError: : "'float' object is not callable" while evaluating u'result = payslip.salaryfunction()' ValueError :: 在评估 u'result = payslip.salaryfunction()' 时“'float' 对象不可调用”

Could you please help with that?你能帮忙吗?

I searched about some similar issue but without success我搜索了一些类似的问题,但没有成功

Some more information:更多信息:

I want to call a function defined in my model that inherits from hr.payslip我想调用从 hr.payslip 继承的模型中定义的函数

class MyModelTest(models.Model):
  _inherit = 'hr.payslip'

  @api.multi
  def salaryfunction(self):
    return 100.00

In my salary rule I wrote the amount_python_compute :在我的工资规则中,我写了 amount_python_compute :

result = payslip.salaryfunction()

I finally solved my problem, after read all the code of the payslip model. 在阅读了工资单模型的所有代码之后,我终于解决了我的问题。

The payslip object used in the salary rule has a env variable reference, so the correct way to call a function of other model class is below (according to my example): 工资规则中使用的薪资单对象具有env变量引用,因此以下是调用其他模型类的函数的正确方法(根据我的示例):

result = payslip.env['hr.payslip'].salaryfunction()

I hope this works to another dude 我希望这对另一个家伙有用

如果您在模型内部('hr.payslip'),则只需要以这种方式调用函数self.salaryfunction() ,如果您在其他模型中, self.env['hr.payslip'].salaryfunction()调用self.env['hr.payslip'].salaryfunction() ,希望对您有所帮助。

To call the function salaryfunction() does not work with payslip.salaryfunction() because previously when declaring the dictionary of local variables where 'payslip': Payslips (employee.id, self, self.env) is a special object, which does not allow access methods of that object.调用函数salaryfunction()不适用于payslip.salaryfunction()因为以前在声明局部变量字典时,其中'payslip': Payslips (employee.id, self, self.env)是一个特殊对象,它不会允许访问该对象的方法。

I recommend you create a new local variable 'self': self in the method _get_base_local_dict from class hr.payslip , this allows you to call the method in the salary rule like this self.salaryfunction() being salaryfunction() a method of the class 'hr.payslip' .我建议你创建一个新的局部变量'self': self的方法_get_base_local_dict从类hr.payslip ,这可以让你调用这样的薪酬规则的方法self.salaryfunction()salaryfunction()类的方法'hr.payslip'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM