Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- holiday_project_id = 1079
- holiday_task_id = 36181
- automation_start_date = datetime.datetime(2021, 1, 1,00) #Establecemos una fecha inicial para la creación de planning-shifts
- allRecords = env['hr.leave'].search(['&',('request_date_from','>=',automation_start_date),('state',"in",['confirm', 'validate1', 'validate'])]) #Obtenemos todas las leaves de leaves Confirmados, aprobados o validados(no en draft o refused) con la fecha inicial igual o mayor que start
- #PRIMERO BORRA LOS PLANNING SLOTS DEL PROYECTO DE VACACIONES.
- forecasts = env['planning.slot'].search(['&',('project_id',"=",holiday_project_id),('start_datetime','>=',automation_start_date)]) #todas las planning slots del proyecto vacaciones que empiecen después de la fecha consignada
- for f in forecasts: #para cada planning shift entre todas las que cumplan la condición de la linea anterior:
- f.unlink() #borrar
- #CREAR LOS PLANNING SLOTS
- for record in allRecords: #para cada registro entre todos los registros que cumplan la condición de la linea 14:
- start = record.request_date_from #Obtengo el campo date start de Time Off
- tz_user = record.employee_id.tz #obtengo la timezone del empleado
- tz_user_str = timezone(str(tz_user)) #le doy formato string
- utc = timezone("utc") #obtengo la timezone utc
- start_no_timezone = datetime.datetime(start.year, start.month, start.day, 9) #doy formato al campo start, y le asigno 9 a las horas
- start_tz_user_str = tz_user_str.localize(start_no_timezone) #asigno timezone Europa/Madrid al campo start_no_timezone
- start_utc = utc.localize(start_no_timezone)#asigno timezone utc al campo start_no_timezone (para poder tener dos campos naive)
- diff = start_utc - start_tz_user_str #obtengo la diferencia entre start_utc y start_europe
- diff_hours = diff.seconds // 3600 #obtengo la diferencia en horas en int del campo diff
- start = datetime.datetime(start_no_timezone.year, start_no_timezone.month, start_no_timezone.day, 9 - diff_hours)
- end = record.request_date_to #Obtengo el campo date end de Time Off
- #end = datetime.datetime.strtime(str(end), DATETIME_FORMAT) + datetime.timedelta(hours=17)
- end_no_timezone = datetime.datetime(end.year, end.month, end.day,17) #Doy formato al campo end, y le asigno 17 a las horas
- end_tz_user_str = tz_user_str.localize(end_no_timezone) #asigno timezone Europa/Madrid al campo start_no_timezone
- end_utc = utc.localize(end_no_timezone)#asigno timezone utc al campo start_no_timezone (para poder tener dos campos naive)
- diff1 = end_utc - end_tz_user_str #obtengo la diferencia entre start_utc y start_europe
- diff1_hours = diff1.seconds // 3600 #obtengo la diferencia en horas en int del campo diff
- end = datetime.datetime(end.year, end.month, end.day,17 - diff1_hours)
- employee = record.employee_id.id #Obtengo el campo Employee de Time Off
- if start.month == end.month and start.year == end.year: #el slot solo ocupa un mes
- forecast = env['planning.slot'].create({'employee_id': employee,'project_id':holiday_project_id, 'task_id':holiday_task_id, 'start_datetime':start, 'end_datetime':end}) #Crea una nueva planning shift con los siguientes datos.
- else:
- m = (((end.year - start.year) - 1) * 12) + 12 - start.month + 1 + end.month
- for i in range(1,m+1):
- idate_no_timezone = datetime.datetime(start.year + (start.month + i -2 )//12, (start.month + i-2) % 12 + 1 , 1, 9) #calcula el primer día del mes en curso y fija las 9:00am
- #2 1983+ (5+2-1)//12,
- idate_tz_user_str = tz_user_str.localize(idate_no_timezone) #asigno timezone Europa/Madrid al campo idate_no_timezone
- idate_utc = utc.localize(idate_no_timezone)#asigno timezone utc al campo idate_no_timezone (para poder tener dos campos naive)
- diff2 = idate_utc - idate_tz_user_str #obtengo la diferencia entre idate_utc y idate_europe
- diff2_hours = diff2.seconds // 3600 #obtengo la diferencia en horas en int del campo diff
- idate = datetime.datetime(start.year + (start.month + i -2 )//12, (start.month + i-2) % 12 + 1 , 1, 9 - diff2_hours)
- if i == 1:
- #primer slot
- istart = start #9-4-1983
- # get close to the end of the month for any day, and add 4 days 'over'
- next_month = istart.replace(day=28) + datetime.timedelta(days=4) #28-4-1983 + 4 = 2-5-1983
- # subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
- iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983
- iend_no_timezone = datetime.datetime(iend.year,iend.month,iend.day,17) #establece la hora del final del turno
- iend_tz_user_str = tz_user_str.localize(iend_no_timezone) #asigno timezone Europa/Madrid al campo idate_no_timezone
- iend_utc = utc.localize(iend_no_timezone)#asigno timezone utc al campo idate_no_timezone (para poder tener dos campos naive)
- diff3 = iend_utc - iend_tz_user_str #obtengo la diferencia entre idate_utc y idate_europe
- diff3_hours = diff3.seconds // 3600 #obtengo la diferencia en horas en int del campo diff
- iend = datetime.datetime(iend.year,iend.month,iend.day,17 - diff3_hours) #establece la hora del final del turno
- elif i == m:
- #último slot
- istart = idate
- iend = end
- else:
- #middle slot
- istart = idate
- # get close to the end of the month for any day, and add 4 days 'over'
- next_month = idate.replace(day=28) + datetime.timedelta(days=4) #28-4-1983 + 4 = 2-5-1983
- # subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
- iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983
- iend_no_timezone = datetime.datetime(iend.year,iend.month,iend.day,17) #establece la hora del final del turno
- iend_tz_user_str = tz_user_str.localize(iend_no_timezone) #asigno timezone Europa/Madrid al campo idate_no_timezone
- iend_utc = utc.localize(iend_no_timezone)#asigno timezone utc al campo idate_no_timezone (para poder tener dos campos naive)
- diff4 = iend_utc - iend_tz_user_str #obtengo la diferencia entre idate_utc y idate_europe
- diff4_hours = diff4.seconds // 3600 #obtengo la diferencia en horas en int del campo diff
- iend = datetime.datetime(iend.year,iend.month,iend.day,17 - diff4_hours) #establece la hora del final del turno
- forecast = env['planning.slot'].create({'employee_id': employee,'project_id':holiday_project_id, 'task_id':holiday_task_id, 'start_datetime':istart, 'end_datetime':iend}) #Crea una nueva planning shift con los siguientes datos.
Add Comment
Please, Sign In to add comment