Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 1 | # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | import json |
| 5 | import os |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 6 | from random import randint |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 9 | from frappe import _ |
Deepesh Garg | 371413a | 2023-07-29 22:39:07 +0530 | [diff] [blame] | 10 | from frappe.utils import add_days, getdate |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 11 | |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 12 | from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry |
Deepesh Garg | 371413a | 2023-07-29 22:39:07 +0530 | [diff] [blame] | 13 | from erpnext.accounts.utils import get_fiscal_year |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 14 | from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_invoice |
| 15 | from erpnext.selling.doctype.sales_order.sales_order import make_sales_invoice |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 16 | from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 17 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 18 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 19 | def setup_demo_data(): |
Ankush Menat | 3a21c90 | 2023-08-10 15:48:57 +0530 | [diff] [blame] | 20 | 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 Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 34 | |
| 35 | |
| 36 | @frappe.whitelist() |
| 37 | def clear_demo_data(): |
Ankush Menat | c8e6e06 | 2023-08-10 18:27:34 +0530 | [diff] [blame] | 38 | from frappe.utils.telemetry import capture |
| 39 | |
Ankush Menat | 3a21c90 | 2023-08-10 15:48:57 +0530 | [diff] [blame] | 40 | frappe.only_for("System Manager") |
Ankush Menat | c8e6e06 | 2023-08-10 18:27:34 +0530 | [diff] [blame] | 41 | |
| 42 | capture("demo_data_erased", "erpnext") |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 43 | 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 Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def create_demo_company(): |
Deepesh Garg | bb5387f | 2023-07-07 10:49:56 +0530 | [diff] [blame] | 60 | company = frappe.db.get_all("Company")[0].name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 61 | 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 Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 74 | # Set Demo Company as default to |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 75 | frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name) |
| 76 | frappe.db.set_default("company", new_company.name) |
| 77 | |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 78 | 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 Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 81 | return new_company.name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 82 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 83 | |
| 84 | def process_masters(): |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 85 | 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 Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 90 | |
| 91 | |
| 92 | def create_demo_record(doctype): |
| 93 | frappe.get_doc(doctype).insert(ignore_permissions=True) |
| 94 | |
| 95 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 96 | def make_transactions(company): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 97 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1) |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 98 | start_date = get_fiscal_year(date=getdate())[1] |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 99 | |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 100 | 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 Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 104 | create_transaction(item, company, start_date) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 105 | |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 106 | convert_order_to_invoices() |
| 107 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 0) |
| 108 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 109 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 110 | def create_transaction(doctype, company, start_date): |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 111 | document_type = doctype.get("doctype") |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 112 | warehouse = get_warehouse(company) |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 113 | |
| 114 | if document_type == "Purchase Order": |
Deepesh Garg | c9fd182 | 2023-12-17 13:36:58 +0530 | [diff] [blame] | 115 | posting_date = get_random_date(start_date, 1, 25) |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 116 | else: |
Deepesh Garg | c9fd182 | 2023-12-17 13:36:58 +0530 | [diff] [blame] | 117 | posting_date = get_random_date(start_date, 31, 350) |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 118 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 119 | doctype.update( |
| 120 | { |
| 121 | "company": company, |
| 122 | "set_posting_time": 1, |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 123 | "transaction_date": posting_date, |
| 124 | "schedule_date": posting_date, |
| 125 | "delivery_date": posting_date, |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 126 | "set_warehouse": warehouse, |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 127 | } |
| 128 | ) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 129 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 130 | doc = frappe.get_doc(doctype) |
| 131 | doc.save(ignore_permissions=True) |
| 132 | doc.submit() |
| 133 | |
| 134 | |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 135 | def 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 Garg | 5b15718 | 2024-01-04 10:48:01 +0530 | [diff] [blame] | 152 | 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 Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 157 | 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 Garg | 333f2a5 | 2023-08-01 10:10:50 +0530 | [diff] [blame] | 164 | |
| 165 | |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 166 | def get_random_date(start_date, start_range, end_range): |
| 167 | return add_days(start_date, randint(start_range, end_range)) |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 168 | |
| 169 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 170 | def 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 Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 175 | |
| 176 | |
| 177 | def 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 Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 185 | def clear_demo_record(document): |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 186 | document_type = document.get("doctype") |
Ankush Menat | e4b863a | 2023-08-10 17:49:28 +0530 | [diff] [blame] | 187 | del document["doctype"] |
Ankush Menat | 8b57979 | 2023-08-14 14:16:48 +0530 | [diff] [blame] | 188 | |
| 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 Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 198 | |
| 199 | |
| 200 | def delete_company(company): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 201 | frappe.db.set_single_value("Global Defaults", "demo_company", "") |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 202 | frappe.delete_doc("Company", company, ignore_permissions=True) |
| 203 | |
| 204 | |
| 205 | def 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 Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 211 | |
| 212 | |
| 213 | def get_warehouse(company): |
Deepesh Garg | 567f4c3 | 2023-08-10 21:46:34 +0530 | [diff] [blame] | 214 | warehouses = frappe.db.get_all("Warehouse", {"company": company, "is_group": 0}) |
| 215 | return warehouses[randint(0, 3)].name |