#https://rustpad.io/#hsExcF #Automation para crear los planning slots a partir de los leaves, aprobados o no. #La automatización primero borra todos los planning slots de vacaciones y luego los crea otra vez, de esta forma evitamos actualizar planning slots existentes #La automatización crea planning slots que como máximo abarcan un mes de tiempo, si un leaves abarca más de un mes el slot se divide en tantos como sea necesario para cumplir con la regla anterior. 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 #DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" #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 #start = datetime.datetime.strptime(str(start), DATETIME_FORMAT) + datetime.timedelta(hours=7) start = datetime.datetime(start.year, start.month, start.day,7) #Doy formato al campo start, y le asigno 7 a las horas end = record.request_date_to #Obtengo el campo date end de Time Off #end = datetime.datetime.strptime(str(end), DATETIME_FORMAT) + datetime.timedelta(hours=17) end = datetime.datetime(end.year, end.month, end.day,17) #Doy formato al campo end, y le asigno 17 a las horas 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 #https://www.w3schools.com/python/python_for_loops.asp #https://stackoverflow.com/questions/42950/how-to-get-the-last-day-of-the-month last day of month #https://stackoverflow.com/questions/546321/how-do-i-calculate-the-date-six-months-from-the-current-date-using-the-datetime offset date by n months #https://docs.python.org/3/library/datetime.html#datetime.datetime #(day, month, year) = (day, (month + 5) % 12 + 1, year + (month + 5)/12) # 2*12 + 12-5+1+6 #-1*12+12-5+1+6 #0*12+12-5+1+2 = 10 #start: 09/05/1983 #end: 13/02/1984 #83: 8 #84: 12 #85: 12 #86: 6 for i in range(1,m+1): #start 9-4-1983 #1 1-05-1983 #2 1-06-1983 #3 1-07-1983 #4 1-08-1983 #... #10 1-02-1984 #end 13-02-1984 idate = datetime.datetime(start.year + (start.month + i -2 )//12, (start.month + i-2) % 12 + 1 , 1,7) #calcula el primer día del mes en curso y fija las 7:00am #1 1983 + 5+1-2//12 (0), 5+1-2 % 12 +1 (5) #2 1983+ (5+2-2)//12, 5+2-2 % 12 + 1(6) #8 1983 + (5+8-2)//12(0), 5+8-2 % 12 + 1 (12) #9 1983 + (5+9-2)//12 (1), 5+9-2 %12 + 1 (1) 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 = datetime.datetime(iend.year,iend.month,iend.day,17) #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 = datetime.datetime(iend.year,iend.month,iend.day,17) #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.