def start_merge(docname): ledger_merge = frappe.get_doc("Ledger Merge", docname) successful_merges = 0 total = len(ledger_merge.merge_accounts) for row in ledger_merge.merge_accounts: if not row.merged: try: merge_account( row.account, ledger_merge.account, ledger_merge.is_group, ledger_merge.root_type, ledger_merge.company, ) row.db_set("merged", 1) frappe.db.commit() successful_merges += 1 frappe.publish_realtime( "ledger_merge_progress", {"ledger_merge": ledger_merge.name, "current": successful_merges, "total": total}, ) except Exception: frappe.db.rollback() ledger_merge.log_error("Ledger merge failed") finally: if successful_merges == total: ledger_merge.db_set("status", "Success") elif successful_merges > 0: ledger_merge.db_set("status", "Partial Success") else: ledger_merge.db_set("status", "Error") frappe.publish_realtime("ledger_merge_refresh", {"ledger_merge": ledger_merge.name})
def test_merge_account(self): if not frappe.db.exists("Account", "Current Assets - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Current Assets" acc.is_group = 1 acc.parent_account = "Application of Funds (Assets) - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Securities and Deposits - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Securities and Deposits" acc.parent_account = "Current Assets - _TC" acc.is_group = 1 acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Earnest Money - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Earnest Money" acc.parent_account = "Securities and Deposits - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Cash In Hand - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Cash In Hand" acc.is_group = 1 acc.parent_account = "Current Assets - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Accumulated Depreciation - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Accumulated Depreciation" acc.parent_account = "Fixed Assets - _TC" acc.company = "_Test Company" acc.insert() doc = frappe.get_doc("Account", "Securities and Deposits - _TC") parent = frappe.db.get_value("Account", "Earnest Money - _TC", "parent_account") self.assertEqual(parent, "Securities and Deposits - _TC") merge_account("Securities and Deposits - _TC", "Cash In Hand - _TC", doc.is_group, doc.root_type, doc.company) parent = frappe.db.get_value("Account", "Earnest Money - _TC", "parent_account") # Parent account of the child account changes after merging self.assertEqual(parent, "Cash In Hand - _TC") # Old account doesn't exist after merging self.assertFalse(frappe.db.exists("Account", "Securities and Deposits - _TC")) doc = frappe.get_doc("Account", "Current Assets - _TC") # Raise error as is_group property doesn't match self.assertRaises(frappe.ValidationError, merge_account, "Current Assets - _TC",\ "Accumulated Depreciation - _TC", doc.is_group, doc.root_type, doc.company) doc = frappe.get_doc("Account", "Capital Stock - _TC") # Raise error as root_type property doesn't match self.assertRaises(frappe.ValidationError, merge_account, "Capital Stock - _TC",\ "Softwares - _TC", doc.is_group, doc.root_type, doc.company)
def test_merge_account(self): if not frappe.db.exists("Account", "Current Assets - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Current Assets" acc.is_group = 1 acc.parent_account = "Application of Funds (Assets) - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Securities and Deposits - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Securities and Deposits" acc.parent_account = "Current Assets - _TC" acc.is_group = 1 acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Earnest Money - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Earnest Money" acc.parent_account = "Securities and Deposits - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Cash In Hand - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Cash In Hand" acc.is_group = 1 acc.parent_account = "Current Assets - _TC" acc.company = "_Test Company" acc.insert() if not frappe.db.exists("Account", "Accumulated Depreciation - _TC"): acc = frappe.new_doc("Account") acc.account_name = "Accumulated Depreciation" acc.parent_account = "Fixed Assets - _TC" acc.company = "_Test Company" acc.insert() doc = frappe.get_doc("Account", "Securities and Deposits - _TC") parent = frappe.db.get_value("Account", "Earnest Money - _TC", "parent_account") self.assertEqual(parent, "Securities and Deposits - _TC") merge_account("Securities and Deposits - _TC", "Cash In Hand - _TC", doc.is_group, doc.root_type, doc.company) parent = frappe.db.get_value("Account", "Earnest Money - _TC", "parent_account") # Parent account of the child account changes after merging self.assertEqual(parent, "Cash In Hand - _TC") # Old account doesn't exist after merging self.assertFalse(frappe.db.exists("Account", "Securities and Deposits - _TC")) doc = frappe.get_doc("Account", "Current Assets - _TC") # Raise error as is_group property doesn't match self.assertRaises(frappe.ValidationError, merge_account, "Current Assets - _TC",\ "Accumulated Depreciation - _TC", doc.is_group, doc.root_type, doc.company) doc = frappe.get_doc("Account", "Capital Stock - _TC") # Raise error as root_type property doesn't match self.assertRaises(frappe.ValidationError, merge_account, "Capital Stock - _TC",\ "Softwares - _TC", doc.is_group, doc.root_type, doc.company)