
from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import set_name_by_naming_series

class Instructor(Document):
	def autoname(self):
		naming_method = frappe.db.get_value("Education Settings", None, "instructor_created_by")
		if not naming_method:
			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
		else:
			if naming_method == 'Naming Series':
				set_name_by_naming_series(self)
			elif naming_method == 'Employee Number':
				if not self.employee:
					frappe.throw(_("Please select Employee"))
				self.name = self.employee
			elif naming_method == 'Full Name':
				self.name = self.instructor_name

	def validate(self):
		self.validate_duplicate_employee()

	def validate_duplicate_employee(self):
		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee}, 'name'):
			frappe.throw(_("Employee ID is linked with another instructor"))




from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import set_name_by_naming_series

class Instructor(Document):
	def autoname(self):
		naming_method = frappe.db.get_value("Education Settings", None, "instructor_created_by")
		if not naming_method:
			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
		else:
			if naming_method == 'Naming Series':
				set_name_by_naming_series(self)
			elif naming_method == 'Employee Number':
				if not self.employee:
					frappe.throw(_("Please select Employee"))
				self.name = self.employee
			elif naming_method == 'Full Name':
				self.name = self.instructor_name

	def validate(self):
		self.validate_duplicate_employee()

	def validate_duplicate_employee(self):
		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee, 'name': ['!=', self.name]}, 'name'):
			frappe.throw(_("Employee ID is linked with another instructor"))



Exclude current record while validating duplicate employee
fix: Exclude current record while validating duplicate employee
  
  from __future__ import unicode_literals
  import frappe
  from frappe import _
  from frappe.model.document import Document
  from frappe.model.naming import set_name_by_naming_series
  
  class Instructor(Document):
  	def autoname(self):
  		naming_method = frappe.db.get_value("Education Settings", None, "instructor_created_by")
  		if not naming_method:
  			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
  		else:
  			if naming_method == 'Naming Series':
  				set_name_by_naming_series(self)
  			elif naming_method == 'Employee Number':
  				if not self.employee:
  					frappe.throw(_("Please select Employee"))
  				self.name = self.employee
  			elif naming_method == 'Full Name':
  				self.name = self.instructor_name
  
  	def validate(self):
  		self.validate_duplicate_employee()
  
  	def validate_duplicate_employee(self):
- 		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee}, 'name'):
+ 		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee, 'name': ['!=', self.name]}, 'name'):
  			frappe.throw(_("Employee ID is linked with another instructor"))
  
  
  
Exclude current record while validating duplicate employee
## Code Before:

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import set_name_by_naming_series

class Instructor(Document):
	def autoname(self):
		naming_method = frappe.db.get_value("Education Settings", None, "instructor_created_by")
		if not naming_method:
			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
		else:
			if naming_method == 'Naming Series':
				set_name_by_naming_series(self)
			elif naming_method == 'Employee Number':
				if not self.employee:
					frappe.throw(_("Please select Employee"))
				self.name = self.employee
			elif naming_method == 'Full Name':
				self.name = self.instructor_name

	def validate(self):
		self.validate_duplicate_employee()

	def validate_duplicate_employee(self):
		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee}, 'name'):
			frappe.throw(_("Employee ID is linked with another instructor"))



## Instruction:
Exclude current record while validating duplicate employee
## Code After:

from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.model.naming import set_name_by_naming_series

class Instructor(Document):
	def autoname(self):
		naming_method = frappe.db.get_value("Education Settings", None, "instructor_created_by")
		if not naming_method:
			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
		else:
			if naming_method == 'Naming Series':
				set_name_by_naming_series(self)
			elif naming_method == 'Employee Number':
				if not self.employee:
					frappe.throw(_("Please select Employee"))
				self.name = self.employee
			elif naming_method == 'Full Name':
				self.name = self.instructor_name

	def validate(self):
		self.validate_duplicate_employee()

	def validate_duplicate_employee(self):
		if self.employee and frappe.db.get_value("Instructor", {'employee': self.employee, 'name': ['!=', self.name]}, 'name'):
			frappe.throw(_("Employee ID is linked with another instructor"))


