Advertisement
mariomolinos

Create SOrder Control Shifts

Sep 2nd, 2022
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. for s in records:
  2.   order = s.id
  3.   createdOrderControls = env['x_order_control'].sudo().search([('x_studio_period_type','=', 'Monthly - Aut'),('x_control_order','=', order)])
  4.   for created in createdOrderControls:
  5.     created.unlink()
  6.  
  7.  
  8.   if s.x_sorder_start and s.x_sorder_end:
  9.     start = s.x_sorder_start.replace(day=1)
  10.     end = s.x_sorder_end.replace(day=28) + datetime.timedelta(days=4)
  11.     end = end - datetime.timedelta(days=end.day)
  12.    
  13.    
  14.     if start.month == end.month and start.year == end.year: #solo ocupa un mes
  15.       name = s.name.split()[0] + " - " + str(start.year) + " - " + str(start.month) + " - MA"
  16.       orderControl = env['x_order_control'].create({'x_name': name, 'x_control_order': s.id, 'x_control_start': start, 'x_control_end': end, 'x_studio_period_type': 'Monthly - Aut'})
  17.      
  18.     else:
  19.       m = (((end.year - start.year) - 1) * 12) + 12 - start.month + 1 + end.month
  20.  
  21.       for i in range(1,m+1):
  22.         idate = datetime.datetime(start.year + (start.month + i -2 )//12, (start.month + i-2) % 12 + 1 , 1) #calcula el primer día del mes
  23.          
  24.  
  25.         if i == 1:
  26.           #first
  27.           istart = start #9-4-1983
  28.           next_month = istart.replace(day=28) + datetime.timedelta(days=4)
  29.           iend = next_month - datetime.timedelta(days=next_month.day)
  30.           name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA"
  31.          
  32.          
  33.         elif i == m:
  34.             #last
  35.             istart = idate
  36.             iend = end
  37.             name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA"
  38.         else:
  39.             #middle
  40.             istart = idate
  41.             next_month = idate.replace(day=28) + datetime.timedelta(days=4)
  42.             iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983    
  43.             name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA"
  44.      
  45.         orderControl = env['x_order_control'].create({'x_name': name, 'x_control_order': s.id, 'x_control_start': istart, 'x_control_end': iend, 'x_studio_period_type': 'Monthly - Aut'})        
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement