Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 1 | |
| 2 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 3 | # License: GNU General Public License v3. See license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 7 | import random |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | from datetime import timedelta |
| 9 | |
| 10 | import frappe |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 11 | from frappe.utils import cstr |
| 12 | from frappe.utils.make_random import get_random |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 13 | |
| 14 | from erpnext.education.api import ( |
| 15 | collect_fees, |
| 16 | enroll_student, |
| 17 | get_course, |
| 18 | get_fee_schedule, |
| 19 | get_student_group_students, |
| 20 | make_attendance_records, |
| 21 | ) |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 22 | |
| 23 | |
| 24 | def work(): |
| 25 | frappe.set_user(frappe.db.get_global('demo_education_user')) |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 26 | for d in range(20): |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 27 | approve_random_student_applicant() |
| 28 | enroll_random_student(frappe.flags.current_date) |
| 29 | # if frappe.flags.current_date.weekday()== 0: |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 30 | # make_course_schedule(frappe.flags.current_date, frappe.utils.add_days(frappe.flags.current_date, 5)) |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 31 | mark_student_attendance(frappe.flags.current_date) |
| 32 | # make_assessment_plan() |
| 33 | make_fees() |
| 34 | |
| 35 | def approve_random_student_applicant(): |
| 36 | random_student = get_random("Student Applicant", {"application_status": "Applied"}) |
| 37 | if random_student: |
| 38 | status = ["Approved", "Rejected"] |
| 39 | frappe.db.set_value("Student Applicant", random_student, "application_status", status[weighted_choice([9,3])]) |
| 40 | |
| 41 | def enroll_random_student(current_date): |
| 42 | batch = ["Section-A", "Section-B"] |
| 43 | random_student = get_random("Student Applicant", {"application_status": "Approved"}) |
| 44 | if random_student: |
| 45 | enrollment = enroll_student(random_student) |
| 46 | enrollment.academic_year = get_random("Academic Year") |
| 47 | enrollment.enrollment_date = current_date |
| 48 | enrollment.student_batch_name = batch[weighted_choice([9,3])] |
| 49 | fee_schedule = get_fee_schedule(enrollment.program) |
| 50 | for fee in fee_schedule: |
| 51 | enrollment.append("fees", fee) |
| 52 | enrolled_courses = get_course(enrollment.program) |
| 53 | for course in enrolled_courses: |
| 54 | enrollment.append("courses", course) |
| 55 | enrollment.submit() |
| 56 | frappe.db.commit() |
| 57 | assign_student_group(enrollment.student, enrollment.student_name, enrollment.program, |
| 58 | enrolled_courses, enrollment.student_batch_name) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 59 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 60 | def assign_student_group(student, student_name, program, courses, batch): |
| 61 | course_list = [d["course"] for d in courses] |
Shreya Shah | 1f4c263 | 2018-07-27 10:59:14 +0530 | [diff] [blame] | 62 | for d in frappe.get_list("Student Group", fields=("name"), filters={"program": program, "course":("in", course_list), "disabled": 0}): |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 63 | student_group = frappe.get_doc("Student Group", d.name) |
| 64 | student_group.append("students", {"student": student, "student_name": student_name, |
| 65 | "group_roll_number":len(student_group.students)+1, "active":1}) |
| 66 | student_group.save() |
Shreya Shah | 1f4c263 | 2018-07-27 10:59:14 +0530 | [diff] [blame] | 67 | student_batch = frappe.get_list("Student Group", fields=("name"), filters={"program": program, "group_based_on":"Batch", "batch":batch, "disabled": 0})[0] |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 68 | student_batch_doc = frappe.get_doc("Student Group", student_batch.name) |
| 69 | student_batch_doc.append("students", {"student": student, "student_name": student_name, |
| 70 | "group_roll_number":len(student_batch_doc.students)+1, "active":1}) |
| 71 | student_batch_doc.save() |
| 72 | frappe.db.commit() |
| 73 | |
| 74 | def mark_student_attendance(current_date): |
| 75 | status = ["Present", "Absent"] |
Shreya Shah | 1f4c263 | 2018-07-27 10:59:14 +0530 | [diff] [blame] | 76 | for d in frappe.db.get_list("Student Group", filters={"group_based_on": "Batch", "disabled": 0}): |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 77 | students = get_student_group_students(d.name) |
| 78 | for stud in students: |
| 79 | make_attendance_records(stud.student, stud.student_name, status[weighted_choice([9,4])], None, d.name, current_date) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 80 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 81 | def make_fees(): |
| 82 | for d in range(1,10): |
| 83 | random_fee = get_random("Fees", {"paid_amount": 0}) |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 84 | collect_fees(random_fee, frappe.db.get_value("Fees", random_fee, "outstanding_amount")) |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 85 | |
| 86 | def make_assessment_plan(date): |
| 87 | for d in range(1,4): |
Shreya Shah | 1f4c263 | 2018-07-27 10:59:14 +0530 | [diff] [blame] | 88 | random_group = get_random("Student Group", {"group_based_on": "Course", "disabled": 0}, True) |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 89 | doc = frappe.new_doc("Assessment Plan") |
| 90 | doc.student_group = random_group.name |
| 91 | doc.course = random_group.course |
| 92 | doc.assessment_group = get_random("Assessment Group", {"is_group": 0, "parent": "2017-18 (Semester 2)"}) |
| 93 | doc.grading_scale = get_random("Grading Scale") |
| 94 | doc.maximum_assessment_score = 100 |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 95 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 96 | def make_course_schedule(start_date, end_date): |
| 97 | for d in frappe.db.get_list("Student Group"): |
| 98 | cs = frappe.new_doc("Scheduling Tool") |
| 99 | cs.student_group = d.name |
| 100 | cs.room = get_random("Room") |
| 101 | cs.instructor = get_random("Instructor") |
| 102 | cs.course_start_date = cstr(start_date) |
| 103 | cs.course_end_date = cstr(end_date) |
| 104 | day = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 105 | for x in range(3): |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 106 | random_day = random.choice(day) |
| 107 | cs.day = random_day |
| 108 | cs.from_time = timedelta(hours=(random.randrange(7, 17,1))) |
| 109 | cs.to_time = cs.from_time + timedelta(hours=1) |
| 110 | cs.schedule_course() |
| 111 | day.remove(random_day) |
| 112 | |
| 113 | |
| 114 | def weighted_choice(weights): |
| 115 | totals = [] |
| 116 | running_total = 0 |
| 117 | |
| 118 | for w in weights: |
| 119 | running_total += w |
| 120 | totals.append(running_total) |
| 121 | |
| 122 | rnd = random.random() * running_total |
| 123 | for i, total in enumerate(totals): |
| 124 | if rnd < total: |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 125 | return i |