Merge pull request #2958 from anandpdoshi/anand-mar-13

manipulate grand total for inclusive tax
diff --git a/erpnext/controllers/accounts_controller.py b/erpnext/controllers/accounts_controller.py
index fcc350a..431eab5 100644
--- a/erpnext/controllers/accounts_controller.py
+++ b/erpnext/controllers/accounts_controller.py
@@ -185,6 +185,7 @@
 
 		self.calculate_net_total()
 		self.calculate_taxes()
+		self.manipulate_grand_total_for_inclusive_tax()
 		self.calculate_totals()
 		self._cleanup()
 
@@ -353,6 +354,19 @@
 			self.precision(base_field, item))
 		item.set(base_field, value_in_company_currency)
 
+	def manipulate_grand_total_for_inclusive_tax(self):
+		# if fully inclusive taxes and diff
+		if (self.meta.get_field("net_total_export") and self.tax_doclist
+			and all(cint(t.included_in_print_rate) for t in self.tax_doclist)):
+
+			last_tax = self.tax_doclist[-1]
+
+			diff = self.net_total_export - flt(last_tax.total / self.conversion_rate,
+				self.precision("grand_total_export"))
+
+			if diff and abs(diff) <= (2.0 / 10**(self.precision("total", last_tax))):
+				last_tax.total = last_tax.total + flt(diff * self.conversion_rate, self.precision("total", last_tax))
+
 	def calculate_total_advance(self, parenttype, advance_parentfield):
 		if self.doctype == parenttype and self.docstatus < 2:
 			sum_of_allocated_amount = sum([flt(adv.allocated_amount, self.precision("allocated_amount", adv))
diff --git a/erpnext/public/js/transaction.js b/erpnext/public/js/transaction.js
index ebdb136..ba0fa40 100644
--- a/erpnext/public/js/transaction.js
+++ b/erpnext/public/js/transaction.js
@@ -632,6 +632,7 @@
 		this.determine_exclusive_rate && this.determine_exclusive_rate();
 		this.calculate_net_total();
 		this.calculate_taxes();
+		this.manipulate_grand_total_for_inclusive_tax && this.manipulate_grand_total_for_inclusive_tax();
 		this.calculate_totals();
 		this._cleanup();
 
diff --git a/erpnext/selling/sales_common.js b/erpnext/selling/sales_common.js
index 1c5df6e..f440315 100644
--- a/erpnext/selling/sales_common.js
+++ b/erpnext/selling/sales_common.js
@@ -458,6 +458,26 @@
 			});
 	},
 
+	manipulate_grand_total_for_inclusive_tax: function() {
+		// if fully inclusive taxes and diff
+		if (this.frm.tax_doclist.length) {
+			var all_inclusive = frappe.utils.all(this.frm.tax_doclist.map(function(d) {
+				return cint(d.included_in_print_rate);
+			}));
+
+			if (all_inclusive) {
+				var last_tax = this.frm.tax_doclist.slice(-1)[0];
+
+				var diff = this.frm.doc.net_total_export
+					- flt(last_tax.total / this.frm.doc.conversion_rate, precision("grand_total_export"));
+
+				if ( diff && Math.abs(diff) <= (2.0 / Math.pow(10, precision("total", last_tax)) ) {
+					last_tax.total += flt(diff * this.frm.doc.conversion_rate, precision("total", last_tax));
+				}
+			}
+		}
+	},
+
 	_cleanup: function() {
 		this._super();
 		this.frm.doc.in_words = this.frm.doc.in_words_export = "";