This question has been flagged
1 Reply
3414 Views


#
#/#############################################################################
from openerp.osv import fields, osv
import time
from openerp.tools.translate import _

class project_project(osv.osv):
    _inherit = 'hr.contract'
    _columns = {
                'name_line':fields.one2many('contarctdetails.line', 'detailsname', 'Contract Details'),
                'medical_amount':fields.float('Medical'),
                'food_amount':fields.float('Food'),
                'transport_amount':fields.float('Transport'),
                'housing_amount':fields.float('houuse Rent'),
                'city':fields.many2one('res.country.state','City')
                
                }
    
    
project_project()

 

class hr_employee(osv.osv):
    _inherit = 'hr.employee'
    _columns = {
                'badge_number':fields.char('B/No',required=True),
                        }
    
    
project_project()

 

 

class contarctdetails_line(osv.osv):
   
    _name = 'contarctdetails.line'
   
       
    _columns = {
            'detailsname':fields.many2one('hr.contract','Details', size=64, required=False, readonly=False),
            'qty':fields.many2one('document.type','Document Type'),
            'qty1':fields.date('Expiry Date', size=64, required=False, readonly=False),
            'number_doc':fields.integer('Number of Document'),
            'date_issue':fields.date('Date of issue')
            
          }
    
contarctdetails_line()


class documnent_type(osv.osv):
   
    _name = 'document.type'
    
       
    _columns = {
                
            'name':fields.char('Document Model'),
            
                }
    
documnent_type()

 

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
#          
#     def onchange_overtime_hours(self, cr, uid, ids, overtime_hours, context=None):
#         for i in self.read(cr,uid,ids,['overtime_hours'],context=context):
#              if i['overtime_hours'] >16.00:
#                  print("##############################inside overtime hours")
#                  raise osv.except_osv(_('Warning'),_('Overtime Hours Cannot be greater than 16'))
#              return True
    

            #This function is not working and validating
    def _check_worked_hours(self,cr,uid,ids,context=None):
        i=self.read(cr,uid,ids,['worked_hours'],context=context)
        if i[0]['worked_hours'] <=8:
                return True
        return False

     

This function is working correctly


    def _check_overtime_hours(self,cr,uid,ids,context=None):
        i=self.read(cr,uid,ids,['overtime_hours'],context=context)
        if i[0]['overtime_hours'] >16.00:
            print "###############################value",i
            return False
        return True

     
    _constraints=[(_check_worked_hours,'Worked Hours Cannot be greater than 8',['worked_hours'])]
     
    _constraints=[(_check_overtime_hours,'Overtime Hours Cannot be greater than 16',['overtime_hours'])]
    
    def create(self, cr, uid, values, context=None):
        worked = values.get('worked_hours');
        overtime = values.get('overtime_hours');
        total=0
        total =worked+overtime
        values['total'] = float(total);
        if values['total']>24:
            raise osv.except_osv(_('Warning'),_('Total Hours Cannot be greater than 24'))
        return super(hr_analytic_timesheet, self).create(cr, uid, values, context=context);
    
hr_analytic_timesheet()

 

#raise osv.except_osv(_('Warning'), _('Worked Hours Cannot be greater than 8'))

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

 

Avatar
Discard
Best Answer

The first value of _constraints is overwritten with the second _constraints.  So add your constraint as list of conditions.

 _constraints=[(_check_worked_hours,'Worked Hours Cannot be greater than 8',['worked_hours']),  
                          (_check_overtime_hours,'Overtime Hours Cannot be greater than 16',['overtime_hours'])]

Avatar
Discard
Author

Thank you @atchuthan