~inddiana/sisb/sisb_extratos_bancarios_campo_transpasos

« back to all changes in this revision

Viewing changes to sisb_horas_extras/model/hr_hours_per_day.py


[IMP] Se agrega modulo para la carga de horas extras.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*--
 
3
 
 
4
from osv import fields, osv
 
5
import datetime
 
6
 
 
7
class horas_extras_parametros_turnos(osv.osv):
 
8
        
 
9
    _name = 'horas.extras.parametros.turnos'
 
10
    _description = 'parametros de horas extras'
 
11
    _columns = {
 
12
    'name': fields.char('Nombre', size=60),
 
13
    'concept_id': fields.many2one('hr.concept', 'Concepto', required=True),
 
14
    'feriado_id': fields.many2one('hr.concept', 'Concepto feriado'),
 
15
    'hr_hours_per_day_id': fields.many2one('hr.hours.per.day', 'Turno', required=True),
 
16
    'hour_start': fields.time('hora inicial', required=True),
 
17
    'hour_end': fields.time('hora final', required=True),
 
18
    'day_start': fields.selection([(1, 'Lunes'),(2, 'Martes'),
 
19
    (3, 'Miercoles'),(4, 'Jueves'),(5, 'Viernes'),(6, 'Sabado'),(7, 'Domingo')], 
 
20
    'Dia desde', required=True), 
 
21
    'day_end': fields.selection([(1, 'Lunes'),(2, 'Martes'),
 
22
    (3, 'Miercoles'),(4, 'Jueves'),(5, 'Viernes'),(6, 'Sabado'),(7, 'Domingo')], 
 
23
    'Dia hasta', required=True), 
 
24
    }
 
25
 
 
26
    def diff(self, hr, ha):
 
27
        criterio = 3600
 
28
        r1 =  datetime.datetime.strptime(hr, '%H:%M:%S')
 
29
        r2 =  datetime.datetime.strptime(ha, '%H:%M:%S')
 
30
        dif = r2 - r1 
 
31
        ab = abs(dif)
 
32
        time =  float(ab.seconds / float(criterio))
 
33
        time = round(time, 2)
 
34
        return time
 
35
 
 
36
    def search_parameters_overtime(self,cr, uid, hr_hours_per_day_id, dia, hour_start, hour_end, feriado=None):
 
37
        sql = """
 
38
        select id FROM horas_extras_parametros_turnos
 
39
        WHERE %s BETWEEN day_start and day_end 
 
40
        and (hour_start >= '%s' and hour_end <= '%s' OR  
 
41
        (hour_start <= '%s' and hour_end >= '%s') OR
 
42
        (hour_start <= '%s' and hour_end >= '%s')) 
 
43
        and hr_hours_per_day_id = %s
 
44
        """%(dia, hour_start, hour_end, hour_start, hour_start, hour_end, hour_end, hr_hours_per_day_id)
 
45
        cr.execute(sql)
 
46
        turnos_ids = cr.fetchall()
 
47
        turnos_ids = [t for i in turnos_ids for t in i]
 
48
        parametro_read = ['hour_start', 'hour_end', 'concept_id']
 
49
        if feriado:
 
50
                   parametro_read.append('feriado_id')
 
51
        turnos_read = self.read(cr, uid, turnos_ids, parametro_read)
 
52
        return turnos_read
 
53
    
 
54
    def conditionals(self, e1, e2, p1, p2, c, val):
 
55
        diff = self.diff
 
56
        param = diff(p1,p2)
 
57
        extra = diff(e1,e2)
 
58
        if extra > param:
 
59
            if e1 >= p1 and e1 <= p2 and e2 > p1 and e2 > p2: 
 
60
                val[c] += diff(e1,p2)
 
61
            elif e1 <= p1 and e1 <= p2 and e2 > p1 and e2 > p2:
 
62
                val[c] += diff(p1,p2)
 
63
            elif e1 <= p1 and e1 <= p2 and e2 > p1 and e2 < p2:
 
64
                val[c] += diff(p1,e2)
 
65
            else:
 
66
                val[c] += diff(p1,e2) 
 
67
        else:
 
68
            if e1 > e2:
 
69
                val[c] += diff(p1, e2)
 
70
            else:
 
71
                if e1 >= p1 and e2 <= p2:
 
72
                   val[c] += diff(e2, e1)
 
73
                elif e1 <= p1 and e2 <= p2:
 
74
                     val[c] += diff(p1, e2)
 
75
                else:
 
76
                    val[c] += diff(e1, p2)
 
77
        return val 
 
78
    
 
79
    def calculte_overtime(self, cr, uid, hr_hours_per_day_id, dia, hour_start, hour_end, feriado=None): 
 
80
        parameters  = self.search_parameters_overtime(cr, uid, hr_hours_per_day_id, dia, hour_start, hour_end, feriado=feriado)
 
81
        diff = self.diff
 
82
        val = {} 
 
83
        for i in parameters:
 
84
            if feriado:
 
85
                if not i['feriado_id']:
 
86
                   val[i['concept_id'][0]] = 0.00
 
87
                else:
 
88
                    val[i['feriado_id'][0]] = 0.00
 
89
            else:
 
90
                val[i['concept_id'][0]] = 0.00
 
91
        e1 = hour_start
 
92
        e2 = hour_end
 
93
        for p in parameters:
 
94
            p1 = p['hour_start']
 
95
            p2 = p['hour_end']
 
96
            c = p['feriado_id'][0] if feriado and p['feriado_id'] else p['concept_id'][0]
 
97
            #Turno normal, 1er grupo, 2do grupo, 3er grupo, 4to grupo
 
98
            val = self.conditionals(e1, e2, p1, p2, c, val)
 
99
        return val
 
100
    
 
101
    def validate_parameters_day_hour(self, cr, day_start,day_end, hour_start,hour_end, hr_hours_per_day_id):
 
102
        sql = """
 
103
        select id FROM horas_extras_parametros_turnos
 
104
        WHERE (day_start >= %s and day_end <= %s 
 
105
        or (day_start <= %s and day_end >= %s) 
 
106
        or (day_start <= %s and day_end >= %s))   
 
107
        and (hour_start >= '%s' and hour_end <= '%s' OR  
 
108
        (hour_start <= '%s' and hour_end >= '%s') OR
 
109
        (hour_start <= '%s' and hour_end >= '%s')) 
 
110
        and hr_hours_per_day_id = %s
 
111
        """%(day_start, day_end, day_start,day_start, day_end, day_end, hour_start, hour_end, hour_start, hour_start, hour_end, hour_end, hr_hours_per_day_id)
 
112
        cr.execute(sql)
 
113
        query  = cr.fetchall()
 
114
        query = [t for i in query for t in i]
 
115
        return query
 
116
    
 
117
    def validate_ranges_hours(self, hour_start,hour_end):
 
118
        if datetime.datetime.strptime(hour_end, '%H:%M:%S') <  datetime.datetime.strptime(hour_start, '%H:%M:%S'):
 
119
           raise osv.except_osv('ERROR', "La hora final es debe ser mayor a la inicial")
 
120
        return True  
 
121
         
 
122
 
 
123
    def create(self, cr, uid, values, context=None):
 
124
        day_start = values['day_start']
 
125
        day_end = values['day_end']
 
126
        hour_start = values['hour_start']
 
127
        hour_end = values['hour_end']
 
128
        hr_hours_per_day_id = values['hr_hours_per_day_id'] 
 
129
        self.validate_ranges_hours(hour_start,hour_end)
 
130
        validar  = self.validate_parameters_day_hour(cr, day_start, day_end, hour_start,hour_end, hr_hours_per_day_id)
 
131
        if validar:
 
132
           raise osv.except_osv('ERROR', "No se puede crear un nuevo parametro si los rangos de horas y fechas coinciden con las anteriores")
 
133
        else:              
 
134
            super(horas_extras_parametros_turnos, self).create(cr, uid, values, context=None) 
 
135
        return True
 
136
    
 
137
    #~ def write(self, cr, uid, ids, values, context=None):
 
138
        #~ parameters_brw = self.browse(cr, uid, ids[0])
 
139
        #~ day_start = values['day_start']
 
140
        #~ day_end = values['day_end']
 
141
        #~ hour_start = values['hour_start']
 
142
        #~ hour_end = values['hour_end']
 
143
        #~ hr_hours_per_day_id = parameters_brw.hr_hours_per_day_id.id 
 
144
        #~ self.validate_ranges_hours(hour_start,hour_end)
 
145
        #~ validar  = self.validate_parameters_day_hour(cr, day_start, day_end, hour_start,hour_end, hr_hours_per_day_id)
 
146
        #~ if validar:
 
147
           #~ raise osv.except_osv('ERROR', "No se puede actualizar los parametros si los rangos de horas y fechas coinciden con las anteriores")
 
148
        #~ else:              
 
149
            #~ super(horas_extras_parametros_turnos, self).create(cr, uid, values, context=None) 
 
150
        #~ return True
 
151
        
 
152
        
 
153
 
 
154
 
 
155
horas_extras_parametros_turnos()
 
156
 
 
157
class hr_hours_per_day(osv.osv):
 
158
 
 
159
    _inherit = 'hr.hours.per.day'
 
160
    _description = 'Horario'
 
161
 
 
162
    _columns = {
 
163
        'reglas_horas_extras_ids': fields.one2many('horas.extras.parametros.turnos', 'hr_hours_per_day_id', 'Reglas de sobretiempo del turno'),
 
164
 
 
165
    }
 
166
    
 
167
hr_hours_per_day()