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