blob: b8cdc05dd3ec80c61eda0e7471e87341bbebb40a [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 Garg371413a2023-07-29 22:39:07 +05309from frappe.utils import add_days, getdate
Deepesh Garg8ef257a2023-06-14 12:54:10 +053010
Deepesh Garg77a29572023-06-16 13:43:55 +053011import erpnext
Deepesh Garg371413a2023-07-29 22:39:07 +053012from erpnext.accounts.utils import get_fiscal_year
Deepesh Garg85e1c852023-06-19 14:12:23 +053013from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
Deepesh Garg77a29572023-06-16 13:43:55 +053014
Deepesh Garg8ef257a2023-06-14 12:54:10 +053015
16@frappe.whitelist()
17def setup_demo_data():
Deepesh Garg77a29572023-06-16 13:43:55 +053018 company = create_demo_company()
19 process_masters()
20 make_transactions(company)
21
22
23@frappe.whitelist()
24def clear_demo_data():
Deepesh Garg86744b62023-06-19 09:44:57 +053025 company = frappe.db.get_single_value("Global Defaults", "demo_company")
Deepesh Garg77a29572023-06-16 13:43:55 +053026 create_transaction_deletion_record(company)
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053027 clear_masters()
28 delete_company(company)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053029
30
31def create_demo_company():
Deepesh Gargbb5387f2023-07-07 10:49:56 +053032 company = frappe.db.get_all("Company")[0].name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053033 company_doc = frappe.get_doc("Company", company)
34
35 # Make a dummy company
36 new_company = frappe.new_doc("Company")
37 new_company.company_name = company_doc.company_name + " (Demo)"
38 new_company.abbr = company_doc.abbr + "D"
39 new_company.enable_perpetual_inventory = 1
40 new_company.default_currency = company_doc.default_currency
41 new_company.country = company_doc.country
42 new_company.chart_of_accounts_based_on = "Standard Template"
43 new_company.chart_of_accounts = company_doc.chart_of_accounts
44 new_company.insert()
45
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053046 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053047 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
48 frappe.db.set_default("company", new_company.name)
49
Deepesh Garg85e1c852023-06-19 14:12:23 +053050 bank_account = create_bank_account({"company_name": new_company.name})
51 frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name)
52
Deepesh Garg77a29572023-06-16 13:43:55 +053053 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053054
Deepesh Garg77a29572023-06-16 13:43:55 +053055
56def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053057 for doctype in frappe.get_hooks("demo_master_doctypes"):
58 data = read_data_file_using_hooks(doctype)
59 if data:
60 for item in json.loads(data):
61 create_demo_record(item)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053062
63
64def create_demo_record(doctype):
65 frappe.get_doc(doctype).insert(ignore_permissions=True)
66
67
Deepesh Garg77a29572023-06-16 13:43:55 +053068def make_transactions(company):
Deepesh Garg371413a2023-07-29 22:39:07 +053069 start_date = get_fiscal_year(date=getdate())[1]
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