Data import Tool bug fix (#13165)

* fixes #13064

* commonified naming series checks

* test fixes
replaced test employee names according to naming series
diff --git a/erpnext/buying/doctype/supplier/supplier.py b/erpnext/buying/doctype/supplier/supplier.py
index c715fbd..6aa3b01 100644
--- a/erpnext/buying/doctype/supplier/supplier.py
+++ b/erpnext/buying/doctype/supplier/supplier.py
@@ -5,7 +5,7 @@
 import frappe
 import frappe.defaults
 from frappe import msgprint, _
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 from frappe.contacts.address_and_contact import load_address_and_contact, delete_contact_and_address
 from erpnext.utilities.transaction_base import TransactionBase
 from erpnext.accounts.party import validate_party_accounts, get_dashboard_info, get_timeline_data # keep this
@@ -28,7 +28,7 @@
 		if supp_master_name == 'Supplier Name':
 			self.name = self.supplier_name
 		else:
-			self.name = make_autoname(self.naming_series + '.#####')
+			set_name_by_naming_series(self)
 
 	def on_update(self):
 		if not self.naming_series:
diff --git a/erpnext/education/doctype/instructor/instructor.py b/erpnext/education/doctype/instructor/instructor.py
index 78e4261..0756b5f 100644
--- a/erpnext/education/doctype/instructor/instructor.py
+++ b/erpnext/education/doctype/instructor/instructor.py
@@ -6,7 +6,7 @@
 import frappe
 from frappe import _
 from frappe.model.document import Document
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 
 class Instructor(Document):
 	def autoname(self):
@@ -15,7 +15,7 @@
 			frappe.throw(_("Please setup Instructor Naming System in Education > Education Settings"))
 		else:
 			if naming_method == 'Naming Series':
-				self.name = make_autoname(self.naming_series + '.####')
+				set_name_by_naming_series(self)
 			elif naming_method == 'Employee Number':
 				if not self.employee:
 					frappe.throw(_("Please select Employee"))
diff --git a/erpnext/education/doctype/instructor/test_records.json b/erpnext/education/doctype/instructor/test_records.json
index 3747c0d..220d84e 100644
--- a/erpnext/education/doctype/instructor/test_records.json
+++ b/erpnext/education/doctype/instructor/test_records.json
@@ -1,12 +1,12 @@
 [
 	{
 		"naming_series": "_T-Instructor-",
-		"employee": "_T-Employee-0001",
+		"employee": "_T-Employee-00001",
 		"instructor_name": "_Test Instructor"
 	},
 	{
 		"naming_series": "_T-Instructor-",
-		"employee": "_T-Employee-0002", 
+		"employee": "_T-Employee-00002",
 		"instructor_name": "_Test Instructor 2"
 	}
 ]
diff --git a/erpnext/healthcare/doctype/patient/patient.py b/erpnext/healthcare/doctype/patient/patient.py
index b01f56a..d0332d8 100644
--- a/erpnext/healthcare/doctype/patient/patient.py
+++ b/erpnext/healthcare/doctype/patient/patient.py
@@ -8,7 +8,7 @@
 from frappe.model.document import Document
 from frappe.utils import cint, cstr, getdate
 import dateutil
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 from erpnext.healthcare.doctype.healthcare_settings.healthcare_settings import get_receivable_account,get_income_account,send_registration_sms
 
 class Patient(Document):
@@ -42,10 +42,7 @@
 		if patient_master_name == 'Patient Name':
 			self.name = self.get_patient_name()
 		else:
-			if not self.naming_series:
-				frappe.throw(_("Series is mandatory"), frappe.MandatoryError)
-
-			self.name = make_autoname(self.naming_series+'.#####')
+			set_name_by_naming_series(self)
 
 	def get_patient_name(self):
 		name = self.patient_name
diff --git a/erpnext/hr/doctype/attendance/test_records.json b/erpnext/hr/doctype/attendance/test_records.json
index 1c8f3b5..096f95c 100644
--- a/erpnext/hr/doctype/attendance/test_records.json
+++ b/erpnext/hr/doctype/attendance/test_records.json
@@ -2,7 +2,7 @@
 	{
 		"doctype": "Attendance",
 		"name": "_Test Attendance 1",
-		"employee": "_T-Employee-0001",
+		"employee": "_T-Employee-00001",
 		"status": "Present",
 		"attendance_date": "2014-02-01",
 		"company": "_Test Company"
diff --git a/erpnext/hr/doctype/employee/employee.py b/erpnext/hr/doctype/employee/employee.py
index 25d3ec4..5446a6e 100755
--- a/erpnext/hr/doctype/employee/employee.py
+++ b/erpnext/hr/doctype/employee/employee.py
@@ -5,7 +5,7 @@
 import frappe
 
 from frappe.utils import getdate, validate_email_add, today, add_years
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 from frappe import throw, _, scrub
 import frappe.permissions
 from frappe.model.document import Document
@@ -24,7 +24,7 @@
 			throw(_("Please setup Employee Naming System in Human Resource > HR Settings"))
 		else:
 			if naming_method == 'Naming Series':
-				self.name = make_autoname(self.naming_series + '.####')
+				set_name_by_naming_series(self)
 			elif naming_method == 'Employee Number':
 				self.name = self.employee_number
 			elif naming_method == 'Full Name':
diff --git a/erpnext/hr/doctype/employee_advance/test_employee_advance.py b/erpnext/hr/doctype/employee_advance/test_employee_advance.py
index cacf90e..2097e71 100644
--- a/erpnext/hr/doctype/employee_advance/test_employee_advance.py
+++ b/erpnext/hr/doctype/employee_advance/test_employee_advance.py
@@ -35,7 +35,7 @@
 
 def make_employee_advance():
 	doc = frappe.new_doc("Employee Advance")
-	doc.employee = "_T-Employee-0001"
+	doc.employee = "_T-Employee-00001"
 	doc.company  = "_Test company"
 	doc.purpose = "For site visit"
 	doc.advance_amount = 1000
@@ -43,5 +43,5 @@
 	doc.advance_account = "_Test Employee Advance - _TC"
 	doc.insert()
 	doc.submit()
-	
+
 	return doc
\ No newline at end of file
diff --git a/erpnext/hr/doctype/expense_claim/test_expense_claim.py b/erpnext/hr/doctype/expense_claim/test_expense_claim.py
index e0ba088..f89beba 100644
--- a/erpnext/hr/doctype/expense_claim/test_expense_claim.py
+++ b/erpnext/hr/doctype/expense_claim/test_expense_claim.py
@@ -87,7 +87,7 @@
 def make_expense_claim(payable_account,claim_amount, sanctioned_amount, company, account, project=None, task_name=None):
 	expense_claim = frappe.get_doc({
 		 "doctype": "Expense Claim",
-		 "employee": "_T-Employee-0001",
+		 "employee": "_T-Employee-00001",
 		 "payable_account": payable_account,
 		 "company": company,
 		 "expenses":
diff --git a/erpnext/hr/doctype/leave_allocation/test_records.json b/erpnext/hr/doctype/leave_allocation/test_records.json
index 106ed0e..23acbb0 100644
--- a/erpnext/hr/doctype/leave_allocation/test_records.json
+++ b/erpnext/hr/doctype/leave_allocation/test_records.json
@@ -2,7 +2,7 @@
 	{
 		"docstatus": 1,
 		"doctype": "Leave Allocation",
-		"employee": "_T-Employee-0001",
+		"employee": "_T-Employee-00001",
 		"from_date": "2013-01-01",
 		"to_date": "2013-12-31",
 		"leave_type": "_Test Leave Type",
@@ -11,7 +11,7 @@
 	{
 		"docstatus": 1,
 		"doctype": "Leave Allocation",
-		"employee": "_T-Employee-0002",
+		"employee": "_T-Employee-00002",
 		"from_date": "2013-01-01",
 		"to_date": "2013-12-31",
 		"leave_type": "_Test Leave Type",
diff --git a/erpnext/hr/doctype/leave_application/test_leave_application.py b/erpnext/hr/doctype/leave_application/test_leave_application.py
index 4a9a001..38b10f7 100644
--- a/erpnext/hr/doctype/leave_application/test_leave_application.py
+++ b/erpnext/hr/doctype/leave_application/test_leave_application.py
@@ -14,7 +14,7 @@
  {
   "company": "_Test Company",
   "doctype": "Leave Application",
-  "employee": "_T-Employee-0001",
+  "employee": "_T-Employee-00001",
   "from_date": "2013-05-01",
   "leave_type": "_Test Leave Type",
   "posting_date": "2013-01-02",
@@ -23,7 +23,7 @@
  {
   "company": "_Test Company",
   "doctype": "Leave Application",
-  "employee": "_T-Employee-0002",
+  "employee": "_T-Employee-00002",
   "from_date": "2013-05-01",
   "leave_type": "_Test Leave Type",
   "posting_date": "2013-01-02",
@@ -32,7 +32,7 @@
  {
   "company": "_Test Company",
   "doctype": "Leave Application",
-  "employee": "_T-Employee-0001",
+  "employee": "_T-Employee-00001",
   "from_date": "2013-01-15",
   "leave_type": "_Test Leave Type LWP",
   "posting_date": "2013-01-02",
@@ -188,7 +188,7 @@
 		application.half_day_date = application.from_date
 
 		self.assertRaises(OverlapError, application.insert)
-		
+
 	def test_overlap_with_half_day_3(self):
 		self._clear_roles()
 		self._clear_applications()
@@ -206,14 +206,14 @@
 		application.half_day = 1
 		application.half_day_date = "2013-01-05"
 		application.insert()
-		
+
 		# Apply leave from 4-7, half day on 5th
 		application = self.get_application(_test_records[0])
 		application.from_date = "2013-01-04"
 		application.to_date = "2013-01-07"
 		application.half_day = 1
 		application.half_day_date = "2013-01-05"
-		
+
 		self.assertRaises(OverlapError, application.insert)
 
 		# Apply leave from 5-7, half day on 5th
@@ -230,15 +230,15 @@
 		from frappe.utils.user import add_role
 		add_role("test1@example.com", "Employee")
 		add_role("test@example.com", "Leave Approver")
-		self._add_employee_leave_approver("_T-Employee-0002", "test@example.com")
+		self._add_employee_leave_approver("_T-Employee-00002", "test@example.com")
 
-		make_allocation_record(employee="_T-Employee-0002")
+		make_allocation_record(employee="_T-Employee-00002")
 
 		application = self.get_application(_test_records[1])
 
 		frappe.db.set_value("Leave Block List", "_Test Leave Block List",
 			"applies_to_all_departments", 1)
-		frappe.db.set_value("Employee", "_T-Employee-0002", "department",
+		frappe.db.set_value("Employee", "_T-Employee-00002", "department",
 			"_Test Department")
 
 		frappe.set_user("test1@example.com")
@@ -255,7 +255,7 @@
 
 	allocation = frappe.get_doc({
 		"doctype": "Leave Allocation",
-		"employee": employee or "_T-Employee-0001",
+		"employee": employee or "_T-Employee-00001",
 		"leave_type": leave_type or "_Test Leave Type",
 		"from_date": "2013-01-01",
 		"to_date": "2015-12-31",
diff --git a/erpnext/projects/doctype/activity_cost/test_activity_cost.py b/erpnext/projects/doctype/activity_cost/test_activity_cost.py
index 58c3f21..67d76eb 100644
--- a/erpnext/projects/doctype/activity_cost/test_activity_cost.py
+++ b/erpnext/projects/doctype/activity_cost/test_activity_cost.py
@@ -13,7 +13,7 @@
 		frappe.db.sql("delete from `tabActivity Cost`")
 		activity_cost1 = frappe.new_doc('Activity Cost')
 		activity_cost1.update({
-			"employee": "_T-Employee-0001",
+			"employee": "_T-Employee-00001",
 			"employee_name": "_Test Employee",
 			"activity_type": "_Test Activity Type 1",
 			"billing_rate": 100,
diff --git a/erpnext/projects/doctype/timesheet/test_timesheet.py b/erpnext/projects/doctype/timesheet/test_timesheet.py
index 793355e..2458db0 100644
--- a/erpnext/projects/doctype/timesheet/test_timesheet.py
+++ b/erpnext/projects/doctype/timesheet/test_timesheet.py
@@ -14,8 +14,8 @@
 
 class TestTimesheet(unittest.TestCase):
 	def test_timesheet_billing_amount(self):
-		make_salary_structure("_T-Employee-0001")
-		timesheet = make_timesheet("_T-Employee-0001", simulate=True, billable=1)
+		make_salary_structure("_T-Employee-00001")
+		timesheet = make_timesheet("_T-Employee-00001", simulate=True, billable=1)
 
 		self.assertEqual(timesheet.total_hours, 2)
 		self.assertEqual(timesheet.total_billable_hours, 2)
@@ -24,8 +24,8 @@
 		self.assertEqual(timesheet.total_billable_amount, 100)
 
 	def test_timesheet_billing_amount_not_billable(self):
-		make_salary_structure("_T-Employee-0001")
-		timesheet = make_timesheet("_T-Employee-0001", simulate=True, billable=0)
+		make_salary_structure("_T-Employee-00001")
+		timesheet = make_timesheet("_T-Employee-00001", simulate=True, billable=0)
 
 		self.assertEqual(timesheet.total_hours, 2)
 		self.assertEqual(timesheet.total_billable_hours, 0)
@@ -34,8 +34,8 @@
 		self.assertEqual(timesheet.total_billable_amount, 0)
 
 	def test_salary_slip_from_timesheet(self):
-		salary_structure = make_salary_structure("_T-Employee-0001")
-		timesheet = make_timesheet("_T-Employee-0001", simulate = True, billable=1)
+		salary_structure = make_salary_structure("_T-Employee-00001")
+		timesheet = make_timesheet("_T-Employee-00001", simulate = True, billable=1)
 		salary_slip = make_salary_slip(timesheet.name)
 		salary_slip.submit()
 
@@ -44,7 +44,7 @@
 		self.assertEqual(salary_slip.net_pay, 150)
 		self.assertEqual(salary_slip.timesheets[0].time_sheet, timesheet.name)
 		self.assertEqual(salary_slip.timesheets[0].working_hours, 2)
-		
+
 		timesheet = frappe.get_doc('Timesheet', timesheet.name)
 		self.assertEqual(timesheet.status, 'Payslip')
 		salary_slip.cancel()
@@ -53,7 +53,7 @@
 		self.assertEqual(timesheet.status, 'Submitted')
 
 	def test_sales_invoice_from_timesheet(self):
-		timesheet = make_timesheet("_T-Employee-0001", simulate=True, billable=1)
+		timesheet = make_timesheet("_T-Employee-00001", simulate=True, billable=1)
 		sales_invoice = make_sales_invoice(timesheet.name, '_Test Item', '_Test Customer')
 		sales_invoice.due_date = nowdate()
 		sales_invoice.submit()
@@ -68,7 +68,7 @@
 		self.assertEqual(item.rate, 50.00)
 
 	def test_timesheet_billing_based_on_project(self):
-		timesheet = make_timesheet("_T-Employee-0001", simulate=True, billable=1, project = '_Test Project', company='_Test Company')
+		timesheet = make_timesheet("_T-Employee-00001", simulate=True, billable=1, project = '_Test Project', company='_Test Company')
 		sales_invoice = create_sales_invoice(do_not_save=True)
 		sales_invoice.project = '_Test Project'
 		sales_invoice.submit()
@@ -85,7 +85,7 @@
 
 		update_activity_type("_Test Activity Type")
 		timesheet = frappe.new_doc("Timesheet")
-		timesheet.employee = "_T-Employee-0001"
+		timesheet.employee = "_T-Employee-00001"
 		timesheet.append(
 			'time_logs',
 			{
@@ -140,11 +140,11 @@
 			"base": 1200,
 			"from_date": add_months(nowdate(),-1)
 		})
-		
-		
+
+
 		es = salary_structure.append('earnings', {
 			"salary_component": "_Test Allowance",
-			"amount": 100 
+			"amount": 100
 		})
 
 		ds = salary_structure.append('deductions', {
diff --git a/erpnext/selling/doctype/campaign/campaign.py b/erpnext/selling/doctype/campaign/campaign.py
index 6ce457d..1094542 100644
--- a/erpnext/selling/doctype/campaign/campaign.py
+++ b/erpnext/selling/doctype/campaign/campaign.py
@@ -5,11 +5,11 @@
 import frappe
 
 from frappe.model.document import Document
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 
 class Campaign(Document):
 	def autoname(self):
 		if frappe.defaults.get_global_default('campaign_naming_by') != 'Naming Series':
 			self.name = self.campaign_name
 		else:
-			self.name = make_autoname(self.naming_series+'.#####')
+			set_name_by_naming_series(self)
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index ccf4082..78f892b 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -3,7 +3,7 @@
 
 from __future__ import unicode_literals
 import frappe
-from frappe.model.naming import make_autoname
+from frappe.model.naming import set_name_by_naming_series
 from frappe import _, msgprint, throw
 import frappe.defaults
 from frappe.utils import flt, cint, cstr
@@ -31,10 +31,7 @@
 		if cust_master_name == 'Customer Name':
 			self.name = self.get_customer_name()
 		else:
-			if not self.naming_series:
-				frappe.throw(_("Series is mandatory"), frappe.MandatoryError)
-
-			self.name = make_autoname(self.naming_series+'.#####')
+			set_name_by_naming_series(self)
 
 	def get_customer_name(self):
 		if frappe.db.get_value("Customer", self.customer_name):
diff --git a/erpnext/setup/doctype/sales_person/test_records.json b/erpnext/setup/doctype/sales_person/test_records.json
index 75d6cd3..536552a 100644
--- a/erpnext/setup/doctype/sales_person/test_records.json
+++ b/erpnext/setup/doctype/sales_person/test_records.json
@@ -1,23 +1,23 @@
 [
  {
-  "doctype": "Sales Person", 
-  "employee": "_T-Employee-0001", 
-  "is_group": 0, 
-  "parent_sales_person": "Sales Team", 
+  "doctype": "Sales Person",
+  "employee": "_T-Employee-00001",
+  "is_group": 0,
+  "parent_sales_person": "Sales Team",
   "sales_person_name": "_Test Sales Person"
- }, 
+ },
  {
-  "doctype": "Sales Person", 
-  "employee": "_T-Employee-0002", 
-  "is_group": 0, 
-  "parent_sales_person": "Sales Team", 
+  "doctype": "Sales Person",
+  "employee": "_T-Employee-00002",
+  "is_group": 0,
+  "parent_sales_person": "Sales Team",
   "sales_person_name": "_Test Sales Person 1"
- }, 
+ },
  {
-  "doctype": "Sales Person", 
-  "employee": "_T-Employee-0003", 
-  "is_group": 0, 
-  "parent_sales_person": "Sales Team", 
+  "doctype": "Sales Person",
+  "employee": "_T-Employee-00003",
+  "is_group": 0,
+  "parent_sales_person": "Sales Team",
   "sales_person_name": "_Test Sales Person 2"
  }
 ]
\ No newline at end of file
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 1c85ecd..42a63ee 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -53,8 +53,8 @@
 					template_item_name = frappe.db.get_value("Item", self.variant_of, "item_name")
 					self.item_code = make_variant_item_code(self.variant_of, template_item_name, self)
 			else:
-				from frappe.model.naming import make_autoname
-				self.item_code = make_autoname(self.naming_series + '.#####')
+				from frappe.model.naming import set_name_by_naming_series
+				set_name_by_naming_series(self)
 		elif not self.item_code:
 			msgprint(_("Item Code is mandatory because Item is not automatically numbered"), raise_exception=1)