Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Constraint is not working in odoo 8

By
Odoo
on 12/23/14, 12:34 AM 1,310 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:

 

0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 12/23/14, 12:41 AM

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'])]

Thank you @atchuthan

Odoo
on 12/23/14, 1:14 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 12/23/14, 12:34 AM
Seen: 1310 times
Last updated: 3/16/15, 8:10 AM