blob: 78b31e4ec357be8073b8b1ddccff06623c0443ac [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 Menat3a21c902023-08-10 15:48:57 +053038 frappe.only_for("System Manager")
Ankush Menate4b863a2023-08-10 17:49:28 +053039 try:
40 company = frappe.db.get_single_value("Global Defaults", "demo_company")
41 create_transaction_deletion_record(company)
42 clear_masters()
43 delete_company(company)
44 default_company = frappe.db.get_single_value("Global Defaults", "default_company")
45 frappe.db.set_default("company", default_company)
46 except Exception:
47 frappe.db.rollback()
48 frappe.log_error("Failed to erase demo data")
49 frappe.throw(
50 _("Failed to erase demo data, please delete the demo company manually."),
51 title=_("Could Not Delete Demo Data"),
52 )
Deepesh Garg8ef257a2023-06-14 12:54:10 +053053
54
55def create_demo_company():
Deepesh Gargbb5387f2023-07-07 10:49:56 +053056 company = frappe.db.get_all("Company")[0].name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053057 company_doc = frappe.get_doc("Company", company)
58
59 # Make a dummy company
60 new_company = frappe.new_doc("Company")
61 new_company.company_name = company_doc.company_name + " (Demo)"
62 new_company.abbr = company_doc.abbr + "D"
63 new_company.enable_perpetual_inventory = 1
64 new_company.default_currency = company_doc.default_currency
65 new_company.country = company_doc.country
66 new_company.chart_of_accounts_based_on = "Standard Template"
67 new_company.chart_of_accounts = company_doc.chart_of_accounts
68 new_company.insert()
69
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053070 # Set Demo Company as default to
Deepesh Garg86744b62023-06-19 09:44:57 +053071 frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name)
72 frappe.db.set_default("company", new_company.name)
73
Deepesh Garg85e1c852023-06-19 14:12:23 +053074 bank_account = create_bank_account({"company_name": new_company.name})
75 frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name)
76
Deepesh Garg77a29572023-06-16 13:43:55 +053077 return new_company.name
Deepesh Garg8ef257a2023-06-14 12:54:10 +053078
Deepesh Garg77a29572023-06-16 13:43:55 +053079
80def process_masters():
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053081 for doctype in frappe.get_hooks("demo_master_doctypes"):
82 data = read_data_file_using_hooks(doctype)
83 if data:
84 for item in json.loads(data):
85 create_demo_record(item)
Deepesh Garg8ef257a2023-06-14 12:54:10 +053086
87
88def create_demo_record(doctype):
89 frappe.get_doc(doctype).insert(ignore_permissions=True)
90
91
Deepesh Garg77a29572023-06-16 13:43:55 +053092def make_transactions(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +053093 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1)
Deepesh Garg567f4c32023-08-10 21:46:34 +053094 start_date = get_fiscal_year(date=getdate())[1]
Deepesh Garg86744b62023-06-19 09:44:57 +053095
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +053096 for doctype in frappe.get_hooks("demo_transaction_doctypes"):
97 data = read_data_file_using_hooks(doctype)
98 if data:
99 for item in json.loads(data):
Deepesh Garg86744b62023-06-19 09:44:57 +0530100 create_transaction(item, company, start_date)
Deepesh Garg77a29572023-06-16 13:43:55 +0530101
Deepesh Garg567f4c32023-08-10 21:46:34 +0530102 convert_order_to_invoices()
103 frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 0)
104
Deepesh Garg77a29572023-06-16 13:43:55 +0530105
Deepesh Garg86744b62023-06-19 09:44:57 +0530106def create_transaction(doctype, company, start_date):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530107 document_type = doctype.get("doctype")
Deepesh Garg85e1c852023-06-19 14:12:23 +0530108 warehouse = get_warehouse(company)
Deepesh Garg567f4c32023-08-10 21:46:34 +0530109
110 if document_type == "Purchase Order":
111 posting_date = get_random_date(start_date, 1, 30)
112 else:
113 posting_date = get_random_date(start_date, 31, 365)
Deepesh Garg85e1c852023-06-19 14:12:23 +0530114
Deepesh Garg86744b62023-06-19 09:44:57 +0530115 doctype.update(
116 {
117 "company": company,
118 "set_posting_time": 1,
Deepesh Garg567f4c32023-08-10 21:46:34 +0530119 "transaction_date": posting_date,
120 "schedule_date": posting_date,
121 "delivery_date": posting_date,
Deepesh Garg85e1c852023-06-19 14:12:23 +0530122 "set_warehouse": warehouse,
Deepesh Garg86744b62023-06-19 09:44:57 +0530123 }
124 )
Deepesh Garg77a29572023-06-16 13:43:55 +0530125
Deepesh Garg77a29572023-06-16 13:43:55 +0530126 doc = frappe.get_doc(doctype)
127 doc.save(ignore_permissions=True)
128 doc.submit()
129
130
Deepesh Garg567f4c32023-08-10 21:46:34 +0530131def convert_order_to_invoices():
132 for document in ["Purchase Order", "Sales Order"]:
133 # Keep some orders intentionally unbilled/unpaid
134 for i, order in enumerate(
135 frappe.db.get_all(
136 document, filters={"docstatus": 1}, fields=["name", "transaction_date"], limit=6
137 )
138 ):
139
140 if document == "Purchase Order":
141 invoice = make_purchase_invoice(order.name)
142 elif document == "Sales Order":
143 invoice = make_sales_invoice(order.name)
144
145 invoice.set_posting_time = 1
146 invoice.posting_date = order.transaction_date
147 invoice.due_date = order.transaction_date
148 invoice.update_stock = 1
149 invoice.submit()
150
151 if i % 2 != 0:
152 payment = get_payment_entry(invoice.doctype, invoice.name)
153 payment.reference_no = invoice.name
154 payment.submit()
Deepesh Garg333f2a52023-08-01 10:10:50 +0530155
156
Deepesh Garg567f4c32023-08-10 21:46:34 +0530157def get_random_date(start_date, start_range, end_range):
158 return add_days(start_date, randint(start_range, end_range))
Deepesh Garg86744b62023-06-19 09:44:57 +0530159
160
Deepesh Garg77a29572023-06-16 13:43:55 +0530161def create_transaction_deletion_record(company):
162 transaction_deletion_record = frappe.new_doc("Transaction Deletion Record")
163 transaction_deletion_record.company = company
164 transaction_deletion_record.save(ignore_permissions=True)
165 transaction_deletion_record.submit()
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530166
167
168def clear_masters():
169 for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]:
170 data = read_data_file_using_hooks(doctype)
171 if data:
172 for item in json.loads(data):
173 clear_demo_record(item)
174
175
Ankush Menate4b863a2023-08-10 17:49:28 +0530176def clear_demo_record(document):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530177 document_type = document.get("doctype")
Ankush Menate4b863a2023-08-10 17:49:28 +0530178 del document["doctype"]
Deepesh Garg567f4c32023-08-10 21:46:34 +0530179 doc = frappe.get_doc(document_type, document)
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530180 frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True)
181
182
183def delete_company(company):
Deepesh Garg85e1c852023-06-19 14:12:23 +0530184 frappe.db.set_single_value("Global Defaults", "demo_company", "")
Deepesh Garg5b6a9fc2023-06-17 13:08:18 +0530185 frappe.delete_doc("Company", company, ignore_permissions=True)
186
187
188def read_data_file_using_hooks(doctype):
189 path = os.path.join(os.path.dirname(__file__), "demo_data")
190 with open(os.path.join(path, doctype + ".json"), "r") as f:
191 data = f.read()
192
193 return data
Deepesh Garg85e1c852023-06-19 14:12:23 +0530194
195
196def get_warehouse(company):
Deepesh Garg567f4c32023-08-10 21:46:34 +0530197 warehouses = frappe.db.get_all("Warehouse", {"company": company, "is_group": 0})
198 return warehouses[randint(0, 3)].name