for s in records: order = s.id createdOrderControls = env['x_order_control'].sudo().search([('x_studio_period_type','=', 'Monthly - Aut'),('x_control_order','=', order)]) for created in createdOrderControls: created.unlink() if s.x_sorder_start and s.x_sorder_end: start = s.x_sorder_start.replace(day=1) end = s.x_sorder_end.replace(day=28) + datetime.timedelta(days=4) end = end - datetime.timedelta(days=end.day) if start.month == end.month and start.year == end.year: #solo ocupa un mes name = s.name.split()[0] + " - " + str(start.year) + " - " + str(start.month) + " - MA" 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'}) else: m = (((end.year - start.year) - 1) * 12) + 12 - start.month + 1 + end.month for i in range(1,m+1): idate = datetime.datetime(start.year + (start.month + i -2 )//12, (start.month + i-2) % 12 + 1 , 1) #calcula el primer día del mes if i == 1: #first istart = start #9-4-1983 next_month = istart.replace(day=28) + datetime.timedelta(days=4) iend = next_month - datetime.timedelta(days=next_month.day) name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA" elif i == m: #last istart = idate iend = end name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA" else: #middle istart = idate next_month = idate.replace(day=28) + datetime.timedelta(days=4) iend = next_month - datetime.timedelta(days=next_month.day) #30-4-1983 name = s.name.split()[0] + " - " + str(istart.year) + " - " + str(istart.month) + " - MA" 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'})