[flat discount] implemented flat discount on server side
diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py
index c76258a..41d4859 100644
--- a/controllers/accounts_controller.py
+++ b/controllers/accounts_controller.py
@@ -148,6 +148,10 @@
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})
+
+ # for buying
+ if not hasattr(self, "flat_discount_applied"):
+ self.flat_discount_applied = False
self.calculate_item_values()
self.initialize_taxes()
@@ -232,24 +236,26 @@
_on_previous_row_error("1 - %d" % (tax.row_id,))
def calculate_taxes(self):
- for item in self.item_doclist:
+ # maintain actual tax rate based on idx
+ actual_tax_dict = dict([[tax.idx, tax.rate] for tax in self.tax_doclist
+ if tax.charge_type == "Actual"])
+
+ for n, item in enumerate(self.item_doclist):
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
for i, tax in enumerate(self.tax_doclist):
# tax_amount represents the amount of tax for the current step
current_tax_amount = self.get_current_tax_amount(item, tax, item_tax_map)
+
+ # Adjust divisional loss to the last item
+ if tax.charge_type == "Actual":
+ actual_tax_dict[tax.idx] -= current_tax_amount;
+ if n == len(self.item_doclist) - 1:
+ current_tax_amount += actual_tax_dict[tax.idx]
if hasattr(self, "set_item_tax_amount"):
self.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" and \
- not (current_tax_amount or self.doc.net_total or tax.tax_amount):
- zero_net_total_adjustment = flt(tax.rate, self.precision("tax_amount", tax))
- current_tax_amount += zero_net_total_adjustment
-
# 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
@@ -271,17 +277,31 @@
# note: grand_total_for_current_item contains the contribution of
# item's amount, previously applied tax and the current tax on that item
if i==0:
- tax.grand_total_for_current_item = flt(item.amount +
- current_tax_amount, self.precision("total", tax))
-
+ tax.grand_total_for_current_item = flt(item.amount + current_tax_amount,
+ self.precision("total", tax))
else:
tax.grand_total_for_current_item = \
- flt(self.tax_doclist[i-1].grand_total_for_current_item +
+ flt(self.tax_doclist[i-1].grand_total_for_current_item +
current_tax_amount, self.precision("total", tax))
+
# in tax.total, accumulate grand total of each item
tax.total += tax.grand_total_for_current_item
+ # set precision in the last item iteration
+ if n == len(self.item_doclist) - 1:
+ tax.total = flt(tax.total, self.precision("total", tax))
+ tax.tax_amount = flt(tax.tax_amount, self.precision("tax_amount", tax))
+ tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount,
+ self.precision("tax_amount", tax))
+
+ # adjust discount loss in last tax iteration
+ if i == (len(self.tax_doclist) - 1) and self.flat_discount_applied:
+ flat_discount_loss = self.doc.grand_total - self.doc.flat_discount - tax.total
+ tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount +
+ flat_discount_loss, self.precision("tax_amount", tax))
+ tax.total = flt(tax.total + flat_discount_loss, self.precision("total", tax))
+
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
@@ -300,9 +320,9 @@
elif tax.charge_type == "On Previous Row Total":
current_tax_amount = (tax_rate / 100.0) * \
self.tax_doclist[cint(tax.row_id) - 1].grand_total_for_current_item
-
+
current_tax_amount = flt(current_tax_amount, self.precision("tax_amount", tax))
-
+
# store tax breakup for each item
key = item.item_code or item.item_name
if tax.item_wise_tax_detail.get(key):
diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py
index ede6ca9..2571c3c 100644
--- a/controllers/selling_controller.py
+++ b/controllers/selling_controller.py
@@ -185,7 +185,6 @@
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,
@@ -199,26 +198,33 @@
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:
+ total_amount_for_flat_discount = self.get_flat_discountable_amount()
- 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))
+ if 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 / 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()
+ self.flat_discount_applied = True
+ self.calculate_taxes_and_totals()
+
+ def get_flat_discountable_amount(self):
+ actual_taxes_dict = {}
+
+ for tax in self.tax_doclist:
+ if tax.charge_type == "Actual":
+ actual_taxes_dict.setdefault(tax.idx, tax.tax_amount)
+ elif tax.row_id in actual_taxes_dict:
+ actual_tax_amount = flt(actual_taxes_dict.get(tax.row_id, 0)) * \
+ flt(tax.rate) / 100
+ actual_taxes_dict.setdefault(tax.idx, actual_tax_amount)
+
+ total_amount_for_flat_discount = flt(self.doc.grand_total - sum(actual_taxes_dict.values()),
+ self.precision("grand_total"))
+ return total_amount_for_flat_discount
def calculate_outstanding_amount(self):
# NOTE: