Merge pull request #4316 from nabinhait/advance_fix

[fix] Invoice Outstanding calculation related to advance
diff --git a/erpnext/controllers/taxes_and_totals.py b/erpnext/controllers/taxes_and_totals.py
index 27ec618..b348da6 100644
--- a/erpnext/controllers/taxes_and_totals.py
+++ b/erpnext/controllers/taxes_and_totals.py
@@ -398,17 +398,18 @@
 			return
 		
 		self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
-		total_amount_to_pay = flt(self.doc.grand_total  - self.doc.total_advance - self.doc.write_off_amount,
-			self.doc.precision("grand_total"))
+		if self.doc.party_account_currency == self.doc.currency:
+			total_amount_to_pay = flt(self.doc.grand_total  - self.doc.total_advance 
+				- flt(self.doc.write_off_amount), self.doc.precision("grand_total"))
+		else:
+			total_amount_to_pay = flt(self.doc.base_grand_total  - self.doc.total_advance 
+				- flt(self.doc.base_write_off_amount), self.doc.precision("grand_total"))
 			
 		if self.doc.doctype == "Sales Invoice":
 			self.doc.round_floats_in(self.doc, ["paid_amount"])
-			outstanding_amount = flt(total_amount_to_pay - self.doc.paid_amount, self.doc.precision("outstanding_amount"))
+			paid_amount = self.doc.paid_amount \
+				if self.doc.party_account_currency == self.doc.currency else self.doc.base_paid_amount
+			self.doc.outstanding_amount = flt(total_amount_to_pay - flt(paid_amount), 
+				self.doc.precision("outstanding_amount"))
 		elif self.doc.doctype == "Purchase Invoice":
-			outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
-		
-		if self.doc.party_account_currency == self.doc.currency:
-			self.doc.outstanding_amount = outstanding_amount
-		else:
-			self.doc.outstanding_amount = flt(outstanding_amount * self.doc.conversion_rate, 
-				self.doc.precision("outstanding_amount"))
\ No newline at end of file
+			self.doc.outstanding_amount = flt(total_amount_to_pay, self.doc.precision("outstanding_amount"))
\ No newline at end of file
diff --git a/erpnext/public/js/controllers/taxes_and_totals.js b/erpnext/public/js/controllers/taxes_and_totals.js
index b1b24cf..413c7ae 100644
--- a/erpnext/public/js/controllers/taxes_and_totals.js
+++ b/erpnext/public/js/controllers/taxes_and_totals.js
@@ -500,9 +500,13 @@
 		if(this.frm.doc.is_return || this.frm.doc.docstatus > 0) return;
 		
 		frappe.model.round_floats_in(this.frm.doc, ["grand_total", "total_advance", "write_off_amount"]);
-		
-		var total_amount_to_pay = flt((this.frm.doc.grand_total - this.frm.doc.total_advance 
-			- this.frm.doc.write_off_amount), precision("grand_total"));
+		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {	
+			var total_amount_to_pay = flt((this.frm.doc.grand_total - this.frm.doc.total_advance 
+				- this.frm.doc.write_off_amount), precision("grand_total"));
+		else {
+			var total_amount_to_pay = flt((this.frm.doc.base_grand_total - this.frm.doc.total_advance 
+				- this.frm.doc.base_write_off_amount), precision("base_grand_total"));
+		}
 		
 		if(this.frm.doc.doctype == "Sales Invoice") {
 			frappe.model.round_floats_in(this.frm.doc, ["paid_amount"]);
@@ -518,18 +522,15 @@
 			this.frm.refresh_field("paid_amount");
 			this.frm.refresh_field("base_paid_amount");
 			
-			var outstanding_amount =  flt(total_amount_to_pay - this.frm.doc.paid_amount, 
+			var paid_amount = (this.frm.doc.party_account_currency == this.frm.doc.currency) ? 
+				this.frm.doc.paid_amount : this.frm.doc.base_paid_amount;
+			
+			var outstanding_amount =  flt(total_amount_to_pay - flt(paid_amount), 
 				precision("outstanding_amount"));
 				
 		} else if(this.frm.doc.doctype == "Purchase Invoice") {
 			var outstanding_amount = flt(total_amount_to_pay, precision("outstanding_amount"));
-		}
-		
-		if(this.frm.doc.party_account_currency == this.frm.doc.currency) {	
-			this.frm.set_value("outstanding_amount", outstanding_amount);
-		} else {
-			this.frm.set_value("outstanding_amount", 
-				flt(outstanding_amount * this.frm.doc.conversion_rate, precision("outstanding_amount")));
-		}
+		}		
+		this.frm.set_value("outstanding_amount", outstanding_amount);
 	}
 })