Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2015, Frappe Technologies and contributors |
| 3 | # For license information, please see license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | import frappe |
| 7 | import json |
| 8 | from frappe import _ |
| 9 | from frappe.model.mapper import get_mapped_doc |
| 10 | from frappe.utils import flt |
| 11 | |
| 12 | @frappe.whitelist() |
| 13 | def 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 | } |
| 24 | }}) |
| 25 | student.save() |
| 26 | |
| 27 | program_enrollment = frappe.new_doc("Program Enrollment") |
| 28 | program_enrollment.student = student.name |
| 29 | program_enrollment.student_name = student.title |
| 30 | program_enrollment.program = frappe.db.get_value("Student Applicant", source_name, "program") |
| 31 | return program_enrollment |
| 32 | |
| 33 | @frappe.whitelist() |
| 34 | def check_attendance_records_exist(course_schedule): |
| 35 | """Check if Attendance Records are made against the specified Course Schedule. |
| 36 | |
| 37 | :param course_schedule: Course Schedule. |
| 38 | """ |
| 39 | return frappe.get_list("Student Attendance", filters={"course_schedule": course_schedule}) |
| 40 | |
| 41 | @frappe.whitelist() |
| 42 | def mark_attendance(students_present, students_absent, course_schedule): |
| 43 | """Creates Multiple Attendance Records. |
| 44 | |
| 45 | :param students_present: Students Present JSON. |
| 46 | :param students_absent: Students Absent JSON. |
| 47 | :param course_schedule: Course Schedule. |
| 48 | """ |
| 49 | present = json.loads(students_present) |
| 50 | absent = json.loads(students_absent) |
| 51 | |
| 52 | for d in present: |
| 53 | make_attendance_records(d["student"], d["student_name"], course_schedule, "Present") |
| 54 | |
| 55 | for d in absent: |
| 56 | make_attendance_records(d["student"], d["student_name"], course_schedule, "Absent") |
| 57 | |
| 58 | frappe.msgprint(_("Attendance has been marked successfully.")) |
| 59 | |
| 60 | def make_attendance_records(student, student_name, course_schedule, status): |
| 61 | """Creates Attendance Record. |
| 62 | |
| 63 | :param student: Student. |
| 64 | :param student_name: Student Name. |
| 65 | :param course_schedule: Course Schedule. |
| 66 | :param status: Status (Present/Absent) |
| 67 | """ |
| 68 | student_attendance = frappe.new_doc("Student Attendance") |
| 69 | student_attendance.student = student |
| 70 | student_attendance.student_name = student_name |
| 71 | student_attendance.course_schedule = course_schedule |
| 72 | student_attendance.status = status |
| 73 | student_attendance.submit() |
| 74 | frappe.db.commit() |
| 75 | |
| 76 | @frappe.whitelist() |
| 77 | def get_student_group_students(student_group): |
| 78 | """Returns List of student, student_name in Student Group. |
| 79 | |
| 80 | :param student_group: Student Group. |
| 81 | """ |
| 82 | students = frappe.get_list("Student Group Student", fields=["student", "student_name"] , filters={"parent": student_group}, order_by= "idx") |
| 83 | return students |
| 84 | |
| 85 | @frappe.whitelist() |
| 86 | def get_fee_structure(program, academic_term=None): |
| 87 | """Returns Fee Structure. |
| 88 | |
| 89 | :param program: Program. |
| 90 | :param academic_term: Academic Term. |
| 91 | """ |
| 92 | fee_structure = frappe.db.get_values("Fee Structure", {"program": program, |
| 93 | "academic_term": academic_term}, 'name', as_dict=True) |
| 94 | return fee_structure[0].name if fee_structure else None |
| 95 | |
| 96 | @frappe.whitelist() |
| 97 | def get_fee_amount(fee_structure): |
| 98 | """Returns Fee Amount. |
| 99 | |
| 100 | :param fee_structure: Fee Structure. |
| 101 | """ |
| 102 | if fee_structure: |
| 103 | fs = frappe.get_list("Fee Amount", fields=["fees_category", "amount"] , filters={"parent": fee_structure}, order_by= "idx") |
| 104 | return fs |
| 105 | |
| 106 | @frappe.whitelist() |
| 107 | def get_fee_schedule(program): |
| 108 | """Returns Fee Schedule. |
| 109 | |
| 110 | :param program: Program. |
| 111 | """ |
| 112 | fs = frappe.get_list("Program Fee", fields=["academic_term", "fee_structure", "due_date", "amount"] , |
| 113 | filters={"parent": program}, order_by= "idx") |
| 114 | return fs |
| 115 | |
| 116 | @frappe.whitelist() |
| 117 | def collect_fees(fees, amt): |
| 118 | paid_amount = flt(amt) + flt(frappe.db.get_value("Fees", fees, "paid_amount")) |
| 119 | total_amount = flt(frappe.db.get_value("Fees", fees, "total_amount")) |
| 120 | frappe.db.set_value("Fees", fees, "paid_amount", paid_amount) |
| 121 | frappe.db.set_value("Fees", fees, "outstanding_amount", (total_amount - paid_amount)) |
| 122 | return paid_amount |
| 123 | |
| 124 | @frappe.whitelist() |
| 125 | def get_course_schedule_events(start, end, filters=None): |
| 126 | """Returns events for Course Schedule Calendar view rendering. |
| 127 | |
| 128 | :param start: Start date-time. |
| 129 | :param end: End date-time. |
| 130 | :param filters: Filters (JSON). |
| 131 | """ |
| 132 | from frappe.desk.calendar import get_event_conditions |
| 133 | conditions = get_event_conditions("Course Schedule", filters) |
| 134 | |
| 135 | data = frappe.db.sql("""select name, course, |
| 136 | timestamp(schedule_date, from_time) as from_datetime, |
| 137 | timestamp(schedule_date, to_time) as to_datetime, |
| 138 | room, student_group, 0 as 'allDay' |
| 139 | from `tabCourse Schedule` |
| 140 | where ( schedule_date between %(start)s and %(end)s ) |
| 141 | {conditions}""".format(conditions=conditions), { |
| 142 | "start": start, |
| 143 | "end": end |
| 144 | }, as_dict=True, update={"allDay": 0}) |
| 145 | |
| 146 | return data |