feat: allow user to change the parent company

This commit is contained in:
Anupam Kumar 2022-01-04 19:05:36 +05:30 committed by GitHub
parent 733c9defdf
commit 08dcbd6fce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 8 deletions

View File

@ -79,14 +79,11 @@ frappe.ui.form.on("Company", {
},
refresh: function(frm) {
if(!frm.doc.__islocal) {
frm.doc.abbr && frm.set_df_property("abbr", "read_only", 1);
frm.set_df_property("parent_company", "read_only", 1);
disbale_coa_fields(frm);
}
frm.toggle_display('address_html', !frm.is_new());
frm.toggle_display('address_html', !frm.doc.__islocal);
if(!frm.doc.__islocal) {
if (!frm.is_new()) {
frm.doc.abbr && frm.set_df_property("abbr", "read_only", 1);
disbale_coa_fields(frm);
frappe.contacts.render_address_and_contact(frm);
frappe.dynamic_link = {doc: frm.doc, fieldname: 'name', doctype: 'Company'}

View File

@ -47,6 +47,7 @@ class Company(NestedSet):
self.validate_perpetual_inventory()
self.validate_perpetual_inventory_for_non_stock_items()
self.check_country_change()
self.check_parent_changed()
self.set_chart_of_accounts()
self.validate_parent_company()
@ -130,6 +131,10 @@ class Company(NestedSet):
self.name in frappe.local.enable_perpetual_inventory:
frappe.local.enable_perpetual_inventory[self.name] = self.enable_perpetual_inventory
if frappe.flags.parent_company_changed:
from frappe.utils.nestedset import rebuild_tree
rebuild_tree("Company", "parent_company")
frappe.clear_cache()
def create_default_warehouses(self):
@ -191,7 +196,7 @@ class Company(NestedSet):
def check_country_change(self):
frappe.flags.country_change = False
if not self.get('__islocal') and \
if not self.is_new() and \
self.country != frappe.get_cached_value('Company', self.name, 'country'):
frappe.flags.country_change = True
@ -396,6 +401,13 @@ class Company(NestedSet):
if not frappe.db.get_value('GL Entry', {'company': self.name}):
frappe.db.sql("delete from `tabProcess Deferred Accounting` where company=%s", self.name)
def check_parent_changed(self):
frappe.flags.parent_company_changed = False
if not self.is_new() and \
self.parent_company != frappe.db.get_value("Company", self.name, "parent_company"):
frappe.flags.parent_company_changed = True
def get_name_with_abbr(name, company):
company_abbr = frappe.get_cached_value('Company', company, "abbr")
parts = name.split(" - ")

View File

@ -93,6 +93,61 @@ class TestCompany(unittest.TestCase):
frappe.db.sql(""" delete from `tabMode of Payment Account`
where company =%s """, (company))
def test_basic_tree(self, records=None):
min_lft = 1
max_rgt = frappe.db.sql("select max(rgt) from `tabCompany`")[0][0]
if not records:
records = test_records[2:]
for company in records:
lft, rgt, parent_company = frappe.db.get_value("Company", company["company_name"],
["lft", "rgt", "parent_company"])
if parent_company:
parent_lft, parent_rgt = frappe.db.get_value("Company", parent_company,
["lft", "rgt"])
else:
# root
parent_lft = min_lft - 1
parent_rgt = max_rgt + 1
self.assertTrue(lft)
self.assertTrue(rgt)
self.assertTrue(lft < rgt)
self.assertTrue(parent_lft < parent_rgt)
self.assertTrue(lft > parent_lft)
self.assertTrue(rgt < parent_rgt)
self.assertTrue(lft >= min_lft)
self.assertTrue(rgt <= max_rgt)
def get_no_of_children(self, company):
def get_no_of_children(companies, no_of_children):
children = []
for company in companies:
children += frappe.db.sql_list("""select name from `tabCompany`
where ifnull(parent_company, '')=%s""", company or '')
if len(children):
return get_no_of_children(children, no_of_children + len(children))
else:
return no_of_children
return get_no_of_children([company], 0)
def test_change_parent_company(self):
child_company = frappe.get_doc("Company", "_Test Company 5")
# changing parent of company
child_company.parent_company = "_Test Company 3"
child_company.save()
self.test_basic_tree()
# move it back
child_company.parent_company = "_Test Company 4"
child_company.save()
self.test_basic_tree()
def create_company_communication(doctype, docname):
comm = frappe.get_doc({
"doctype": "Communication",