Merge pull request #1326 from nabinhait/hotfix

Calculate taxes and charges total in server side
diff --git a/buying/doctype/purchase_common/purchase_common.js b/buying/doctype/purchase_common/purchase_common.js
index 14b8279..e3957ab 100644
--- a/buying/doctype/purchase_common/purchase_common.js
+++ b/buying/doctype/purchase_common/purchase_common.js
@@ -302,11 +302,11 @@
 	
 	calculate_totals: function() {
 		var tax_count = this.frm.tax_doclist.length;
-		this.frm.doc.grand_total = flt(
-			tax_count ? this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
+		this.frm.doc.grand_total = flt(tax_count ? 
+			this.frm.tax_doclist[tax_count - 1].total : this.frm.doc.net_total,
 			precision("grand_total"));
-		this.frm.doc.grand_total_import = flt(this.frm.doc.grand_total / this.frm.doc.conversion_rate,
-			precision("grand_total_import"));
+		this.frm.doc.grand_total_import = flt(this.frm.doc.grand_total / 
+			this.frm.doc.conversion_rate, precision("grand_total_import"));
 			
 		this.frm.doc.total_tax = flt(this.frm.doc.grand_total - this.frm.doc.net_total,
 			precision("total_tax"));
@@ -321,20 +321,26 @@
 		}
 		
 		// other charges added/deducted
+		this.frm.doc.other_charges_added = 0.0
+		this.frm.doc.other_charges_deducted = 0.0
 		if(tax_count) {
 			this.frm.doc.other_charges_added = wn.utils.sum($.map(this.frm.tax_doclist, 
-				function(tax) { return (tax.add_deduct_tax == "Add" && in_list(["Valuation and Total", "Total"], tax.category)) ? tax.tax_amount : 0.0; }));
+				function(tax) { return (tax.add_deduct_tax == "Add" 
+					&& in_list(["Valuation and Total", "Total"], tax.category)) ? 
+					tax.tax_amount : 0.0; }));
 		
 			this.frm.doc.other_charges_deducted = wn.utils.sum($.map(this.frm.tax_doclist, 
-				function(tax) { return (tax.add_deduct_tax == "Deduct" && in_list(["Valuation and Total", "Total"], tax.category)) ? tax.tax_amount : 0.0; }));
+				function(tax) { return (tax.add_deduct_tax == "Deduct" 
+					&& in_list(["Valuation and Total", "Total"], tax.category)) ? 
+					tax.tax_amount : 0.0; }));
 			
-			wn.model.round_floats_in(this.frm.doc, ["other_charges_added", "other_charges_deducted"]);
-			
-			this.frm.doc.other_charges_added_import = flt(this.frm.doc.other_charges_added / this.frm.doc.conversion_rate,
-				precision("other_charges_added_import"));
-			this.frm.doc.other_charges_deducted_import = flt(this.frm.doc.other_charges_deducted / this.frm.doc.conversion_rate,
-				precision("other_charges_deducted_import"));
+			wn.model.round_floats_in(this.frm.doc,
+				["other_charges_added", "other_charges_deducted"]);
 		}
+		this.frm.doc.other_charges_added_import = flt(this.frm.doc.other_charges_added /
+			this.frm.doc.conversion_rate, precision("other_charges_added_import"));
+		this.frm.doc.other_charges_deducted_import = flt(this.frm.doc.other_charges_deducted / 
+			this.frm.doc.conversion_rate, precision("other_charges_deducted_import"));
 	},
 	
 	_cleanup: function() {
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index 7954ca0..bdc7327 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -124,8 +124,8 @@
 		self.round_floats_in(self.doc, ["net_total", "net_total_import"])
 		
 	def calculate_totals(self):
-		self.doc.grand_total = flt(self.tax_doclist and \
-			self.tax_doclist[-1].total or self.doc.net_total, self.precision("grand_total"))
+		self.doc.grand_total = flt(self.tax_doclist[-1].total if self.tax_doclist 
+			else self.doc.net_total, self.precision("grand_total"))
 		self.doc.grand_total_import = flt(self.doc.grand_total / self.doc.conversion_rate,
 			self.precision("grand_total_import"))
 
@@ -137,6 +137,24 @@
 		
 		if self.meta.get_field("rounded_total_import"):
 			self.doc.rounded_total_import = _round(self.doc.grand_total_import)
+				
+		if self.meta.get_field("other_charges_added"):
+			self.doc.other_charges_added = flt(sum([flt(d.tax_amount) for d in self.tax_doclist 
+				if d.add_deduct_tax=="Add" and d.category in ["Valuation and Total", "Total"]]), 
+				self.precision("other_charges_added"))
+				
+		if self.meta.get_field("other_charges_deducted"):
+			self.doc.other_charges_deducted = flt(sum([flt(d.tax_amount) for d in self.tax_doclist 
+				if d.add_deduct_tax=="Deduct" and d.category in ["Valuation and Total", "Total"]]), 
+				self.precision("other_charges_deducted"))
+				
+		if self.meta.get_field("other_charges_added_import"):
+			self.doc.other_charges_added_import = flt(self.doc.other_charges_added / 
+				self.doc.conversion_rate, self.precision("other_charges_added_import"))
+				
+		if self.meta.get_field("other_charges_deducted_import"):
+			self.doc.other_charges_deducted_import = flt(self.doc.other_charges_deducted / 
+				self.doc.conversion_rate, self.precision("other_charges_deducted_import"))
 			
 	def calculate_outstanding_amount(self):
 		if self.doc.doctype == "Purchase Invoice" and self.doc.docstatus < 2:
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 67c1462..4806c73 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -191,7 +191,8 @@
 			
 		self.doc.other_charges_total = flt(self.doc.grand_total - self.doc.net_total,
 			self.precision("other_charges_total"))
-		self.doc.other_charges_total_export = flt(self.doc.grand_total_export - self.doc.net_total_export,
+		self.doc.other_charges_total_export = flt(
+			self.doc.grand_total_export - self.doc.net_total_export,
 			self.precision("other_charges_total_export"))
 		
 		self.doc.rounded_total = _round(self.doc.grand_total)