Repository: frappe/erpnext
Pull request: [Feature] Advance Account Balance in Employee Advance
fixes #13054 
![advance_balance](https://user-images.githubusercontent.com/33081409/37241901-ca529720-2486-11e8-90e8-7e59891547c3.gif)


Problem statement:
Advance Account Balance in Employee Advance
Hi,

Please add a field in Employee Advance to show the balance on the Employee's Advance Account. This is information that the approver usually needs to see before making a decision to approve or not

Thanks

Hints from issue discussion:

Commit message: fixes codacy

Patch to review:
diff --git a/erpnext/hr/doctype/employee_advance/employee_advance.js b/erpnext/hr/doctype/employee_advance/employee_advance.js
index b6dd7eeb3f79..8ae24d3e4fa4 100644
--- a/erpnext/hr/doctype/employee_advance/employee_advance.js
+++ b/erpnext/hr/doctype/employee_advance/employee_advance.js
@@ -81,5 +81,16 @@ frappe.ui.form.on('Employee Advance', {
 				frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
 			}
 		});
+	},
+
+	employee: function (frm) {
+		return frappe.call({
+			method: "get_due_advance_amount",
+			doc: cur_frm.doc,
+			callback: function(r) {
+				frm.set_value("due_advance_amount",r.message);
+				refresh_field("due_advance_amount");
+			}
+		});
 	}
-});
+});
\ No newline at end of file
diff --git a/erpnext/hr/doctype/employee_advance/employee_advance.json b/erpnext/hr/doctype/employee_advance/employee_advance.json
index 79e49b0f6149..a919d7380a88 100644
--- a/erpnext/hr/doctype/employee_advance/employee_advance.json
+++ b/erpnext/hr/doctype/employee_advance/employee_advance.json
@@ -42,6 +42,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -73,6 +74,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -104,6 +106,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -133,6 +136,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -165,6 +169,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -194,6 +199,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -224,6 +230,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -253,6 +260,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -284,6 +292,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -315,6 +324,40 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
+   "unique": 0
+  }, 
+  {
+   "allow_bulk_edit": 0, 
+   "allow_on_submit": 0, 
+   "bold": 0, 
+   "collapsible": 0, 
+   "columns": 0, 
+   "depends_on": "eval:cur_frm.doc.employee", 
+   "fieldname": "due_advance_amount", 
+   "fieldtype": "Currency", 
+   "hidden": 0, 
+   "ignore_user_permissions": 0, 
+   "ignore_xss_filter": 0, 
+   "in_filter": 0, 
+   "in_global_search": 0, 
+   "in_list_view": 0, 
+   "in_standard_filter": 0, 
+   "label": "Due Advance Amount", 
+   "length": 0, 
+   "no_copy": 0, 
+   "options": "Company:company:default_currency", 
+   "permlevel": 0, 
+   "precision": "", 
+   "print_hide": 0, 
+   "print_hide_if_no_value": 0, 
+   "read_only": 1, 
+   "remember_last_selected_value": 0, 
+   "report_hide": 0, 
+   "reqd": 0, 
+   "search_index": 0, 
+   "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -346,6 +389,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -375,6 +419,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -406,6 +451,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -437,6 +483,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -467,6 +514,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -496,6 +544,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -527,6 +576,7 @@
    "reqd": 1, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }, 
   {
@@ -558,6 +608,7 @@
    "reqd": 0, 
    "search_index": 0, 
    "set_only_once": 0, 
+   "translatable": 0, 
    "unique": 0
   }
  ], 
@@ -571,8 +622,8 @@
  "issingle": 0, 
  "istable": 0, 
  "max_attachments": 0, 
- "modified": "2017-12-14 17:53:11.084810", 
- "modified_by": "nabinhait@gmail.com", 
+ "modified": "2018-03-10 17:09:35.969874", 
+ "modified_by": "Administrator", 
  "module": "HR", 
  "name": "Employee Advance", 
  "name_case": "", 
diff --git a/erpnext/hr/doctype/employee_advance/employee_advance.py b/erpnext/hr/doctype/employee_advance/employee_advance.py
index 28547fb28599..03365cab0e56 100644
--- a/erpnext/hr/doctype/employee_advance/employee_advance.py
+++ b/erpnext/hr/doctype/employee_advance/employee_advance.py
@@ -53,6 +53,10 @@ def set_total_advance_paid(self):
 		self.set_status()
 		frappe.db.set_value("Employee Advance", self.name , "status", self.status)
 
+	def get_due_advance_amount(self):
+		employee_due_amount = frappe.get_all("Employee Advance",filters={"employee":self.employee,"docstatus":1,"posting_date":("<=",self.posting_date)},fields=["advance_amount","paid_amount"])
+		return sum([(emp.advance_amount -emp.paid_amount) for emp in employee_due_amount])
+
 	def update_claimed_amount(self):
 		claimed_amount = frappe.db.sql("""
 			select sum(ifnull(allocated_amount, 0))


Code review comments:
diff_hunk: @@ -53,6 +53,10 @@ def set_total_advance_paid(self):
 		self.set_status()
 		frappe.db.set_value("Employee Advance", self.name , "status", self.status)
 
+	def get_due_advance_amount(self):
path: erpnext/hr/doctype/employee_advance/employee_advance.py
text: @user1:
Make it a whitelisted function. If we write it inside the class, then we need to pass the entire doc to the server, which is unnecessary. We only need employee and posting date.