2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2015-02-17 05:41:11 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import json
|
2015-02-23 10:31:33 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2015-02-17 05:41:11 +00:00
|
|
|
from frappe.utils import cint, flt, rounded
|
|
|
|
from erpnext.setup.utils import get_company_currency
|
2015-02-23 06:28:15 +00:00
|
|
|
from erpnext.controllers.accounts_controller import validate_conversion_rate, \
|
|
|
|
validate_taxes_and_charges, validate_inclusive_tax
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-18 06:53:18 +00:00
|
|
|
class calculate_taxes_and_totals(object):
|
2015-02-17 05:41:11 +00:00
|
|
|
def __init__(self, doc):
|
|
|
|
self.doc = doc
|
2015-02-18 06:53:18 +00:00
|
|
|
self.calculate()
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def calculate(self):
|
|
|
|
self.discount_amount_applied = False
|
|
|
|
self._calculate()
|
|
|
|
|
|
|
|
if self.doc.meta.get_field("discount_amount"):
|
|
|
|
self.apply_discount_amount()
|
|
|
|
|
2015-02-17 07:20:51 +00:00
|
|
|
if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
|
2015-02-17 05:41:11 +00:00
|
|
|
self.calculate_total_advance()
|
|
|
|
|
|
|
|
def _calculate(self):
|
2015-02-20 09:10:35 +00:00
|
|
|
self.calculate_item_values()
|
|
|
|
self.initialize_taxes()
|
|
|
|
self.determine_exclusive_rate()
|
|
|
|
self.calculate_net_total()
|
|
|
|
self.calculate_taxes()
|
2015-03-17 05:20:47 +00:00
|
|
|
self.manipulate_grand_total_for_inclusive_tax()
|
2015-02-20 09:10:35 +00:00
|
|
|
self.calculate_totals()
|
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def validate_conversion_rate(self):
|
2015-02-17 05:41:11 +00:00
|
|
|
# validate conversion rate
|
|
|
|
company_currency = get_company_currency(self.doc.company)
|
|
|
|
if not self.doc.currency or self.doc.currency == company_currency:
|
|
|
|
self.doc.currency = company_currency
|
|
|
|
self.doc.conversion_rate = 1.0
|
|
|
|
else:
|
|
|
|
validate_conversion_rate(self.doc.currency, self.doc.conversion_rate,
|
|
|
|
self.doc.meta.get_label("conversion_rate"), self.doc.company)
|
|
|
|
|
|
|
|
self.doc.conversion_rate = flt(self.doc.conversion_rate)
|
|
|
|
|
|
|
|
def calculate_item_values(self):
|
|
|
|
if not self.discount_amount_applied:
|
|
|
|
for item in self.doc.get("items"):
|
|
|
|
self.doc.round_floats_in(item)
|
|
|
|
|
|
|
|
if item.discount_percentage == 100:
|
|
|
|
item.rate = 0.0
|
|
|
|
elif not item.rate:
|
2015-02-20 09:10:35 +00:00
|
|
|
item.rate = flt(item.price_list_rate *
|
|
|
|
(1.0 - (item.discount_percentage / 100.0)), item.precision("rate"))
|
|
|
|
|
|
|
|
item.net_rate = item.rate
|
|
|
|
item.amount = flt(item.rate * item.qty, item.precision("amount"))
|
|
|
|
item.net_amount = item.amount
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self._set_in_company_currency(item, ["price_list_rate", "rate", "net_rate", "amount", "net_amount"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
item.item_tax_amount = 0.0
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
def _set_in_company_currency(self, doc, fields):
|
2015-02-17 05:41:11 +00:00
|
|
|
"""set values in base currency"""
|
2015-02-20 09:10:35 +00:00
|
|
|
for f in fields:
|
|
|
|
val = flt(flt(doc.get(f), doc.precision(f)) * self.doc.conversion_rate, doc.precision("base_" + f))
|
|
|
|
doc.set("base_" + f, val)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def initialize_taxes(self):
|
|
|
|
for tax in self.doc.get("taxes"):
|
2015-02-28 13:41:51 +00:00
|
|
|
if not self.discount_amount_applied:
|
|
|
|
validate_taxes_and_charges(tax)
|
|
|
|
validate_inclusive_tax(tax, self.doc)
|
2015-02-23 06:28:15 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
tax.item_wise_tax_detail = {}
|
|
|
|
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"]
|
|
|
|
|
2015-02-22 19:36:00 +00:00
|
|
|
if tax.charge_type != "Actual" and \
|
|
|
|
not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
|
|
|
|
tax_fields.append("tax_amount")
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
for fieldname in tax_fields:
|
|
|
|
tax.set(fieldname, 0.0)
|
|
|
|
|
|
|
|
self.doc.round_floats_in(tax)
|
|
|
|
|
|
|
|
def determine_exclusive_rate(self):
|
2015-02-23 10:31:33 +00:00
|
|
|
if not any((cint(tax.included_in_print_rate) for tax in self.doc.get("taxes"))):
|
|
|
|
return
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
for item in self.doc.get("items"):
|
|
|
|
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
|
|
|
|
cumulated_tax_fraction = 0
|
|
|
|
for i, tax in enumerate(self.doc.get("taxes")):
|
|
|
|
tax.tax_fraction_for_current_item = self.get_current_tax_fraction(tax, item_tax_map)
|
|
|
|
|
|
|
|
if i==0:
|
|
|
|
tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
|
|
|
|
else:
|
|
|
|
tax.grand_total_fraction_for_current_item = \
|
|
|
|
self.doc.get("taxes")[i-1].grand_total_fraction_for_current_item \
|
|
|
|
+ tax.tax_fraction_for_current_item
|
|
|
|
|
|
|
|
cumulated_tax_fraction += tax.tax_fraction_for_current_item
|
|
|
|
|
|
|
|
if cumulated_tax_fraction and not self.discount_amount_applied and item.qty:
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_amount = flt(item.amount / (1 + cumulated_tax_fraction), item.precision("net_amount"))
|
|
|
|
item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
|
|
|
|
item.discount_percentage = flt(item.discount_percentage, item.precision("discount_percentage"))
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self._set_in_company_currency(item, ["net_rate", "net_amount"])
|
|
|
|
|
|
|
|
# below part need to be fixed???
|
|
|
|
|
|
|
|
# if item.discount_percentage == 100:
|
|
|
|
# item.price_list_rate = item.net_rate
|
|
|
|
# item.base_price_list_rate = flt(item.price_list_rate*self.doc.conversion_rate,
|
|
|
|
# self.doc.precision("base_price_list_rate", item))
|
|
|
|
# item.rate = item.base_rate = item.net_rate = item.base_net_rate = 0.0
|
|
|
|
# else:
|
|
|
|
# item.base_price_list_rate = flt(item.net_rate / (1 - (item.discount_percentage / 100.0)),
|
|
|
|
# self.doc.precision("price_list_rate", item))
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def _load_item_tax_rate(self, item_tax_rate):
|
|
|
|
return json.loads(item_tax_rate) if item_tax_rate else {}
|
|
|
|
|
|
|
|
def get_current_tax_fraction(self, tax, item_tax_map):
|
|
|
|
"""
|
|
|
|
Get tax fraction for calculating tax exclusive amount
|
|
|
|
from tax inclusive amount
|
|
|
|
"""
|
|
|
|
current_tax_fraction = 0
|
|
|
|
|
|
|
|
if cint(tax.included_in_print_rate):
|
|
|
|
tax_rate = self._get_tax_rate(tax, item_tax_map)
|
|
|
|
|
|
|
|
if tax.charge_type == "On Net Total":
|
|
|
|
current_tax_fraction = tax_rate / 100.0
|
|
|
|
|
|
|
|
elif tax.charge_type == "On Previous Row Amount":
|
|
|
|
current_tax_fraction = (tax_rate / 100.0) * \
|
|
|
|
self.doc.get("taxes")[cint(tax.row_id) - 1].tax_fraction_for_current_item
|
|
|
|
|
|
|
|
elif tax.charge_type == "On Previous Row Total":
|
|
|
|
current_tax_fraction = (tax_rate / 100.0) * \
|
|
|
|
self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_fraction_for_current_item
|
|
|
|
|
|
|
|
return current_tax_fraction
|
|
|
|
|
|
|
|
def _get_tax_rate(self, tax, item_tax_map):
|
|
|
|
if item_tax_map.has_key(tax.account_head):
|
|
|
|
return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
|
|
|
|
else:
|
|
|
|
return tax.rate
|
|
|
|
|
|
|
|
def calculate_net_total(self):
|
2015-02-22 20:10:01 +00:00
|
|
|
self.doc.total = self.doc.base_total = self.doc.net_total = self.doc.base_net_total = 0.0
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
for item in self.doc.get("items"):
|
2015-02-22 20:10:01 +00:00
|
|
|
self.doc.total += item.amount
|
|
|
|
self.doc.base_total += item.base_amount
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.net_total += item.net_amount
|
|
|
|
self.doc.base_net_total += item.base_net_amount
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-22 20:10:01 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def calculate_taxes(self):
|
|
|
|
# maintain actual tax rate based on idx
|
2015-02-20 09:10:35 +00:00
|
|
|
actual_tax_dict = dict([[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
|
2015-02-17 05:41:11 +00:00
|
|
|
for tax in self.doc.get("taxes") if tax.charge_type == "Actual"])
|
|
|
|
|
|
|
|
for n, item in enumerate(self.doc.get("items")):
|
|
|
|
item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
|
|
|
|
|
|
|
|
for i, tax in enumerate(self.doc.get("taxes")):
|
|
|
|
# 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.doc.get("items")) - 1:
|
|
|
|
current_tax_amount += actual_tax_dict[tax.idx]
|
|
|
|
|
2015-02-22 17:33:07 +00:00
|
|
|
# accumulate tax amount into tax.tax_amount
|
2015-02-22 19:36:00 +00:00
|
|
|
if tax.charge_type != "Actual" and \
|
|
|
|
not (self.discount_amount_applied and self.doc.apply_discount_on=="Grand Total"):
|
|
|
|
tax.tax_amount += current_tax_amount
|
2015-02-22 17:33:07 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
# 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
|
|
|
|
|
2015-02-22 17:33:07 +00:00
|
|
|
# set tax after discount
|
2015-02-17 05:41:11 +00:00
|
|
|
tax.tax_amount_after_discount_amount += current_tax_amount
|
|
|
|
|
|
|
|
if getattr(tax, "category", None):
|
|
|
|
# if just for valuation, do not add the tax amount in total
|
|
|
|
# hence, setting it as 0 for further steps
|
|
|
|
current_tax_amount = 0.0 if (tax.category == "Valuation") \
|
|
|
|
else current_tax_amount
|
|
|
|
|
|
|
|
current_tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
|
|
|
|
|
|
|
|
# Calculate tax.total viz. grand total till that step
|
|
|
|
# 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:
|
2015-02-24 11:38:34 +00:00
|
|
|
tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount, tax.precision("total"))
|
2015-02-17 05:41:11 +00:00
|
|
|
else:
|
|
|
|
tax.grand_total_for_current_item = \
|
2015-02-25 10:25:57 +00:00
|
|
|
flt(self.doc.get("taxes")[i-1].grand_total_for_current_item + current_tax_amount, tax.precision("total"))
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
# 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.doc.get("items")) - 1:
|
|
|
|
self.round_off_totals(tax)
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
# adjust Discount Amount loss in last tax iteration
|
2015-02-22 19:36:00 +00:00
|
|
|
if i == (len(self.doc.get("taxes")) - 1) and self.discount_amount_applied \
|
2015-03-04 09:36:56 +00:00
|
|
|
and self.doc.discount_amount:
|
2015-02-22 19:36:00 +00:00
|
|
|
self.adjust_discount_amount_loss(tax)
|
2015-03-05 14:01:23 +00:00
|
|
|
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if tax.charge_type == "Actual":
|
|
|
|
# distribute the tax amount proportionally to each item row
|
2015-02-20 09:10:35 +00:00
|
|
|
actual = flt(tax.tax_amount, tax.precision("tax_amount"))
|
|
|
|
current_tax_amount = item.net_amount*actual / self.doc.net_total if self.doc.net_total else 0.0
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
elif tax.charge_type == "On Net Total":
|
2015-02-20 09:10:35 +00:00
|
|
|
current_tax_amount = (tax_rate / 100.0) * item.net_amount
|
2015-02-17 05:41:11 +00:00
|
|
|
elif tax.charge_type == "On Previous Row Amount":
|
|
|
|
current_tax_amount = (tax_rate / 100.0) * \
|
|
|
|
self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
|
|
|
|
elif tax.charge_type == "On Previous Row Total":
|
|
|
|
current_tax_amount = (tax_rate / 100.0) * \
|
|
|
|
self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
|
|
|
|
|
2015-02-24 11:38:34 +00:00
|
|
|
current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
|
2015-02-20 09:10:35 +00:00
|
|
|
|
|
|
|
self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
return current_tax_amount
|
|
|
|
|
|
|
|
def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
|
2015-02-17 05:41:11 +00:00
|
|
|
# store tax breakup for each item
|
|
|
|
key = item.item_code or item.item_name
|
2015-02-20 09:10:35 +00:00
|
|
|
item_wise_tax_amount = current_tax_amount*self.doc.conversion_rate
|
2015-02-17 05:41:11 +00:00
|
|
|
if tax.item_wise_tax_detail.get(key):
|
2015-02-20 09:10:35 +00:00
|
|
|
item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
tax.item_wise_tax_detail[key] = [tax_rate,flt(item_wise_tax_amount, tax.precision("base_tax_amount"))]
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def round_off_totals(self, tax):
|
2015-02-20 09:10:35 +00:00
|
|
|
tax.total = flt(tax.total, tax.precision("total"))
|
|
|
|
tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
|
|
|
|
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount, tax.precision("tax_amount"))
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-22 19:36:00 +00:00
|
|
|
self._set_in_company_currency(tax, ["total", "tax_amount", "tax_amount_after_discount_amount"])
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def adjust_discount_amount_loss(self, tax):
|
2015-02-20 09:10:35 +00:00
|
|
|
discount_amount_loss = self.doc.grand_total - flt(self.doc.discount_amount) - tax.total
|
2015-02-17 05:41:11 +00:00
|
|
|
tax.tax_amount_after_discount_amount = flt(tax.tax_amount_after_discount_amount +
|
2015-03-04 10:32:03 +00:00
|
|
|
discount_amount_loss, tax.precision("tax_amount"))
|
|
|
|
tax.total = flt(tax.total + discount_amount_loss, tax.precision("total"))
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-03-04 10:32:03 +00:00
|
|
|
self._set_in_company_currency(tax, ["total", "tax_amount_after_discount_amount"])
|
2015-03-17 05:20:47 +00:00
|
|
|
|
|
|
|
def manipulate_grand_total_for_inclusive_tax(self):
|
|
|
|
# if fully inclusive taxes and diff
|
|
|
|
if self.doc.get("taxes") and all(cint(t.included_in_print_rate) for t in self.doc.get("taxes")):
|
|
|
|
|
|
|
|
last_tax = self.doc.get("taxes")[-1]
|
|
|
|
|
|
|
|
diff = self.doc.net_total - flt(last_tax.total / self.doc.conversion_rate,
|
|
|
|
self.precision("grand_total_export"))
|
|
|
|
|
|
|
|
if diff and abs(diff) <= (2.0 / 10**last_tax.precision("tax_amount")):
|
|
|
|
adjustment_amount = flt(diff * self.doc.conversion_rate, last_tax.precision("tax_amount"))
|
|
|
|
last_tax.tax_amount += adjustment_amount
|
|
|
|
last_tax.tax_amount_after_discount_amount += adjustment_amount
|
|
|
|
last_tax.total += adjustment_amount
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def calculate_totals(self):
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.grand_total = flt(self.doc.get("taxes")[-1].total
|
|
|
|
if self.doc.get("taxes") else self.doc.net_total)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-03-05 14:01:23 +00:00
|
|
|
self.doc.total_taxes_and_charges = flt(self.doc.grand_total - self.doc.net_total,
|
|
|
|
self.doc.precision("total_taxes_and_charges"))
|
|
|
|
|
|
|
|
self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])
|
|
|
|
|
2015-02-17 07:20:51 +00:00
|
|
|
if self.doc.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
|
|
|
|
if self.doc.total_taxes_and_charges else self.doc.base_net_total
|
2015-02-17 05:41:11 +00:00
|
|
|
else:
|
2015-03-05 14:01:23 +00:00
|
|
|
self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
|
2015-02-17 05:41:11 +00:00
|
|
|
for tax in self.doc.get("taxes"):
|
|
|
|
if tax.category in ["Valuation and Total", "Total"]:
|
|
|
|
if tax.add_deduct_tax == "Add":
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.taxes_and_charges_added += flt(tax.tax_amount)
|
2015-02-17 05:41:11 +00:00
|
|
|
else:
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.taxes_and_charges_deducted += flt(tax.tax_amount)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.base_grand_total = flt(self.doc.grand_total * self.doc.conversion_rate) \
|
|
|
|
if (self.doc.taxes_and_charges_added or self.doc.taxes_and_charges_deducted) \
|
|
|
|
else self.doc.base_net_total
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
if self.doc.meta.get_field("rounded_total"):
|
|
|
|
self.doc.rounded_total = rounded(self.doc.grand_total)
|
2015-02-20 09:10:35 +00:00
|
|
|
if self.doc.meta.get_field("base_rounded_total"):
|
|
|
|
self.doc.base_rounded_total = rounded(self.doc.base_grand_total)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def _cleanup(self):
|
|
|
|
for tax in self.doc.get("taxes"):
|
|
|
|
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(',', ':'))
|
|
|
|
|
|
|
|
def apply_discount_amount(self):
|
|
|
|
if self.doc.discount_amount:
|
2015-02-23 10:31:33 +00:00
|
|
|
if not self.doc.apply_discount_on:
|
|
|
|
frappe.throw(_("Please select Apply Discount On"))
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
self.doc.base_discount_amount = flt(self.doc.discount_amount * self.doc.conversion_rate,
|
|
|
|
self.doc.precision("base_discount_amount"))
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
total_for_discount_amount = self.get_total_for_discount_amount()
|
2015-03-04 09:36:56 +00:00
|
|
|
taxes = self.doc.get("taxes")
|
|
|
|
net_total = 0
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
if total_for_discount_amount:
|
2015-02-17 05:41:11 +00:00
|
|
|
# calculate item amount after Discount Amount
|
2015-03-04 09:36:56 +00:00
|
|
|
for i, item in enumerate(self.doc.get("items")):
|
|
|
|
distributed_amount = flt(self.doc.discount_amount) * \
|
|
|
|
item.net_amount / total_for_discount_amount
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_amount = flt(item.net_amount - distributed_amount, item.precision("net_amount"))
|
2015-03-04 09:36:56 +00:00
|
|
|
net_total += item.net_amount
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-03-04 09:36:56 +00:00
|
|
|
# discount amount rounding loss adjustment if no taxes
|
|
|
|
if (not taxes or self.doc.apply_discount_on == "Net Total") \
|
|
|
|
and i == len(self.doc.get("items")) - 1:
|
2015-03-05 14:01:23 +00:00
|
|
|
discount_amount_loss = flt(self.doc.total - net_total - self.doc.discount_amount,
|
2015-03-04 09:36:56 +00:00
|
|
|
self.doc.precision("net_total"))
|
2015-03-05 14:01:23 +00:00
|
|
|
item.net_amount = flt(item.net_amount + discount_amount_loss,
|
2015-03-04 09:36:56 +00:00
|
|
|
item.precision("net_amount"))
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self._set_in_company_currency(item, ["net_rate", "net_amount"])
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
self.discount_amount_applied = True
|
|
|
|
self._calculate()
|
|
|
|
else:
|
|
|
|
self.doc.base_discount_amount = 0
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
def get_total_for_discount_amount(self):
|
2015-02-22 19:36:00 +00:00
|
|
|
if self.doc.apply_discount_on == "Net Total":
|
|
|
|
return self.doc.net_total
|
2015-02-20 09:10:35 +00:00
|
|
|
else:
|
|
|
|
actual_taxes_dict = {}
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
for tax in self.doc.get("taxes"):
|
|
|
|
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)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
return flt(self.doc.grand_total - sum(actual_taxes_dict.values()), self.doc.precision("grand_total"))
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
|
2015-02-24 04:12:24 +00:00
|
|
|
def calculate_total_advance(self):
|
|
|
|
if self.doc.docstatus < 2:
|
2015-02-20 09:10:35 +00:00
|
|
|
total_allocated_amount = sum([flt(adv.allocated_amount, adv.precision("allocated_amount"))
|
2015-02-17 05:41:11 +00:00
|
|
|
for adv in self.doc.get("advances")])
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-18 14:52:59 +00:00
|
|
|
if self.doc.docstatus == 0:
|
2015-02-17 05:41:11 +00:00
|
|
|
self.calculate_outstanding_amount()
|
|
|
|
|
|
|
|
def calculate_outstanding_amount(self):
|
|
|
|
# NOTE:
|
|
|
|
# write_off_amount is only for POS Invoice
|
|
|
|
# total_advance is only for non POS Invoice
|
|
|
|
|
2015-02-17 07:20:51 +00:00
|
|
|
if self.doc.doctype == "Sales Invoice":
|
|
|
|
self.doc.round_floats_in(self.doc, ["base_grand_total", "total_advance", "write_off_amount", "paid_amount"])
|
2015-02-17 05:41:11 +00:00
|
|
|
total_amount_to_pay = self.doc.base_grand_total - self.doc.write_off_amount
|
|
|
|
self.doc.outstanding_amount = flt(total_amount_to_pay - self.doc.total_advance - self.doc.paid_amount,
|
|
|
|
self.doc.precision("outstanding_amount"))
|
|
|
|
else:
|
2015-02-17 07:20:51 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["total_advance", "write_off_amount"])
|
2015-02-17 05:41:11 +00:00
|
|
|
self.doc.total_amount_to_pay = flt(self.doc.base_grand_total - self.doc.write_off_amount,
|
|
|
|
self.doc.precision("total_amount_to_pay"))
|
|
|
|
self.doc.outstanding_amount = flt(self.doc.total_amount_to_pay - self.doc.total_advance,
|
|
|
|
self.doc.precision("outstanding_amount"))
|