[flat discount] flat discount implemented on sales cycle
diff --git a/erpnext/public/js/transaction.js b/erpnext/public/js/transaction.js
index 5b48820..e716c27 100644
--- a/erpnext/public/js/transaction.js
+++ b/erpnext/public/js/transaction.js
@@ -146,7 +146,9 @@
 	},
 	
 	validate: function() {
+		this.flat_discount_applied = false;
 		this.calculate_taxes_and_totals();
+		this.apply_flat_discount && this.apply_flat_discount();
 	},
 	
 	set_default_values: function() {
@@ -514,7 +516,7 @@
 		this.validate_conversion_rate();
 		this.frm.item_doclist = this.get_item_doclist();
 		this.frm.tax_doclist = this.get_tax_doclist();
-		
+
 		this.calculate_item_values();
 		this.initialize_taxes();
 		this.determine_exclusive_rate && this.determine_exclusive_rate();
@@ -528,12 +530,17 @@
 	
 	initialize_taxes: function() {
 		var me = this;
+
 		$.each(this.frm.tax_doclist, function(i, tax) {
 			tax.item_wise_tax_detail = {};
-			$.each(["tax_amount", "total",
+			tax_fields = ["total", "tax_amount_after_flat_discount", 
 				"tax_amount_for_current_item", "grand_total_for_current_item",
-				"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"],
-				function(i, fieldname) { tax[fieldname] = 0.0 });
+				"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
+
+			if (!me.flat_discount_applied)
+				tax_fields.push("tax_amount");
+
+			$.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
 			
 			me.validate_on_previous_row(tax);
 			me.validate_inclusive_tax(tax);
@@ -543,31 +550,41 @@
 	
 	calculate_taxes: function() {
 		var me = this;
+		var actual_tax_dict = {};
 		
+		// maintain actual tax rate based on idx
+		$.each(this.frm.tax_doclist, function(i, tax) {
+			if (tax.charge_type == "Actual") {
+				actual_tax_dict[tax.idx] = flt(tax.rate);
+			}
+		});
+
 		$.each(this.frm.item_doclist, function(n, item) {
 			var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
-			
+
 			$.each(me.frm.tax_doclist, function(i, tax) {
 				// tax_amount represents the amount of tax for the current step
 				var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
 
-				me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
-				
-				// case when net total is 0 but there is an actual type charge
-				// in this case add the actual amount to tax.tax_amount
-				// and tax.grand_total_for_current_item for the first such iteration
-				if(tax.charge_type == "Actual" && 
-					!(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
-						var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
-						current_tax_amount += zero_net_total_adjustment;
+				// Adjust divisional loss to the last item
+				if (tax.charge_type == "Actual") {
+					actual_tax_dict[tax.idx] -= current_tax_amount;
+					if (n == me.frm.item_doclist.length - 1) {
+						current_tax_amount += actual_tax_dict[tax.idx]
 					}
-				
+				}
+
+				me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
+
 				// store tax_amount for current item as it will be used for
 				// charge type = 'On Previous Row Amount'
 				tax.tax_amount_for_current_item = current_tax_amount;
 				
 				// accumulate tax amount into tax.tax_amount
-				tax.tax_amount += current_tax_amount;
+				if (!me.flat_discount_applied)
+					tax.tax_amount += current_tax_amount;
+
+				tax.tax_amount_after_flat_discount += current_tax_amount;
 				
 				// for buying
 				if(tax.category) {
@@ -592,9 +609,32 @@
 				
 				// in tax.total, accumulate grand total for each item
 				tax.total += tax.grand_total_for_current_item;
+
+				// set precision in the last item iteration
+				if (n == me.frm.item_doclist.length - 1) {
+					me.round_off_totals(tax);
+
+					// adjust flat discount loss in last tax iteration
+					if ((i == me.frm.tax_doclist.length - 1) && me.flat_discount_applied)
+						me.adjust_flat_discount_loss(tax);
+				}
 			});
 		});
 	},
+
+	round_off_totals: function(tax) {
+		tax.total = flt(tax.total, precision("total", tax));
+		tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
+		tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount, 
+			precision("tax_amount", tax));
+	},
+
+	adjust_flat_discount_loss: function(tax) {
+		var flat_discount_loss = this.frm.doc.grand_total - this.frm.doc.flat_discount - tax.total;
+		tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount + 
+			flat_discount_loss, precision("tax_amount", tax));
+		tax.total = flt(tax.total + flat_discount_loss, precision("total", tax));
+	},
 	
 	get_current_tax_amount: function(item, tax, item_tax_map) {
 		var tax_rate = this._get_tax_rate(tax, item_tax_map);
@@ -617,9 +657,8 @@
 		} else if(tax.charge_type == "On Previous Row Total") {
 			current_tax_amount = (tax_rate / 100.0) *
 				this.frm.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item;
-			
 		}
-		
+
 		current_tax_amount = flt(current_tax_amount, precision("tax_amount", tax));
 		
 		// store tax breakup for each item