Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 1 | # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | import json |
| 5 | import os |
| 6 | |
| 7 | import frappe |
| 8 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame^] | 9 | import erpnext |
| 10 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 11 | |
| 12 | @frappe.whitelist() |
| 13 | def setup_demo_data(): |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame^] | 14 | company = create_demo_company() |
| 15 | process_masters() |
| 16 | make_transactions(company) |
| 17 | |
| 18 | |
| 19 | @frappe.whitelist() |
| 20 | def clear_demo_data(): |
| 21 | company = frappe.db.get_single_value("Global Defaults", "demo_company") |
| 22 | create_transaction_deletion_record(company) |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def create_demo_company(): |
| 26 | company = frappe.db.get_value("Company", {"docstatus": 0}) |
| 27 | company_doc = frappe.get_doc("Company", company) |
| 28 | |
| 29 | # Make a dummy company |
| 30 | new_company = frappe.new_doc("Company") |
| 31 | new_company.company_name = company_doc.company_name + " (Demo)" |
| 32 | new_company.abbr = company_doc.abbr + "D" |
| 33 | new_company.enable_perpetual_inventory = 1 |
| 34 | new_company.default_currency = company_doc.default_currency |
| 35 | new_company.country = company_doc.country |
| 36 | new_company.chart_of_accounts_based_on = "Standard Template" |
| 37 | new_company.chart_of_accounts = company_doc.chart_of_accounts |
| 38 | new_company.insert() |
| 39 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame^] | 40 | frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name) |
| 41 | return new_company.name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 42 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame^] | 43 | |
| 44 | def process_masters(): |
| 45 | demo_doctypes = frappe.get_hooks("demo_master_doctypes") or [] |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 46 | path = os.path.join(os.path.dirname(__file__), "demo_data") |
| 47 | for doctype in demo_doctypes: |
| 48 | with open(os.path.join(path, doctype + ".json"), "r") as f: |
| 49 | data = f.read() |
| 50 | if data: |
| 51 | for item in json.loads(data): |
| 52 | create_demo_record(item) |
| 53 | |
| 54 | |
| 55 | def create_demo_record(doctype): |
| 56 | frappe.get_doc(doctype).insert(ignore_permissions=True) |
| 57 | |
| 58 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame^] | 59 | def make_transactions(company): |
| 60 | transaction_doctypes = frappe.get_hooks("demo_transaction_doctypes") or [] |
| 61 | path = os.path.join(os.path.dirname(__file__), "demo_data") |
| 62 | for transaction in transaction_doctypes: |
| 63 | with open(os.path.join(path, transaction + ".json"), "r") as f: |
| 64 | data = f.read() |
| 65 | if data: |
| 66 | for item in json.loads(data): |
| 67 | create_transaction(item, company) |
| 68 | |
| 69 | |
| 70 | def create_transaction(doctype, company): |
| 71 | doctype.update({"company": company}) |
| 72 | |
| 73 | income_account, expense_account = frappe.db.get_value( |
| 74 | "Company", company, ["default_income_account", "default_expense_account"] |
| 75 | ) |
| 76 | |
| 77 | for item in doctype.get("items"): |
| 78 | item.update( |
| 79 | { |
| 80 | "cost_center": erpnext.get_default_cost_center(company), |
| 81 | "income_account": income_account, |
| 82 | "expense_account": expense_account, |
| 83 | } |
| 84 | ) |
| 85 | |
| 86 | doc = frappe.get_doc(doctype) |
| 87 | doc.save(ignore_permissions=True) |
| 88 | doc.submit() |
| 89 | |
| 90 | |
| 91 | def create_transaction_deletion_record(company): |
| 92 | transaction_deletion_record = frappe.new_doc("Transaction Deletion Record") |
| 93 | transaction_deletion_record.company = company |
| 94 | transaction_deletion_record.save(ignore_permissions=True) |
| 95 | transaction_deletion_record.submit() |