Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge accounts functionality #14993

Merged
merged 5 commits into from Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions erpnext/accounts/doctype/account/account.js
Expand Up @@ -53,6 +53,12 @@ frappe.ui.form.on('Account', {
frm.trigger("update_account_number");
});
}

if(!frm.doc.__islocal) {
frm.add_custom_button(__('Merge Account'), function () {
frm.trigger("merge_account");
});
}
},
account_type: function (frm) {
if (frm.doc.is_group == 0) {
Expand Down Expand Up @@ -98,6 +104,44 @@ frappe.ui.form.on('Account', {
}
},

merge_account: function(frm) {
var d = new frappe.ui.Dialog({
title: __('Merge with Existing Account'),
fields: [
{
"label" : "Name",
"fieldname": "name",
"fieldtype": "Data",
"reqd": 1,
"default": frm.doc.name
}
],
primary_action: function() {
var data = d.get_values();
frappe.call({
method: "erpnext.accounts.doctype.account.account.merge_account",
args: {
old: frm.doc.name,
new: data.name,
is_group: frm.doc.is_group,
root_type: frm.doc.root_type,
company: frm.doc.company
},
callback: function(r) {
if(!r.exc) {
if(r.message) {
frappe.set_route("Form", "Account", r.message);
}
d.hide();
}
}
});
},
primary_action_label: __('Merge')
});
d.show();
},

update_account_number: function(frm) {
var d = new frappe.ui.Dialog({
title: __('Update Account Number / Name'),
Expand Down
20 changes: 20 additions & 0 deletions erpnext/accounts/doctype/account/account.py
Expand Up @@ -217,3 +217,23 @@ def update_account_number(name, account_name, account_number=None):
if name != new_name:
frappe.rename_doc("Account", name, new_name, ignore_permissions=1)
return new_name

@frappe.whitelist()
def merge_account(old, new, is_group, root_type, company):
# Validate properties before merging
if not frappe.db.exists("Account", new):
throw(_("Account {0} does not exist").format(new))

val = list(frappe.db.get_value("Account", new,
["is_group", "root_type", "company"]))

if val != [cint(is_group), root_type, company]:
throw(_("""Merging is only possible if following properties are same in both records. Is Group, Root Type, Company"""))

if is_group and frappe.db.get_value("Account", new, "parent_account") == old:
frappe.db.set_value("Account", new, "parent_account",
frappe.db.get_value("Account", old, "parent_account"))

frappe.rename_doc("Account", old, new, merge=1, ignore_permissions=1)

return new
30 changes: 29 additions & 1 deletion erpnext/accounts/doctype/account/test_account.py
Expand Up @@ -6,6 +6,7 @@
import frappe
from erpnext.stock import get_warehouse_account, get_company_default_inventory_account
from erpnext.accounts.doctype.account.account import update_account_number
from erpnext.accounts.doctype.account.account import merge_account

class TestAccount(unittest.TestCase):
def test_rename_account(self):
Expand All @@ -25,7 +26,7 @@ def test_rename_account(self):
new_account_number = "1211-11-4 - 6 - "
new_account_name = "Debtors 1 - Test - "

update_account_number("1210 - Debtors - _TC", new_account_number, new_account_name)
update_account_number("1210 - Debtors - _TC", new_account_name, new_account_number)

new_acc = frappe.db.get_value("Account", "1211-11-4 - 6 - - Debtors 1 - Test - - _TC",
["account_name", "account_number"], as_dict=1)
Expand All @@ -35,6 +36,33 @@ def test_rename_account(self):

frappe.delete_doc("Account", "1211-11-4 - 6 - Debtors 1 - Test - - _TC")

def test_merge_account(self):
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.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.parent_account = "Current 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")
self.assertEqual(parent, "Cash In Hand - _TC")

def _make_test_records(verbose):
from frappe.test_runner import make_test_objects

Expand Down