blob: 50bc43d3258dbe1e71f0c7815dbab20cdd5cf9e8 [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 )
Deepesh Garg333f2a52023-08-01 10:10:50 +053086 bank_account, default_receivable_account = frappe.db.get_value(
87 "Company", company, ["default_bank_account", "default_receivable_account"]
88 )
Deepesh Garg85e1c852023-06-19 14:12:23 +053089 bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from"
90
Deepesh Garg86744b62023-06-19 09:44:57 +053091 doctype.update(
92 {
93 "company": company,
94 "set_posting_time": 1,
Deepesh Garg85e1c852023-06-19 14:12:23 +053095 "posting_date": posting_date,
96 "set_warehouse": warehouse,
97 bank_field: bank_account,
98 "reference_date": posting_date,
Deepesh Garg86744b62023-06-19 09:44:57 +053099 }
100 )
Deepesh Garg77a29572023-06-16 13:43:55 +0530101
102 income_account, expense_account = frappe.db.get_value(
103 "Company", company, ["default_income_account", "default_expense_account"]
104 )
105
Deepesh Garg333f2a52023-08-01 10:10:50 +0530106 if doctype in ("Purchase Invoice", "Sales Invoice"):
107 for item in doctype.get("items") or []:
108 item.update(
109 {
110 "cost_center": erpnext.get_default_cost_center(company),
111 "income_account": income_account,
112 "expense_account": expense_account,
113 }
114 )
115 elif doctype == "Journal Entry":
116 pass
117 # update_accounts(doctype, bank_account, default_receivable_account)
Deepesh Garg77a29572023-06-16 13:43:55 +0530118
119 doc = frappe.get_doc(doctype)
120 doc.save(ignore_permissions=True)
121 doc.submit()
122
123
Deepesh Garg333f2a52023-08-01 10:10:50 +0530124# def update_accounts(doctype, company, bank_account):
125
126
Deepesh Garg86744b62023-06-19 09:44:57 +0530127def get_random_date(start_date):
128 return add_days(start_date, randint(1, 365))
129
130
Deepesh Garg77a29572023-06-16 13:43:55 +0530131def create_transaction_deletion_record(company):
132 transaction_deletion_record = frappe.new_doc("Transaction Deletion Record")
133 transaction_deletion_record.company = company
134 transaction_deletion_record.save(ignore_permissions=True)
135 transaction_deletion_record.submit()
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530136
137
138def clear_masters():
139 for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]:
140 data = read_data_file_using_hooks(doctype)
141 if data:
142 for item in json.loads(data):
143 clear_demo_record(item)
144
145
146def clear_demo_record(doctype):
147 doc_type = doctype.get("doctype")
148 del doctype["doctype"]
149 doc = frappe.get_doc(doc_type, doctype)
150 frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
151
152
153def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530154 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530155 frappe.delete_doc("Company", company, ignore_permissions=True)
156
157
158def read_data_file_using_hooks(doctype):
159 path = os.path.join(os.path.dirname(__file__), "demo_data")
160 with open(os.path.join(path, doctype + ".json"), "r") as f:
161 data = f.read()
162
163 return data
Deepesh Garg85e1c852023-06-19 14:12:23 +0530164
165
166def get_warehouse(company):
167 abbr = frappe.db.get_value("Company", company, "abbr")
168 warehouse = "Stores - {0}".format(abbr)
169
170 return warehouse