flat discount replaced to discount amount

This commit is contained in:
Akhilesh Darjee
2014-01-03 18:15:07 +05:30
parent 829eda47a8
commit 57738a0ff3
30 changed files with 144 additions and 125 deletions

View File

@@ -16,10 +16,7 @@ class AccountsController(TransactionBase):
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()
@@ -136,6 +133,12 @@ class AccountsController(TransactionBase):
self.doclist.append(tax)
def calculate_taxes_and_totals(self):
self.discount_amount_applied = False
self._calculate_taxes_and_totals()
if self.meta.get_field(self.doc.doctype, "discount_amount"):
self.apply_discount_amount()
def _calculate_taxes_and_totals(self):
# validate conversion rate
company_currency = get_company_currency(self.doc.company)
if not self.doc.currency or self.doc.currency == company_currency:
@@ -148,10 +151,6 @@ class AccountsController(TransactionBase):
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()
@@ -164,17 +163,14 @@ class AccountsController(TransactionBase):
self.calculate_totals()
self._cleanup()
# TODO
# print format: show net_total_export instead of net_total
def initialize_taxes(self):
for tax in self.tax_doclist:
tax.item_wise_tax_detail = {}
tax_fields = ["total", "tax_amount_after_flat_discount",
tax_fields = ["total", "tax_amount_after_discount_amount",
"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:
if not self.discount_amount_applied:
tax_fields.append("tax_amount")
for fieldname in tax_fields:
@@ -261,10 +257,10 @@ class AccountsController(TransactionBase):
tax.tax_amount_for_current_item = current_tax_amount
# accumulate tax amount into tax.tax_amount
if not self.flat_discount_applied:
if not self.discount_amount_applied:
tax.tax_amount += current_tax_amount
tax.tax_amount_after_flat_discount += current_tax_amount
tax.tax_amount_after_discount_amount += current_tax_amount
if tax.category:
# if just for valuation, do not add the tax amount in total
@@ -291,21 +287,21 @@ class AccountsController(TransactionBase):
if n == len(self.item_doclist) - 1:
self.round_off_totals(tax)
# adjust flat discount loss in last tax iteration
if i == (len(self.tax_doclist) - 1) and self.flat_discount_applied:
self.adjust_flat_discount_loss(tax)
# adjust Discount Amount loss in last tax iteration
if i == (len(self.tax_doclist) - 1) and self.discount_amount_applied:
self.adjust_discount_amount_loss(tax)
def round_off_totals(self, tax):
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,
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount,
self.precision("tax_amount", tax))
def adjust_flat_discount_loss(self, tax):
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 adjust_discount_amount_loss(self, tax):
discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
discount_amount_loss, self.precision("tax_amount", tax))
tax.total = flt(tax.total + discount_amount_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)

View File

@@ -147,7 +147,7 @@ class BuyingController(StockController):
def _cleanup(self):
super(BuyingController, self)._cleanup()
# except in purchase invoice, rate field is purchase_rate
# except in purchase invoice, rate field is purchase_rate
# reset fieldname of rate
if self.doc.doctype != "Purchase Invoice":
df = self.meta.get_field("rate", parentfield=self.fname)
@@ -161,9 +161,9 @@ class BuyingController(StockController):
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):
if not self.meta.get_field("tax_amount_after_discount_amount", parentfield=self.other_fname):
for tax in self.tax_doclist:
del tax.fields["tax_amount_after_flat_discount"]
del tax.fields["tax_amount_after_discount_amount"]
def set_item_tax_amount(self, item, tax, current_tax_amount):
"""

View File

@@ -121,7 +121,7 @@ class SellingController(StockController):
cumulated_tax_fraction += tax.tax_fraction_for_current_item
if cumulated_tax_fraction and not self.flat_discount_applied:
if cumulated_tax_fraction and not self.discount_amount_applied:
item.amount = flt((item.export_amount * self.doc.conversion_rate) /
(1 + cumulated_tax_fraction), self.precision("amount", item))
@@ -158,7 +158,7 @@ class SellingController(StockController):
return current_tax_fraction
def calculate_item_values(self):
if not self.flat_discount_applied:
if not self.discount_amount_applied:
for item in self.item_doclist:
self.round_floats_in(item)
@@ -193,25 +193,25 @@ class SellingController(StockController):
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 + flt(self.doc.flat_discount), self.precision("other_charges_total_export"))
self.doc.net_total_export + flt(self.doc.discount_amount), 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)
def apply_flat_discount(self):
if self.doc.flat_discount:
total_amount_for_flat_discount = self.get_flat_discountable_amount()
def apply_discount_amount(self):
if self.doc.discount_amount:
grand_total_for_discount_amount = self.get_grand_total_for_discount_amount()
if total_amount_for_flat_discount:
# calculate item amount after flat discount
if grand_total_for_discount_amount:
# calculate item amount after Discount Amount
for item in self.item_doclist:
distributed_amount = self.doc.flat_discount * item.amount / total_amount_for_flat_discount
distributed_amount = flt(self.doc.discount_amount) * item.amount / grand_total_for_discount_amount
item.amount = flt(item.amount - distributed_amount, self.precision("amount", item))
self.flat_discount_applied = True
self.calculate_taxes_and_totals()
self.discount_amount_applied = True
self._calculate_taxes_and_totals()
def get_flat_discountable_amount(self):
def get_grand_total_for_discount_amount(self):
actual_taxes_dict = {}
for tax in self.tax_doclist:
@@ -222,9 +222,9 @@ class SellingController(StockController):
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()),
grand_total_for_discount_amount = flt(self.doc.grand_total - sum(actual_taxes_dict.values()),
self.precision("grand_total"))
return total_amount_for_flat_discount
return grand_total_for_discount_amount
def calculate_outstanding_amount(self):
# NOTE: