blob: a6d90eda96df0d2b6cd9f17aa0d52f0ee123da13 [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 Garg7805abbb2023-07-30 20:29:20 +053029 default_company = frappe.db.get_single_value("Global Defaults", "default_company")
30 frappe.db.set_default("company", default_company)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053031
32
33def create_demo_company():
Deepesh Gargbb5387f2023-07-07 10:49:56 +053034 company = frappe.db.get_all("Company")[0].name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053035 company_doc = frappe.get_doc("Company", company)
36
37 # Make a dummy company
38 new_company = frappe.new_doc("Company")
39 new_company.company_name = company_doc.company_name + " (Demo)"
40 new_company.abbr = company_doc.abbr + "D"
41 new_company.enable_perpetual_inventory = 1
42 new_company.default_currency = company_doc.default_currency
43 new_company.country = company_doc.country
44 new_company.chart_of_accounts_based_on = "Standard Template"
45 new_company.chart_of_accounts = company_doc.chart_of_accounts
46 new_company.insert()
47
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053048 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053049 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
50 frappe.db.set_default("company", new_company.name)
51
Deepesh Garg85e1c852023-06-19 14:12:23 +053052 bank_account = create_bank_account({"company_name": new_company.name})
53 frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name)
54
Deepesh Garg77a29572023-06-16 13:43:55 +053055 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053056
Deepesh Garg77a29572023-06-16 13:43:55 +053057
58def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053059 for doctype in frappe.get_hooks("demo_master_doctypes"):
60 data = read_data_file_using_hooks(doctype)
61 if data:
62 for item in json.loads(data):
63 create_demo_record(item)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053064
65
66def create_demo_record(doctype):
67 frappe.get_doc(doctype).insert(ignore_permissions=True)
68
69
Deepesh Garg77a29572023-06-16 13:43:55 +053070def make_transactions(company):
Deepesh Garg371413a2023-07-29 22:39:07 +053071 start_date = get_fiscal_year(date=getdate())[1]
Deepesh Garg85e1c852023-06-19 14:12:23 +053072 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Deepesh Garg86744b62023-06-19 09:44:57 +053073
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053074 for doctype in frappe.get_hooks("demo_transaction_doctypes"):
75 data = read_data_file_using_hooks(doctype)
76 if data:
77 for item in json.loads(data):
Deepesh Garg86744b62023-06-19 09:44:57 +053078 create_transaction(item, company, start_date)
Deepesh Garg77a29572023-06-16 13:43:55 +053079
80
Deepesh Garg86744b62023-06-19 09:44:57 +053081def create_transaction(doctype, company, start_date):
Deepesh Garg85e1c852023-06-19 14:12:23 +053082 warehouse = get_warehouse(company)
83 posting_date = (
84 start_date if doctype.get("doctype") == "Purchase Invoice" else get_random_date(start_date)
85 )
86 bank_account = frappe.db.get_value("Company", company, "default_bank_account")
87 bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from"
88
Deepesh Garg86744b62023-06-19 09:44:57 +053089 doctype.update(
90 {
91 "company": company,
92 "set_posting_time": 1,
Deepesh Garg85e1c852023-06-19 14:12:23 +053093 "posting_date": posting_date,
94 "set_warehouse": warehouse,
95 bank_field: bank_account,
96 "reference_date": posting_date,
Deepesh Garg86744b62023-06-19 09:44:57 +053097 }
98 )
Deepesh Garg77a29572023-06-16 13:43:55 +053099
100 income_account, expense_account = frappe.db.get_value(
101 "Company", company, ["default_income_account", "default_expense_account"]
102 )
103
Deepesh Garg85e1c852023-06-19 14:12:23 +0530104 for item in doctype.get("items") or []:
Deepesh Garg77a29572023-06-16 13:43:55 +0530105 item.update(
106 {
107 "cost_center": erpnext.get_default_cost_center(company),
108 "income_account": income_account,
109 "expense_account": expense_account,
110 }
111 )
112
113 doc = frappe.get_doc(doctype)
114 doc.save(ignore_permissions=True)
115 doc.submit()
116
117
Deepesh Garg86744b62023-06-19 09:44:57 +0530118def get_random_date(start_date):
119 return add_days(start_date, randint(1, 365))
120
121
Deepesh Garg77a29572023-06-16 13:43:55 +0530122def create_transaction_deletion_record(company):
123 transaction_deletion_record = frappe.new_doc("Transaction Deletion Record")
124 transaction_deletion_record.company = company
125 transaction_deletion_record.save(ignore_permissions=True)
126 transaction_deletion_record.submit()
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530127
128
129def clear_masters():
130 for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]:
131 data = read_data_file_using_hooks(doctype)
132 if data:
133 for item in json.loads(data):
134 clear_demo_record(item)
135
136
137def clear_demo_record(doctype):
138 doc_type = doctype.get("doctype")
139 del doctype["doctype"]
140 doc = frappe.get_doc(doc_type, doctype)
141 frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
142
143
144def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530145 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530146 frappe.delete_doc("Company", company, ignore_permissions=True)
147
148
149def read_data_file_using_hooks(doctype):
150 path = os.path.join(os.path.dirname(__file__), "demo_data")
151 with open(os.path.join(path, doctype + ".json"), "r") as f:
152 data = f.read()
153
154 return data
Deepesh Garg85e1c852023-06-19 14:12:23 +0530155
156
157def get_warehouse(company):
158 abbr = frappe.db.get_value("Company", company, "abbr")
159 warehouse = "Stores - {0}".format(abbr)
160
161 return warehouse