blob: 46500da15c76a77d4597001fe3ae89c4687c54e3 [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
Deepesh Garg86744b62023-06-19 09:44:57 +05309from frappe.utils import add_days
Deepesh Garg8ef257a2023-06-14 12:54:10 +053010
Deepesh Garg77a29572023-06-16 13:43:55 +053011import erpnext
Deepesh Garg85e1c852023-06-19 14:12:23 +053012from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
Deepesh Garg77a29572023-06-16 13:43:55 +053013
Deepesh Garg8ef257a2023-06-14 12:54:10 +053014
15@frappe.whitelist()
16def setup_demo_data():
Deepesh Garg77a29572023-06-16 13:43:55 +053017 company = create_demo_company()
18 process_masters()
19 make_transactions(company)
20
21
22@frappe.whitelist()
23def clear_demo_data():
Deepesh Garg86744b62023-06-19 09:44:57 +053024 company = frappe.db.get_single_value("Global Defaults", "demo_company")
Deepesh Garg77a29572023-06-16 13:43:55 +053025 create_transaction_deletion_record(company)
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053026 clear_masters()
27 delete_company(company)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053028
29
30def create_demo_company():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053031 company = erpnext.get_default_company()
Deepesh Garg8ef257a2023-06-14 12:54:10 +053032 company_doc = frappe.get_doc("Company", company)
33
34 # Make a dummy company
35 new_company = frappe.new_doc("Company")
36 new_company.company_name = company_doc.company_name + " (Demo)"
37 new_company.abbr = company_doc.abbr + "D"
38 new_company.enable_perpetual_inventory = 1
39 new_company.default_currency = company_doc.default_currency
40 new_company.country = company_doc.country
41 new_company.chart_of_accounts_based_on = "Standard Template"
42 new_company.chart_of_accounts = company_doc.chart_of_accounts
43 new_company.insert()
44
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053045 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053046 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
47 frappe.db.set_default("company", new_company.name)
48
Deepesh Garg85e1c852023-06-19 14:12:23 +053049 bank_account = create_bank_account({"company_name": new_company.name})
50 frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name)
51
Deepesh Garg77a29572023-06-16 13:43:55 +053052 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053053
Deepesh Garg77a29572023-06-16 13:43:55 +053054
55def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053056 for doctype in frappe.get_hooks("demo_master_doctypes"):
57 data = read_data_file_using_hooks(doctype)
58 if data:
59 for item in json.loads(data):
60 create_demo_record(item)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053061
62
63def create_demo_record(doctype):
64 frappe.get_doc(doctype).insert(ignore_permissions=True)
65
66
Deepesh Garg77a29572023-06-16 13:43:55 +053067def make_transactions(company):
Deepesh Garg86744b62023-06-19 09:44:57 +053068 fiscal_year = frappe.db.get_single_value("Global Defaults", "current_fiscal_year")
69 start_date = frappe.db.get_value("Fiscal Year", fiscal_year, "year_start_date")
Deepesh Garg85e1c852023-06-19 14:12:23 +053070 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Deepesh Garg86744b62023-06-19 09:44:57 +053071
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053072 for doctype in frappe.get_hooks("demo_transaction_doctypes"):
73 data = read_data_file_using_hooks(doctype)
74 if data:
75 for item in json.loads(data):
Deepesh Garg86744b62023-06-19 09:44:57 +053076 create_transaction(item, company, start_date)
Deepesh Garg77a29572023-06-16 13:43:55 +053077
78
Deepesh Garg86744b62023-06-19 09:44:57 +053079def create_transaction(doctype, company, start_date):
Deepesh Garg85e1c852023-06-19 14:12:23 +053080 warehouse = get_warehouse(company)
81 posting_date = (
82 start_date if doctype.get("doctype") == "Purchase Invoice" else get_random_date(start_date)
83 )
84 bank_account = frappe.db.get_value("Company", company, "default_bank_account")
85 bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from"
86
Deepesh Garg86744b62023-06-19 09:44:57 +053087 doctype.update(
88 {
89 "company": company,
90 "set_posting_time": 1,
Deepesh Garg85e1c852023-06-19 14:12:23 +053091 "posting_date": posting_date,
92 "set_warehouse": warehouse,
93 bank_field: bank_account,
94 "reference_date": posting_date,
Deepesh Garg86744b62023-06-19 09:44:57 +053095 }
96 )
Deepesh Garg77a29572023-06-16 13:43:55 +053097
98 income_account, expense_account = frappe.db.get_value(
99 "Company", company, ["default_income_account", "default_expense_account"]
100 )
101
Deepesh Garg85e1c852023-06-19 14:12:23 +0530102 for item in doctype.get("items") or []:
Deepesh Garg77a29572023-06-16 13:43:55 +0530103 item.update(
104 {
105 "cost_center": erpnext.get_default_cost_center(company),
106 "income_account": income_account,
107 "expense_account": expense_account,
108 }
109 )
110
111 doc = frappe.get_doc(doctype)
112 doc.save(ignore_permissions=True)
113 doc.submit()
114
115
Deepesh Garg86744b62023-06-19 09:44:57 +0530116def get_random_date(start_date):
117 return add_days(start_date, randint(1, 365))
118
119
Deepesh Garg77a29572023-06-16 13:43:55 +0530120def create_transaction_deletion_record(company):
121 transaction_deletion_record = frappe.new_doc("Transaction Deletion Record")
122 transaction_deletion_record.company = company
123 transaction_deletion_record.save(ignore_permissions=True)
124 transaction_deletion_record.submit()
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530125
126
127def clear_masters():
128 for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]:
129 data = read_data_file_using_hooks(doctype)
130 if data:
131 for item in json.loads(data):
132 clear_demo_record(item)
133
134
135def clear_demo_record(doctype):
136 doc_type = doctype.get("doctype")
137 del doctype["doctype"]
138 doc = frappe.get_doc(doc_type, doctype)
139 frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
140
141
142def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530143 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530144 frappe.delete_doc("Company", company, ignore_permissions=True)
145
146
147def read_data_file_using_hooks(doctype):
148 path = os.path.join(os.path.dirname(__file__), "demo_data")
149 with open(os.path.join(path, doctype + ".json"), "r") as f:
150 data = f.read()
151
152 return data
Deepesh Garg85e1c852023-06-19 14:12:23 +0530153
154
155def get_warehouse(company):
156 abbr = frappe.db.get_value("Company", company, "abbr")
157 warehouse = "Stores - {0}".format(abbr)
158
159 return warehouse