import frappe from frappe.utils.nestedset import rebuild_tree from default_thai_company.tax_withholding import company_ready, get_or_create_account, thai_companies # Asset Category -> fixed asset account. Names follow ERPNext's Standard chart so # setup-wizard companies reuse their accounts; the rest are created under the # same "Fixed Assets" group. Depreciation accounts are left to the Company # defaults (Asset falls back to them), so one change there covers every category. FIXED_ASSET_ACCOUNTS = { "Land": "Land", "Buildings": "Buildings", "Plant and Machinery": "Plants and Machineries", "Vehicles": "Vehicles", "Furniture and Fixtures": "Furnitures and Fixtures", "Office Equipment": "Office Equipments", "Computers and Electronics": "Electronic Equipments", "Software": "Softwares", "Intangible Assets": "Intangible Assets", } FIXED_ASSET_GROUPS = ("Fixed Assets",) def fixed_asset_account(company, category): return get_or_create_account( company, FIXED_ASSET_ACCOUNTS[category], "Asset", "Fixed Asset", FIXED_ASSET_GROUPS ) def link_company(category, company): """Append the company's fixed asset account row to the category if missing.""" if any(row.company_name == company for row in category.accounts): return False category.append( "accounts", {"company_name": company, "fixed_asset_account": fixed_asset_account(company, category.name)}, ) return True def prepare_fixture_accounts(doc, method=None): """Asset Category fixtures ship without `accounts` (company-specific). Same contract as the Tax Withholding Category fixtures: fixture import re-inserts the doc on every migrate, so carry over the rows already on this site, then link every Thai company. Mandatory is relaxed for sites without a Thai company yet; `setup_company` links them later. """ if doc.name not in FIXED_ASSET_ACCOUNTS: return doc.flags.ignore_mandatory = True if not doc.accounts and frappe.db.exists(doc.doctype, doc.name): for row in frappe.get_all( "Asset Category Account", filters={"parent": doc.name, "parenttype": doc.doctype}, fields=[ "company_name", "fixed_asset_account", "accumulated_depreciation_account", "depreciation_expense_account", "capital_work_in_progress_account", ], order_by="idx", ): doc.append("accounts", row) for company in thai_companies(): link_company(doc, company) def setup_company(doc, method=None): """Company.on_update: link a Thai company on every default Asset Category.""" if not company_ready(doc): return for name in FIXED_ASSET_ACCOUNTS: if not frappe.db.exists("Asset Category", name): continue category = frappe.get_doc("Asset Category", name) if link_company(category, doc.name): category.save(ignore_permissions=True) def rebuild_locations(): """after_migrate: fixture import re-inserts the Location tree nodes (fresh lft/rgt), which strands locations users added beneath them; rebuild.""" if frappe.db.exists("Location", "All Locations"): rebuild_tree("Location")