Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | import frappe, json |
| 5 | from frappe.utils.make_random import get_random |
| 6 | from datetime import datetime |
| 7 | from erpnext.demo.setup.setup_data import import_json |
| 8 | import random |
| 9 | |
| 10 | def setup_data(): |
| 11 | frappe.flags.mute_emails = True |
| 12 | make_masters() |
| 13 | setup_item() |
| 14 | make_student_applicants() |
| 15 | make_student_group() |
| 16 | make_fees_category() |
| 17 | make_fees_structure() |
| 18 | frappe.db.commit() |
| 19 | frappe.clear_cache() |
| 20 | |
| 21 | def make_masters(): |
| 22 | import_json("Room") |
| 23 | import_json("Department") |
| 24 | import_json("Instructor") |
| 25 | import_json("Course") |
| 26 | import_json("Program") |
| 27 | frappe.db.commit() |
| 28 | |
| 29 | def setup_item(): |
| 30 | items = json.loads(open(frappe.get_app_path('erpnext', 'demo', 'data', 'item_schools.json')).read()) |
| 31 | for i in items: |
| 32 | item = frappe.new_doc('Item') |
| 33 | item.update(i) |
| 34 | item.min_order_qty = random.randint(10, 30) |
| 35 | item.default_warehouse = frappe.get_all('Warehouse', filters={'warehouse_name': item.default_warehouse}, limit=1)[0].name |
| 36 | item.insert() |
| 37 | |
| 38 | def make_student_applicants(): |
| 39 | blood_group = ["A+", "A-", "B+", "B-", "AB+", "AB-", "O+", "O-"] |
| 40 | male_names = [] |
| 41 | female_names = [] |
| 42 | |
| 43 | file_path = get_json_path("Random Student Data") |
| 44 | with open(file_path, "r") as open_file: |
| 45 | random_student_data = json.loads(open_file.read()) |
| 46 | count = 1 |
| 47 | |
| 48 | for d in random_student_data: |
| 49 | if d.get('gender') == "Male": |
| 50 | male_names.append(d.get('first_name').title()) |
| 51 | |
| 52 | if d.get('gender') == "Female": |
| 53 | female_names.append(d.get('first_name').title()) |
| 54 | |
| 55 | for idx, d in enumerate(random_student_data): |
| 56 | student_applicant = frappe.new_doc("Student Applicant") |
| 57 | student_applicant.first_name = d.get('first_name').title() |
| 58 | student_applicant.last_name = d.get('last_name').title() |
| 59 | student_applicant.image = d.get('image') |
| 60 | student_applicant.gender = d.get('gender') |
| 61 | student_applicant.program = get_random("Program") |
| 62 | student_applicant.blood_group = random.choice(blood_group) |
| 63 | year = random.randint(1990, 1998) |
| 64 | month = random.randint(1, 12) |
| 65 | day = random.randint(1, 28) |
| 66 | student_applicant.date_of_birth = datetime(year, month, day) |
| 67 | student_applicant.mother_name = random.choice(female_names) + " " + d.get('last_name').title() |
| 68 | student_applicant.father_name = random.choice(male_names) + " " + d.get('last_name').title() |
| 69 | if student_applicant.gender == "Male": |
| 70 | student_applicant.middle_name = random.choice(male_names) |
| 71 | else: |
| 72 | student_applicant.middle_name = random.choice(female_names) |
| 73 | student_applicant.student_email_id = d.get('first_name') + "_" + \ |
| 74 | student_applicant.middle_name + "_" + d.get('last_name') + "@example.com" |
| 75 | if count <5: |
| 76 | student_applicant.insert() |
| 77 | frappe.db.commit() |
| 78 | else: |
| 79 | student_applicant.submit() |
| 80 | frappe.db.commit() |
| 81 | count+=1 |
| 82 | |
| 83 | def make_student_group(): |
| 84 | for d in frappe.db.get_list("Academic Term"): |
| 85 | sg_tool = frappe.new_doc("Student Group Creation Tool") |
| 86 | sg_tool.academic_year = "2016-17" |
| 87 | sg_tool.academic_term = d.name |
| 88 | sg_tool.courses = sg_tool.get_courses() |
| 89 | sg_tool.create_student_groups() |
| 90 | frappe.db.commit() |
| 91 | |
| 92 | def make_fees_category(): |
| 93 | fee_type = ["Tuition Fee", "Hostel Fee", "Logistics Fee", |
| 94 | "Medical Fee", "Mess Fee", "Security Deposit"] |
| 95 | |
| 96 | fee_desc = {"Tuition Fee" : "Curricular activities which includes books, notebooks and faculty charges" , |
| 97 | "Hostel Fee" : "Stay of students in institute premises", |
| 98 | "Logistics Fee" : "Lodging boarding of the students" , |
| 99 | "Medical Fee" : "Medical welfare of the students", |
| 100 | "Mess Fee" : "Food and beverages for your ward", |
| 101 | "Security Deposit" : "In case your child is found to have damaged institutes property" |
| 102 | } |
| 103 | |
| 104 | for i in fee_type: |
| 105 | fee_category = frappe.new_doc("Fee Category") |
| 106 | fee_category.category_name = i |
| 107 | fee_category.description = fee_desc[i] |
| 108 | fee_category.insert() |
| 109 | frappe.db.commit() |
| 110 | |
| 111 | def make_fees_structure(): |
| 112 | for d in frappe.db.get_list("Program"): |
| 113 | program = frappe.get_doc("Program", d.name) |
| 114 | for academic_term in ["Semester 1", "Semester 2", "Semester 3"]: |
| 115 | fee_structure = frappe.new_doc("Fee Structure") |
| 116 | fee_structure.program = d.name |
| 117 | fee_structure.academic_term = random.choice(frappe.db.get_list("Academic Term")).name |
| 118 | for j in range(1,4): |
| 119 | temp = {"fees_category": random.choice(frappe.db.get_list("Fee Category")).name , "amount" : random.randint(500,1000)} |
| 120 | fee_structure.append("components", temp) |
| 121 | fee_structure.insert() |
| 122 | program.append("fees", {"academic_term": academic_term, "fee_structure": fee_structure.name, "amount": fee_structure.total_amount}) |
| 123 | program.save() |
| 124 | frappe.db.commit() |
| 125 | |
| 126 | def get_json_path(doctype): |
| 127 | return frappe.get_app_path('erpnext', 'demo', 'data', frappe.scrub(doctype) + '.json') |
| 128 | |
| 129 | def weighted_choice(weights): |
| 130 | totals = [] |
| 131 | running_total = 0 |
| 132 | |
| 133 | for w in weights: |
| 134 | running_total += w |
| 135 | totals.append(running_total) |
| 136 | |
| 137 | rnd = random.random() * running_total |
| 138 | for i, total in enumerate(totals): |
| 139 | if rnd < total: |
| 140 | return i |