blob: dea851515966dd906d64cc5f89d6dcb61401e15c [file] [log] [blame]
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +05301# -*- coding: utf-8 -*-
2# Copyright (c) 2015, Frappe Technologies and contributors
3# For license information, please see license.txt
4
5from __future__ import unicode_literals
6import frappe
7import json
8from frappe import _
9from frappe.model.mapper import get_mapped_doc
10from frappe.utils import flt
11
12@frappe.whitelist()
13def enroll_student(source_name):
14 """Creates a Student Record and returns a Program Enrollment.
15
16 :param source_name: Student Applicant.
17 """
18 student = get_mapped_doc("Student Applicant", source_name,
19 {"Student Applicant": {
20 "doctype": "Student",
21 "field_map": {
22 "name": "student_applicant"
23 }
Neil Trini Lasrado06724592016-08-22 12:57:09 +053024 }}, ignore_permissions=True)
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053025 student.save()
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053026 program_enrollment = frappe.new_doc("Program Enrollment")
27 program_enrollment.student = student.name
28 program_enrollment.student_name = student.title
29 program_enrollment.program = frappe.db.get_value("Student Applicant", source_name, "program")
30 return program_enrollment
31
32@frappe.whitelist()
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053033def check_attendance_records_exist(course_schedule=None, student_batch=None, date=None):
34 """Check if Attendance Records are made against the specified Course Schedule or Student Batch for given date.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053035
36 :param course_schedule: Course Schedule.
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053037 :param student_batch: Student Batch.
38 :param date: Date.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053039 """
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053040 if course_schedule:
41 return frappe.get_list("Student Attendance", filters={"course_schedule": course_schedule})
42 else:
43 return frappe.get_list("Student Attendance", filters={"student_batch": student_batch, "date": date})
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053044
45@frappe.whitelist()
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053046def mark_attendance(students_present, students_absent, course_schedule=None, student_batch=None, date=None):
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053047 """Creates Multiple Attendance Records.
48
49 :param students_present: Students Present JSON.
50 :param students_absent: Students Absent JSON.
51 :param course_schedule: Course Schedule.
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053052 :param student_batch: Student Batch.
53 :param date: Date.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053054 """
55 present = json.loads(students_present)
56 absent = json.loads(students_absent)
57
58 for d in present:
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053059 make_attendance_records(d["student"], d["student_name"], "Present", course_schedule, student_batch, date)
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053060
61 for d in absent:
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053062 make_attendance_records(d["student"], d["student_name"], "Absent", course_schedule, student_batch, date)
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053063
64 frappe.msgprint(_("Attendance has been marked successfully."))
65
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053066def make_attendance_records(student, student_name, status, course_schedule=None, student_batch=None, date=None):
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053067 """Creates Attendance Record.
68
69 :param student: Student.
70 :param student_name: Student Name.
71 :param course_schedule: Course Schedule.
72 :param status: Status (Present/Absent)
73 """
74 student_attendance = frappe.new_doc("Student Attendance")
75 student_attendance.student = student
76 student_attendance.student_name = student_name
77 student_attendance.course_schedule = course_schedule
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053078 student_attendance.student_batch = student_batch
79 student_attendance.date = date
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053080 student_attendance.status = status
81 student_attendance.submit()
82 frappe.db.commit()
83
84@frappe.whitelist()
Neil Trini Lasradod9bc5592016-11-17 15:35:54 +053085def get_student_batch_students(student_batch):
86 """Returns List of student, student_name in Student Batch.
87
88 :param student_batch: Student Batch.
89 """
90 students = frappe.get_list("Student Batch Student", fields=["student", "student_name"] , filters={"parent": student_batch}, order_by= "idx")
91 return students
92
93@frappe.whitelist()
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +053094def get_student_group_students(student_group):
95 """Returns List of student, student_name in Student Group.
96
97 :param student_group: Student Group.
98 """
99 students = frappe.get_list("Student Group Student", fields=["student", "student_name"] , filters={"parent": student_group}, order_by= "idx")
100 return students
101
102@frappe.whitelist()
103def get_fee_structure(program, academic_term=None):
104 """Returns Fee Structure.
105
106 :param program: Program.
107 :param academic_term: Academic Term.
108 """
109 fee_structure = frappe.db.get_values("Fee Structure", {"program": program,
110 "academic_term": academic_term}, 'name', as_dict=True)
111 return fee_structure[0].name if fee_structure else None
112
113@frappe.whitelist()
Neil Trini Lasradof02a0d42016-08-05 15:50:52 +0530114def get_fee_components(fee_structure):
115 """Returns Fee Components.
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +0530116
117 :param fee_structure: Fee Structure.
118 """
119 if fee_structure:
Neil Trini Lasradof02a0d42016-08-05 15:50:52 +0530120 fs = frappe.get_list("Fee Component", fields=["fees_category", "amount"] , filters={"parent": fee_structure}, order_by= "idx")
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +0530121 return fs
122
123@frappe.whitelist()
Neil Trini Lasradoc20adb52016-09-05 16:55:54 +0530124def get_fee_schedule(program, student_category=None):
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +0530125 """Returns Fee Schedule.
126
127 :param program: Program.
Neil Trini Lasradoc20adb52016-09-05 16:55:54 +0530128 :param student_category: Student Category
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +0530129 """
130 fs = frappe.get_list("Program Fee", fields=["academic_term", "fee_structure", "due_date", "amount"] ,
Neil Trini Lasradoc20adb52016-09-05 16:55:54 +0530131 filters={"parent": program, "student_category": student_category }, order_by= "idx")
Neil Trini Lasrado3f0a5812016-07-19 14:17:33 +0530132 return fs
133
134@frappe.whitelist()
135def collect_fees(fees, amt):
136 paid_amount = flt(amt) + flt(frappe.db.get_value("Fees", fees, "paid_amount"))
137 total_amount = flt(frappe.db.get_value("Fees", fees, "total_amount"))
138 frappe.db.set_value("Fees", fees, "paid_amount", paid_amount)
139 frappe.db.set_value("Fees", fees, "outstanding_amount", (total_amount - paid_amount))
140 return paid_amount
141
142@frappe.whitelist()
143def get_course_schedule_events(start, end, filters=None):
144 """Returns events for Course Schedule Calendar view rendering.
145
146 :param start: Start date-time.
147 :param end: End date-time.
148 :param filters: Filters (JSON).
149 """
150 from frappe.desk.calendar import get_event_conditions
151 conditions = get_event_conditions("Course Schedule", filters)
152
153 data = frappe.db.sql("""select name, course,
154 timestamp(schedule_date, from_time) as from_datetime,
155 timestamp(schedule_date, to_time) as to_datetime,
156 room, student_group, 0 as 'allDay'
157 from `tabCourse Schedule`
158 where ( schedule_date between %(start)s and %(end)s )
159 {conditions}""".format(conditions=conditions), {
160 "start": start,
161 "end": end
162 }, as_dict=True, update={"allDay": 0})
163
164 return data