Merge pull request #6774 from KanchanChauhan/salarystruct-process-payroll

[Fix] Error in pulling in Salary Structure fixed
diff --git a/erpnext/hr/doctype/process_payroll/process_payroll.js b/erpnext/hr/doctype/process_payroll/process_payroll.js
index 6c057e1..2ab6aef 100644
--- a/erpnext/hr/doctype/process_payroll/process_payroll.js
+++ b/erpnext/hr/doctype/process_payroll/process_payroll.js
@@ -82,7 +82,7 @@
 cur_frm.cscript.submit_salary_slip = function(doc, cdt, cdn) {
 	cur_frm.cscript.display_activity_log("");
 
-	frappe.confirm(__("Do you really want to Submit all Salary Slip for Account {0} from {1} to {2}", [doc.payment_account, doc.from_date, doc.to_date]), function() {
+	frappe.confirm(__("Do you really want to Submit all Salary Slip from {0} to {1}", [doc.from_date, doc.to_date]), function() {
 		// clear all in locals
 		if(locals["Salary Slip"]) {
 			$.each(locals["Salary Slip"], function(name, d) {
diff --git a/erpnext/hr/doctype/process_payroll/process_payroll.py b/erpnext/hr/doctype/process_payroll/process_payroll.py
index f31ddfb..3554669 100644
--- a/erpnext/hr/doctype/process_payroll/process_payroll.py
+++ b/erpnext/hr/doctype/process_payroll/process_payroll.py
@@ -22,9 +22,9 @@
 
 		sal_struct = frappe.db.sql("""
 				select name from `tabSalary Structure`
-				where docstatus != 2 and payment_account = '%(payment_account)s' and 
-				ifnull(salary_slip_based_on_timesheet,0) = %(salary_slip_based_on_timesheet)s"""% 
-				{"payment_account": self.payment_account, "salary_slip_based_on_timesheet":self.salary_slip_based_on_timesheet})
+				where docstatus != 2 and company = %(company)s and
+				ifnull(salary_slip_based_on_timesheet,0) = %(salary_slip_based_on_timesheet)s""",
+				{"company": self.company, "salary_slip_based_on_timesheet":self.salary_slip_based_on_timesheet})
 		
 		if sal_struct:
 			cond += "and t2.parent IN %(sal_struct)s "
@@ -82,6 +82,7 @@
 							"start_date": self.from_date,
 							"end_date": self.to_date,
 							"employee": emp[0],
+							"employee_name": frappe.get_value("Employee", {"name":emp[0]}, "employee_name"),
 							"company": self.company,
 							"posting_date": self.posting_date
 						})
@@ -92,6 +93,7 @@
 							"fiscal_year": self.fiscal_year,
 							"month": self.month,
 							"employee": emp[0],
+							"employee_name": frappe.get_value("Employee", {"name":emp[0]}, "employee_name"),
 							"company": self.company,
 							"posting_date": self.posting_date
 						})	
@@ -222,8 +224,8 @@
 	
 	def make_journal_entry(self, reference_number = None, reference_date = None):
 		self.check_permission('write')
-		earnings = self.get_salary_component_total(component_type = "earnings")
-		deductions = self.get_salary_component_total(component_type = "deductions")
+		earnings = self.get_salary_component_total(component_type = "earnings") or {}
+		deductions = self.get_salary_component_total(component_type = "deductions") or {}
 		jv_name = ""
 
 		if earnings or deductions:
diff --git a/erpnext/hr/doctype/process_payroll/test_process_payroll.py b/erpnext/hr/doctype/process_payroll/test_process_payroll.py
index 4e3a588..3be4b4c 100644
--- a/erpnext/hr/doctype/process_payroll/test_process_payroll.py
+++ b/erpnext/hr/doctype/process_payroll/test_process_payroll.py
@@ -12,7 +12,12 @@
 	def test_process_payroll(self):
 		month = "11"
 		fiscal_year = "_Test Fiscal Year 2016"
-		payment_account = frappe.get_all("Account")[0].name
+
+		for data in frappe.get_all('Salary Component', fields = ["name"]):
+			if not frappe.db.get_value('Salary Component Account', {'parent': data.name, 'company': erpnext.get_default_company()}, 'name'):
+				get_salary_component_account(data.name)
+				
+		payment_account = frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name")
 		if not frappe.db.get_value("Salary Slip", {"fiscal_year": fiscal_year, "month": month}):
 			process_payroll = frappe.get_doc("Process Payroll", "Process Payroll")
 			process_payroll.company = erpnext.get_default_company()
@@ -21,7 +26,27 @@
 			process_payroll.from_date = "2016-11-01"
 			process_payroll.to_date = "2016-11-30"
 			process_payroll.payment_account = payment_account
+			process_payroll.posting_date = nowdate()
 			process_payroll.create_sal_slip()
 			process_payroll.submit_salary_slip()
 			if process_payroll.get_sal_slip_list(ss_status = 1):
-				r = process_payroll.make_journal_entry(reference_number=random_string(10),reference_date=nowdate())
\ No newline at end of file
+				r = process_payroll.make_journal_entry(reference_number=random_string(10),reference_date=nowdate())
+	
+
+def get_salary_component_account(sal_comp):
+	company = erpnext.get_default_company()
+	sal_comp = frappe.get_doc("Salary Component", sal_comp)
+	sc = sal_comp.append("accounts")
+	sc.company = company
+	sc.default_account = create_account(company)
+	
+def create_account(company):
+	salary_account = frappe.db.get_value("Account", "Salary - " + frappe.db.get_value('Company', company, 'abbr'))
+	if not salary_account:
+		frappe.get_doc({
+		"doctype": "Account",
+		"account_name": "Salary",
+		"parent_account": "Indirect Expenses - " + frappe.db.get_value('Company', company, 'abbr'),
+		"company": company
+		}).insert()
+	return salary_account
\ No newline at end of file
diff --git a/erpnext/hr/doctype/salary_slip/salary_slip.py b/erpnext/hr/doctype/salary_slip/salary_slip.py
index e101c3d..1bceff7 100644
--- a/erpnext/hr/doctype/salary_slip/salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/salary_slip.py
@@ -168,7 +168,7 @@
 			return st_name and st_name[0][0] or ''
 		else:
 			self.salary_structure = None
-			frappe.throw(_("No active or default Salary Structure found for employee {0} for the given dates")
+			frappe.msgprint(_("No active or default Salary Structure found for employee {0} for the given dates")
 				.format(self.employee), title=_('Salary Structure Missing'))	
 
 	def pull_sal_struct(self):
diff --git a/erpnext/hr/doctype/salary_slip/test_salary_slip.py b/erpnext/hr/doctype/salary_slip/test_salary_slip.py
index 8bb9653..8cf0688 100644
--- a/erpnext/hr/doctype/salary_slip/test_salary_slip.py
+++ b/erpnext/hr/doctype/salary_slip/test_salary_slip.py
@@ -8,11 +8,11 @@
 from frappe.utils.make_random import get_random
 from frappe.utils import today, now_datetime, getdate, cstr, add_years, nowdate
 from erpnext.hr.doctype.salary_structure.salary_structure import make_salary_slip
-from erpnext.hr.doctype.leave_application.test_leave_application import make_allocation_record
+from erpnext.hr.doctype.process_payroll.test_process_payroll import get_salary_component_account
 
 class TestSalarySlip(unittest.TestCase):
 	def setUp(self):
-		self.make_salary_component(["Basic Salary", "Allowance", "HRA", "Professional Tax", "TDS"])
+		make_salary_component(["Basic Salary", "Allowance", "HRA", "Professional Tax", "TDS"])
 		
 		for dt in ["Leave Application", "Leave Allocation", "Salary Slip"]:
 			frappe.db.sql("delete from `tab%s`" % dt)
@@ -160,16 +160,7 @@
 			}).insert()	
 			holiday_list.get_weekly_off_dates()
 			holiday_list.save()
-			
-	def make_salary_component(self, salary_components):
-		for salary_component in salary_components:
-			if not frappe.db.exists('Salary Component', salary_component):
-				sal_comp = frappe.get_doc({
-					"doctype": "Salary Component",
-					"salary_component": salary_component
-				})
-				sal_comp.insert()		
-		
+				
 	def make_employee_salary_slip(self, user):
 		employee = frappe.db.get_value("Employee", {"user_id": user})
 		salary_structure = make_salary_structure("Salary Structure Test for Salary Slip")
@@ -194,6 +185,15 @@
 		activity_type.wage_rate = 25
 		activity_type.save()
 
+def make_salary_component(salary_components):
+	for salary_component in salary_components:
+		if not frappe.db.exists('Salary Component', salary_component):
+			sal_comp = frappe.get_doc({
+				"doctype": "Salary Component",
+				"salary_component": salary_component
+			})
+			sal_comp.insert()
+		get_salary_component_account(salary_component)
 
 def make_salary_structure(sal_struct):
 	if not frappe.db.exists('Salary Structure', sal_struct):
@@ -205,7 +205,7 @@
 			"employees": get_employee_details(),
 			"earnings": get_earnings_component(),
 			"deductions": get_deductions_component(),
-			"payment_account": get_random("Account")		
+			"payment_account": frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name")
 		}).insert()
 	return sal_struct
 			
diff --git a/erpnext/hr/doctype/salary_structure/test_salary_structure.py b/erpnext/hr/doctype/salary_structure/test_salary_structure.py
index cd7ee47..81f6743 100644
--- a/erpnext/hr/doctype/salary_structure/test_salary_structure.py
+++ b/erpnext/hr/doctype/salary_structure/test_salary_structure.py
@@ -8,6 +8,7 @@
 from frappe.utils.make_random import get_random
 from frappe.utils import nowdate, add_days, add_years
 from erpnext.hr.doctype.salary_structure.salary_structure import make_salary_slip
+from erpnext.hr.doctype.salary_slip.test_salary_slip import make_salary_component
 # test_records = frappe.get_test_records('Salary Structure')
 
 class TestSalaryStructure(unittest.TestCase):
@@ -23,7 +24,7 @@
 			
 		self.make_holiday_list()
 		frappe.db.set_value("Company", erpnext.get_default_company(), "default_holiday_list", "Salary Structure Test Holiday List")
-		self.make_salary_component(["Basic Salary", "Allowance", "HRA", "Professional Tax", "TDS"])
+		make_salary_component(["Basic Salary", "Allowance", "HRA", "Professional Tax", "TDS"])
 		employee1 = self.make_employee("test_employee@salary.com")
 		employee2 = self.make_employee("test_employee_2@salary.com")
 		
@@ -69,17 +70,6 @@
 			return emp.name
 		else:
 			return frappe.get_value("Employee", {"employee_name":user}, "name")
-	
-	def make_salary_component(self, salary_components):
-		for salary_component in salary_components:
-			if not frappe.db.exists('Salary Component', salary_component):
-				sal_comp = frappe.get_doc({
-					"doctype": "Salary Component",
-					"salary_component": salary_component
-				})
-				sal_comp.insert()
-				
-
 		
 	def test_amount_totals(self):
 		sal_slip = frappe.get_value("Salary Slip", {"employee_name":"test_employee@salary.com"})
@@ -114,7 +104,7 @@
 			"employees": get_employee_details(),
 			"earnings": get_earnings_component(),
 			"deductions": get_deductions_component(),
-			"payment_account": frappe.get_all("Account")[0].name
+			"payment_account": frappe.get_value('Account', {'account_type': 'Cash', 'company': erpnext.get_default_company(),'is_group':0}, "name")
 		}).insert()
 	return sal_struct