Skip to Content
Menu
This question has been flagged
4 Replies
1680 Views

Hi,

I am newbie in python and odoo. Please help me.

Below is my code:

work_type = fields.Selection([('normal', 'Normal')
                                   ,('overtime', 'Overtime'), ('holiday', 'Holiday Overtime')],'Work Type')

overtimes_hours = fields.Float(compute='onchange_overtimes', string='Overtime Hours',store=True)

@api.depends('timesheet_ids')
      def onchange_overtimes(self):
          for record in self:
              total = 0.0
              if self.timesheet_ids:
                 for line in self.timesheet_ids:
                    
                     total += line.unit_amount
              self.overtimes_hours = total


timesheet_ids is my one2many field. I added work_type as a selection field into this one2many field.

Each time i add a new record i select work_type, if the work_type = overtime. i want to call the above function.

For ex: first overtimes_hours = 0. On each addition of record (work_type = overtime). Each time value added to the existing value. After 1 week we will get the total overtime working hours seperately. But my code is not working properly.

Please correct me.


Avatar
Discard
Author

first time for loop working. Next time it shows Singleton error. I think error in for loop. Please correct me.

Updated code, please check self.overtimes_hours change into record.overtimes_hours

Best Answer

Functional calculate based on work_type field?

In that case depends also add work_type.

And also code updated, please check below code.

@api.depends('work_type', 'timesheet_ids')
      def onchange_overtimes(self):
          for record in self:
              total = 0.0
              if  record.work_type: 
                 for line in record.timesheet_ids:
                    
                     total += line.unit_amount
              record.overtimes_hours = total

Avatar
Discard