Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +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, erpnext |
| 5 | |
| 6 | import json |
| 7 | from frappe import _ |
| 8 | from frappe.utils import flt |
| 9 | from erpnext.setup.doctype.setup_progress.setup_progress import update_domain_actions, get_domain_actions_state |
| 10 | |
| 11 | @frappe.whitelist() |
Prateeksha Singh | 242218d | 2017-09-13 18:41:01 +0530 | [diff] [blame] | 12 | def set_sales_target(args_data): |
| 13 | args = json.loads(args_data) |
| 14 | defaults = frappe.defaults.get_defaults() |
| 15 | frappe.db.set_value("Company", defaults.get("company"), "monthly_sales_target", args.get('monthly_sales_target')) |
| 16 | |
| 17 | @frappe.whitelist() |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 18 | def create_customers(args_data): |
| 19 | args = json.loads(args_data) |
| 20 | defaults = frappe.defaults.get_defaults() |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 21 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 22 | customer = args.get("customer_" + str(i)) |
| 23 | if customer: |
| 24 | try: |
| 25 | doc = frappe.get_doc({ |
| 26 | "doctype":"Customer", |
| 27 | "customer_name": customer, |
| 28 | "customer_type": "Company", |
| 29 | "customer_group": _("Commercial"), |
| 30 | "territory": defaults.get("country"), |
| 31 | "company": defaults.get("company") |
| 32 | }).insert() |
| 33 | |
| 34 | if args.get("customer_contact_" + str(i)): |
| 35 | create_contact(args.get("customer_contact_" + str(i)), |
| 36 | "Customer", doc.name) |
| 37 | except frappe.NameError: |
| 38 | pass |
| 39 | |
| 40 | @frappe.whitelist() |
Shreya Shah | 8d9edbf | 2017-11-28 16:09:58 +0530 | [diff] [blame] | 41 | def create_letterhead(args_data): |
| 42 | args = json.loads(args_data) |
Prateeksha Singh | 4c58299 | 2018-01-25 17:29:57 +0530 | [diff] [blame] | 43 | letterhead = args.get("letterhead") |
Shreya Shah | 8d9edbf | 2017-11-28 16:09:58 +0530 | [diff] [blame] | 44 | if letterhead: |
| 45 | try: |
| 46 | frappe.get_doc({ |
| 47 | "doctype":"Letter Head", |
Prateeksha Singh | 4c58299 | 2018-01-25 17:29:57 +0530 | [diff] [blame] | 48 | "content":"""<div><img src="{0}" style='max-width: 100%%;'><br></div>""".format(letterhead.encode('utf-8')), |
Shreya Shah | 8d9edbf | 2017-11-28 16:09:58 +0530 | [diff] [blame] | 49 | "letter_head_name": _("Standard"), |
| 50 | "is_default": 1 |
| 51 | }).insert() |
| 52 | except frappe.NameError: |
| 53 | pass |
| 54 | |
| 55 | @frappe.whitelist() |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 56 | def create_suppliers(args_data): |
| 57 | args = json.loads(args_data) |
| 58 | defaults = frappe.defaults.get_defaults() |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 59 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 60 | supplier = args.get("supplier_" + str(i)) |
| 61 | if supplier: |
| 62 | try: |
| 63 | doc = frappe.get_doc({ |
| 64 | "doctype":"Supplier", |
| 65 | "supplier_name": supplier, |
| 66 | "supplier_type": _("Local"), |
| 67 | "company": defaults.get("company") |
| 68 | }).insert() |
| 69 | |
| 70 | if args.get("supplier_contact_" + str(i)): |
| 71 | create_contact(args.get("supplier_contact_" + str(i)), |
| 72 | "Supplier", doc.name) |
| 73 | except frappe.NameError: |
| 74 | pass |
| 75 | |
| 76 | def create_contact(contact, party_type, party): |
| 77 | """Create contact based on given contact name""" |
| 78 | contact = contact .split(" ") |
| 79 | |
| 80 | contact = frappe.get_doc({ |
| 81 | "doctype":"Contact", |
| 82 | "first_name":contact[0], |
| 83 | "last_name": len(contact) > 1 and contact[1] or "" |
| 84 | }) |
| 85 | contact.append('links', dict(link_doctype=party_type, link_name=party)) |
| 86 | contact.insert() |
| 87 | |
| 88 | @frappe.whitelist() |
| 89 | def create_items(args_data): |
| 90 | args = json.loads(args_data) |
| 91 | defaults = frappe.defaults.get_defaults() |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 92 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 93 | item = args.get("item_" + str(i)) |
| 94 | if item: |
| 95 | default_warehouse = "" |
| 96 | default_warehouse = frappe.db.get_value("Warehouse", filters={ |
| 97 | "warehouse_name": _("Finished Goods"), |
| 98 | "company": defaults.get("company_name") |
| 99 | }) |
| 100 | |
| 101 | try: |
| 102 | frappe.get_doc({ |
| 103 | "doctype":"Item", |
| 104 | "item_code": item, |
| 105 | "item_name": item, |
| 106 | "description": item, |
| 107 | "show_in_website": 1, |
| 108 | "is_sales_item": 1, |
| 109 | "is_purchase_item": 1, |
| 110 | "is_stock_item": 1, |
Zarrar | 92109b0 | 2018-04-06 10:27:55 +0530 | [diff] [blame] | 111 | "item_group": _("Products"), |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 112 | "stock_uom": _(args.get("item_uom_" + str(i))), |
| 113 | "default_warehouse": default_warehouse |
| 114 | }).insert() |
| 115 | |
Prateeksha Singh | 3942cd3 | 2017-12-19 12:48:27 +0530 | [diff] [blame] | 116 | except frappe.NameError: |
| 117 | pass |
| 118 | else: |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 119 | if args.get("item_price_" + str(i)): |
| 120 | item_price = flt(args.get("item_price_" + str(i))) |
| 121 | |
| 122 | price_list_name = frappe.db.get_value("Price List", {"selling": 1}) |
| 123 | make_item_price(item, price_list_name, item_price) |
| 124 | price_list_name = frappe.db.get_value("Price List", {"buying": 1}) |
| 125 | make_item_price(item, price_list_name, item_price) |
| 126 | |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 127 | |
| 128 | def make_item_price(item, price_list_name, item_price): |
| 129 | frappe.get_doc({ |
| 130 | "doctype": "Item Price", |
| 131 | "price_list": price_list_name, |
| 132 | "item_code": item, |
| 133 | "price_list_rate": item_price |
| 134 | }).insert() |
| 135 | |
Manas Solanki | 966f141 | 2017-11-23 15:22:10 +0530 | [diff] [blame] | 136 | # Education |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 137 | @frappe.whitelist() |
| 138 | def create_program(args_data): |
| 139 | args = json.loads(args_data) |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 140 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 141 | if args.get("program_" + str(i)): |
| 142 | program = frappe.new_doc("Program") |
| 143 | program.program_code = args.get("program_" + str(i)) |
| 144 | program.program_name = args.get("program_" + str(i)) |
| 145 | try: |
| 146 | program.save() |
| 147 | except frappe.DuplicateEntryError: |
| 148 | pass |
| 149 | |
| 150 | @frappe.whitelist() |
| 151 | def create_course(args_data): |
| 152 | args = json.loads(args_data) |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 153 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 154 | if args.get("course_" + str(i)): |
| 155 | course = frappe.new_doc("Course") |
| 156 | course.course_code = args.get("course_" + str(i)) |
| 157 | course.course_name = args.get("course_" + str(i)) |
| 158 | try: |
| 159 | course.save() |
| 160 | except frappe.DuplicateEntryError: |
| 161 | pass |
| 162 | |
| 163 | @frappe.whitelist() |
| 164 | def create_instructor(args_data): |
| 165 | args = json.loads(args_data) |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 166 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 167 | if args.get("instructor_" + str(i)): |
| 168 | instructor = frappe.new_doc("Instructor") |
| 169 | instructor.instructor_name = args.get("instructor_" + str(i)) |
| 170 | try: |
| 171 | instructor.save() |
| 172 | except frappe.DuplicateEntryError: |
| 173 | pass |
| 174 | |
| 175 | @frappe.whitelist() |
| 176 | def create_room(args_data): |
| 177 | args = json.loads(args_data) |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 178 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 179 | if args.get("room_" + str(i)): |
| 180 | room = frappe.new_doc("Room") |
| 181 | room.room_name = args.get("room_" + str(i)) |
| 182 | room.seating_capacity = args.get("room_capacity_" + str(i)) |
| 183 | try: |
| 184 | room.save() |
| 185 | except frappe.DuplicateEntryError: |
| 186 | pass |
| 187 | |
| 188 | @frappe.whitelist() |
| 189 | def create_users(args_data): |
| 190 | if frappe.session.user == 'Administrator': |
| 191 | return |
| 192 | args = json.loads(args_data) |
| 193 | defaults = frappe.defaults.get_defaults() |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 194 | for i in range(1,4): |
Prateeksha Singh | 95d8fd3 | 2017-09-04 11:14:04 +0530 | [diff] [blame] | 195 | email = args.get("user_email_" + str(i)) |
| 196 | fullname = args.get("user_fullname_" + str(i)) |
| 197 | if email: |
| 198 | if not fullname: |
| 199 | fullname = email.split("@")[0] |
| 200 | |
| 201 | parts = fullname.split(" ", 1) |
| 202 | |
| 203 | user = frappe.get_doc({ |
| 204 | "doctype": "User", |
| 205 | "email": email, |
| 206 | "first_name": parts[0], |
| 207 | "last_name": parts[1] if len(parts) > 1 else "", |
| 208 | "enabled": 1, |
| 209 | "user_type": "System User" |
| 210 | }) |
| 211 | |
| 212 | # default roles |
| 213 | user.append_roles("Projects User", "Stock User", "Support Team") |
| 214 | user.flags.delay_emails = True |
| 215 | |
| 216 | if not frappe.db.get_value("User", email): |
| 217 | user.insert(ignore_permissions=True) |
| 218 | |
| 219 | # create employee |
| 220 | emp = frappe.get_doc({ |
| 221 | "doctype": "Employee", |
| 222 | "employee_name": fullname, |
| 223 | "user_id": email, |
| 224 | "status": "Active", |
| 225 | "company": defaults.get("company") |
| 226 | }) |
| 227 | emp.flags.ignore_mandatory = True |
| 228 | emp.insert(ignore_permissions = True) |
| 229 | |
| 230 | # Ennumerate the setup hooks you're going to need, apart from the slides |
| 231 | |
| 232 | @frappe.whitelist() |
| 233 | def update_default_domain_actions_and_get_state(): |
| 234 | domain = frappe.db.get_value('Company', erpnext.get_default_company(), 'domain') |
| 235 | update_domain_actions(domain) |
| 236 | return get_domain_actions_state(domain) |