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 |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 6 | from random import randint |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 9 | from frappe import _ |
Deepesh Garg | 371413a | 2023-07-29 22:39:07 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, getdate |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 11 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 12 | import erpnext |
Deepesh Garg | 371413a | 2023-07-29 22:39:07 +0530 | [diff] [blame] | 13 | from erpnext.accounts.utils import get_fiscal_year |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 14 | from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 15 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 16 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 17 | def setup_demo_data(): |
Ankush Menat | 3a21c90 | 2023-08-10 15:48:57 +0530 | [diff] [blame] | 18 | from frappe.utils.telemetry import capture |
| 19 | |
| 20 | capture("demo_data_creation_started", "erpnext") |
| 21 | try: |
| 22 | company = create_demo_company() |
| 23 | process_masters() |
| 24 | make_transactions(company) |
| 25 | frappe.cache.delete_keys("bootinfo") |
| 26 | frappe.publish_realtime("demo_data_complete") |
| 27 | except Exception: |
| 28 | frappe.log_error("Failed to create demo data") |
| 29 | capture("demo_data_creation_failed", "erpnext", properties={"exception": frappe.get_traceback()}) |
| 30 | raise |
| 31 | capture("demo_data_creation_completed", "erpnext") |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 32 | |
| 33 | |
| 34 | @frappe.whitelist() |
| 35 | def clear_demo_data(): |
Ankush Menat | 3a21c90 | 2023-08-10 15:48:57 +0530 | [diff] [blame] | 36 | frappe.only_for("System Manager") |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 37 | try: |
| 38 | company = frappe.db.get_single_value("Global Defaults", "demo_company") |
| 39 | create_transaction_deletion_record(company) |
| 40 | clear_masters() |
| 41 | delete_company(company) |
| 42 | default_company = frappe.db.get_single_value("Global Defaults", "default_company") |
| 43 | frappe.db.set_default("company", default_company) |
| 44 | except Exception: |
| 45 | frappe.db.rollback() |
| 46 | frappe.log_error("Failed to erase demo data") |
| 47 | frappe.throw( |
| 48 | _("Failed to erase demo data, please delete the demo company manually."), |
| 49 | title=_("Could Not Delete Demo Data"), |
| 50 | ) |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def create_demo_company(): |
Deepesh Garg | bb5387f | 2023-07-07 10:49:56 +0530 | [diff] [blame] | 54 | company = frappe.db.get_all("Company")[0].name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 55 | company_doc = frappe.get_doc("Company", company) |
| 56 | |
| 57 | # Make a dummy company |
| 58 | new_company = frappe.new_doc("Company") |
| 59 | new_company.company_name = company_doc.company_name + " (Demo)" |
| 60 | new_company.abbr = company_doc.abbr + "D" |
| 61 | new_company.enable_perpetual_inventory = 1 |
| 62 | new_company.default_currency = company_doc.default_currency |
| 63 | new_company.country = company_doc.country |
| 64 | new_company.chart_of_accounts_based_on = "Standard Template" |
| 65 | new_company.chart_of_accounts = company_doc.chart_of_accounts |
| 66 | new_company.insert() |
| 67 | |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 68 | # Set Demo Company as default to |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 69 | frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name) |
| 70 | frappe.db.set_default("company", new_company.name) |
| 71 | |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 72 | bank_account = create_bank_account({"company_name": new_company.name}) |
| 73 | frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name) |
| 74 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 75 | return new_company.name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 76 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 77 | |
| 78 | def process_masters(): |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 79 | for doctype in frappe.get_hooks("demo_master_doctypes"): |
| 80 | data = read_data_file_using_hooks(doctype) |
| 81 | if data: |
| 82 | for item in json.loads(data): |
| 83 | create_demo_record(item) |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 84 | |
| 85 | |
| 86 | def create_demo_record(doctype): |
| 87 | frappe.get_doc(doctype).insert(ignore_permissions=True) |
| 88 | |
| 89 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 90 | def make_transactions(company): |
Deepesh Garg | 371413a | 2023-07-29 22:39:07 +0530 | [diff] [blame] | 91 | start_date = get_fiscal_year(date=getdate())[1] |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 92 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1) |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 93 | |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 94 | for doctype in frappe.get_hooks("demo_transaction_doctypes"): |
| 95 | data = read_data_file_using_hooks(doctype) |
| 96 | if data: |
| 97 | for item in json.loads(data): |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 98 | create_transaction(item, company, start_date) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 99 | |
| 100 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 101 | def create_transaction(doctype, company, start_date): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 102 | warehouse = get_warehouse(company) |
| 103 | posting_date = ( |
| 104 | start_date if doctype.get("doctype") == "Purchase Invoice" else get_random_date(start_date) |
| 105 | ) |
Deepesh Garg | 333f2a5 | 2023-08-01 10:10:50 +0530 | [diff] [blame] | 106 | bank_account, default_receivable_account = frappe.db.get_value( |
| 107 | "Company", company, ["default_bank_account", "default_receivable_account"] |
| 108 | ) |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 109 | bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from" |
| 110 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 111 | doctype.update( |
| 112 | { |
| 113 | "company": company, |
| 114 | "set_posting_time": 1, |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 115 | "posting_date": posting_date, |
| 116 | "set_warehouse": warehouse, |
| 117 | bank_field: bank_account, |
| 118 | "reference_date": posting_date, |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 119 | } |
| 120 | ) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 121 | |
| 122 | income_account, expense_account = frappe.db.get_value( |
| 123 | "Company", company, ["default_income_account", "default_expense_account"] |
| 124 | ) |
| 125 | |
Deepesh Garg | 333f2a5 | 2023-08-01 10:10:50 +0530 | [diff] [blame] | 126 | if doctype in ("Purchase Invoice", "Sales Invoice"): |
| 127 | for item in doctype.get("items") or []: |
| 128 | item.update( |
| 129 | { |
| 130 | "cost_center": erpnext.get_default_cost_center(company), |
| 131 | "income_account": income_account, |
| 132 | "expense_account": expense_account, |
| 133 | } |
| 134 | ) |
| 135 | elif doctype == "Journal Entry": |
| 136 | pass |
| 137 | # update_accounts(doctype, bank_account, default_receivable_account) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 138 | |
| 139 | doc = frappe.get_doc(doctype) |
| 140 | doc.save(ignore_permissions=True) |
| 141 | doc.submit() |
| 142 | |
| 143 | |
Deepesh Garg | 333f2a5 | 2023-08-01 10:10:50 +0530 | [diff] [blame] | 144 | # def update_accounts(doctype, company, bank_account): |
| 145 | |
| 146 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 147 | def get_random_date(start_date): |
| 148 | return add_days(start_date, randint(1, 365)) |
| 149 | |
| 150 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 151 | def create_transaction_deletion_record(company): |
| 152 | transaction_deletion_record = frappe.new_doc("Transaction Deletion Record") |
| 153 | transaction_deletion_record.company = company |
| 154 | transaction_deletion_record.save(ignore_permissions=True) |
| 155 | transaction_deletion_record.submit() |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 156 | |
| 157 | |
| 158 | def clear_masters(): |
| 159 | for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]: |
| 160 | data = read_data_file_using_hooks(doctype) |
| 161 | if data: |
| 162 | for item in json.loads(data): |
| 163 | clear_demo_record(item) |
| 164 | |
| 165 | |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 166 | def clear_demo_record(document): |
| 167 | doc_type = document.get("doctype") |
| 168 | del document["doctype"] |
| 169 | doc = frappe.get_doc(doc_type, document) |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 170 | frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True) |
| 171 | |
| 172 | |
| 173 | def delete_company(company): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 174 | frappe.db.set_single_value("Global Defaults", "demo_company", "") |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 175 | frappe.delete_doc("Company", company, ignore_permissions=True) |
| 176 | |
| 177 | |
| 178 | def read_data_file_using_hooks(doctype): |
| 179 | path = os.path.join(os.path.dirname(__file__), "demo_data") |
| 180 | with open(os.path.join(path, doctype + ".json"), "r") as f: |
| 181 | data = f.read() |
| 182 | |
| 183 | return data |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def get_warehouse(company): |
| 187 | abbr = frappe.db.get_value("Company", company, "abbr") |
| 188 | warehouse = "Stores - {0}".format(abbr) |
| 189 | |
| 190 | return warehouse |