Optimised code for Student Monthly Attendance Report
This commit is contained in:
parent
94f42ea145
commit
df4b9ab630
@ -15,8 +15,9 @@ def execute(filters=None):
|
|||||||
to_date = get_last_day(filters["month"] + '-' + filters["year"])
|
to_date = get_last_day(filters["month"] + '-' + filters["year"])
|
||||||
total_days_in_month = date_diff(to_date, from_date) +1
|
total_days_in_month = date_diff(to_date, from_date) +1
|
||||||
columns = get_columns(total_days_in_month)
|
columns = get_columns(total_days_in_month)
|
||||||
att_map = get_attendance_list(from_date, to_date, filters.get("student_batch"))
|
|
||||||
students = get_student_batch_students(filters.get("student_batch"))
|
students = get_student_batch_students(filters.get("student_batch"))
|
||||||
|
students_list = get_students_list(students)
|
||||||
|
att_map = get_attendance_list(from_date, to_date, filters.get("student_batch"), students_list)
|
||||||
data = []
|
data = []
|
||||||
for stud in students:
|
for stud in students:
|
||||||
row = [stud.student, stud.student_name]
|
row = [stud.student, stud.student_name]
|
||||||
@ -44,32 +45,55 @@ def get_columns(days_in_month):
|
|||||||
columns += [_("Total Present") + ":Int:95", _("Total Absent") + ":Int:90"]
|
columns += [_("Total Present") + ":Int:95", _("Total Absent") + ":Int:90"]
|
||||||
return columns
|
return columns
|
||||||
|
|
||||||
def get_attendance_list(from_date, to_date, student_batch):
|
def get_students_list(students):
|
||||||
|
student_list = []
|
||||||
|
for stud in students:
|
||||||
|
student_list.append(stud.student)
|
||||||
|
return student_list
|
||||||
|
|
||||||
|
def get_attendance_list(from_date, to_date, student_batch, students_list):
|
||||||
attendance_list = frappe.db.sql("""select student, date, status
|
attendance_list = frappe.db.sql("""select student, date, status
|
||||||
from `tabStudent Attendance` where docstatus = 1 and student_batch = %s
|
from `tabStudent Attendance` where docstatus = 1 and student_batch = %s
|
||||||
and date between %s and %s
|
and date between %s and %s
|
||||||
order by student, date""",
|
order by student, date""",
|
||||||
(student_batch, from_date, to_date), as_dict=1)
|
(student_batch, from_date, to_date), as_dict=1)
|
||||||
att_map = {}
|
att_map = {}
|
||||||
|
students_with_leave_application = get_students_with_leave_application(from_date, to_date, students_list)
|
||||||
for d in attendance_list:
|
for d in attendance_list:
|
||||||
att_map.setdefault(d.student, frappe._dict()).setdefault(d.date, "")
|
att_map.setdefault(d.student, frappe._dict()).setdefault(d.date, "")
|
||||||
students_with_leave_application = get_students_with_leave_application(d.date)
|
if students_with_leave_application and d.student in students_with_leave_application.get(d.date):
|
||||||
if students_with_leave_application:
|
|
||||||
for stud in students_with_leave_application:
|
|
||||||
if stud.student== d.student:
|
|
||||||
att_map[d.student][d.date] = "Present"
|
att_map[d.student][d.date] = "Present"
|
||||||
break
|
|
||||||
else:
|
|
||||||
att_map[d.student][d.date] = d.status
|
|
||||||
else:
|
else:
|
||||||
att_map[d.student][d.date] = d.status
|
att_map[d.student][d.date] = d.status
|
||||||
return att_map
|
return att_map
|
||||||
|
|
||||||
def get_students_with_leave_application(date):
|
def get_students_with_leave_application(from_date, to_date, students_list):
|
||||||
students_with_leave_application = frappe.db.sql("""select student from
|
leave_applications = frappe.db.sql("""
|
||||||
`tabStudent Leave Application` where mark_as_present and docstatus = 1 and
|
select student, from_date, to_date
|
||||||
%s between from_date and to_date""", date, as_dict=1)
|
from `tabStudent Leave Application`
|
||||||
return students_with_leave_application
|
where
|
||||||
|
mark_as_present and docstatus = 1
|
||||||
|
and student in %(students)s
|
||||||
|
and (
|
||||||
|
from_date between %(from_date)s and %(to_date)s
|
||||||
|
or to_date between %(from_date)s and %(to_date)s
|
||||||
|
or (%(from_date)s between from_date and to_date and %(to_date)s between from_date and to_date)
|
||||||
|
)
|
||||||
|
""", {
|
||||||
|
"students": students_list,
|
||||||
|
"from_date": from_date,
|
||||||
|
"to_date": to_date
|
||||||
|
}, as_dict=True)
|
||||||
|
students_with_leaves= {}
|
||||||
|
for application in leave_applications:
|
||||||
|
for date in daterange(application.from_date, application.to_date):
|
||||||
|
students_with_leaves.setdefault(date, []).append(application.student)
|
||||||
|
|
||||||
|
return students_with_leaves
|
||||||
|
|
||||||
|
def daterange(d1, d2):
|
||||||
|
import datetime
|
||||||
|
return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1))
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_attendance_years():
|
def get_attendance_years():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user