[fix] Validate Company in Account autoname
This commit is contained in:
		
							parent
							
								
									719504a0fe
								
							
						
					
					
						commit
						19fec06366
					
				| @ -19,8 +19,12 @@ class Account(Document): | |||||||
| 			self.get("__onload").can_freeze_account = True | 			self.get("__onload").can_freeze_account = True | ||||||
| 
 | 
 | ||||||
| 	def autoname(self): | 	def autoname(self): | ||||||
| 		self.name = self.account_name.strip() + ' - ' + \ | 		# first validate if company exists | ||||||
| 			frappe.db.get_value("Company", self.company, "abbr") | 		company = frappe.db.get_value("Company", self.company, ["abbr", "name"], as_dict=True) | ||||||
|  | 		if not company: | ||||||
|  | 			frappe.throw(_('Company {0} does not exist').format(self.company)) | ||||||
|  | 
 | ||||||
|  | 		self.name = self.account_name.strip() + ' - ' + company.abbr | ||||||
| 
 | 
 | ||||||
| 	def validate(self): | 	def validate(self): | ||||||
| 		if frappe.local.flags.allow_unverified_charts: | 		if frappe.local.flags.allow_unverified_charts: | ||||||
| @ -68,7 +72,7 @@ class Account(Document): | |||||||
| 				if self.root_type != db_value.root_type: | 				if self.root_type != db_value.root_type: | ||||||
| 					frappe.db.sql("update `tabAccount` set root_type=%s where lft > %s and rgt < %s", | 					frappe.db.sql("update `tabAccount` set root_type=%s where lft > %s and rgt < %s", | ||||||
| 						(self.root_type, self.lft, self.rgt)) | 						(self.root_type, self.lft, self.rgt)) | ||||||
| 						 | 
 | ||||||
| 		if self.root_type and not self.report_type: | 		if self.root_type and not self.report_type: | ||||||
| 			self.report_type = "Balance Sheet" \ | 			self.report_type = "Balance Sheet" \ | ||||||
| 				if self.root_type in ("Asset", "Liability", "Equity") else "Profit and Loss" | 				if self.root_type in ("Asset", "Liability", "Equity") else "Profit and Loss" | ||||||
| @ -78,14 +82,14 @@ class Account(Document): | |||||||
| 		if frappe.db.exists("Account", self.name): | 		if frappe.db.exists("Account", self.name): | ||||||
| 			if not frappe.db.get_value("Account", self.name, "parent_account"): | 			if not frappe.db.get_value("Account", self.name, "parent_account"): | ||||||
| 				throw(_("Root cannot be edited."), RootNotEditable) | 				throw(_("Root cannot be edited."), RootNotEditable) | ||||||
| 				 | 
 | ||||||
| 		if not self.parent_account and not self.is_group: | 		if not self.parent_account and not self.is_group: | ||||||
| 			frappe.throw(_("Root Account must be a group")) | 			frappe.throw(_("Root Account must be a group")) | ||||||
| 			 | 
 | ||||||
| 	def validate_group_or_ledger(self): | 	def validate_group_or_ledger(self): | ||||||
| 		if self.get("__islocal"): | 		if self.get("__islocal"): | ||||||
| 			return | 			return | ||||||
| 		 | 
 | ||||||
| 		existing_is_group = frappe.db.get_value("Account", self.name, "is_group") | 		existing_is_group = frappe.db.get_value("Account", self.name, "is_group") | ||||||
| 		if self.is_group != existing_is_group: | 		if self.is_group != existing_is_group: | ||||||
| 			if self.check_gle_exists(): | 			if self.check_gle_exists(): | ||||||
| @ -153,7 +157,7 @@ class Account(Document): | |||||||
| 	def validate_mandatory(self): | 	def validate_mandatory(self): | ||||||
| 		if not self.root_type: | 		if not self.root_type: | ||||||
| 			throw(_("Root Type is mandatory")) | 			throw(_("Root Type is mandatory")) | ||||||
| 			 | 
 | ||||||
| 		if not self.report_type: | 		if not self.report_type: | ||||||
| 			throw(_("Report Type is mandatory")) | 			throw(_("Report Type is mandatory")) | ||||||
| 
 | 
 | ||||||
| @ -216,9 +220,9 @@ class Account(Document): | |||||||
| 
 | 
 | ||||||
| 			if val != [self.is_group, self.root_type, self.company]: | 			if val != [self.is_group, self.root_type, self.company]: | ||||||
| 				throw(_("""Merging is only possible if following properties are same in both records. Is Group, Root Type, Company""")) | 				throw(_("""Merging is only possible if following properties are same in both records. Is Group, Root Type, Company""")) | ||||||
| 				 | 
 | ||||||
| 			if self.is_group and frappe.db.get_value("Account", new, "parent_account") == old: | 			if self.is_group and frappe.db.get_value("Account", new, "parent_account") == old: | ||||||
| 				frappe.db.set_value("Account", new, "parent_account",  | 				frappe.db.set_value("Account", new, "parent_account", | ||||||
| 					frappe.db.get_value("Account", old, "parent_account")) | 					frappe.db.get_value("Account", old, "parent_account")) | ||||||
| 
 | 
 | ||||||
| 		return new_account | 		return new_account | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user