[fix] [selling] reduce round-off loss when using tax inclusive price
diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py
index 38cf5c3..95bbf67 100644
--- a/accounts/doctype/sales_invoice/test_sales_invoice.py
+++ b/accounts/doctype/sales_invoice/test_sales_invoice.py
@@ -161,7 +161,7 @@
 		expected_values = {
 			"keys": ["ref_rate", "adj_rate", "export_rate", "export_amount", 
 				"base_ref_rate", "basic_rate", "amount"],
-			"_Test Item Home Desktop 100": [62.5, 0, 62.5, 625.0, 50, 50, 500],
+			"_Test Item Home Desktop 100": [62.5, 0, 62.5, 625.0, 50, 50, 499.98],
 			"_Test Item Home Desktop 200": [190.66, 0, 190.66, 953.3, 150, 150, 750],
 		}
 		
@@ -175,28 +175,28 @@
 				self.assertEquals(d.fields.get(k), expected_values[d.item_code][i])
 		
 		# check net total
-		self.assertEquals(si.doc.net_total, 1250)
+		self.assertEquals(si.doc.net_total, 1249.98)
 		self.assertEquals(si.doc.net_total_export, 1578.3)
 		
 		# check tax calculation
 		expected_values = {
 			"keys": ["tax_amount", "total"],
-			"_Test Account Excise Duty - _TC": [140, 1390],
-			"_Test Account Education Cess - _TC": [2.8, 1392.8],
-			"_Test Account S&H Education Cess - _TC": [1.4, 1394.2],
-			"_Test Account CST - _TC": [27.88, 1422.08],
-			"_Test Account VAT - _TC": [156.25, 1578.33],
-			"_Test Account Customs Duty - _TC": [125, 1703.33],
-			"_Test Account Shipping Charges - _TC": [100, 1803.33],
-			"_Test Account Discount - _TC": [-180.33, 1623]
+			"_Test Account Excise Duty - _TC": [140, 1389.98],
+			"_Test Account Education Cess - _TC": [2.8, 1392.78],
+			"_Test Account S&H Education Cess - _TC": [1.4, 1394.18],
+			"_Test Account CST - _TC": [27.88, 1422.06],
+			"_Test Account VAT - _TC": [156.25, 1578.31],
+			"_Test Account Customs Duty - _TC": [125, 1703.31],
+			"_Test Account Shipping Charges - _TC": [100, 1803.31],
+			"_Test Account Discount - _TC": [-180.33, 1622.98]
 		}
 		
 		for d in si.doclist.get({"parentfield": "other_charges"}):
 			for i, k in enumerate(expected_values["keys"]):
 				self.assertEquals(flt(d.fields.get(k), 6), expected_values[d.account_head][i])
 		
-		self.assertEquals(si.doc.grand_total, 1623)
-		self.assertEquals(si.doc.grand_total_export, 1623)
+		self.assertEquals(si.doc.grand_total, 1622.98)
+		self.assertEquals(si.doc.grand_total_export, 1622.98)
 		
 	def test_sales_invoice_calculation_export_currency_with_tax_inclusive_price(self):
 		# prepare
@@ -215,8 +215,8 @@
 		expected_values = {
 			"keys": ["ref_rate", "adj_rate", "export_rate", "export_amount", 
 				"base_ref_rate", "basic_rate", "amount"],
-			"_Test Item Home Desktop 100": [55.56, 10, 50, 500, 2222.11, 1999.9, 19999.0],
-			"_Test Item Home Desktop 200": [187.5, 20, 150, 750, 7375.66, 5900.53, 29502.65],
+			"_Test Item Home Desktop 100": [55.56, 10, 50, 500, 2222.11, 1999.9, 19999.04],
+			"_Test Item Home Desktop 200": [187.5, 20, 150, 750, 7375.66, 5900.53, 29502.66],
 		}
 		
 		# check if children are saved
@@ -229,27 +229,27 @@
 				self.assertEquals(d.fields.get(k), expected_values[d.item_code][i])
 		
 		# check net total
-		self.assertEquals(si.doc.net_total, 49501.65)
+		self.assertEquals(si.doc.net_total, 49501.7)
 		self.assertEquals(si.doc.net_total_export, 1250)
 		
 		# check tax calculation
 		expected_values = {
 			"keys": ["tax_amount", "total"],
-			"_Test Account Excise Duty - _TC": [5540.22, 55041.87],
-			"_Test Account Education Cess - _TC": [110.81, 55152.68],
-			"_Test Account S&H Education Cess - _TC": [55.4, 55208.08],
-			"_Test Account CST - _TC": [1104.16, 56312.24],
-			"_Test Account VAT - _TC": [6187.71, 62499.95],
-			"_Test Account Customs Duty - _TC": [4950.17, 67450.12],
-			"_Test Account Shipping Charges - _TC": [5000, 72450.12],
-			"_Test Account Discount - _TC": [-7245.01, 65205.11]
+			"_Test Account Excise Duty - _TC": [5540.22, 55041.92],
+			"_Test Account Education Cess - _TC": [110.81, 55152.73],
+			"_Test Account S&H Education Cess - _TC": [55.4, 55208.13],
+			"_Test Account CST - _TC": [1104.16, 56312.29],
+			"_Test Account VAT - _TC": [6187.71, 62500],
+			"_Test Account Customs Duty - _TC": [4950.17, 67450.17],
+			"_Test Account Shipping Charges - _TC": [5000, 72450.17],
+			"_Test Account Discount - _TC": [-7245.01, 65205.16]
 		}
 		
 		for d in si.doclist.get({"parentfield": "other_charges"}):
 			for i, k in enumerate(expected_values["keys"]):
 				self.assertEquals(flt(d.fields.get(k), 6), expected_values[d.account_head][i])
 		
-		self.assertEquals(si.doc.grand_total, 65205.11)
+		self.assertEquals(si.doc.grand_total, 65205.16)
 		self.assertEquals(si.doc.grand_total_export, 1304.1)
 
 	def test_outstanding(self):
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 46449ed..eb2e9ef 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -150,10 +150,10 @@
 				cumulated_tax_fraction += tax.tax_fraction_for_current_item
 			
 			if cumulated_tax_fraction:
-				item.basic_rate = flt((item.export_rate * self.doc.conversion_rate) / 
-					(1 + cumulated_tax_fraction), self.precision("basic_rate", item))
-				
-				item.amount = flt(item.basic_rate * item.qty, self.precision("amount", item))
+				item.amount = flt((item.export_amount * self.doc.conversion_rate) /
+					(1 + cumulated_tax_fraction), self.precision("amount", item))
+					
+				item.basic_rate = flt(item.amount / item.qty, self.precision("basic_rate", item))
 				
 				if item.adj_rate == 100:
 					item.base_ref_rate = item.basic_rate
diff --git a/selling/doctype/sales_common/sales_common.js b/selling/doctype/sales_common/sales_common.js
index 0c4edd4..80b1d95 100644
--- a/selling/doctype/sales_common/sales_common.js
+++ b/selling/doctype/sales_common/sales_common.js
@@ -359,11 +359,11 @@
 			});
 			
 			if(cumulated_tax_fraction) {
-				item.basic_rate = flt(
-					(item.export_rate * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
-					precision("basic_rate", item));
-				
-				item.amount = flt(item.basic_rate * item.qty, precision("amount", item));
+				item.amount = flt(
+					(item.export_amount * me.frm.doc.conversion_rate) / (1 + cumulated_tax_fraction),
+					precision("amount", item));
+					
+				item.basic_rate = flt(item.amount / item.qty, precision("basic_rate", item));
 				
 				if(item.adj_rate == 100) {
 					item.base_ref_rate = item.basic_rate;