blob: df2c49b2b625838a307be8da5aac1807f73b538c [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 Garg567f4c32023-08-10 21:46:34 +053012from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
Deepesh Garg371413a2023-07-29 22:39:07 +053013from erpnext.accounts.utils import get_fiscal_year
Deepesh Garg567f4c32023-08-10 21:46:34 +053014from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_invoice
15from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice
Deepesh Garg85e1c852023-06-19 14:12:23 +053016from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
Deepesh Garg77a29572023-06-16 13:43:55 +053017
Deepesh Garg8ef257a2023-06-14 12:54:10 +053018
Deepesh Garg8ef257a2023-06-14 12:54:10 +053019def setup_demo_data():
Ankush Menat3a21c902023-08-10 15:48:57 +053020 from frappe.utils.telemetry import capture
21
22 capture("demo_data_creation_started", "erpnext")
23 try:
24 company = create_demo_company()
25 process_masters()
26 make_transactions(company)
27 frappe.cache.delete_keys("bootinfo")
28 frappe.publish_realtime("demo_data_complete")
29 except Exception:
30 frappe.log_error("Failed to create demo data")
31 capture("demo_data_creation_failed", "erpnext", properties={"exception": frappe.get_traceback()})
32 raise
33 capture("demo_data_creation_completed", "erpnext")
Deepesh Garg77a29572023-06-16 13:43:55 +053034
35
36@frappe.whitelist()
37def clear_demo_data():
Ankush Menatc8e6e062023-08-10 18:27:34 +053038 from frappe.utils.telemetry import capture
39
Ankush Menat3a21c902023-08-10 15:48:57 +053040 frappe.only_for("System Manager")
Ankush Menatc8e6e062023-08-10 18:27:34 +053041
42 capture("demo_data_erased", "erpnext")
Ankush Menate4b863a2023-08-10 17:49:28 +053043 try:
44 company = frappe.db.get_single_value("Global Defaults", "demo_company")
45 create_transaction_deletion_record(company)
46 clear_masters()
47 delete_company(company)
48 default_company = frappe.db.get_single_value("Global Defaults", "default_company")
49 frappe.db.set_default("company", default_company)
50 except Exception:
51 frappe.db.rollback()
52 frappe.log_error("Failed to erase demo data")
53 frappe.throw(
54 _("Failed to erase demo data, please delete the demo company manually."),
55 title=_("Could Not Delete Demo Data"),
56 )
Deepesh Garg8ef257a2023-06-14 12:54:10 +053057
58
59def create_demo_company():
Deepesh Gargbb5387f2023-07-07 10:49:56 +053060 company = frappe.db.get_all("Company")[0].name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053061 company_doc = frappe.get_doc("Company", company)
62
63 # Make a dummy company
64 new_company = frappe.new_doc("Company")
65 new_company.company_name = company_doc.company_name + " (Demo)"
66 new_company.abbr = company_doc.abbr + "D"
67 new_company.enable_perpetual_inventory = 1
68 new_company.default_currency = company_doc.default_currency
69 new_company.country = company_doc.country
70 new_company.chart_of_accounts_based_on = "Standard Template"
71 new_company.chart_of_accounts = company_doc.chart_of_accounts
72 new_company.insert()
73
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053074 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053075 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
76 frappe.db.set_default("company", new_company.name)
77
Deepesh Garg85e1c852023-06-19 14:12:23 +053078 bank_account = create_bank_account({"company_name": new_company.name})
79 frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name)
80
Deepesh Garg77a29572023-06-16 13:43:55 +053081 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053082
Deepesh Garg77a29572023-06-16 13:43:55 +053083
84def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053085 for doctype in frappe.get_hooks("demo_master_doctypes"):
86 data = read_data_file_using_hooks(doctype)
87 if data:
88 for item in json.loads(data):
89 create_demo_record(item)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053090
91
92def create_demo_record(doctype):
93 frappe.get_doc(doctype).insert(ignore_permissions=True)
94
95
Deepesh Garg77a29572023-06-16 13:43:55 +053096def make_transactions(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +053097 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Deepesh Garg567f4c32023-08-10 21:46:34 +053098 start_date = get_fiscal_year(date=getdate())[1]
Deepesh Garg86744b62023-06-19 09:44:57 +053099
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530100 for doctype in frappe.get_hooks("demo_transaction_doctypes"):
101 data = read_data_file_using_hooks(doctype)
102 if data:
103 for item in json.loads(data):
Deepesh Garg86744b62023-06-19 09:44:57 +0530104 create_transaction(item, company, start_date)
Deepesh Garg77a29572023-06-16 13:43:55 +0530105
Deepesh Garg567f4c32023-08-10 21:46:34 +0530106 convert_order_to_invoices()
107 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 0)
108
Deepesh Garg77a29572023-06-16 13:43:55 +0530109
Deepesh Garg86744b62023-06-19 09:44:57 +0530110def create_transaction(doctype, company, start_date):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530111 document_type = doctype.get("doctype")
Deepesh Garg85e1c852023-06-19 14:12:23 +0530112 warehouse = get_warehouse(company)
Deepesh Garg567f4c32023-08-10 21:46:34 +0530113
114 if document_type == "Purchase Order":
Deepesh Gargc9fd1822023-12-17 13:36:58 +0530115 posting_date = get_random_date(start_date, 1, 25)
Deepesh Garg567f4c32023-08-10 21:46:34 +0530116 else:
Deepesh Gargc9fd1822023-12-17 13:36:58 +0530117 posting_date = get_random_date(start_date, 31, 350)
Deepesh Garg85e1c852023-06-19 14:12:23 +0530118
Deepesh Garg86744b62023-06-19 09:44:57 +0530119 doctype.update(
120 {
121 "company": company,
122 "set_posting_time": 1,
Deepesh Garg567f4c32023-08-10 21:46:34 +0530123 "transaction_date": posting_date,
124 "schedule_date": posting_date,
125 "delivery_date": posting_date,
Deepesh Garg85e1c852023-06-19 14:12:23 +0530126 "set_warehouse": warehouse,
Deepesh Garg86744b62023-06-19 09:44:57 +0530127 }
128 )
Deepesh Garg77a29572023-06-16 13:43:55 +0530129
Deepesh Garg77a29572023-06-16 13:43:55 +0530130 doc = frappe.get_doc(doctype)
131 doc.save(ignore_permissions=True)
132 doc.submit()
133
134
Deepesh Garg567f4c32023-08-10 21:46:34 +0530135def convert_order_to_invoices():
136 for document in ["Purchase Order", "Sales Order"]:
137 # Keep some orders intentionally unbilled/unpaid
138 for i, order in enumerate(
139 frappe.db.get_all(
140 document, filters={"docstatus": 1}, fields=["name", "transaction_date"], limit=6
141 )
142 ):
143
144 if document == "Purchase Order":
145 invoice = make_purchase_invoice(order.name)
146 elif document == "Sales Order":
147 invoice = make_sales_invoice(order.name)
148
149 invoice.set_posting_time = 1
150 invoice.posting_date = order.transaction_date
151 invoice.due_date = order.transaction_date
Deepesh Garg5b157182024-01-04 10:48:01 +0530152 invoice.bill_date = order.transaction_date
153
154 if invoice.get("payment_schedule"):
155 invoice.payment_schedule[0].due_date = order.transaction_date
156
Deepesh Garg567f4c32023-08-10 21:46:34 +0530157 invoice.update_stock = 1
158 invoice.submit()
159
160 if i % 2 != 0:
161 payment = get_payment_entry(invoice.doctype, invoice.name)
162 payment.reference_no = invoice.name
163 payment.submit()
Deepesh Garg333f2a52023-08-01 10:10:50 +0530164
165
Deepesh Garg567f4c32023-08-10 21:46:34 +0530166def get_random_date(start_date, start_range, end_range):
167 return add_days(start_date, randint(start_range, end_range))
Deepesh Garg86744b62023-06-19 09:44:57 +0530168
169
Deepesh Garg77a29572023-06-16 13:43:55 +0530170def create_transaction_deletion_record(company):
171 transaction_deletion_record = frappe.new_doc("Transaction Deletion Record")
172 transaction_deletion_record.company = company
173 transaction_deletion_record.save(ignore_permissions=True)
174 transaction_deletion_record.submit()
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530175
176
177def clear_masters():
178 for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]:
179 data = read_data_file_using_hooks(doctype)
180 if data:
181 for item in json.loads(data):
182 clear_demo_record(item)
183
184
Ankush Menate4b863a2023-08-10 17:49:28 +0530185def clear_demo_record(document):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530186 document_type = document.get("doctype")
Ankush Menate4b863a2023-08-10 17:49:28 +0530187 del document["doctype"]
Ankush Menat8b579792023-08-14 14:16:48 +0530188
189 valid_columns = frappe.get_meta(document_type).get_valid_columns()
190
191 filters = document
192 for key in list(filters):
193 if key not in valid_columns:
194 filters.pop(key, None)
195
196 doc = frappe.get_doc(document_type, filters)
197 doc.delete(ignore_permissions=True)
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530198
199
200def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530201 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530202 frappe.delete_doc("Company", company, ignore_permissions=True)
203
204
205def read_data_file_using_hooks(doctype):
206 path = os.path.join(os.path.dirname(__file__), "demo_data")
207 with open(os.path.join(path, doctype + ".json"), "r") as f:
208 data = f.read()
209
210 return data
Deepesh Garg85e1c852023-06-19 14:12:23 +0530211
212
213def get_warehouse(company):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530214 warehouses = frappe.db.get_all("Warehouse", {"company": company, "is_group": 0})
215 return warehouses[randint(0, 3)].name