mariomolinos

Forecasting Time Off 3

Aug 12th, 2021 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.62 KB | None | 0 0
  1. #https://rustpad.io/#hsExcF
  2. #Automation para crear los planning slots a partir de los leaves, aprobados o no.
  3. #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
  4. #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.
  5.  
  6. holiday_project_id = 1079
  7. holiday_task_id = 36181
  8. automation_start_date = datetime.datetime(2021, 1, 1,00) #Establecemos una fecha inicial para la creación de planning-shifts
  9.  
  10. 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
  11.  
  12. #DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  13.  
  14. #PRIMERO BORRA LOS PLANNING SLOTS DEL PROYECTO DE VACACIONES.
  15. 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
  16. for f in forecasts: #para cada planning shift entre todas las que cumplan la condición de la linea anterior:
  17.     f.unlink() #borrar
  18.  
  19. #CREAR LOS PLANNING SLOTS
  20. for record in allRecords: #para cada registro entre todos los registros que cumplan la condición de la linea 14:
  21.     start = record.request_date_from       #Obtengo el campo date start de Time Off
  22.     #start = datetime.datetime.strptime(str(start), DATETIME_FORMAT) + datetime.timedelta(hours=7)
  23.     start = datetime.datetime(start.year, start.month, start.day,7) #Doy formato al campo start, y le asigno 7 a las horas
  24.     end = record.request_date_to           #Obtengo el campo date end de Time Off
  25.      #end = datetime.datetime.strptime(str(end), DATETIME_FORMAT) + datetime.timedelta(hours=17)
  26.     end = datetime.datetime(end.year, end.month, end.day,17) #Doy formato al campo end, y le asigno 17 a las horas
  27.     employee = record.employee_id.id       #Obtengo el campo Employee de Time Off
  28.  
  29.     if start.month == end.month and start.year == end.year: #el slot solo ocupa un mes
  30.         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.
  31.     else:
  32.         m = (((end.year - start.year) - 1) * 12) + 12 - start.month + 1 + end.month
  33.         #https://www.w3schools.com/python/python_for_loops.asp
  34.         #https://stackoverflow.com/questions/42950/how-to-get-the-last-day-of-the-month last day of month
  35.         #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
  36.         #https://docs.python.org/3/library/datetime.html#datetime.datetime
  37.  
  38.         #(day, month, year) = (day, (month + 5) % 12 + 1, year + (month + 5)/12)
  39.         # 2*12 + 12-5+1+6
  40.         #-1*12+12-5+1+6
  41.         #0*12+12-5+1+2 = 10
  42.         #start: 09/05/1983
  43.         #end: 13/02/1984
  44.         #83: 8
  45.         #84: 12
  46.         #85: 12
  47.         #86: 6
  48.  
  49.         for i in range(1,m+1):
  50.             #start 9-4-1983
  51.             #1 1-05-1983
  52.             #2 1-06-1983
  53.             #3 1-07-1983
  54.             #4 1-08-1983
  55.             #...
  56.             #10 1-02-1984
  57.             #end 13-02-1984    
  58.            
  59.             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
  60.             #1 1983 + 5+1-2//12 (0), 5+1-2 % 12 +1 (5)
  61.             #2 1983+ (5+2-2)//12, 5+2-2 % 12 + 1(6)
  62.             #8 1983 + (5+8-2)//12(0), 5+8-2 % 12 + 1 (12)
  63.             #9 1983 + (5+9-2)//12 (1), 5+9-2 %12 + 1 (1)
  64.  
  65.             if i == 1:
  66.                 #primer slot
  67.                 istart = start #9-4-1983
  68.                 # get close to the end of the month for any day, and add 4 days 'over'
  69.                 next_month = istart.replace(day=28) + datetime.timedelta(days=4) #28-4-1983 + 4 = 2-5-1983
  70.                 # 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
  71.                 iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983    
  72.                 iend = datetime.datetime(iend.year,iend.month,iend.day,17) #establece la hora del final del turno
  73.             elif i == m:
  74.                 #último slot
  75.                 istart = idate
  76.                 iend = end                
  77.             else:
  78.                 #middle slot
  79.                 istart = idate
  80.                 # get close to the end of the month for any day, and add 4 days 'over'
  81.                 next_month = idate.replace(day=28) + datetime.timedelta(days=4) #28-4-1983 + 4 = 2-5-1983
  82.                 # 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
  83.                 iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983    
  84.                 iend = datetime.datetime(iend.year,iend.month,iend.day,17) #establece la hora del final del turno
  85.                              
  86.             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.
  87.  
Add Comment
Please, Sign In to add comment