Advertisement
Guest User

timesheet_simplified.py

a guest
Mar 11th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. from openerp import fields, models, api
  2.  
  3. class tc_timesheet_simplified(models.Model):
  4.     _name = "tc_timesheet_simplified"
  5.    
  6.     # ritorna la data attuale
  7.     def _today(self):
  8.         return fields.Date.today()
  9.    
  10.     # ritorna la data e l'ora attuali
  11.     def _now(self):
  12.         return fields.Datetime.now()
  13.    
  14.     # ritorna l'id del dipendente corrente
  15.     def _employee(self):
  16.         ids = self.env['hr.employee'].search([('user_id', '=', self._uid)])
  17.         return ids and ids[0] or False
  18.    
  19.     # controlla la validità delle ore inserite (sign IN/OUT)
  20.     @api.one
  21.     @api.onchange('signin','signout')
  22.     def _check_hours(self):
  23.         # TODO: controllo signin < signout
  24.         if (self.signin > self.signout):
  25.             self.signout = 0
  26.            
  27.     # crea una nuova presenza in 'hr.attendance' con i dati inseriti
  28.     def create_attendance(self, action, employee_id, name):
  29.         attendance = self.env['hr.attendance'].create({'action': action, 'employee_id': employee_id, 'name': name, 'action_desc': False})
  30.    
  31.     # alla creazione di una nuova entry, genera sign IN e sign OUT in 'hr.attendance'
  32.     @api.model
  33.     def create(self, values):
  34.         new_id = super(tc_timesheet_simplified, self).create(values)
  35.                
  36.         signin_name = new_id.date + ' ' + new_id.signin[11:16] + ':00'
  37.         signout_name = new_id.date + ' ' + new_id.signout[11:16] + ':00'
  38.        
  39.         self.create_attendance('sign_in', new_id.employee.id, signin_name)
  40.         self.create_attendance('sign_out', new_id.employee.id, signout_name)
  41.                
  42.         return new_id
  43.    
  44.     date = fields.Date(default=_today, required=True)
  45.     signin = fields.Datetime(string='Sign in', default=_now, required=True)
  46.     signout = fields.Datetime(string='Sign out', default=_now, required=True)
  47.     hours = fields.Float(string='Hours', default=0.0, required=True)
  48.     note = fields.Text()
  49.     action = fields.Selection([('ferie','Ferie'), ('permesso','Permesso'), ('malattia','Malattia')], string='Action')
  50.     employee = fields.Many2one('hr.employee', string='Dipendente', ondelete='cascade', default=_employee, required=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement