[flat discount] first commit
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index 5388ee1..c76258a 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -16,7 +16,10 @@
 		self.set_missing_values(for_validate=True)
 		self.validate_date_with_fiscal_year()
 		if self.meta.get_field("currency"):
+			self.flat_discount_applied = False
 			self.calculate_taxes_and_totals()
+			if hasattr(self, "apply_flat_discount"):
+				self.apply_flat_discount()
 			self.validate_value("grand_total", ">=", 0)
 			self.set_total_in_words()
 			
@@ -141,7 +144,7 @@
 		else:
 			validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
 				self.meta.get_label("conversion_rate"), self.doc.company)
-		
+
 		self.doc.conversion_rate = flt(self.doc.conversion_rate)
 		self.item_doclist = self.doclist.get({"parentfield": self.fname})
 		self.tax_doclist = self.doclist.get({"parentfield": self.other_fname})
@@ -163,11 +166,16 @@
 	def initialize_taxes(self):
 		for tax in self.tax_doclist:
 			tax.item_wise_tax_detail = {}
-			for fieldname in ["tax_amount", "total", 
-				"tax_amount_for_current_item", "grand_total_for_current_item",
-				"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]:
-					tax.fields[fieldname] = 0.0
-			
+			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"]
+
+			if not self.flat_discount_applied:
+				tax_fields.append("tax_amount")
+
+			for fieldname in tax_fields:
+				tax.fields[fieldname] = 0.0
+
 			self.validate_on_previous_row(tax)
 			self.validate_inclusive_tax(tax)
 			self.round_floats_in(tax)
@@ -247,7 +255,10 @@
 				tax.tax_amount_for_current_item = current_tax_amount
 
 				# accumulate tax amount into tax.tax_amount
-				tax.tax_amount += current_tax_amount
+				if not self.flat_discount_applied:
+					tax.tax_amount += current_tax_amount
+
+				tax.tax_amount_after_flat_discount += current_tax_amount
 				
 				if tax.category:
 					# if just for valuation, do not add the tax amount in total
@@ -270,7 +281,7 @@
 				
 				# in tax.total, accumulate grand total of each item
 				tax.total += tax.grand_total_for_current_item
-				
+
 	def get_current_tax_amount(self, item, tax, item_tax_map):
 		tax_rate = self._get_tax_rate(tax, item_tax_map)
 		current_tax_amount = 0.0
diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py
index b52d51c..5e265e7 100644
--- a/controllers/buying_controller.py
+++ b/controllers/buying_controller.py
@@ -5,7 +5,6 @@
 import webnotes
 from webnotes import _, msgprint
 from webnotes.utils import flt, _round
-
 from buying.utils import get_item_details
 from setup.utils import get_company_currency
 
@@ -162,6 +161,10 @@
 		if not self.meta.get_field("item_tax_amount", parentfield=self.fname):
 			for item in self.item_doclist:
 				del item.fields["item_tax_amount"]
+
+		if not self.meta.get_field("tax_amount_after_flat_discount", parentfield=self.other_fname):
+			for tax in self.tax_doclist:
+				del tax.fields["tax_amount_after_flat_discount"]
 				
 	def set_item_tax_amount(self, item, tax, current_tax_amount):
 		"""
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index 67c1462..ede6ca9 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -121,7 +121,7 @@
 						
 				cumulated_tax_fraction += tax.tax_fraction_for_current_item
 			
-			if cumulated_tax_fraction:
+			if cumulated_tax_fraction and not self.flat_discount_applied:
 				item.amount = flt((item.export_amount * self.doc.conversion_rate) /
 					(1 + cumulated_tax_fraction), self.precision("amount", item))
 					
@@ -158,22 +158,23 @@
 		return current_tax_fraction
 		
 	def calculate_item_values(self):
-		for item in self.item_doclist:
-			self.round_floats_in(item)
-			
-			if item.adj_rate == 100:
-				item.export_rate = 0
-			elif not item.export_rate:
-				item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)),
-					self.precision("export_rate", item))
-						
-			item.export_amount = flt(item.export_rate * item.qty,
-				self.precision("export_amount", item))
+		if not self.flat_discount_applied:
+			for item in self.item_doclist:
+				self.round_floats_in(item)
 
-			self._set_in_company_currency(item, "ref_rate", "base_ref_rate")
-			self._set_in_company_currency(item, "export_rate", "basic_rate")
-			self._set_in_company_currency(item, "export_amount", "amount")
-			
+				if item.adj_rate == 100:
+					item.export_rate = 0
+				elif not item.export_rate:
+					item.export_rate = flt(item.ref_rate * (1.0 - (item.adj_rate / 100.0)),
+						self.precision("export_rate", item))
+
+				item.export_amount = flt(item.export_rate * item.qty,
+					self.precision("export_amount", item))
+
+				self._set_in_company_currency(item, "ref_rate", "base_ref_rate")
+				self._set_in_company_currency(item, "export_rate", "basic_rate")
+				self._set_in_company_currency(item, "export_amount", "amount")
+
 	def calculate_net_total(self):
 		self.doc.net_total = self.doc.net_total_export = 0.0
 
@@ -184,6 +185,7 @@
 		self.round_floats_in(self.doc, ["net_total", "net_total_export"])
 				
 	def calculate_totals(self):
+		self.total_tax_excluding_actual = 0.0
 		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_export = flt(self.doc.grand_total / self.doc.conversion_rate, 
@@ -191,12 +193,33 @@
 			
 		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.precision("other_charges_total_export"))
+		self.doc.other_charges_total_export = flt(self.doc.grand_total_export - 
+			self.doc.net_total_export + flt(self.doc.flat_discount), self.precision("other_charges_total_export"))
 		
 		self.doc.rounded_total = _round(self.doc.grand_total)
 		self.doc.rounded_total_export = _round(self.doc.grand_total_export)
-		
+
+		if self.doc.flat_discount:
+			# calculate total tax for flat discount excluding actual
+			for tax in self.tax_doclist:
+				if tax.charge_type != "Actual":
+					self.total_tax_excluding_actual += tax.tax_amount
+
+			self.total_amount_for_flat_discount = flt(self.doc.net_total + 
+				self.total_tax_excluding_actual, self.precision("grand_total"))
+
+	def apply_flat_discount(self):
+		distributed_amount = 0.0
+
+		if self.doc.flat_discount and self.total_amount_for_flat_discount:
+			# calculate item amount after flat discount
+			for item in self.item_doclist:
+				distributed_amount = self.doc.flat_discount * item.amount / self.total_amount_for_flat_discount
+				item.amount = flt(item.amount - distributed_amount, self.precision("amount", item))
+
+			self.flat_discount_applied = True
+			self.calculate_taxes_and_totals()
+
 	def calculate_outstanding_amount(self):
 		# NOTE: 
 		# write_off_amount is only for POS Invoice