Merge pull request #9896 from rohitwaghchaure/rejected_expense_claim_issue

[Fix] Expense claim status issue
diff --git a/erpnext/accounts/doctype/journal_entry/journal_entry.js b/erpnext/accounts/doctype/journal_entry/journal_entry.js
index 4880224..ba4cc83 100644
--- a/erpnext/accounts/doctype/journal_entry/journal_entry.js
+++ b/erpnext/accounts/doctype/journal_entry/journal_entry.js
@@ -96,7 +96,14 @@
 
 			// expense claim
 			if(jvd.reference_type==="Expense Claim") {
-				return {};
+				return {
+					filters: {
+						'approval_status': 'Approved',
+						'total_sanctioned_amount': ['>', 0],
+						'status': ['!=', 'Paid'],
+						'docstatus': 1
+					}
+				};
 			}
 
 			// journal entry
diff --git a/erpnext/hr/doctype/expense_claim/expense_claim.json b/erpnext/hr/doctype/expense_claim/expense_claim.json
index 48dcfbb..5708c04 100644
--- a/erpnext/hr/doctype/expense_claim/expense_claim.json
+++ b/erpnext/hr/doctype/expense_claim/expense_claim.json
@@ -905,7 +905,7 @@
    "label": "Status", 
    "length": 0, 
    "no_copy": 1, 
-   "options": "Draft\nPaid\nUnpaid\nSubmitted\nCancelled", 
+   "options": "Draft\nPaid\nUnpaid\nRejected\nSubmitted\nCancelled", 
    "permlevel": 0, 
    "precision": "", 
    "print_hide": 1, 
@@ -964,7 +964,7 @@
  "istable": 0, 
  "max_attachments": 0, 
  "menu_index": 0, 
- "modified": "2017-06-13 14:29:16.914609", 
+ "modified": "2017-07-17 15:47:23.255142", 
  "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Expense Claim", 
diff --git a/erpnext/hr/doctype/expense_claim/expense_claim.py b/erpnext/hr/doctype/expense_claim/expense_claim.py
index 1cd7257..2781f28 100644
--- a/erpnext/hr/doctype/expense_claim/expense_claim.py
+++ b/erpnext/hr/doctype/expense_claim/expense_claim.py
@@ -39,10 +39,13 @@
 			"2": "Cancelled"
 		}[cstr(self.docstatus or 0)]
 
-		if self.total_sanctioned_amount == self.total_amount_reimbursed and self.docstatus == 1:
+		if self.total_sanctioned_amount > 0 and self.total_sanctioned_amount == self.total_amount_reimbursed \
+			and self.docstatus == 1 and self.approval_status == 'Approved':
 			self.status = "Paid"
-		elif self.docstatus == 1:
+		elif self.total_sanctioned_amount > 0 and self.docstatus == 1 and self.approval_status == 'Approved':
 			self.status = "Unpaid"
+		elif self.docstatus == 1 and self.approval_status == 'Rejected':
+			self.status = 'Rejected'
 
 	def set_payable_account(self):
 		if not self.payable_account and not self.is_paid:
@@ -157,6 +160,9 @@
 		self.total_claimed_amount = 0
 		self.total_sanctioned_amount = 0
 		for d in self.get('expenses'):
+			if self.approval_status == 'Rejected':
+				d.sanctioned_amount = 0.0
+
 			self.total_claimed_amount += flt(d.claim_amount)
 			self.total_sanctioned_amount += flt(d.sanctioned_amount)
 
diff --git a/erpnext/hr/doctype/expense_claim/expense_claim_list.js b/erpnext/hr/doctype/expense_claim/expense_claim_list.js
index 7cff8e2..f5a6f4c 100644
--- a/erpnext/hr/doctype/expense_claim/expense_claim_list.js
+++ b/erpnext/hr/doctype/expense_claim/expense_claim_list.js
@@ -4,8 +4,10 @@
 	get_indicator: function(doc) {
 		if(doc.status == "Paid") {
 			return [__("Paid"), "green", "status,=,'Paid'"];
-		} else {
+		}else if(doc.status == "Unpaid") {
 			return [__("Unpaid"), "orange"];
+		} else if(doc.status == "Rejected") {
+			return [__("Rejected"), "grey"];
 		}
 	}
 };
diff --git a/erpnext/hr/doctype/expense_claim/test_expense_claim.py b/erpnext/hr/doctype/expense_claim/test_expense_claim.py
index 20df7be..e8c24bb 100644
--- a/erpnext/hr/doctype/expense_claim/test_expense_claim.py
+++ b/erpnext/hr/doctype/expense_claim/test_expense_claim.py
@@ -113,5 +113,23 @@
 			self.assertEquals(expected_values[gle.account][1], gle.debit)
 			self.assertEquals(expected_values[gle.account][2], gle.credit)
 
+	def test_rejected_expense_claim(self):
+		payable_account = get_payable_account("Wind Power LLC")
+		expense_claim = frappe.get_doc({
+			 "doctype": "Expense Claim",
+			 "employee": "_T-Employee-0001",
+			 "payable_account": payable_account,
+			 "approval_status": "Rejected",
+			 "expenses":
+			 	[{ "expense_type": "Travel", "default_account": "Travel Expenses - WP", "claim_amount": 300, "sanctioned_amount": 200 }]
+		})
+		expense_claim.submit()
+
+		self.assertEquals(expense_claim.status, 'Rejected')
+		self.assertEquals(expense_claim.total_sanctioned_amount, 0.0)
+
+		gl_entry = frappe.get_all('GL Entry', {'voucher_type': 'Expense Claim', 'voucher_no': expense_claim.name})
+		self.assertEquals(len(gl_entry), 0)
+
 def get_payable_account(company):
 	return frappe.db.get_value('Company', company, 'default_payable_account')
diff --git a/erpnext/patches.txt b/erpnext/patches.txt
index d68f9f2..70d6744 100644
--- a/erpnext/patches.txt
+++ b/erpnext/patches.txt
@@ -419,4 +419,5 @@
 erpnext.patches.v8_1.update_gst_state #17-07-2017
 erpnext.patches.v8_1.removed_report_support_hours
 erpnext.patches.v8_1.add_indexes_in_transaction_doctypes
+erpnext.patches.v8_1.update_expense_claim_status
 erpnext.patches.v8_3.update_company_total_sales
\ No newline at end of file
diff --git a/erpnext/patches/v8_1/update_expense_claim_status.py b/erpnext/patches/v8_1/update_expense_claim_status.py
new file mode 100644
index 0000000..4c1b85a
--- /dev/null
+++ b/erpnext/patches/v8_1/update_expense_claim_status.py
@@ -0,0 +1,23 @@
+# Copyright (c) 2017, Frappe and Contributors
+# License: GNU General Public License v3. See license.txt
+
+from __future__ import unicode_literals
+import frappe
+
+def execute():
+	frappe.reload_doctype('Expense Claim')
+
+	for data in frappe.db.sql(""" select name from `tabExpense Claim`
+		where (docstatus=1 and total_sanctioned_amount=0 and status = 'Paid') or 
+		(docstatus = 1 and approval_status = 'Rejected' and total_sanctioned_amount > 0)""", as_dict=1):
+		doc = frappe.get_doc('Expense Claim', data.name)
+		if doc.approval_status == 'Rejected':
+			for d in doc.expenses:
+				d.db_set("sanctioned_amount", 0, update_modified = False)
+			doc.db_set("total_sanctioned_amount", 0, update_modified = False)
+
+			frappe.db.sql(""" delete from `tabGL Entry` where voucher_type = 'Expense Claim'
+				and voucher_no = %s""", (doc.name))
+
+		doc.set_status()
+		doc.db_set("status", doc.status, update_modified = False)
\ No newline at end of file