def report_worked_hours_all_drivers(date_from, date_to): wb = Workbook() drivers = get_drivers() for driver in drivers: wc = wb.create_sheet(f"Водій {driver.get_full_name()}") wc.title = f'Водій {driver.get_full_name()}' shifts = Shift.objects.filter(driver=driver, date__range=[date_from, date_to]) schedules = [] res = [] hours = 6 * len(shifts) for shift in shifts: schedules += [Schedule.objects.filter(shift=shift)] res.append(f'{shift.date}, {shift}') wc['A1'] = f'Report "worked hours"' wc['B1'] = f'from {date_from} to {date_to}' wc['C1'] = f'{driver.get_full_name()}' wc['D1'] = f'total hours: {hours}' for i in range(len(res)): wc[f'A{i + 2}'] = res[i] return wb
def post(self, request): default = timezone.datetime.now().strftime('%Y-%m-%d') report = self.request.POST.get('report') date_from = request.POST.get('from') date_to = request.POST.get('to') date_from = default if not date_from else date_from date_to = default if not date_to else date_to context = {'drivers': get_drivers()} if report == 'hours': if self.request.user.groups.filter(name='Водій').exists(): wb = reports.report_worked_hours(self.request.user, date_from, date_to) else: driver = self.request.POST.get('driver') if driver == 'all_drivers': wb = reports.report_worked_hours_all_drivers( date_from, date_to) else: wb = reports.report_worked_hours( User.objects.get(id=int(driver)), date_from, date_to) return HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel') if report == 'drivers_dest': wb = reports.report_drivers_dest(date_from, date_to) return HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel') if report == 'kzot': if self.request.user.groups.filter(name='Водій').exists(): wb = reports.report_kzot(self.request.user, date_from, date_to) return HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel') else: driver = self.request.POST.get('driver') if driver == 'all_drivers': wb = reports.report_kzot_all_drivers(date_from, date_to) else: wb = reports.report_kzot(User.objects.get(id=int(driver)), date_from, date_to) return HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel') return render(self.request, 'routes/reports.html', context=context)
def report_drivers_dest(date_from, date_to): wb = Workbook() drivers = get_drivers() for driver in drivers: wc = wb.create_sheet(f"Водій {driver.get_full_name()}") wc.title = f'Водій {driver.get_full_name()}' shifts = Shift.objects.filter(driver=driver, date__range=[date_from, date_to]) wc['A1'] = f'Report "drivers dest"' wc['B1'] = f'from {date_from} to {date_to}' wc['C1'] = f'{driver.get_full_name()}' i = 0 for shift in shifts: wc[f'A{i + 2}'] = str(shift) i += 1 return wb
def report_kzot_all_drivers(date_from, date_to): wb = Workbook() date_from = datetime.strptime(date_from, '%Y-%m-%d') date_to = datetime.strptime(date_to, '%Y-%m-%d') weeks = [] cur_date = date_from while cur_date < date_to: monday_date = cur_date - timedelta(days=cur_date.weekday()) sunday_date = monday_date + timedelta(days=6) weeks.append((monday_date, sunday_date)) cur_date = cur_date + timedelta(days=7) drivers = get_drivers() for driver in drivers: wc = wb.create_sheet(f"Водій {driver.get_full_name()}") wc.title = f'Водій {driver.get_full_name()}' violation = False wc['A1'] = f'Report "KZOT"' wc['B1'] = f'from {date_from} to {date_to}' wc['C1'] = f'{driver.first_name} {driver.last_name}' i = 2 for week in weeks: start, end = week shift_num = Shift.objects.filter(driver=driver, date__range=[start, end]).count() if shift_num > 7: violation = True wc[f'A{i}'] = f'week from {start.strftime("%Y-%m-%d")} to {end.strftime("%Y-%m-%d")}' wc[f'B{i}'] = f'worked hours {shift_num * 6}' i += 1 if not violation: wc['A2'] = 'There were no violations' return wb
def get(self, request): context = {'drivers': get_drivers()} return render(self.request, 'routes/reports.html', context=context)