From f7f3d0fd100fd0c95b4f8fd7590654217cee2003 Mon Sep 17 00:00:00 2001 From: Horilla Date: Mon, 24 Nov 2025 14:33:55 +0530 Subject: [PATCH] [UPDT] ATTENDANCE: Updated work record to include date range --- attendance/views/views.py | 74 ++++++++++--------- .../work_record/work_record_list.html | 11 +++ .../work_record/work_record_view.html | 46 ++++++++---- 3 files changed, 85 insertions(+), 46 deletions(-) diff --git a/attendance/views/views.py b/attendance/views/views.py index a279c43ac..9b045a41c 100644 --- a/attendance/views/views.py +++ b/attendance/views/views.py @@ -2476,45 +2476,53 @@ def work_records_change_month(request): start_date_str = request.GET.get("start_date") end_date_str = request.GET.get("end_date") - # Initialize as None - start_date = None - end_date = None + if start_date_str or end_date_str: + # Initialize as None + start_date = None + end_date = None - # Try parsing the start date - if start_date_str: - try: - start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date() - except ValueError: - start_date = None + # Try parsing the start date + if start_date_str: + try: + start_date = datetime.strptime(start_date_str, "%Y-%m-%d").date() + except ValueError: + start_date = None - # Try parsing the end date - if end_date_str: - try: - end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date() - except ValueError: - end_date = None + # Try parsing the end date + if end_date_str: + try: + end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date() + except ValueError: + end_date = None - # Default end_date to today if missing or invalid - if not end_date: - today = date.today() - last_day = calendar.monthrange(today.year, today.month)[1] - end_date = date(today.year, today.month, last_day) + # Default end_date to today if missing or invalid + if not end_date: + today = date.today() + last_day = calendar.monthrange(today.year, today.month)[1] + end_date = date(today.year, today.month, last_day) - # Default start_date to first day of end_date's month if missing or invalid - if not start_date: - start_date = date(year=end_date.year, month=end_date.month, day=1) + # Default start_date to first day of end_date's month if missing or invalid + if not start_date: + start_date = date(year=end_date.year, month=end_date.month, day=1) - # Ensure start_date is not after end_date - if start_date > end_date: - # Optional: raise error or swap, depending on your use case - start_date = date(year=end_date.year, month=end_date.month, day=1) + # Ensure start_date is not after end_date + if start_date > end_date: + # Optional: raise error or swap, depending on your use case + start_date = date(year=end_date.year, month=end_date.month, day=1) - # Generate list of dates between start_date and end_date (inclusive) - month_dates = [] - current_date = start_date - while current_date <= end_date: - month_dates.append(current_date) - current_date += timedelta(days=1) + # Generate list of dates between start_date and end_date (inclusive) + month_dates = [] + current_date = start_date + while current_date <= end_date: + month_dates.append(current_date) + current_date += timedelta(days=1) + else: + month_dates = [ + datetime(year, month, day).date() + for week in calendar.monthcalendar(year, month) + for day in week + if day + ] work_records = WorkRecords.objects.filter( date__in=month_dates, employee_id__in=page_emp.object_list diff --git a/horilla_theme/templates/attendance/work_record/work_record_list.html b/horilla_theme/templates/attendance/work_record/work_record_list.html index 45889b0fc..af5925390 100644 --- a/horilla_theme/templates/attendance/work_record/work_record_list.html +++ b/horilla_theme/templates/attendance/work_record/work_record_list.html @@ -146,3 +146,14 @@ {% endif %} + + diff --git a/horilla_theme/templates/attendance/work_record/work_record_view.html b/horilla_theme/templates/attendance/work_record/work_record_view.html index 8771d6fdd..261d6a053 100644 --- a/horilla_theme/templates/attendance/work_record/work_record_view.html +++ b/horilla_theme/templates/attendance/work_record/work_record_view.html @@ -1,5 +1,7 @@ -{% extends 'index.html' %} {% block content %} {% load i18n static %} +{% extends 'index.html' %} +{% load i18n static %} +{% block content %}
@@ -20,7 +22,7 @@ id="monthYearField" value="{{ current_date|date:"Y-m" }}" name="month" - onchange="$('#employeeFilter').click()" + onchange="$('#startDate').val('');$('#endDate').val('');$('#employeeFilter').click();" />