İçereği Atla
Menü
Bu soru işaretlendi
2 Cevaplar
3210 Görünümler

Since v15 its possible to create 'Salary Rule Parameters'. I'm trying to reference such a rule in a python code.

I'm aware of how to reference to, for example, an input parameter:



                    # Available variables:

                    #----------------------

                    # payslip: object containing the payslips

                    # employee: hr.employee object

                    # contract: hr.contract object

                    # rules: object containing the rules code (previously computed)

                    # categories: object containing the computed salary rule categories (sum of amount of all rules belonging to that category).

                    # worked_days: object containing the computed worked days.

                    # inputs: object containing the computed inputs.


                    # Note: returned value have to be set in the variable 'result'


So in order to reference an input variable, this is the syntax:

                    result = inputs.variable_name.amount * 0.10


But the documentation is not showing how to reference the newly introduced 'Salary Rule Parameter'. I already tried:

rule_parameter.variable_name.amount

and some other combinations of the word rule and parameter, but to no avail. Anybody knows how to reference it?

Avatar
Vazgeç
En İyi Yanıt

In v17, you can reference a rule parameter using the code payslip._rule_parameter('parameter_code')

Avatar
Vazgeç
Üretici

are you sure to include the "_" before rule?

In v16 it would be payslip.rule_parameter('code') but in 17 you need to include "_" before.

Which version are you using?

Üretici

I'm on 17.0.

I just tried:

result = payslip._rule_parameter('abc')

And it's working as expected. Thank you so much!

En İyi Yanıt

I was able to use a rule parameter in v16 like this:

result = payslip.rule_parameter('NameOfRuleParameter')

It returns the parameter value of it respecting the valid from date specified in the rule parameter.

Avatar
Vazgeç

argh. It gives me a nervous tick
- Error: <class 'AttributeError'>: "'hr.payslip' object has no attribute 'rule_parameter'" while evaluating

Üretici

Hmm, same problem here:

Fout: <class 'AttributeError'>: "'hr.payslip' object has no attribute 'rule_parameter'" while evaluating

Are you really sure, the synax is payslip.rule_parameter('<code-of-rule-paramter?') ?

You can use
payslip.rule_parameter('code_of_parameter').

The below is details from Odoo 15.0 commit.

In a salary rule, you can directly use `payslip.rule_parameter(code)`.
The version valid at the payslip end date is automatically chosen.

To retrieve a parameter value at a particular date,
use `self.env['hr.rule.parameter']._get_parameter_from_code(code, date)`.
The date is optional and set to today by default.

Üretici

@Waleed, thanks for helping out. The first option you gave is already given by other people and is not working. I've tried the 2nd option you gave (retrieve a parameter value at a particular date) and this isn't working neither:

<class 'NameError'>: "name 'self' is not defined" while evaluating
"result = self.env['hr.rule.parameter']._get_parameter_from_code('abc', '2024-01-01')"

I'd say: please try it yourself. Create a "salary rule parameter" with code 'abc' and value '1'. Then create a "salary rule", choose 'python code' at 'amount type' and try to reference to the newly created salary rule parameter by entering:

result = payslip.rule_parameter('abc')

I'm pretty sure you will end up with the same error:

<class 'AttributeError'>: "'hr.payslip' object has no attribute 'rule_parameter'" while evaluating
"result = payslip.rule_parameter('abc')"