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 |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 9 | from frappe.utils import add_days |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 10 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 11 | import erpnext |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 12 | from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 13 | |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 14 | |
| 15 | @frappe.whitelist() |
| 16 | def setup_demo_data(): |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 17 | company = create_demo_company() |
| 18 | process_masters() |
| 19 | make_transactions(company) |
| 20 | |
| 21 | |
| 22 | @frappe.whitelist() |
| 23 | def clear_demo_data(): |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 24 | company = frappe.db.get_single_value("Global Defaults", "demo_company") |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 25 | create_transaction_deletion_record(company) |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 26 | clear_masters() |
| 27 | delete_company(company) |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 28 | |
| 29 | |
| 30 | def create_demo_company(): |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 31 | company = erpnext.get_default_company() |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 32 | company_doc = frappe.get_doc("Company", company) |
| 33 | |
| 34 | # Make a dummy company |
| 35 | new_company = frappe.new_doc("Company") |
| 36 | new_company.company_name = company_doc.company_name + " (Demo)" |
| 37 | new_company.abbr = company_doc.abbr + "D" |
| 38 | new_company.enable_perpetual_inventory = 1 |
| 39 | new_company.default_currency = company_doc.default_currency |
| 40 | new_company.country = company_doc.country |
| 41 | new_company.chart_of_accounts_based_on = "Standard Template" |
| 42 | new_company.chart_of_accounts = company_doc.chart_of_accounts |
| 43 | new_company.insert() |
| 44 | |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 45 | # Set Demo Company as default to |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 46 | frappe.db.set_single_value("Global Defaults", "demo_company", new_company.name) |
| 47 | frappe.db.set_default("company", new_company.name) |
| 48 | |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 49 | bank_account = create_bank_account({"company_name": new_company.name}) |
| 50 | frappe.db.set_value("Company", new_company.name, "default_bank_account", bank_account.name) |
| 51 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 52 | return new_company.name |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 53 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 54 | |
| 55 | def process_masters(): |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 56 | for doctype in frappe.get_hooks("demo_master_doctypes"): |
| 57 | data = read_data_file_using_hooks(doctype) |
| 58 | if data: |
| 59 | for item in json.loads(data): |
| 60 | create_demo_record(item) |
Deepesh Garg | 8ef257a | 2023-06-14 12:54:10 +0530 | [diff] [blame] | 61 | |
| 62 | |
| 63 | def create_demo_record(doctype): |
| 64 | frappe.get_doc(doctype).insert(ignore_permissions=True) |
| 65 | |
| 66 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 67 | def make_transactions(company): |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 68 | fiscal_year = frappe.db.get_single_value("Global Defaults", "current_fiscal_year") |
| 69 | start_date = frappe.db.get_value("Fiscal Year", fiscal_year, "year_start_date") |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 70 | frappe.db.set_single_value("Stock Settings", "allow_negative_stock", 1) |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 71 | |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 72 | for doctype in frappe.get_hooks("demo_transaction_doctypes"): |
| 73 | data = read_data_file_using_hooks(doctype) |
| 74 | if data: |
| 75 | for item in json.loads(data): |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 76 | create_transaction(item, company, start_date) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 77 | |
| 78 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 79 | def create_transaction(doctype, company, start_date): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 80 | warehouse = get_warehouse(company) |
| 81 | posting_date = ( |
| 82 | start_date if doctype.get("doctype") == "Purchase Invoice" else get_random_date(start_date) |
| 83 | ) |
| 84 | bank_account = frappe.db.get_value("Company", company, "default_bank_account") |
| 85 | bank_field = "paid_to" if doctype.get("party_type") == "Customer" else "paid_from" |
| 86 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 87 | doctype.update( |
| 88 | { |
| 89 | "company": company, |
| 90 | "set_posting_time": 1, |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 91 | "posting_date": posting_date, |
| 92 | "set_warehouse": warehouse, |
| 93 | bank_field: bank_account, |
| 94 | "reference_date": posting_date, |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 95 | } |
| 96 | ) |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 97 | |
| 98 | income_account, expense_account = frappe.db.get_value( |
| 99 | "Company", company, ["default_income_account", "default_expense_account"] |
| 100 | ) |
| 101 | |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 102 | for item in doctype.get("items") or []: |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 103 | item.update( |
| 104 | { |
| 105 | "cost_center": erpnext.get_default_cost_center(company), |
| 106 | "income_account": income_account, |
| 107 | "expense_account": expense_account, |
| 108 | } |
| 109 | ) |
| 110 | |
| 111 | doc = frappe.get_doc(doctype) |
| 112 | doc.save(ignore_permissions=True) |
| 113 | doc.submit() |
| 114 | |
| 115 | |
Deepesh Garg | 86744b6 | 2023-06-19 09:44:57 +0530 | [diff] [blame] | 116 | def get_random_date(start_date): |
| 117 | return add_days(start_date, randint(1, 365)) |
| 118 | |
| 119 | |
Deepesh Garg | 77a2957 | 2023-06-16 13:43:55 +0530 | [diff] [blame] | 120 | def create_transaction_deletion_record(company): |
| 121 | transaction_deletion_record = frappe.new_doc("Transaction Deletion Record") |
| 122 | transaction_deletion_record.company = company |
| 123 | transaction_deletion_record.save(ignore_permissions=True) |
| 124 | transaction_deletion_record.submit() |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def clear_masters(): |
| 128 | for doctype in frappe.get_hooks("demo_master_doctypes")[::-1]: |
| 129 | data = read_data_file_using_hooks(doctype) |
| 130 | if data: |
| 131 | for item in json.loads(data): |
| 132 | clear_demo_record(item) |
| 133 | |
| 134 | |
| 135 | def clear_demo_record(doctype): |
| 136 | doc_type = doctype.get("doctype") |
| 137 | del doctype["doctype"] |
| 138 | doc = frappe.get_doc(doc_type, doctype) |
| 139 | frappe.delete_doc(doc.doctype, doc.name, ignore_permissions=True) |
| 140 | |
| 141 | |
| 142 | def delete_company(company): |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 143 | frappe.db.set_single_value("Global Defaults", "demo_company", "") |
Deepesh Garg | 5b6a9fc | 2023-06-17 13:08:18 +0530 | [diff] [blame] | 144 | frappe.delete_doc("Company", company, ignore_permissions=True) |
| 145 | |
| 146 | |
| 147 | def read_data_file_using_hooks(doctype): |
| 148 | path = os.path.join(os.path.dirname(__file__), "demo_data") |
| 149 | with open(os.path.join(path, doctype + ".json"), "r") as f: |
| 150 | data = f.read() |
| 151 | |
| 152 | return data |
Deepesh Garg | 85e1c85 | 2023-06-19 14:12:23 +0530 | [diff] [blame] | 153 | |
| 154 | |
| 155 | def get_warehouse(company): |
| 156 | abbr = frappe.db.get_value("Company", company, "abbr") |
| 157 | warehouse = "Stores - {0}".format(abbr) |
| 158 | |
| 159 | return warehouse |