Skip to Content
Menu
This question has been flagged
3 Replies
5464 Views

What I'm trying to do:

I have a model for defining expressions that I use in a few models other models, if the expression is only numbers like 1*3 it gets the correct value. The problem becomes when I use variables like 'H' for employee hours. The models are connected with relations (not directly but trough salary).

from the model salary i can get the hours with: self.salary_data_ids.employee_id.hours

from the expressions model i can get the hours: self.salary_ids.salary_data_ids.employee_id.hours, but this is in the method that processes variables and computes the value, so it replaces the variable H with the employee hours. Expression H * 1,35 becomes 168 * 1,35 and it should return the value. 

How can I get the correct employee hours. Can I pass the self object from salary_data to the method in expressions that computes the values. Like passing an object to a method/function. Don't know if that is possible in python, but I use this in php and java. Below "code" is only an approximation of my code, only to show how the models are related.

class Salary (model.Models):

    salary_data_ids = fields.One2many()

    expressions_ids = fields.Many2many()

class Expression():

    salary_ids =  fields.Many2many()

    def compute_expression(self):

        if var == 'H':

            return self.salary_ids.salary_data_ids.employee_id.hours * 1.35

class Salary_data()

    salary_id = fields.Many2one()

    work_hours_salary = fields.Float()

   @api.multi

    def work_hours(self):

        for rec in self:

            rec.work_hours_salary = salary_id. expressions_ids.compute_expression()  

Can I pass the "rec" to the  compute_expression()  method so it can get the correct data from the correct employee and correct salary record like bellow? (record is the salary_data object)

def compute_expression(self, record):

    work_hours = record.employee_id.hours  


Or do I need to pass the salary_data.id and then use search?

def compute_expression(self, salary_data_id):

    salary_data = self.env['salary_data'].search([('id', '=', salary_data_id)])

    work_hours = salary_data.employee_id.hours

If it can be done with passing the object then my expressions don't have to be to much hard coded and any example how to do it would help a lot. 

Avatar
Discard

don't you store these information where you have one2many or similar relation?

Author

of course I have them connected with relations. But I can't have the expressions hard coded to one model. The expressions can be accessed from different models, so getting the data for the different vars can't be done by the relations... or I couldn't get it to work. But the relations are not the question but if I can pass an object(self) to a method in a different model.

Author Best Answer

There was no way that I found to pass an objects state to another model, so I had to duplicate the code so that now it fetches the expression from the 2nd model to the 1st, in that one it than replaces the variables with the values and sends the expression with only numbers to the 2nd model for calculation. 

Avatar
Discard
Related Posts Replies Views Activity
0
Mar 21
5199
3
Jul 20
18532
2
Jan 19
5101
1
Dec 18
1842
1
Jul 18
6092