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
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
import json
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2017-03-31 07:14:29 +00:00
|
|
|
import frappe
|
2015-12-18 07:42:02 +00:00
|
|
|
from frappe import _, scrub
|
2017-07-17 12:32:31 +00:00
|
|
|
from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-08-26 12:53:12 +00:00
|
|
|
import erpnext
|
2020-12-25 13:04:39 +00:00
|
|
|
from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate
|
2020-08-26 12:53:12 +00:00
|
|
|
from erpnext.accounts.doctype.pricing_rule.utils import get_applied_pricing_rules
|
2015-02-23 06:28:15 +00:00
|
|
|
from erpnext.controllers.accounts_controller import (
|
|
|
|
validate_conversion_rate,
|
2015-02-28 13:41:51 +00:00
|
|
|
validate_inclusive_tax,
|
2015-02-23 06:28:15 +00:00
|
|
|
validate_taxes_and_charges,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2020-01-06 10:04:15 +00:00
|
|
|
from erpnext.stock.get_item_details import _get_item_tax_template
|
2021-09-02 11:14:59 +00:00
|
|
|
|
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
|
2021-02-19 09:00:23 +00:00
|
|
|
frappe.flags.round_off_applicable_accounts = []
|
|
|
|
get_round_off_applicable_accounts(self.doc.company, frappe.flags.round_off_applicable_accounts)
|
2015-02-18 06:53:18 +00:00
|
|
|
self.calculate()
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def calculate(self):
|
2019-07-12 08:57:19 +00:00
|
|
|
if not len(self.doc.get("items")):
|
|
|
|
return
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
self.discount_amount_applied = False
|
|
|
|
self._calculate()
|
|
|
|
|
|
|
|
if self.doc.meta.get_field("discount_amount"):
|
2015-12-18 07:42:02 +00:00
|
|
|
self.set_discount_amount()
|
2015-02-17 05:41:11 +00:00
|
|
|
self.apply_discount_amount()
|
|
|
|
|
2022-08-21 12:21:05 +00:00
|
|
|
# Update grand total as per cash and non trade discount
|
|
|
|
if self.doc.apply_discount_on == "Grand Total" and self.doc.get("is_cash_or_non_trade_discount"):
|
|
|
|
self.doc.grand_total -= self.doc.discount_amount
|
|
|
|
self.doc.base_grand_total -= self.doc.base_discount_amount
|
2022-08-29 08:48:39 +00:00
|
|
|
self.set_rounded_total()
|
2022-08-21 12:21:05 +00:00
|
|
|
|
2022-03-10 15:26:36 +00:00
|
|
|
self.calculate_shipping_charges()
|
|
|
|
|
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()
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2017-07-05 07:28:19 +00:00
|
|
|
if self.doc.meta.get_field("other_charges_calculation"):
|
|
|
|
self.set_item_wise_tax_breakup()
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def _calculate(self):
|
2018-05-02 06:49:30 +00:00
|
|
|
self.validate_conversion_rate()
|
2015-02-20 09:10:35 +00:00
|
|
|
self.calculate_item_values()
|
2020-01-06 10:04:15 +00:00
|
|
|
self.validate_item_tax_template()
|
2015-02-20 09:10:35 +00:00
|
|
|
self.initialize_taxes()
|
|
|
|
self.determine_exclusive_rate()
|
|
|
|
self.calculate_net_total()
|
2022-10-03 11:09:35 +00:00
|
|
|
self.calculate_tax_withholding_net_total()
|
2015-02-20 09:10:35 +00:00
|
|
|
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()
|
2018-05-26 03:53:02 +00:00
|
|
|
self.calculate_total_net_weight()
|
2015-02-20 09:10:35 +00:00
|
|
|
|
2022-10-03 11:09:35 +00:00
|
|
|
def calculate_tax_withholding_net_total(self):
|
|
|
|
if hasattr(self.doc, "tax_withholding_net_total"):
|
|
|
|
sum_net_amount = 0
|
2022-11-02 06:49:51 +00:00
|
|
|
sum_base_net_amount = 0
|
2022-10-03 11:09:35 +00:00
|
|
|
for item in self.doc.get("items"):
|
|
|
|
if hasattr(item, "apply_tds") and item.apply_tds:
|
|
|
|
sum_net_amount += item.net_amount
|
2022-11-02 06:49:51 +00:00
|
|
|
sum_base_net_amount += item.base_net_amount
|
|
|
|
|
2022-10-03 11:09:35 +00:00
|
|
|
self.doc.tax_withholding_net_total = sum_net_amount
|
2022-11-02 06:49:51 +00:00
|
|
|
self.doc.base_tax_withholding_net_total = sum_base_net_amount
|
2022-10-03 11:09:35 +00:00
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
def validate_item_tax_template(self):
|
|
|
|
for item in self.doc.get("items"):
|
|
|
|
if item.item_code and item.get("item_tax_template"):
|
|
|
|
item_doc = frappe.get_cached_doc("Item", item.item_code)
|
|
|
|
args = {
|
2021-06-04 17:23:26 +00:00
|
|
|
"net_rate": item.net_rate or item.rate,
|
2020-01-06 10:04:15 +00:00
|
|
|
"tax_category": self.doc.get("tax_category"),
|
|
|
|
"posting_date": self.doc.get("posting_date"),
|
|
|
|
"bill_date": self.doc.get("bill_date"),
|
2020-06-18 07:21:42 +00:00
|
|
|
"transaction_date": self.doc.get("transaction_date"),
|
|
|
|
"company": self.doc.get("company"),
|
2020-01-06 10:04:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item_group = item_doc.item_group
|
|
|
|
item_group_taxes = []
|
|
|
|
|
|
|
|
while item_group:
|
|
|
|
item_group_doc = frappe.get_cached_doc("Item Group", item_group)
|
|
|
|
item_group_taxes += item_group_doc.taxes or []
|
|
|
|
item_group = item_group_doc.parent_item_group
|
|
|
|
|
|
|
|
item_taxes = item_doc.taxes or []
|
|
|
|
|
|
|
|
if not item_group_taxes and (not item_taxes):
|
|
|
|
# No validation if no taxes in item or item group
|
|
|
|
continue
|
|
|
|
|
|
|
|
taxes = _get_item_tax_template(args, item_taxes + item_group_taxes, for_validate=True)
|
|
|
|
|
2021-06-06 07:55:34 +00:00
|
|
|
if taxes:
|
|
|
|
if item.item_tax_template not in taxes:
|
|
|
|
item.item_tax_template = taxes[0]
|
|
|
|
frappe.msgprint(
|
|
|
|
_("Row {0}: Item Tax template updated as per validity and rate applied").format(
|
|
|
|
item.idx, frappe.bold(item.item_code)
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-01-06 10:04:15 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
def validate_conversion_rate(self):
|
2015-02-17 05:41:11 +00:00
|
|
|
# validate conversion rate
|
2017-03-31 07:14:29 +00:00
|
|
|
company_currency = erpnext.get_company_currency(self.doc.company)
|
2015-02-17 05:41:11 +00:00
|
|
|
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):
|
2022-02-08 05:56:23 +00:00
|
|
|
if self.doc.get("is_consolidated"):
|
|
|
|
return
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
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
|
2019-04-05 14:05:02 +00:00
|
|
|
elif item.price_list_rate:
|
2022-03-25 06:58:55 +00:00
|
|
|
if not item.rate or (item.pricing_rules and item.discount_percentage > 0):
|
2019-04-05 14:05:02 +00:00
|
|
|
item.rate = flt(
|
|
|
|
item.price_list_rate * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2022-03-01 17:39:59 +00:00
|
|
|
|
2022-03-25 06:58:55 +00:00
|
|
|
item.discount_amount = item.price_list_rate * (item.discount_percentage / 100.0)
|
2022-03-01 17:39:59 +00:00
|
|
|
|
2022-03-25 07:09:59 +00:00
|
|
|
elif item.discount_amount and item.pricing_rules:
|
2019-04-05 14:05:02 +00:00
|
|
|
item.rate = item.price_list_rate - item.discount_amount
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-03-01 17:39:59 +00:00
|
|
|
if item.doctype in [
|
|
|
|
"Quotation Item",
|
|
|
|
"Sales Order Item",
|
|
|
|
"Delivery Note Item",
|
|
|
|
"Sales Invoice Item",
|
|
|
|
"POS Invoice Item",
|
|
|
|
"Purchase Invoice Item",
|
|
|
|
"Purchase Order Item",
|
|
|
|
"Purchase Receipt Item",
|
|
|
|
]:
|
2017-11-14 11:52:41 +00:00
|
|
|
item.rate_with_margin, item.base_rate_with_margin = self.calculate_margin(item)
|
2019-04-23 08:07:19 +00:00
|
|
|
if flt(item.rate_with_margin) > 0:
|
|
|
|
item.rate = flt(
|
|
|
|
item.rate_with_margin * (1.0 - (item.discount_percentage / 100.0)), item.precision("rate")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-04-13 10:16:01 +00:00
|
|
|
|
2021-04-03 14:18:46 +00:00
|
|
|
if item.discount_amount and not item.discount_percentage:
|
2021-04-13 10:16:01 +00:00
|
|
|
item.rate = item.rate_with_margin - item.discount_amount
|
2021-04-03 14:18:46 +00:00
|
|
|
else:
|
|
|
|
item.discount_amount = item.rate_with_margin - item.rate
|
2021-04-13 10:16:01 +00:00
|
|
|
|
2019-04-23 08:07:19 +00:00
|
|
|
elif flt(item.price_list_rate) > 0:
|
|
|
|
item.discount_amount = item.price_list_rate - item.rate
|
2019-03-18 09:04:19 +00:00
|
|
|
elif flt(item.price_list_rate) > 0 and not item.discount_amount:
|
|
|
|
item.discount_amount = item.price_list_rate - item.rate
|
2016-03-05 09:40:25 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_rate = item.rate
|
2019-07-31 10:28:01 +00:00
|
|
|
|
2019-08-03 08:10:37 +00:00
|
|
|
if not item.qty and self.doc.get("is_return"):
|
2019-07-31 10:28:01 +00:00
|
|
|
item.amount = flt(-1 * item.rate, item.precision("amount"))
|
2021-12-22 07:53:42 +00:00
|
|
|
elif not item.qty and self.doc.get("is_debit_note"):
|
|
|
|
item.amount = flt(item.rate, item.precision("amount"))
|
2019-07-31 10:28:01 +00:00
|
|
|
else:
|
|
|
|
item.amount = flt(item.rate * item.qty, item.precision("amount"))
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
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)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-20 09:10:35 +00:00
|
|
|
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
|
|
|
|
2021-07-23 10:20:37 +00:00
|
|
|
if not (self.doc.get("is_consolidated") or tax.get("dont_recompute_tax")):
|
2021-04-12 05:26:47 +00:00
|
|
|
tax.item_wise_tax_detail = {}
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
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",
|
|
|
|
]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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
|
2020-08-11 15:04:57 +00:00
|
|
|
total_inclusive_tax_amount_per_qty = 0
|
2015-02-17 05:41:11 +00:00
|
|
|
for i, tax in enumerate(self.doc.get("taxes")):
|
2022-03-28 13:22:46 +00:00
|
|
|
(
|
2020-08-11 15:04:57 +00:00
|
|
|
tax.tax_fraction_for_current_item,
|
|
|
|
inclusive_tax_amount_per_qty,
|
|
|
|
) = self.get_current_tax_fraction(tax, item_tax_map)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
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
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
cumulated_tax_fraction += tax.tax_fraction_for_current_item
|
2020-08-12 15:28:03 +00:00
|
|
|
total_inclusive_tax_amount_per_qty += inclusive_tax_amount_per_qty * flt(item.qty)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2020-08-11 15:04:57 +00:00
|
|
|
if (
|
|
|
|
not self.discount_amount_applied
|
|
|
|
and item.qty
|
|
|
|
and (cumulated_tax_fraction or total_inclusive_tax_amount_per_qty)
|
|
|
|
):
|
|
|
|
amount = flt(item.amount) - total_inclusive_tax_amount_per_qty
|
|
|
|
|
|
|
|
item.net_amount = flt(amount / (1 + cumulated_tax_fraction))
|
2015-02-20 09:10:35 +00:00
|
|
|
item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate"))
|
2017-09-19 09:23:16 +00:00
|
|
|
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"])
|
|
|
|
|
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
|
2020-08-11 15:04:57 +00:00
|
|
|
inclusive_tax_amount_per_qty = 0
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
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
|
2020-08-26 12:53:12 +00:00
|
|
|
|
2020-08-11 15:04:57 +00:00
|
|
|
elif tax.charge_type == "On Item Quantity":
|
|
|
|
inclusive_tax_amount_per_qty = flt(tax_rate)
|
|
|
|
|
|
|
|
if getattr(tax, "add_deduct_tax", None) and tax.add_deduct_tax == "Deduct":
|
|
|
|
current_tax_fraction *= -1.0
|
|
|
|
inclusive_tax_amount_per_qty *= -1.0
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2020-08-11 15:04:57 +00:00
|
|
|
return current_tax_fraction, inclusive_tax_amount_per_qty
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def _get_tax_rate(self, tax, item_tax_map):
|
2018-03-08 08:51:48 +00:00
|
|
|
if tax.account_head in item_tax_map:
|
2015-02-17 05:41:11 +00:00
|
|
|
return flt(item_tax_map.get(tax.account_head), self.doc.precision("rate", tax))
|
|
|
|
else:
|
|
|
|
return tax.rate
|
|
|
|
|
|
|
|
def calculate_net_total(self):
|
2018-05-28 06:19:08 +00:00
|
|
|
self.doc.total_qty = (
|
|
|
|
self.doc.total
|
|
|
|
) = self.doc.base_total = self.doc.net_total = self.doc.base_net_total = 0.0
|
2018-06-25 04:40:29 +00:00
|
|
|
|
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
|
2018-05-28 06:19:08 +00:00
|
|
|
self.doc.total_qty += item.qty
|
2015-02-22 20:10:01 +00:00
|
|
|
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
|
|
|
|
2021-11-16 13:36:49 +00:00
|
|
|
def calculate_shipping_charges(self):
|
2022-04-04 14:35:10 +00:00
|
|
|
|
|
|
|
# Do not apply shipping rule for POS
|
2022-04-06 03:57:40 +00:00
|
|
|
if self.doc.get("is_pos"):
|
2022-04-04 14:35:10 +00:00
|
|
|
return
|
|
|
|
|
2021-11-10 11:19:12 +00:00
|
|
|
if hasattr(self.doc, "shipping_rule") and self.doc.shipping_rule:
|
2021-11-10 10:27:41 +00:00
|
|
|
shipping_rule = frappe.get_doc("Shipping Rule", self.doc.shipping_rule)
|
|
|
|
shipping_rule.apply(self.doc)
|
|
|
|
|
2022-03-10 15:26:36 +00:00
|
|
|
self._calculate()
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def calculate_taxes(self):
|
2022-03-07 12:31:07 +00:00
|
|
|
rounding_adjustment_computed = self.doc.get("is_consolidated") and self.doc.get(
|
|
|
|
"rounding_adjustment"
|
|
|
|
)
|
|
|
|
if not rounding_adjustment_computed:
|
2021-10-29 11:15:04 +00:00
|
|
|
self.doc.rounding_adjustment = 0
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
# maintain actual tax rate based on idx
|
2015-02-20 09:10:35 +00:00
|
|
|
actual_tax_dict = dict(
|
2022-03-28 13:22:46 +00:00
|
|
|
[
|
2015-02-20 09:10:35 +00:00
|
|
|
[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"
|
2022-03-28 13:22:46 +00:00
|
|
|
]
|
2015-02-17 05:41:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
current_tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(current_tax_amount, tax)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
# 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:
|
2017-07-31 12:37:45 +00:00
|
|
|
tax.grand_total_for_current_item = flt(item.net_amount + current_tax_amount)
|
2015-02-17 05:41:11 +00:00
|
|
|
else:
|
|
|
|
tax.grand_total_for_current_item = flt(
|
2017-07-31 12:37:45 +00:00
|
|
|
self.doc.get("taxes")[i - 1].grand_total_for_current_item + current_tax_amount
|
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
# set precision in the last item iteration
|
|
|
|
if n == len(self.doc.get("items")) - 1:
|
|
|
|
self.round_off_totals(tax)
|
2021-04-14 05:50:27 +00:00
|
|
|
self._set_in_company_currency(tax, ["tax_amount", "tax_amount_after_discount_amount"])
|
|
|
|
|
|
|
|
self.round_off_base_values(tax)
|
2017-07-31 12:37:45 +00:00
|
|
|
self.set_cumulative_total(i, tax)
|
|
|
|
|
2021-04-14 05:50:27 +00:00
|
|
|
self._set_in_company_currency(tax, ["total"])
|
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
|
2021-10-29 11:15:04 +00:00
|
|
|
and self.doc.discount_amount
|
|
|
|
and self.doc.apply_discount_on == "Grand Total"
|
2022-03-07 12:31:07 +00:00
|
|
|
and not rounding_adjustment_computed
|
|
|
|
):
|
2017-09-19 09:23:16 +00:00
|
|
|
self.doc.rounding_adjustment = flt(
|
|
|
|
self.doc.grand_total - flt(self.doc.discount_amount) - tax.total,
|
|
|
|
self.doc.precision("rounding_adjustment"),
|
|
|
|
)
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
def get_tax_amount_if_for_valuation_or_deduction(self, tax_amount, tax):
|
|
|
|
# if just for valuation, do not add the tax amount in total
|
|
|
|
# if tax/charges is for deduction, multiply by -1
|
|
|
|
if getattr(tax, "category", None):
|
|
|
|
tax_amount = 0.0 if (tax.category == "Valuation") else tax_amount
|
2017-11-17 09:01:09 +00:00
|
|
|
if self.doc.doctype in [
|
|
|
|
"Purchase Order",
|
|
|
|
"Purchase Invoice",
|
|
|
|
"Purchase Receipt",
|
|
|
|
"Supplier Quotation",
|
|
|
|
]:
|
|
|
|
tax_amount *= -1.0 if (tax.add_deduct_tax == "Deduct") else 1.0
|
2017-07-31 12:37:45 +00:00
|
|
|
return tax_amount
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2017-07-31 12:37:45 +00:00
|
|
|
def set_cumulative_total(self, row_idx, tax):
|
|
|
|
tax_amount = tax.tax_amount_after_discount_amount
|
|
|
|
tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(tax_amount, tax)
|
|
|
|
|
|
|
|
if row_idx == 0:
|
|
|
|
tax.total = flt(self.doc.net_total + tax_amount, tax.precision("total"))
|
|
|
|
else:
|
|
|
|
tax.total = flt(self.doc.get("taxes")[row_idx - 1].total + tax_amount, tax.precision("total"))
|
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
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-20 09:10:35 +00:00
|
|
|
|
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
|
2018-11-13 05:43:04 +00:00
|
|
|
elif tax.charge_type == "On Item Quantity":
|
2020-08-11 15:04:57 +00:00
|
|
|
current_tax_amount = tax_rate * item.qty
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2021-07-23 10:20:37 +00:00
|
|
|
if not (self.doc.get("is_consolidated") or tax.get("dont_recompute_tax")):
|
2021-04-12 05:26:47 +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
|
|
|
|
2017-08-24 10:52:28 +00:00
|
|
|
tax.item_wise_tax_detail[key] = [tax_rate, flt(item_wise_tax_amount)]
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def round_off_totals(self, tax):
|
2021-04-14 05:50:27 +00:00
|
|
|
if tax.account_head in frappe.flags.round_off_applicable_accounts:
|
|
|
|
tax.tax_amount = round(tax.tax_amount, 0)
|
|
|
|
tax.tax_amount_after_discount_amount = round(tax.tax_amount_after_discount_amount, 0)
|
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
|
2017-11-29 10:39:59 +00:00
|
|
|
tax.tax_amount_after_discount_amount = flt(
|
|
|
|
tax.tax_amount_after_discount_amount, tax.precision("tax_amount")
|
2017-07-31 12:37:45 +00:00
|
|
|
)
|
2015-02-22 14:44:49 +00:00
|
|
|
|
2021-04-14 05:50:27 +00:00
|
|
|
def round_off_base_values(self, tax):
|
|
|
|
# Round off to nearest integer based on regional settings
|
|
|
|
if tax.account_head in frappe.flags.round_off_applicable_accounts:
|
|
|
|
tax.base_tax_amount = round(tax.base_tax_amount, 0)
|
|
|
|
tax.base_tax_amount_after_discount_amount = round(tax.base_tax_amount_after_discount_amount, 0)
|
|
|
|
|
2015-03-17 05:20:47 +00:00
|
|
|
def manipulate_grand_total_for_inclusive_tax(self):
|
|
|
|
# if fully inclusive taxes and diff
|
2021-06-11 13:10:22 +00:00
|
|
|
if self.doc.get("taxes") and any(cint(t.included_in_print_rate) for t in self.doc.get("taxes")):
|
2015-03-17 05:20:47 +00:00
|
|
|
last_tax = self.doc.get("taxes")[-1]
|
2021-06-11 13:10:22 +00:00
|
|
|
non_inclusive_tax_amount = sum(
|
|
|
|
flt(d.tax_amount_after_discount_amount)
|
|
|
|
for d in self.doc.get("taxes")
|
|
|
|
if not d.included_in_print_rate
|
|
|
|
)
|
2019-12-25 08:29:24 +00:00
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
diff = (
|
|
|
|
self.doc.total + non_inclusive_tax_amount - flt(last_tax.total, last_tax.precision("total"))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-12-25 08:29:24 +00:00
|
|
|
|
|
|
|
# If discount amount applied, deduct the discount amount
|
|
|
|
# because self.doc.total is always without discount, but last_tax.total is after discount
|
|
|
|
if self.discount_amount_applied and self.doc.discount_amount:
|
|
|
|
diff -= flt(self.doc.discount_amount)
|
|
|
|
|
|
|
|
diff = flt(diff, self.doc.precision("rounding_adjustment"))
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
if diff and abs(diff) <= (5.0 / 10 ** last_tax.precision("tax_amount")):
|
2019-12-25 08:29:24 +00:00
|
|
|
self.doc.rounding_adjustment = diff
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
def calculate_totals(self):
|
2021-10-29 11:15:04 +00:00
|
|
|
if self.doc.get("taxes"):
|
|
|
|
self.doc.grand_total = flt(self.doc.get("taxes")[-1].total) + flt(self.doc.rounding_adjustment)
|
|
|
|
else:
|
|
|
|
self.doc.grand_total = flt(self.doc.net_total)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2021-10-29 11:15:04 +00:00
|
|
|
if self.doc.get("taxes"):
|
|
|
|
self.doc.total_taxes_and_charges = flt(
|
|
|
|
self.doc.grand_total - self.doc.net_total - flt(self.doc.rounding_adjustment),
|
2017-09-19 09:23:16 +00:00
|
|
|
self.doc.precision("total_taxes_and_charges"),
|
|
|
|
)
|
2021-10-29 11:15:04 +00:00
|
|
|
else:
|
|
|
|
self.doc.total_taxes_and_charges = 0.0
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
self._set_in_company_currency(self.doc, ["total_taxes_and_charges", "rounding_adjustment"])
|
2015-03-05 14:01:23 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
if self.doc.doctype in [
|
|
|
|
"Quotation",
|
|
|
|
"Sales Order",
|
|
|
|
"Delivery Note",
|
|
|
|
"Sales Invoice",
|
|
|
|
"POS Invoice",
|
|
|
|
]:
|
2019-07-17 09:25:16 +00:00
|
|
|
self.doc.base_grand_total = (
|
|
|
|
flt(self.doc.grand_total * self.doc.conversion_rate, self.doc.precision("base_grand_total"))
|
2015-02-20 09:10:35 +00:00
|
|
|
if self.doc.total_taxes_and_charges
|
|
|
|
else self.doc.base_net_total
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
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-07-31 11:23:13 +00:00
|
|
|
self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
|
2015-02-17 05:41:11 +00:00
|
|
|
else:
|
2015-07-31 11:23:13 +00:00
|
|
|
self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_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
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2017-09-19 09:23:16 +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
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
self.set_rounded_total()
|
|
|
|
|
2018-05-26 03:53:02 +00:00
|
|
|
def calculate_total_net_weight(self):
|
|
|
|
if self.doc.meta.get_field("total_net_weight"):
|
|
|
|
self.doc.total_net_weight = 0.0
|
|
|
|
for d in self.doc.items:
|
|
|
|
if d.total_weight:
|
|
|
|
self.doc.total_net_weight += d.total_weight
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
def set_rounded_total(self):
|
2022-03-07 12:31:07 +00:00
|
|
|
if self.doc.get("is_consolidated") and self.doc.get("rounding_adjustment"):
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.doc.meta.get_field("rounded_total"):
|
|
|
|
if self.doc.is_rounded_total_disabled():
|
|
|
|
self.doc.rounded_total = self.doc.base_rounded_total = 0
|
|
|
|
return
|
2017-11-17 06:57:43 +00:00
|
|
|
|
2022-03-07 12:31:07 +00:00
|
|
|
self.doc.rounded_total = round_based_on_smallest_currency_fraction(
|
|
|
|
self.doc.grand_total, self.doc.currency, self.doc.precision("rounded_total")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2017-09-19 09:23:16 +00:00
|
|
|
|
2022-03-07 12:31:07 +00:00
|
|
|
# if print_in_rate is set, we would have already calculated rounding adjustment
|
|
|
|
self.doc.rounding_adjustment += flt(
|
|
|
|
self.doc.rounded_total - self.doc.grand_total, self.doc.precision("rounding_adjustment")
|
|
|
|
)
|
2017-11-17 06:57:43 +00:00
|
|
|
|
2022-03-07 12:31:07 +00:00
|
|
|
self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"])
|
2017-11-17 06:57:43 +00:00
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def _cleanup(self):
|
2021-04-12 05:26:47 +00:00
|
|
|
if not self.doc.get("is_consolidated"):
|
|
|
|
for tax in self.doc.get("taxes"):
|
2021-07-23 10:20:37 +00:00
|
|
|
if not tax.get("dont_recompute_tax"):
|
|
|
|
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(",", ":"))
|
2016-04-04 09:33:28 +00:00
|
|
|
|
2015-12-18 07:42:02 +00:00
|
|
|
def set_discount_amount(self):
|
2016-10-13 06:44:32 +00:00
|
|
|
if self.doc.additional_discount_percentage:
|
2016-04-04 09:33:28 +00:00
|
|
|
self.doc.discount_amount = flt(
|
|
|
|
flt(self.doc.get(scrub(self.doc.apply_discount_on)))
|
2015-12-18 07:42:02 +00:00
|
|
|
* self.doc.additional_discount_percentage
|
|
|
|
/ 100,
|
|
|
|
self.doc.precision("discount_amount"),
|
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
|
|
|
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"))
|
|
|
|
|
2022-08-21 12:21:05 +00:00
|
|
|
self.doc.base_discount_amount = flt(
|
|
|
|
self.doc.discount_amount * self.doc.conversion_rate, self.doc.precision("base_discount_amount")
|
|
|
|
)
|
|
|
|
|
2022-07-03 05:32:21 +00:00
|
|
|
if self.doc.apply_discount_on == "Grand Total" and self.doc.get(
|
|
|
|
"is_cash_or_non_trade_discount"
|
|
|
|
):
|
2022-06-19 15:48:12 +00:00
|
|
|
self.discount_amount_applied = True
|
|
|
|
return
|
|
|
|
|
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
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
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
|
2019-05-30 10:20:46 +00:00
|
|
|
if (
|
|
|
|
self.doc.apply_discount_on == "Net Total"
|
|
|
|
or not taxes
|
|
|
|
or total_for_discount_amount == self.doc.net_total
|
2015-03-04 09:36:56 +00:00
|
|
|
) and i == len(self.doc.get("items")) - 1:
|
2016-12-21 12:00:29 +00:00
|
|
|
discount_amount_loss = flt(
|
|
|
|
self.doc.net_total - net_total - self.doc.discount_amount, self.doc.precision("net_total")
|
2015-03-04 09:36:56 +00:00
|
|
|
)
|
2016-12-21 12:00:29 +00:00
|
|
|
|
2015-03-05 14:01:23 +00:00
|
|
|
item.net_amount = flt(item.net_amount + discount_amount_loss, item.precision("net_amount"))
|
|
|
|
|
2015-10-10 12:40:05 +00:00
|
|
|
item.net_rate = flt(item.net_amount / item.qty, item.precision("net_rate")) if item.qty else 0
|
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"):
|
2020-08-11 15:04:57 +00:00
|
|
|
if tax.charge_type in ["Actual", "On Item Quantity"]:
|
2017-12-12 13:20:05 +00:00
|
|
|
tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(tax.tax_amount, tax)
|
|
|
|
actual_taxes_dict.setdefault(tax.idx, tax_amount)
|
2015-02-20 09:10:35 +00:00
|
|
|
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
|
|
|
|
2017-11-17 06:57:43 +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:
|
2021-06-11 13:10:22 +00:00
|
|
|
total_allocated_amount = sum(
|
|
|
|
flt(adv.allocated_amount, adv.precision("allocated_amount"))
|
|
|
|
for adv in self.doc.get("advances")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-20 09:10:35 +00:00
|
|
|
self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))
|
2016-04-04 09:33:28 +00:00
|
|
|
|
2018-02-08 08:03:52 +00:00
|
|
|
grand_total = self.doc.rounded_total or self.doc.grand_total
|
|
|
|
|
2016-02-08 05:36:55 +00:00
|
|
|
if self.doc.party_account_currency == self.doc.currency:
|
2018-02-08 08:03:52 +00:00
|
|
|
invoice_total = flt(
|
|
|
|
grand_total - flt(self.doc.write_off_amount), self.doc.precision("grand_total")
|
2016-02-08 05:36:55 +00:00
|
|
|
)
|
2017-04-03 11:56:22 +00:00
|
|
|
else:
|
2017-11-29 10:39:59 +00:00
|
|
|
base_write_off_amount = flt(
|
|
|
|
flt(self.doc.write_off_amount) * self.doc.conversion_rate,
|
2017-04-03 11:56:22 +00:00
|
|
|
self.doc.precision("base_write_off_amount"),
|
|
|
|
)
|
2018-02-08 08:03:52 +00:00
|
|
|
invoice_total = (
|
|
|
|
flt(grand_total * self.doc.conversion_rate, self.doc.precision("grand_total"))
|
2017-04-03 11:56:22 +00:00
|
|
|
- base_write_off_amount
|
|
|
|
)
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2016-02-09 05:01:11 +00:00
|
|
|
if invoice_total > 0 and self.doc.total_advance > invoice_total:
|
2016-02-08 05:36:55 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Advance amount cannot be greater than {0} {1}").format(
|
|
|
|
self.doc.party_account_currency, invoice_total
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2015-02-18 14:52:59 +00:00
|
|
|
if self.doc.docstatus == 0:
|
2022-03-28 14:44:19 +00:00
|
|
|
if self.doc.get("write_off_outstanding_amount_automatically"):
|
2022-03-25 12:32:14 +00:00
|
|
|
self.doc.write_off_amount = 0
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
self.calculate_outstanding_amount()
|
2022-03-24 07:01:37 +00:00
|
|
|
self.calculate_write_off_amount()
|
2015-02-17 05:41:11 +00:00
|
|
|
|
2020-12-11 16:00:39 +00:00
|
|
|
def is_internal_invoice(self):
|
|
|
|
"""
|
|
|
|
Checks if its an internal transfer invoice
|
|
|
|
and decides if to calculate any out standing amount or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.doc.doctype in ("Sales Invoice", "Purchase Invoice") and self.doc.is_internal_transfer():
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2015-02-17 05:41:11 +00:00
|
|
|
def calculate_outstanding_amount(self):
|
|
|
|
# NOTE:
|
|
|
|
# write_off_amount is only for POS Invoice
|
|
|
|
# total_advance is only for non POS Invoice
|
2016-08-24 20:39:53 +00:00
|
|
|
if self.doc.doctype == "Sales Invoice":
|
|
|
|
self.calculate_paid_amount()
|
|
|
|
|
2020-12-11 16:00:39 +00:00
|
|
|
if (
|
|
|
|
self.doc.is_return
|
|
|
|
and self.doc.return_against
|
|
|
|
and not self.doc.get("is_pos")
|
|
|
|
or self.is_internal_invoice()
|
|
|
|
):
|
|
|
|
return
|
2016-04-04 09:33:28 +00:00
|
|
|
|
2015-08-27 06:58:36 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
|
2016-04-04 09:33:28 +00:00
|
|
|
self._set_in_company_currency(self.doc, ["write_off_amount"])
|
|
|
|
|
2017-11-17 06:57:43 +00:00
|
|
|
if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
|
|
|
|
grand_total = self.doc.rounded_total or self.doc.grand_total
|
2022-01-06 13:28:49 +00:00
|
|
|
base_grand_total = self.doc.base_rounded_total or self.doc.base_grand_total
|
|
|
|
|
2017-11-17 06:57:43 +00:00
|
|
|
if self.doc.party_account_currency == self.doc.currency:
|
2018-07-06 07:06:57 +00:00
|
|
|
total_amount_to_pay = flt(
|
2017-11-17 06:57:43 +00:00
|
|
|
grand_total - self.doc.total_advance - flt(self.doc.write_off_amount),
|
|
|
|
self.doc.precision("grand_total"),
|
|
|
|
)
|
|
|
|
else:
|
2022-01-06 13:28:49 +00:00
|
|
|
total_amount_to_pay = flt(
|
|
|
|
flt(base_grand_total, self.doc.precision("base_grand_total"))
|
|
|
|
- self.doc.total_advance
|
|
|
|
- flt(self.doc.base_write_off_amount),
|
|
|
|
self.doc.precision("base_grand_total"),
|
|
|
|
)
|
2016-04-04 09:33:28 +00:00
|
|
|
|
2015-08-27 06:58:36 +00:00
|
|
|
self.doc.round_floats_in(self.doc, ["paid_amount"])
|
2017-11-17 06:57:43 +00:00
|
|
|
change_amount = 0
|
|
|
|
|
2020-02-25 07:51:16 +00:00
|
|
|
if self.doc.doctype == "Sales Invoice" and not self.doc.get("is_return"):
|
2017-11-17 06:57:43 +00:00
|
|
|
self.calculate_change_amount()
|
|
|
|
change_amount = (
|
|
|
|
self.doc.change_amount
|
|
|
|
if self.doc.party_account_currency == self.doc.currency
|
|
|
|
else self.doc.base_change_amount
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2015-11-13 07:33:10 +00:00
|
|
|
paid_amount = (
|
|
|
|
self.doc.paid_amount
|
|
|
|
if self.doc.party_account_currency == self.doc.currency
|
|
|
|
else self.doc.base_paid_amount
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2017-11-17 06:57:43 +00:00
|
|
|
self.doc.outstanding_amount = flt(
|
|
|
|
total_amount_to_pay - flt(paid_amount) + flt(change_amount),
|
|
|
|
self.doc.precision("outstanding_amount"),
|
|
|
|
)
|
2016-12-21 12:00:29 +00:00
|
|
|
|
2022-08-10 08:47:28 +00:00
|
|
|
if (
|
|
|
|
self.doc.doctype == "Sales Invoice"
|
|
|
|
and self.doc.get("is_pos")
|
|
|
|
and self.doc.get("pos_profile")
|
|
|
|
and self.doc.get("is_consolidated")
|
|
|
|
):
|
|
|
|
write_off_limit = flt(
|
|
|
|
frappe.db.get_value("POS Profile", self.doc.pos_profile, "write_off_limit")
|
|
|
|
)
|
|
|
|
if write_off_limit and abs(self.doc.outstanding_amount) <= write_off_limit:
|
|
|
|
self.doc.write_off_outstanding_amount_automatically = 1
|
|
|
|
|
2022-03-12 13:50:48 +00:00
|
|
|
if (
|
|
|
|
self.doc.doctype == "Sales Invoice"
|
|
|
|
and self.doc.get("is_pos")
|
|
|
|
and self.doc.get("is_return")
|
|
|
|
and not self.doc.get("is_consolidated")
|
|
|
|
):
|
2021-08-23 05:35:07 +00:00
|
|
|
self.set_total_amount_to_default_mop(total_amount_to_pay)
|
|
|
|
self.calculate_paid_amount()
|
2020-02-25 07:51:16 +00:00
|
|
|
|
2016-04-09 09:01:09 +00:00
|
|
|
def calculate_paid_amount(self):
|
2018-07-06 07:06:57 +00:00
|
|
|
|
2016-04-09 09:01:09 +00:00
|
|
|
paid_amount = base_paid_amount = 0.0
|
2017-01-17 06:41:57 +00:00
|
|
|
|
|
|
|
if self.doc.is_pos:
|
|
|
|
for payment in self.doc.get("payments"):
|
2017-05-25 08:44:55 +00:00
|
|
|
payment.amount = flt(payment.amount)
|
|
|
|
payment.base_amount = payment.amount * flt(self.doc.conversion_rate)
|
2017-01-17 06:41:57 +00:00
|
|
|
paid_amount += payment.amount
|
|
|
|
base_paid_amount += payment.base_amount
|
2017-05-16 05:59:57 +00:00
|
|
|
elif not self.doc.is_return:
|
|
|
|
self.doc.set("payments", [])
|
2016-04-09 09:01:09 +00:00
|
|
|
|
2018-07-06 07:06:57 +00:00
|
|
|
if self.doc.redeem_loyalty_points and self.doc.loyalty_amount:
|
|
|
|
base_paid_amount += self.doc.loyalty_amount
|
|
|
|
paid_amount += self.doc.loyalty_amount / flt(self.doc.conversion_rate)
|
|
|
|
|
2016-04-09 09:01:09 +00:00
|
|
|
self.doc.paid_amount = flt(paid_amount, self.doc.precision("paid_amount"))
|
|
|
|
self.doc.base_paid_amount = flt(base_paid_amount, self.doc.precision("base_paid_amount"))
|
|
|
|
|
2016-08-02 11:11:10 +00:00
|
|
|
def calculate_change_amount(self):
|
|
|
|
self.doc.change_amount = 0.0
|
2016-08-30 20:34:37 +00:00
|
|
|
self.doc.base_change_amount = 0.0
|
2022-02-08 11:37:51 +00:00
|
|
|
grand_total = self.doc.rounded_total or self.doc.grand_total
|
|
|
|
base_grand_total = self.doc.base_rounded_total or self.doc.base_grand_total
|
2017-11-17 06:57:43 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
self.doc.doctype == "Sales Invoice"
|
2022-02-09 04:35:06 +00:00
|
|
|
and self.doc.paid_amount > grand_total
|
|
|
|
and not self.doc.is_return
|
2021-06-11 13:10:22 +00:00
|
|
|
and any(d.type == "Cash" for d in self.doc.payments)
|
|
|
|
):
|
2018-02-05 12:43:29 +00:00
|
|
|
self.doc.change_amount = flt(
|
2022-03-28 14:44:19 +00:00
|
|
|
self.doc.paid_amount - grand_total, self.doc.precision("change_amount")
|
2016-08-02 11:11:10 +00:00
|
|
|
)
|
2017-05-30 10:05:01 +00:00
|
|
|
|
2018-02-05 12:43:29 +00:00
|
|
|
self.doc.base_change_amount = flt(
|
2022-03-28 14:44:19 +00:00
|
|
|
self.doc.base_paid_amount - base_grand_total, self.doc.precision("base_change_amount")
|
2016-08-30 20:34:37 +00:00
|
|
|
)
|
2016-01-18 11:01:10 +00:00
|
|
|
|
2016-08-04 09:26:15 +00:00
|
|
|
def calculate_write_off_amount(self):
|
2022-03-28 14:44:19 +00:00
|
|
|
if self.doc.get("write_off_outstanding_amount_automatically"):
|
2017-11-17 06:57:43 +00:00
|
|
|
self.doc.write_off_amount = flt(
|
2022-03-28 14:44:19 +00:00
|
|
|
self.doc.outstanding_amount, self.doc.precision("write_off_amount")
|
2017-11-17 06:57:43 +00:00
|
|
|
)
|
2016-08-05 10:11:36 +00:00
|
|
|
self.doc.base_write_off_amount = flt(
|
|
|
|
self.doc.write_off_amount * self.doc.conversion_rate,
|
|
|
|
self.doc.precision("base_write_off_amount"),
|
|
|
|
)
|
2016-08-04 09:26:15 +00:00
|
|
|
|
2022-03-24 07:01:37 +00:00
|
|
|
self.calculate_outstanding_amount()
|
|
|
|
|
2016-01-18 11:01:10 +00:00
|
|
|
def calculate_margin(self, item):
|
2017-05-11 06:10:02 +00:00
|
|
|
rate_with_margin = 0.0
|
2017-11-14 11:52:41 +00:00
|
|
|
base_rate_with_margin = 0.0
|
2016-01-18 11:01:10 +00:00
|
|
|
if item.price_list_rate:
|
2019-03-18 09:04:19 +00:00
|
|
|
if item.pricing_rules and not self.doc.ignore_pricing_rule:
|
2020-07-16 11:57:26 +00:00
|
|
|
has_margin = False
|
2020-08-26 12:53:12 +00:00
|
|
|
for d in get_applied_pricing_rules(item.pricing_rules):
|
2019-11-19 13:17:48 +00:00
|
|
|
pricing_rule = frappe.get_cached_doc("Pricing Rule", d)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-02-01 17:28:22 +00:00
|
|
|
if pricing_rule.margin_rate_or_amount and (
|
2022-03-28 13:22:46 +00:00
|
|
|
(
|
2021-02-01 17:28:22 +00:00
|
|
|
pricing_rule.currency == self.doc.currency
|
|
|
|
and pricing_rule.margin_type in ["Amount", "Percentage"]
|
|
|
|
)
|
|
|
|
or pricing_rule.margin_type == "Percentage"
|
|
|
|
):
|
2019-03-18 09:04:19 +00:00
|
|
|
item.margin_type = pricing_rule.margin_type
|
|
|
|
item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount
|
2020-07-16 11:57:26 +00:00
|
|
|
has_margin = True
|
|
|
|
|
|
|
|
if not has_margin:
|
|
|
|
item.margin_type = None
|
|
|
|
item.margin_rate_or_amount = 0.0
|
2016-01-18 11:01:10 +00:00
|
|
|
|
2021-06-20 19:29:02 +00:00
|
|
|
if not item.pricing_rules and flt(item.rate) > flt(item.price_list_rate):
|
|
|
|
item.margin_type = "Amount"
|
|
|
|
item.margin_rate_or_amount = flt(
|
|
|
|
item.rate - item.price_list_rate, item.precision("margin_rate_or_amount")
|
|
|
|
)
|
|
|
|
item.rate_with_margin = item.rate
|
|
|
|
|
|
|
|
elif item.margin_type and item.margin_rate_or_amount:
|
2016-03-05 09:40:25 +00:00
|
|
|
margin_value = (
|
|
|
|
item.margin_rate_or_amount
|
|
|
|
if item.margin_type == "Amount"
|
|
|
|
else flt(item.price_list_rate) * flt(item.margin_rate_or_amount) / 100
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2017-05-11 06:10:02 +00:00
|
|
|
rate_with_margin = flt(item.price_list_rate) + flt(margin_value)
|
2017-11-14 11:52:41 +00:00
|
|
|
base_rate_with_margin = flt(rate_with_margin) * flt(self.doc.conversion_rate)
|
2016-01-18 11:01:10 +00:00
|
|
|
|
2017-11-14 11:52:41 +00:00
|
|
|
return rate_with_margin, base_rate_with_margin
|
2017-07-05 07:28:19 +00:00
|
|
|
|
|
|
|
def set_item_wise_tax_breakup(self):
|
2017-07-20 08:02:01 +00:00
|
|
|
self.doc.other_charges_calculation = get_itemised_tax_breakup_html(self.doc)
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2021-08-23 05:35:07 +00:00
|
|
|
def set_total_amount_to_default_mop(self, total_amount_to_pay):
|
2022-10-31 14:28:46 +00:00
|
|
|
total_paid_amount = 0
|
|
|
|
for payment in self.doc.get("payments"):
|
|
|
|
total_paid_amount += (
|
|
|
|
payment.amount if self.doc.party_account_currency == self.doc.currency else payment.base_amount
|
|
|
|
)
|
|
|
|
|
|
|
|
pending_amount = total_amount_to_pay - total_paid_amount
|
2020-02-25 07:51:16 +00:00
|
|
|
|
2022-10-31 14:28:46 +00:00
|
|
|
if pending_amount > 0:
|
|
|
|
default_mode_of_payment = frappe.db.get_value(
|
|
|
|
"POS Payment Method",
|
|
|
|
{"parent": self.doc.pos_profile, "default": 1},
|
|
|
|
["mode_of_payment"],
|
|
|
|
as_dict=1,
|
2021-08-23 05:35:07 +00:00
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-10-31 14:28:46 +00:00
|
|
|
if default_mode_of_payment:
|
|
|
|
self.doc.payments = []
|
|
|
|
self.doc.append(
|
|
|
|
"payments",
|
|
|
|
{
|
|
|
|
"mode_of_payment": default_mode_of_payment.mode_of_payment,
|
|
|
|
"amount": pending_amount,
|
|
|
|
"default": 1,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-02-25 07:51:16 +00:00
|
|
|
|
2017-07-20 08:02:01 +00:00
|
|
|
def get_itemised_tax_breakup_html(doc):
|
|
|
|
if not doc.taxes:
|
|
|
|
return
|
|
|
|
frappe.flags.company = doc.company
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2017-07-20 08:02:01 +00:00
|
|
|
# get headers
|
2017-08-24 10:52:28 +00:00
|
|
|
tax_accounts = []
|
|
|
|
for tax in doc.taxes:
|
|
|
|
if getattr(tax, "category", None) and tax.category == "Valuation":
|
|
|
|
continue
|
2017-12-12 09:10:52 +00:00
|
|
|
if tax.description not in tax_accounts:
|
2017-08-24 10:52:28 +00:00
|
|
|
tax_accounts.append(tax.description)
|
|
|
|
|
2017-07-20 08:02:01 +00:00
|
|
|
headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts)
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2017-07-20 08:02:01 +00:00
|
|
|
# get tax breakup data
|
|
|
|
itemised_tax, itemised_taxable_amount = get_itemised_tax_breakup_data(doc)
|
2017-08-24 10:52:28 +00:00
|
|
|
|
|
|
|
get_rounded_tax_amount(itemised_tax, doc.precision("tax_amount", "taxes"))
|
|
|
|
|
2017-12-28 08:50:13 +00:00
|
|
|
update_itemised_tax_data(doc)
|
2017-07-20 08:02:01 +00:00
|
|
|
frappe.flags.company = None
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2017-07-20 08:02:01 +00:00
|
|
|
return frappe.render_template(
|
|
|
|
"templates/includes/itemised_tax_breakup.html",
|
|
|
|
dict(
|
|
|
|
headers=headers,
|
|
|
|
itemised_tax=itemised_tax,
|
|
|
|
itemised_taxable_amount=itemised_taxable_amount,
|
|
|
|
tax_accounts=tax_accounts,
|
2020-04-13 11:10:13 +00:00
|
|
|
doc=doc,
|
2017-07-17 12:32:31 +00:00
|
|
|
),
|
2017-07-20 08:02:01 +00:00
|
|
|
)
|
2017-07-17 12:32:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-02-19 09:00:23 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_round_off_applicable_accounts(company, account_list):
|
|
|
|
account_list = get_regional_round_off_accounts(company, account_list)
|
|
|
|
|
|
|
|
return account_list
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-02-19 09:00:23 +00:00
|
|
|
@erpnext.allow_regional
|
|
|
|
def get_regional_round_off_accounts(company, account_list):
|
|
|
|
pass
|
2017-12-28 08:50:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-12-28 08:50:13 +00:00
|
|
|
@erpnext.allow_regional
|
|
|
|
def update_itemised_tax_data(doc):
|
|
|
|
# Don't delete this method, used for localization
|
|
|
|
pass
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
@erpnext.allow_regional
|
|
|
|
def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
|
|
|
|
return [_("Item"), _("Taxable Amount")] + tax_accounts
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
@erpnext.allow_regional
|
|
|
|
def get_itemised_tax_breakup_data(doc):
|
|
|
|
itemised_tax = get_itemised_tax(doc.taxes)
|
|
|
|
|
|
|
|
itemised_taxable_amount = get_itemised_taxable_amount(doc.items)
|
|
|
|
|
|
|
|
return itemised_tax, itemised_taxable_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def get_itemised_tax(taxes, with_tax_account=False):
|
2017-07-17 12:32:31 +00:00
|
|
|
itemised_tax = {}
|
|
|
|
for tax in taxes:
|
2017-08-24 10:52:28 +00:00
|
|
|
if getattr(tax, "category", None) and tax.category == "Valuation":
|
|
|
|
continue
|
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
item_tax_map = json.loads(tax.item_wise_tax_detail) if tax.item_wise_tax_detail else {}
|
2017-09-19 09:23:16 +00:00
|
|
|
if item_tax_map:
|
|
|
|
for item_code, tax_data in item_tax_map.items():
|
|
|
|
itemised_tax.setdefault(item_code, frappe._dict())
|
2017-11-29 10:39:59 +00:00
|
|
|
|
2018-06-11 08:01:33 +00:00
|
|
|
tax_rate = 0.0
|
|
|
|
tax_amount = 0.0
|
|
|
|
|
2017-09-19 09:23:16 +00:00
|
|
|
if isinstance(tax_data, list):
|
2018-06-11 08:01:33 +00:00
|
|
|
tax_rate = flt(tax_data[0])
|
|
|
|
tax_amount = flt(tax_data[1])
|
2017-09-19 09:23:16 +00:00
|
|
|
else:
|
2018-06-11 08:01:33 +00:00
|
|
|
tax_rate = flt(tax_data)
|
|
|
|
|
|
|
|
itemised_tax[item_code][tax.description] = frappe._dict(
|
|
|
|
dict(tax_rate=tax_rate, tax_amount=tax_amount)
|
|
|
|
)
|
2017-07-10 07:33:29 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
if with_tax_account:
|
|
|
|
itemised_tax[item_code][tax.description].tax_account = tax.account_head
|
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
return itemised_tax
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-07-17 12:32:31 +00:00
|
|
|
def get_itemised_taxable_amount(items):
|
|
|
|
itemised_taxable_amount = frappe._dict()
|
|
|
|
for item in items:
|
2017-07-10 07:33:29 +00:00
|
|
|
item_code = item.item_code or item.item_name
|
2017-07-17 12:32:31 +00:00
|
|
|
itemised_taxable_amount.setdefault(item_code, 0)
|
|
|
|
itemised_taxable_amount[item_code] += item.net_amount
|
|
|
|
|
2017-08-24 10:52:28 +00:00
|
|
|
return itemised_taxable_amount
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-08-24 10:52:28 +00:00
|
|
|
def get_rounded_tax_amount(itemised_tax, precision):
|
|
|
|
# Rounding based on tax_amount precision
|
|
|
|
for taxes in itemised_tax.values():
|
|
|
|
for tax_account in taxes:
|
2018-11-13 05:43:04 +00:00
|
|
|
taxes[tax_account]["tax_amount"] = flt(taxes[tax_account]["tax_amount"], precision)
|
2020-12-25 13:04:39 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-25 13:04:39 +00:00
|
|
|
class init_landed_taxes_and_totals(object):
|
|
|
|
def __init__(self, doc):
|
|
|
|
self.doc = doc
|
|
|
|
self.tax_field = "taxes" if self.doc.doctype == "Landed Cost Voucher" else "additional_costs"
|
|
|
|
self.set_account_currency()
|
|
|
|
self.set_exchange_rate()
|
|
|
|
self.set_amounts_in_company_currency()
|
|
|
|
|
|
|
|
def set_account_currency(self):
|
|
|
|
company_currency = erpnext.get_company_currency(self.doc.company)
|
|
|
|
for d in self.doc.get(self.tax_field):
|
|
|
|
if not d.account_currency:
|
2022-11-07 03:51:03 +00:00
|
|
|
account_currency = frappe.get_cached_value("Account", d.expense_account, "account_currency")
|
2020-12-25 13:04:39 +00:00
|
|
|
d.account_currency = account_currency or company_currency
|
|
|
|
|
|
|
|
def set_exchange_rate(self):
|
|
|
|
company_currency = erpnext.get_company_currency(self.doc.company)
|
|
|
|
for d in self.doc.get(self.tax_field):
|
|
|
|
if d.account_currency == company_currency:
|
|
|
|
d.exchange_rate = 1
|
2021-03-17 05:26:52 +00:00
|
|
|
elif not d.exchange_rate:
|
2020-12-25 13:04:39 +00:00
|
|
|
d.exchange_rate = get_exchange_rate(
|
|
|
|
self.doc.posting_date,
|
|
|
|
account=d.expense_account,
|
|
|
|
account_currency=d.account_currency,
|
|
|
|
company=self.doc.company,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not d.exchange_rate:
|
|
|
|
frappe.throw(_("Row {0}: Exchange Rate is mandatory").format(d.idx))
|
|
|
|
|
|
|
|
def set_amounts_in_company_currency(self):
|
|
|
|
for d in self.doc.get(self.tax_field):
|
|
|
|
d.amount = flt(d.amount, d.precision("amount"))
|
2022-11-03 05:16:30 +00:00
|
|
|
d.base_amount = flt(d.amount * flt(d.exchange_rate), d.precision("base_amount"))
|