blob: bb64ba1db79c2f4e97665cd8909dcae6a746b631 [file] [log] [blame]
Deepesh Garg8ef257a2023-06-14 12:54:10 +05301# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4import json
5import os
Deepesh Garg86744b62023-06-19 09:44:57 +05306from random import randint
Deepesh Garg8ef257a2023-06-14 12:54:10 +05307
8import frappe
Ankush Menate4b863a2023-08-10 17:49:28 +05309from frappe import _
Deepesh Garg371413a2023-07-29 22:39:07 +053010from frappe.utils import add_days, getdate
Deepesh Garg8ef257a2023-06-14 12:54:10 +053011
Deepesh Garg77a29572023-06-16 13:43:55 +053012import erpnext
Deepesh Garg371413a2023-07-29 22:39:07 +053013from erpnext.accounts.utils import get_fiscal_year
Deepesh Garg85e1c852023-06-19 14:12:23 +053014from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
Deepesh Garg77a29572023-06-16 13:43:55 +053015
Deepesh Garg8ef257a2023-06-14 12:54:10 +053016
Deepesh Garg8ef257a2023-06-14 12:54:10 +053017def setup_demo_data():
Ankush Menat3a21c902023-08-10 15:48:57 +053018 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 Garg77a29572023-06-16 13:43:55 +053032
33
34@frappe.whitelist()
35def clear_demo_data():
Ankush Menat3a21c902023-08-10 15:48:57 +053036 frappe.only_for("System Manager")
Ankush Menate4b863a2023-08-10 17:49:28 +053037 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 Garg8ef257a2023-06-14 12:54:10 +053051
52
53def create_demo_company():
Deepesh Gargbb5387f2023-07-07 10:49:56 +053054 company = frappe.db.get_all("Company")[0].name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053055 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 Garg5b6a9fc2023-06-17 13:08:18 +053068 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053069 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
70 frappe.db.set_default("company", new_company.name)
71
Deepesh Garg85e1c852023-06-19 14:12:23 +053072 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 Garg77a29572023-06-16 13:43:55 +053075 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053076
Deepesh Garg77a29572023-06-16 13:43:55 +053077
78def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053079 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 Garg8ef257a2023-06-14 12:54:10 +053084
85
86def create_demo_record(doctype):
87 frappe.get_doc(doctype).insert(ignore_permissions=True)
88
89
Deepesh Garg77a29572023-06-16 13:43:55 +053090def make_transactions(company):
Deepesh Garg371413a2023-07-29 22:39:07 +053091 start_date = get_fiscal_year(date=getdate())[1]
Deepesh Garg85e1c852023-06-19 14:12:23 +053092 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Deepesh Garg86744b62023-06-19 09:44:57 +053093
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053094 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 Garg86744b62023-06-19 09:44:57 +053098 create_transaction(item, company, start_date)
Deepesh Garg77a29572023-06-16 13:43:55 +053099
100
Deepesh Garg86744b62023-06-19 09:44:57 +0530101def create_transaction(doctype, company, start_date):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530102 warehouse = get_warehouse(company)
103 posting_date = (
104 start_date if doctype.get("doctype") == "Purchase Invoice" else get_random_date(start_date)
105 )
Deepesh Garg333f2a52023-08-01 10:10:50 +0530106 bank_account, default_receivable_account = frappe.db.get_value(
107 "Company", company, ["default_bank_account", "default_receivable_account"]
108 )
Deepesh Garg85e1c852023-06-19 14:12:23 +0530109 bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from"
110
Deepesh Garg86744b62023-06-19 09:44:57 +0530111 doctype.update(
112 {
113 "company": company,
114 "set_posting_time": 1,
Deepesh Garg85e1c852023-06-19 14:12:23 +0530115 "posting_date": posting_date,
116 "set_warehouse": warehouse,
117 bank_field: bank_account,
118 "reference_date": posting_date,
Deepesh Garg86744b62023-06-19 09:44:57 +0530119 }
120 )
Deepesh Garg77a29572023-06-16 13:43:55 +0530121
122 income_account, expense_account = frappe.db.get_value(
123 "Company", company, ["default_income_account", "default_expense_account"]
124 )
125
Deepesh Garg333f2a52023-08-01 10:10:50 +0530126 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 Garg77a29572023-06-16 13:43:55 +0530138
139 doc = frappe.get_doc(doctype)
140 doc.save(ignore_permissions=True)
141 doc.submit()
142
143
Deepesh Garg333f2a52023-08-01 10:10:50 +0530144# def update_accounts(doctype, company, bank_account):
145
146
Deepesh Garg86744b62023-06-19 09:44:57 +0530147def get_random_date(start_date):
148 return add_days(start_date, randint(1, 365))
149
150
Deepesh Garg77a29572023-06-16 13:43:55 +0530151def 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 Garg5b6a9fc2023-06-17 13:08:18 +0530156
157
158def 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 Menate4b863a2023-08-10 17:49:28 +0530166def clear_demo_record(document):
167 doc_type = document.get("doctype")
168 del document["doctype"]
169 doc = frappe.get_doc(doc_type, document)
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530170 frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
171
172
173def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530174 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530175 frappe.delete_doc("Company", company, ignore_permissions=True)
176
177
178def 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 Garg85e1c852023-06-19 14:12:23 +0530184
185
186def get_warehouse(company):
187 abbr = frappe.db.get_value("Company", company, "abbr")
188 warehouse = "Stores - {0}".format(abbr)
189
190 return warehouse