This question has been flagged
2 Replies
2589 Views

class hr_analytic_timesheet(osv.osv):
   
    _inherit = 'hr.analytic.timesheet'
    
       
    _columns = {
                
              'worked_hours':fields.float('Worked Hours'),
              'overtime_hours':fields.float('Over Time'),
              'total':fields.float('Total')
            
                }

 

  def onchange_worked_hours(self, cr, uid, ids, worked_hours, context=None):
        for i in self.read(cr,uid,ids,['worked_hours'],context=context):
             if i['worked_hours'] >8.00:
                 print("##########################inside worked hours")
                 raise osv.except_osv(_('Warning'),_('Worked Hours Cannot be greater than 8'))
             return True

 

====================================

 <record id="timesheet_tree_inherited_view" model="ir.ui.view">
        <field name="name">timesheet.tree.inherited</field>
        <field name="model">hr.analytic.timesheet</field>
        <field name="inherit_id" ref="hr_timesheet.hr_timesheet_line_tree"/>
        <field name="arch" type="xml">
        
         <xpath expr="//tree/field[@name='unit_amount']" position="before">
        
            <field name="worked_hours" on_change="onchange_worked_hours(worked_hours)"/>        
            
          
            
              <field name="total"/>
            
            
        
         </xpath>
        
            </field>
        </record> 

Avatar
Discard
Best Answer

Are you sure ids is not empty? Since you are on the on_change method, the object you are working on does not have to be saved yet.

Also, if you supply the worked hours (which can be a list of ids of the working_hours object), then you don't need a read on self, but rather on the object the worked_hours field is referencing.

Print/debug list the values in the beginnen of your method to be sure everything is supplied in the first place and see which types they are.

Avatar
Discard
Best Answer

@LIBU, are you trying to validate the new value of worked_hours or the saved value?  Currently you are checking against the saved ['worked_hours'], if the record has been saved before (in the case of you editing the record).  As @Ludo has mentioned, the record have not been saved if you expect this to be triggered when you create a new record.

Also, wouldn't it be better if you use the 'warning' return value key instead of raising exception?

Avatar
Discard
Author

Yes @Ivan I want to validate the value when I create a new record.....It is not a saved data

So you should be using worked_hours variable directly as @Ludo had explained. if worked_hours >8.00 .... Also, please consider using 'warning' return as opposed to raising errors.

Sorry, forgot to mention, remove the for loop.