Ejemplo n.º 1
0
            def event_update(request, *args, **kwargs):

                start = request.GET.get('start')
                end = request.GET.get('end')
                resourceId = request.GET.get('resourceId')
                taskId = request.GET.get('taskId')
                workerscheduledId = request.GET.get('workerscheduledId')

                start = UDatetime.datetime_str_init(start)
                end = UDatetime.datetime_str_init(end, start,
                                                  timedelta(hours=2))

                date = UDatetime.pick_date_by_two_date(start, end)

                duration = end - start

                worker = Workers.objects.get(id__exact=resourceId)
                task = Tasks.objects.get(work_order__exact=taskId)
                available_id = WorkerAvailable.objects.filter(
                    time_start__lte=start,
                    time_end__gte=end,
                    name__exact=worker)
                if available_id.count() > 0:
                    available_id = available_id[0]

                WorkScheduleReviseDAO.update_or_create_schedule(
                    request.user,
                    start,
                    end,
                    date,
                    duration,
                    available_id,
                    worker,
                    task,
                    schedule_id=workerscheduledId)

                response = []

                return JsonResponse(response, safe=False)
Ejemplo n.º 2
0
            def select_submit(request, *args, **kwargs):
                command_type = request.GET.get('CommandType')
                worker_id = request.GET.get('resourceId')
                start = request.GET.get('start')
                end = request.GET.get('end')

                start = UDatetime.datetime_str_init(start)
                end = UDatetime.datetime_str_init(end, start,
                                                  timedelta(hours=2))

                date = UDatetime.pick_date_by_two_date(start, end)
                duration = end - start

                response = {}

                worker = Workers.objects.get(id=worker_id)
                if command_type == 'ExtendWorkerAvailable':
                    avails = WorkerAvailable.objects.filter(
                        Q(time_start__range=[start, end])
                        | Q(time_end__range=[start, end])
                        | Q(time_start__lte=start, time_end__gte=end),
                        name__exact=worker)

                    if avails.exists():

                        avails_df = pd.DataFrame.from_records(avails.values())
                        if avails.count() == 1:
                            avail_id = avails_df['id'].tolist()[0]
                        else:
                            return

                        start_list = avails_df['time_start'].tolist()
                        start_list.append(start)
                        end_list = avails_df['time_end'].tolist()
                        end_list.append(end)
                        # start_new = min(start_list).astimezone(EST)
                        # end_new = max(end_list).astimezone(EST)
                        start_new = min(start_list).replace(tzinfo=EST)
                        end_new = max(end_list).replace(tzinfo=EST)
                        date = UDatetime.pick_date_by_two_date(
                            start_new, end_new)

                        duration_new = end_new - start_new

                        WorkScheduleReviseDAO.update_or_create_available(
                            request.user,
                            start_new,
                            end_new,
                            date,
                            duration_new,
                            WorkAvailSheet.DEDUCTION,
                            worker,
                            avail_id=avail_id,
                            source='manual')
                        # WorkerAvailable.objects.update_or_create(id=avail_id,
                        #                                          defaults={
                        #                                              'name': worker,
                        #                                              'date': date,
                        #                                              'duration': duration_new,
                        #                                              'deduction': WorkAvailSheet.DEDUCTION,
                        #                                              'time_start': start_new,
                        #                                              'time_end': end_new
                        #                                          })

                    else:
                        duration = end - start
                        WorkScheduleReviseDAO.update_or_create_available(
                            request.user,
                            start,
                            end,
                            start.date(),
                            duration,
                            WorkAvailSheet.DEDUCTION,
                            worker,
                            source='manual')
                        # WorkerAvailable.objects.update_or_create(name=worker,
                        #                                          date=start.date(),
                        #                                          duration=duration,
                        #                                          deduction=WorkAvailSheet.DEDUCTION,
                        #                                          time_start=start,
                        #                                          time_end=end,
                        #                                          source='manual')

                elif command_type == 'CutWorkerAvailable':
                    avails = WorkerAvailable.objects.filter(
                        Q(time_start__range=[start, end])
                        | Q(time_end__range=[start, end])
                        | Q(time_start__lte=start, time_end__gte=end),
                        name__exact=worker)

                    if avails.exists():

                        start = start.replace(tzinfo=pytz.UTC)
                        end = end.replace(tzinfo=pytz.UTC)

                        avails_df = pd.DataFrame.from_records(avails.values())
                        if avails.count() == 1:
                            avail_id = avails_df['id'].tolist()[0]

                            start_list = avails_df['time_start'].tolist()
                            start_list.append(start)
                            end_list = avails_df['time_end'].tolist()
                            end_list.append(end)
                            range_new = UDatetime.remove_overlap(
                                start_list[0], end_list[0], start_list[1],
                                end_list[1])
                            if len(range_new) == 1:
                                start_new = range_new[0][0]
                                end_new = range_new[0][1]
                                if start_new == end_new:
                                    WorkScheduleReviseDAO.remove_available_by_id(
                                        avail_id)
                                    # WorkerAvailable.objects.filter(id__in=avail_id).delete()
                                else:
                                    date = UDatetime.pick_date_by_two_date(
                                        start_new, end_new)

                                    duration_new = end_new - start_new

                                    WorkScheduleReviseDAO.update_or_create_available(
                                        request.user,
                                        start_new,
                                        end_new,
                                        date,
                                        duration_new,
                                        WorkAvailSheet.DEDUCTION,
                                        worker,
                                        avail_id=avail_id,
                                        source='manual')
                                    # WorkerAvailable.objects.update_or_create(id__in=ids,
                                    #                                          defaults={
                                    #                                              'name': worker,
                                    #                                              'date': date,
                                    #                                              'duration': duration_new,
                                    #                                              'deduction': WorkAvailSheet.DEDUCTION,
                                    #                                              'time_start': start_new,
                                    #                                              'time_end': end_new
                                    #                                          })
                            else:
                                response = {}

                elif command_type in ['Lunch', 'Breaks', 'UnionBus']:
                    if command_type == 'Lunch':
                        # task = WorkScheduleReviseDAO.create_or_update_timeoff_lunch_task(request.user, source='manual')
                        task = Tasks.objects.get(work_order__exact='TLUNCH')
                    elif command_type == 'Breaks':
                        # task = WorkScheduleReviseDAO.create_or_update_timeoff_breaks_task(request.user, source='manual')
                        task = Tasks.objects.get(work_order__exact='TBREAK')
                    elif command_type == 'UnionBus':
                        task = WorkScheduleReviseDAO.create_or_update_union_bus_task(
                            request.user, duration, source='manual')
                    else:
                        return JsonResponse(response)

                    available_ids = WorkerAvailable.objects.filter(
                        time_start__lte=start,
                        time_end__gte=end,
                        name__exact=worker)
                    if available_ids.count() > 0:
                        available_id = available_ids[0]
                        WorkScheduleReviseDAO.update_or_create_schedule(
                            request.user,
                            start,
                            end,
                            date,
                            duration,
                            available_id,
                            worker,
                            task,
                            source='manual')
                        # worker_scheduled = WorkerScheduled.objects.update_or_create(name=worker,
                        #                                                             date=date,
                        #                                                             time_start=start,
                        #                                                             time_end=end,
                        #                                                             task_id=task,
                        #                                                             available_id=available_id,
                        #                                                             defaults={'duration': duration})

                return JsonResponse(response)