[UPDT] ATTENDANCE: Updated work record to include date range

This commit is contained in:
Horilla
2025-11-24 14:33:55 +05:30
parent 919739a446
commit f7f3d0fd10
3 changed files with 85 additions and 46 deletions

View File

@@ -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

View File

@@ -146,3 +146,14 @@
</div>
</div>
{% endif %}
<script>
$(document).ready(function(){
{% if request.GET.start_date or request.GET.end_date %}
$("#monthYearField").val("");
{% elif request.GET.month %}
$("#startDate").val("");
$("#endDate").val("");
{% endif %}
})
</script>

View File

@@ -1,5 +1,7 @@
{% extends 'index.html' %} {% block content %} {% load i18n static %}
{% extends 'index.html' %}
{% load i18n static %}
{% block content %}
<div class="">
<div class="bg-white p-3 rounded-md shadow-card">
<div class="flex gap-2 md:gap-0 flex-wrap justify-between items-end mb-2 border-b border-dark-50 pb-3">
@@ -20,7 +22,7 @@
id="monthYearField"
value="{{ current_date|date:"Y-m" }}"
name="month"
onchange="$('#employeeFilter').click()"
onchange="$('#startDate').val('');$('#endDate').val('');$('#employeeFilter').click();"
/>
<div class="relative dropdown-wrapper">
@@ -29,22 +31,40 @@
onclick="event.preventDefault();"
>
<div class="flex items-center gap-2 ">
<img src="{% static 'horilla_theme/assets/img/icons/sort.svg' %}" alt="" width="17"
>
<img src="{% static 'horilla_theme/assets/img/icons/sort.svg' %}" alt="" width="17">
{% trans "Filter" %}
</div>
</button>
<div class="dropdown-content absolute z-10 bg-white rounded-lg [box-shadow:0px_0px_20px_0px_rgb(0_0_0_/_8%)] min-w-[400px] p-3 right-0">
<div class="accordion-wrapper space-y-2">
{% include 'employee_filters.html' %}
<div class="oh-dropdown__filter-footer">
<button
class="oh-btn oh-btn--secondary oh-btn--small w-100"
id="employeeFilter"
>
{% trans "Filter" %}
</button>
{% include 'employee_filters.html' %}
<div class="oh-accordion">
<div class="oh-accordion-header">{% trans "Date Range" %}</div>
<div class="oh-accordion-body">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="input-group">
<label class="oh-label" for="startDate">{% trans "Start Date" %}:</label>
<input type="date" name="start_date" id="startDate" class="oh-input" onchange="$('#monthYearField').val('')">
</div>
</div>
<div class="col-sm-12 col-md-12 col-lg-6">
<div class="input-group">
<label class="oh-label" for="endDate">{% trans "End Date" %}:</label>
<input type="date" name="end_date" id="endDate" class="oh-input" max="{{ today|date:'Y-m-d' }}" onchange="$('#monthYearField').val('')">
</div>
</div>
</div>
</div>
</div>
<div class="oh-dropdown__filter-footer">
<button
class="oh-btn oh-btn--secondary oh-btn--small w-100 float-end"
id="employeeFilter"
>
{% trans "Filter" %}
</button>
</div>
</div>
</div>
</div>
@@ -78,4 +98,4 @@
});
});
</script>
{% endblock content %}
{% endblock %}