# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt


import json

import frappe
from frappe import _, scrub
from frappe.model.document import Document, bulk_insert
from frappe.query_builder import functions
from frappe.utils import cint, flt, round_based_on_smallest_currency_fraction

import erpnext
from erpnext.accounts.doctype.journal_entry.journal_entry import get_exchange_rate
from erpnext.accounts.doctype.pricing_rule.utils import get_applied_pricing_rules
from erpnext.accounts.utils import get_zero_cutoff
from erpnext.controllers.accounts_controller import (
	validate_conversion_rate,
	validate_inclusive_tax,
	validate_taxes_and_charges,
)
from erpnext.deprecation_dumpster import deprecated
from erpnext.stock.get_item_details import (
	NOT_APPLICABLE_TAX,
	ItemDetailsCtx,
	_get_item_tax_template,
	get_item_tax_map,
)
from erpnext.utilities.regional import temporary_flag


class calculate_taxes_and_totals:
	def __init__(self, doc: Document):
		self.doc = doc
		frappe.flags.round_off_applicable_accounts = (
			get_round_off_applicable_accounts(self.doc.company, [], self.doc) or []
		)
		frappe.flags.round_row_wise_tax = frappe.get_single_value("Accounts Settings", "round_row_wise_tax")

		self._items = self.filter_rows() if self.doc.doctype == "Quotation" else self.doc.get("items")
		self.calculate()

	def filter_rows(self):
		"""Exclude rows, that do not fulfill the filter criteria, from totals computation."""
		items = list(filter(lambda item: not item.get("is_alternative"), self.doc.get("items")))
		return items

	def calculate(self, ignore_tax_template_validation=False):
		if not len(self.doc.items):
			return

		self.discount_amount_applied = False
		self.need_recomputation = False
		self.ignore_tax_template_validation = ignore_tax_template_validation

		self._calculate()

		if self.doc.meta.get_field("discount_amount"):
			self.set_discount_amount()
			self.apply_discount_amount()

		if not ignore_tax_template_validation and self.need_recomputation:
			return self.calculate(ignore_tax_template_validation=True)

		# 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
			self.doc.rounding_adjustment = self.doc.base_rounding_adjustment = 0.0
			self.set_rounded_total()

		self.calculate_shipping_charges()

		if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
			self.calculate_total_advance()

		if self.doc.meta.get_field("other_charges_calculation"):
			self.set_item_wise_tax_breakup()

	def _calculate(self):
		self.validate_conversion_rate()
		self.calculate_item_values()
		self.validate_item_tax_template()
		self.update_item_tax_map()
		self.initialize_taxes()
		self.determine_exclusive_rate()
		self.calculate_net_total()
		self.calculate_taxes()
		self.adjust_grand_total_for_inclusive_tax()
		self.calculate_totals()
		self.calculate_total_net_weight()

	def validate_item_tax_template(self):
		if self.ignore_tax_template_validation:
			return

		if self.doc.get("is_return") and self.doc.get("return_against"):
			return

		for item in self.doc.items:
			if item.item_code and item.get("item_tax_template"):
				item_doc = frappe.get_cached_doc("Item", item.item_code)
				ctx = ItemDetailsCtx(
					{
						"net_rate": item.net_rate or item.rate,
						"base_net_rate": item.base_net_rate or item.base_rate,
						"tax_category": self.doc.get("tax_category"),
						"posting_date": self.doc.get("posting_date"),
						"bill_date": self.doc.get("bill_date"),
						"transaction_date": self.doc.get("transaction_date"),
						"company": self.doc.get("company"),
					}
				)

				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(ctx, item_taxes + item_group_taxes, for_validate=True)

				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)
							)
						)

						# For correct tax_amount calculation re-computation is required
						if self.discount_amount_applied and self.doc.apply_discount_on == "Grand Total":
							self.need_recomputation = True

	def update_item_tax_map(self):
		for item in self.doc.items:
			item.item_tax_rate = get_item_tax_map(
				doc=self.doc,
				tax_template=item.item_tax_template,
				as_json=True,
			)

	def validate_conversion_rate(self):
		# validate conversion rate
		company_currency = erpnext.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_rate(self, item):
		if not item.price_list_rate:
			remove_margin(item)
			remove_discount(item)
			item.rate_with_margin = 0
			return

		has_pricing_rules = item.pricing_rules and not self.doc.ignore_pricing_rule
		if has_pricing_rules:
			remove_margin(item)

			for d in get_applied_pricing_rules(item.pricing_rules):
				pricing_rule = frappe.get_cached_doc("Pricing Rule", d)

				if not (
					pricing_rule.margin_type
					and pricing_rule.margin_rate_or_amount
					and (
						pricing_rule.margin_type == "Percentage" or pricing_rule.currency == self.doc.currency
					)
				):
					continue

				item.margin_type = pricing_rule.margin_type
				item.margin_rate_or_amount = pricing_rule.margin_rate_or_amount

		item.rate_with_margin = get_rate_with_margin(item)
		if item.discount_percentage > 0:
			item.discount_amount = flt(
				item.rate_with_margin * item.discount_percentage / 100.0, item.precision("discount_amount")
			)

		calculated_rate = flt(item.rate_with_margin - item.discount_amount, item.precision("rate"))

		# if rate is 0 or pricing rules are applicable, calculated rate is preferred
		if has_pricing_rules or not item.rate:
			item.rate = calculated_rate
			return

		# discount and margin are correct, exit early
		if item.rate == calculated_rate:
			return

		# item rate does not match calculated rate. prefer item rate, reset margin / discount
		if item.rate > 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
			remove_discount(item)
			return

		item.rate_with_margin = item.price_list_rate
		item.discount_amount = flt(item.rate_with_margin - item.rate, item.precision("discount_amount"))
		item.discount_percentage = 0
		remove_margin(item)

	def calculate_item_values(self):
		if self.doc.get("is_consolidated") or self.discount_amount_applied:
			return

		do_not_round_fields = [
			"valuation_rate",
			"incoming_rate",
			"sales_incoming_rate",
			"conversion_factor",
		]
		for item in self.doc.items:
			self.doc.round_floats_in(item, do_not_round_fields=do_not_round_fields)
			self.calculate_item_rate(item)

			item.net_rate = item.rate
			if not item.qty and self.doc.get("is_return") and self.doc.get("doctype") != "Purchase Receipt":
				item.amount = flt(-1 * item.rate, item.precision("amount"))
			elif not item.qty and self.doc.get("is_debit_note"):
				item.amount = flt(item.rate, item.precision("amount"))
			else:
				item.amount = flt(item.rate * item.qty, item.precision("amount"))
			item.net_amount = item.amount
			self._set_in_company_currency(
				item, ["price_list_rate", "rate_with_margin", "rate", "net_rate", "amount", "net_amount"]
			)
			item.item_tax_amount = 0.0

	def _set_in_company_currency(self, doc, fields):
		"""set values in base currency"""
		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)

	def initialize_taxes(self):
		self.reset_item_wise_tax_details()
		for tax in self.doc.get("taxes"):
			if not self.discount_amount_applied:
				validate_taxes_and_charges(tax)
				validate_inclusive_tax(tax, self.doc)

			tax_fields = [
				"net_amount",
				"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 tax.charge_type != "Actual" and not (
				self.discount_amount_applied and self.doc.apply_discount_on == "Grand Total"
			):
				tax_fields.append("tax_amount")

			for fieldname in tax_fields:
				tax.set(fieldname, 0.0)

			self.doc.round_floats_in(tax)

	def reset_item_wise_tax_details(self):
		# Setting flag for adding rows
		self.doc.update_item_wise_tax_details = True
		dont_recompute_taxes = [d for d in self.doc.get("taxes") if d.get("dont_recompute_tax")]

		# Identify taxes that shouldn't be recomputed
		item_wise_tax_details = []
		# retain tax_breakup for dont_recompute_taxes
		for row in self.doc.get("_item_wise_tax_details") or []:
			tax = row.get("tax")
			if tax in dont_recompute_taxes:
				item_wise_tax_details.append(row)

		self.doc._item_wise_tax_details = item_wise_tax_details
		self.doc.item_wise_tax_details = []

		for tax in self.doc.get("taxes"):
			if not tax.get("dont_recompute_tax"):
				tax._running_txn_tax_total = 0.0
				tax._running_base_tax_total = 0.0
				tax._running_txn_taxable_total = 0.0
				tax._running_base_taxable_total = 0.0

	def determine_exclusive_rate(self):
		if not any(cint(tax.included_in_print_rate) for tax in self.doc.get("taxes")):
			return

		for item in self.doc.items:
			item._unrounded_net_amount = None
			item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
			total_tax_slope = 0
			total_tax_intercept = 0
			for i, tax in enumerate(self.doc.get("taxes")):
				(
					tax.tax_fraction_for_current_item,
					tax_intercept_per_qty,
				) = self.get_current_tax_fraction(tax, item_tax_map, item)
				tax.inclusive_amount_per_qty = tax_intercept_per_qty

				if i == 0:
					tax.grand_total_fraction_for_current_item = 1 + tax.tax_fraction_for_current_item
					tax.grand_total_amount_per_qty = tax_intercept_per_qty
				else:
					prev = self.doc.get("taxes")[i - 1]
					tax.grand_total_fraction_for_current_item = (
						prev.grand_total_fraction_for_current_item + tax.tax_fraction_for_current_item
					)
					tax.grand_total_amount_per_qty = prev.grand_total_amount_per_qty + tax_intercept_per_qty

				total_tax_slope += tax.tax_fraction_for_current_item
				total_tax_intercept += tax_intercept_per_qty * flt(item.qty)

			if not self.discount_amount_applied and item.qty and (total_tax_slope or total_tax_intercept):
				amount = flt(item.amount) - total_tax_intercept

				item._unrounded_net_amount = amount / (1 + total_tax_slope)
				item.net_amount = flt(item._unrounded_net_amount, 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")
				)

				self._set_in_company_currency(item, ["net_rate", "net_amount"])

	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, item):
		"""
		tax = slope * net + intercept.
		Returns (slope, intercept_per_qty)
		"""
		tax_slope = 0
		tax_intercept = 0

		if cint(tax.included_in_print_rate):
			tax_rate = self._get_tax_rate(tax, item_tax_map)

			if tax_rate == NOT_APPLICABLE_TAX:
				return tax_slope, tax_intercept

			if tax.charge_type == "On Net Total":
				tax_slope = tax_rate / 100.0

			elif tax.charge_type == "On Previous Row Amount":
				row = self.doc.get("taxes")[cint(tax.row_id) - 1]
				tax_slope = (tax_rate / 100.0) * row.tax_fraction_for_current_item
				tax_intercept = (tax_rate / 100.0) * flt(getattr(row, "inclusive_amount_per_qty", 0))

			elif tax.charge_type == "On Previous Row Total":
				row = self.doc.get("taxes")[cint(tax.row_id) - 1]
				tax_slope = (tax_rate / 100.0) * row.grand_total_fraction_for_current_item
				tax_intercept = (tax_rate / 100.0) * flt(getattr(row, "grand_total_amount_per_qty", 0))

			elif tax.charge_type == "On Item Quantity":
				tax_intercept = flt(tax_rate)

			else:
				# Custom charge_type: the rate applies to a resolved (fixed) base,
				# e.g. a tax on MRP included in the printed price.
				qty = flt(item.qty) or 1
				base = self.get_item_taxable_base(item, tax)
				tax_intercept = (tax_rate / 100.0) * base / qty

		if getattr(tax, "add_deduct_tax", None) and tax.add_deduct_tax == "Deduct":
			tax_slope *= -1.0
			tax_intercept *= -1.0

		return tax_slope, tax_intercept

	def _get_tax_rate(self, tax, item_tax_map):
		if tax.account_head in item_tax_map:
			rate = item_tax_map[tax.account_head]
			if rate == NOT_APPLICABLE_TAX:
				return NOT_APPLICABLE_TAX
			return flt(rate, self.doc.precision("rate", tax))

		return tax.rate

	def calculate_net_total(self):
		self.doc.total_qty = (
			self.doc.total
		) = self.doc.base_total = self.doc.net_total = self.doc.base_net_total = 0.0

		for item in self._items:
			self.doc.total += item.amount
			self.doc.total_qty += item.qty
			self.doc.base_total += item.base_amount
			self.doc.net_total += item.net_amount
			self.doc.base_net_total += item.base_net_amount

		self.doc.round_floats_in(self.doc, ["total", "base_total", "net_total", "base_net_total"])

	def calculate_shipping_charges(self):
		# Do not apply shipping rule for POS
		if self.doc.get("is_pos"):
			return

		if hasattr(self.doc, "shipping_rule") and self.doc.shipping_rule:
			shipping_rule = frappe.get_doc("Shipping Rule", self.doc.shipping_rule)
			shipping_rule.apply(self.doc)

			self._calculate()

	def calculate_taxes(self):
		# reset value from earlier calculations
		self.grand_total_diff = 0

		doc = self.doc
		if not doc.get("taxes"):
			return

		# maintain actual tax rate based on idx
		actual_tax_dict = dict(
			[
				[tax.idx, flt(tax.tax_amount, tax.precision("tax_amount"))]
				for tax in doc.taxes
				if tax.charge_type == "Actual"
			]
		)

		for n, item in enumerate(self._items):
			item_tax_map = self._load_item_tax_rate(item.item_tax_rate)
			for i, tax in enumerate(doc.taxes):
				# tax_amount represents the amount of tax for the current step
				current_net_amount, current_tax_amount = self.get_current_tax_and_net_amount(
					item, tax, item_tax_map
				)
				if frappe.flags.round_row_wise_tax:
					current_tax_amount = flt(current_tax_amount, tax.precision("tax_amount"))
					current_net_amount = flt(current_net_amount, tax.precision("net_amount"))

				# Adjust divisional loss to the last item
				if tax.charge_type == "Actual":
					actual_tax_dict[tax.idx] -= current_tax_amount
					if n == len(self._items) - 1:
						current_tax_amount += actual_tax_dict[tax.idx]

				# accumulate tax amount into tax.tax_amount
				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
					tax.net_amount += current_net_amount

				# 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

				# set tax after discount
				tax.tax_amount_after_discount_amount += current_tax_amount

				current_tax_amount = self.get_tax_amount_if_for_valuation_or_deduction(
					current_tax_amount, tax
				)

				# 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.net_amount + current_tax_amount)
				else:
					tax.grand_total_for_current_item = flt(
						doc.taxes[i - 1].grand_total_for_current_item + current_tax_amount
					)

		discount_amount_applied = self.discount_amount_applied
		if doc.apply_discount_on == "Grand Total" and (
			discount_amount_applied or doc.discount_amount or doc.additional_discount_percentage
		):
			tax_amount_precision = doc.taxes[0].precision("tax_amount")

			for i, tax in enumerate(doc.taxes):
				if discount_amount_applied:
					tax.tax_amount_after_discount_amount = flt(
						tax.tax_amount_after_discount_amount, tax_amount_precision
					)

				self.set_cumulative_total(i, tax)

			if not discount_amount_applied:
				self.grand_total_for_distributing_discount = doc.taxes[-1].total
			else:
				self.grand_total_diff = flt(
					self.grand_total_for_distributing_discount - doc.discount_amount - doc.taxes[-1].total,
					doc.precision("grand_total"),
				)

		for i, tax in enumerate(doc.taxes):
			self.round_off_totals(tax)
			self._set_in_company_currency(
				tax, ["tax_amount", "tax_amount_after_discount_amount", "net_amount"]
			)

			self.round_off_base_values(tax)
			self.set_cumulative_total(i, tax)

			self._set_in_company_currency(tax, ["total"])

		self.adjust_rounding_in_item_wise_tax_details()

	def adjust_rounding_in_item_wise_tax_details(self):
		if ignore_item_wise_tax_details(self.doc):
			return

		if not self.doc.get("_item_wise_tax_details"):
			return

		invalid_rows = []

		# reset temporary attributes
		for tax in self.doc.taxes:
			tax._total_tax_breakup = 0
			tax._last_row_idx = None

		for idx, d in enumerate(self.doc._item_wise_tax_details):
			tax = d.get("tax")
			if not tax or (tax.get("charge_type") == "Actual" and d.rate == 0):
				continue

			tax._total_tax_breakup += d.amount or 0
			tax._last_row_idx = idx

		# Apply rounding difference to the last row
		for tax in self.doc.taxes:
			last_idx = tax._last_row_idx
			if last_idx is None:
				continue

			multiplier = -1 if tax.get("add_deduct_tax") == "Deduct" else 1
			expected_amount = tax.base_tax_amount_after_discount_amount * multiplier
			actual_breakup = tax._total_tax_breakup
			diff = flt(expected_amount - actual_breakup, 5)

			# TODO: fix rounding difference issues
			# Allow up to 1 for zero-precision currencies (e.g. JPY, KRW)
			if abs(diff) <= (1 if tax.precision("tax_amount") == 0 else 0.5):
				detail_row = self.doc._item_wise_tax_details[last_idx]
				detail_row["amount"] = flt(detail_row["amount"] + diff, 5)

			else:
				invalid_rows.append(f"Row {tax.idx} (Difference: {diff})")

		if self.doc.flags.ignore_validate:
			return

		if invalid_rows:
			message = (
				_("Item Wise Tax Details do not match with Taxes and Charges at the following rows:")
				+ "<br>"
				+ "<br>".join(invalid_rows)
			)

			frappe.throw(_(message))

	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
			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
		return tax_amount

	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"))

	def get_current_tax_and_net_amount(self, item, tax, item_tax_map):
		tax_rate = self._get_tax_rate(tax, item_tax_map)
		current_tax_amount = 0.0
		current_net_amount = 0.0

		if tax_rate == NOT_APPLICABLE_TAX:
			return current_net_amount, current_tax_amount

		if tax.charge_type == "Actual":
			current_net_amount = item.net_amount
			# distribute the tax amount proportionally to each item row
			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

		elif tax.charge_type == "On Net Total":
			if tax.account_head in item_tax_map:
				current_net_amount = item.net_amount
			# Use unrounded net for inclusive taxes to avoid double rounding
			if (
				cint(tax.included_in_print_rate)
				and not self.discount_amount_applied
				and item._unrounded_net_amount is not None
			):
				current_tax_amount = (tax_rate / 100.0) * item._unrounded_net_amount
			else:
				current_tax_amount = (tax_rate / 100.0) * item.net_amount
		elif tax.charge_type == "On Previous Row Amount":
			current_net_amount = self.doc.get("taxes")[cint(tax.row_id) - 1].tax_amount_for_current_item
			current_tax_amount = (tax_rate / 100.0) * current_net_amount
		elif tax.charge_type == "On Previous Row Total":
			current_net_amount = self.doc.get("taxes")[cint(tax.row_id) - 1].grand_total_for_current_item
			current_tax_amount = (tax_rate / 100.0) * current_net_amount
		elif tax.charge_type == "On Item Quantity":
			# don't sum current net amount due to the field being a currency field
			current_tax_amount = tax_rate * item.qty
		else:
			# Custom charge_type: rate applies to the resolver-provided base.
			base = self.get_item_taxable_base(item, tax)
			current_net_amount = base
			current_tax_amount = (tax_rate / 100.0) * base

		if not tax.get("dont_recompute_tax"):
			self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount)

		return current_net_amount, current_tax_amount

	def get_item_taxable_base(self, item, tax):
		"""Per-item base a custom charge_type's rate is applied to.

		Override the base (gross, MRP, net of other taxes, …) via the
		`erpnext_taxable_base_resolvers` hook

		Register a resolver in `hooks.py`, keyed by charge_type:

		        erpnext_taxable_base_resolvers = {"On Gross Amount": "my_app.taxes.gross_base"}

		It receives (calc, item, tax) — calc is this instance, calc.doc the parent —
		and returns the base (flt-coerced by the caller):

		        def gross_base(calc, item, tax):
		                return item.custom_field_mrp * item.qty

		A resolver may stamp transient attributes on `item`; it can be called more than once
		per item, so such stamping must be idempotent.
		"""
		resolvers = frappe.get_hooks("erpnext_taxable_base_resolvers") or {}
		path = resolvers.get(tax.charge_type)

		if path:
			method = path[-1] if isinstance(path, list | tuple) else path
			return flt(frappe.get_attr(method)(self, item, tax))

		# fallback
		return flt(item.net_amount)

	def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount, current_net_amount):
		# store tax breakup for each item
		multiplier = -1 if tax.get("add_deduct_tax") == "Deduct" else 1

		# Error diffusion: derive each item's base amount as a delta of the running cumulative total
		# so the sum always equals base_tax_amount_after_discount_amount.
		tax._running_txn_tax_total += current_tax_amount * multiplier
		new_base_tax_total = flt(
			flt(tax._running_txn_tax_total, tax.precision("tax_amount")) * self.doc.conversion_rate,
			tax.precision("base_tax_amount"),
		)
		item_wise_tax_amount = flt(
			new_base_tax_total - tax._running_base_tax_total, tax.precision("base_tax_amount")
		)
		tax._running_base_tax_total = new_base_tax_total

		if tax.charge_type != "On Item Quantity":
			tax._running_txn_taxable_total += current_net_amount * multiplier
			new_base_taxable_total = flt(
				flt(tax._running_txn_taxable_total, tax.precision("net_amount")) * self.doc.conversion_rate,
				tax.precision("base_net_amount"),
			)
			item_wise_taxable_amount = flt(
				new_base_taxable_total - tax._running_base_taxable_total, tax.precision("base_net_amount")
			)
			tax._running_base_taxable_total = new_base_taxable_total
		else:
			item_wise_taxable_amount = 0.0

		# maintaining a temp object with item and tax object because correct name will be available after insertion.
		self.doc._item_wise_tax_details.append(
			frappe._dict(
				item=item,
				tax=tax,
				rate=tax_rate,
				amount=item_wise_tax_amount,
				taxable_amount=item_wise_taxable_amount,
			)
		)

	def round_off_totals(self, tax):
		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)

		tax.tax_amount = flt(tax.tax_amount, tax.precision("tax_amount"))
		tax.net_amount = flt(tax.net_amount, tax.precision("net_amount"))
		tax.tax_amount_after_discount_amount = flt(
			tax.tax_amount_after_discount_amount, tax.precision("tax_amount")
		)

	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)

	@deprecated(
		f"{__name__}.calculate_taxes_and_totals.manipulate_grand_total_for_inclusive_tax",
		"unknown",
		"v16",
		"No known instructions.",
	)
	def manipulate_grand_total_for_inclusive_tax(self):
		# for backward compatablility - if in case used by an external application
		return self.adjust_grand_total_for_inclusive_tax()

	def adjust_grand_total_for_inclusive_tax(self):
		# if any inclusive taxes and diff
		if self.doc.get("taxes") and any(cint(t.included_in_print_rate) for t in self.doc.get("taxes")):
			last_tax = self.doc.get("taxes")[-1]
			non_inclusive_tax_amount = sum(
				self.get_tax_amount_if_for_valuation_or_deduction(d.tax_amount_after_discount_amount, d)
				for d in self.doc.get("taxes")
				if not d.included_in_print_rate
			)

			diff = (
				self.doc.total + non_inclusive_tax_amount - flt(last_tax.total, last_tax.precision("total"))
			)

			# 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"))

			if diff and abs(diff) <= (5.0 / 10 ** last_tax.precision("tax_amount")):
				self.grand_total_diff = diff
			else:
				self.grand_total_diff = 0

			# Apply rounding adjustment to grand_total_for_distributing_discount
			# to prevent precision errors during discount distribution
			if hasattr(self, "grand_total_for_distributing_discount") and not self.discount_amount_applied:
				self.grand_total_for_distributing_discount += self.grand_total_diff

	def calculate_totals(self):
		grand_total_diff = self.grand_total_diff

		if self.doc.get("taxes"):
			self.doc.grand_total = flt(self.doc.get("taxes")[-1].total) + grand_total_diff
		else:
			self.doc.grand_total = flt(self.doc.net_total)

		if self.doc.get("taxes"):
			self.doc.total_taxes_and_charges = flt(
				self.doc.grand_total - self.doc.net_total - grand_total_diff,
				self.doc.precision("total_taxes_and_charges"),
			)
		else:
			self.doc.total_taxes_and_charges = 0.0

		self._set_in_company_currency(self.doc, ["total_taxes_and_charges"])

		if self.doc.doctype in [
			"Quotation",
			"Sales Order",
			"Delivery Note",
			"Sales Invoice",
			"POS Invoice",
		]:
			self.doc.base_grand_total = (
				flt(self.doc.grand_total * self.doc.conversion_rate, self.doc.precision("base_grand_total"))
				if self.doc.total_taxes_and_charges
				else self.doc.base_net_total
			)
		else:
			self.doc.taxes_and_charges_added = self.doc.taxes_and_charges_deducted = 0.0
			for tax in self.doc.get("taxes"):
				if tax.category in ["Valuation and Total", "Total"]:
					if tax.add_deduct_tax == "Add":
						self.doc.taxes_and_charges_added += flt(tax.tax_amount_after_discount_amount)
					else:
						self.doc.taxes_and_charges_deducted += flt(tax.tax_amount_after_discount_amount)

			self.doc.round_floats_in(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])

			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
			)

			self._set_in_company_currency(self.doc, ["taxes_and_charges_added", "taxes_and_charges_deducted"])

		self.doc.round_floats_in(self.doc, ["grand_total", "base_grand_total"])

		self.set_rounded_total()

	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._items:
				if d.total_weight:
					self.doc.total_net_weight += d.total_weight

	def set_rounded_total(self):
		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 = 0
				self.doc.rounding_adjustment = 0

			else:
				self.doc.rounded_total = round_based_on_smallest_currency_fraction(
					self.doc.grand_total, self.doc.currency, self.doc.precision("rounded_total")
				)

				# rounding adjustment should always be the difference between grand and rounded total
				self.doc.rounding_adjustment = flt(
					self.doc.rounded_total - self.doc.grand_total, self.doc.precision("rounding_adjustment")
				)

			self._set_in_company_currency(self.doc, ["rounding_adjustment", "rounded_total"])

	def set_discount_amount(self):
		if self.doc.additional_discount_percentage:
			self.doc.discount_amount = flt(
				flt(self.doc.get(scrub(self.doc.apply_discount_on)))
				* self.doc.additional_discount_percentage
				/ 100,
				self.doc.precision("discount_amount"),
			)

		discount_amount = self.doc.discount_amount or 0
		grand_total = self.doc.grand_total

		if self.doc.get("is_return") and self.doc.get("return_against"):
			doctype = frappe.qb.DocType(self.doc.doctype)

			result = (
				frappe.qb.from_(doctype)
				.select(functions.Sum(doctype.discount_amount).as_("total_return_discount"))
				.where(
					(doctype.return_against == self.doc.return_against)
					& (doctype.is_return == 1)
					& (doctype.docstatus == 1)
				)
			).run(as_dict=True)

			total_return_discount = abs(result[0].get("total_return_discount") or 0)
			discount_amount += total_return_discount

		# validate that discount amount cannot exceed the total before discount
		# only during save (i.e. when `_action` is set)
		if self.doc.get("_action") and (
			(grand_total >= 0 and discount_amount > grand_total)
			or (grand_total < 0 and discount_amount < grand_total)  # returns
		):
			frappe.throw(
				_(
					"Additional Discount Amount ({discount_amount}) cannot exceed "
					"the total before such discount ({total_before_discount})"
				).format(
					discount_amount=self.doc.get_formatted("discount_amount"),
					total_before_discount=self.doc.get_formatted("grand_total"),
				),
				title=_("Invalid Discount Amount"),
			)

	def apply_discount_amount(self):
		if self.doc.discount_amount:
			if not self.doc.apply_discount_on:
				frappe.throw(_("Please select Apply Discount On"))

			self.doc.base_discount_amount = flt(
				self.doc.discount_amount * self.doc.conversion_rate,
				self.doc.precision("base_discount_amount"),
			)

			if self.doc.apply_discount_on == "Grand Total" and self.doc.get("is_cash_or_non_trade_discount"):
				self.discount_amount_applied = True
				return

			total_for_discount_amount = self.get_total_for_discount_amount()
			net_total = 0
			expected_net_total = 0

			if total_for_discount_amount:
				# calculate item amount after Discount Amount
				for item in self._items:
					distributed_amount = (
						flt(self.doc.discount_amount) * item.net_amount / total_for_discount_amount
					)

					adjusted_net_amount = item.net_amount - distributed_amount
					expected_net_total += adjusted_net_amount
					item.net_amount = flt(adjusted_net_amount, item.precision("net_amount"))
					item.distributed_discount_amount = flt(
						distributed_amount, item.precision("distributed_discount_amount")
					)
					net_total += item.net_amount

					# discount amount rounding adjustment
					if rounding_difference := flt(
						expected_net_total - net_total, self.doc.precision("net_total")
					):
						item.net_amount = flt(
							item.net_amount + rounding_difference, item.precision("net_amount")
						)
						# net_amount went up by rounding_difference, so its discount share goes down
						item.distributed_discount_amount = flt(
							distributed_amount - rounding_difference,
							item.precision("distributed_discount_amount"),
						)
						net_total += rounding_difference

					item.net_rate = (
						flt(item.net_amount / item.qty, item.precision("net_rate")) if item.qty else 0
					)

					self._set_in_company_currency(item, ["net_rate", "net_amount"])

				self.discount_amount_applied = True
				self._calculate()
		else:
			self.doc.base_discount_amount = 0

	def get_total_for_discount_amount(self):
		doc = self.doc
		if doc.apply_discount_on == "Net Total" or not doc.get("taxes"):
			return self.doc.net_total

		total_actual_tax = 0
		actual_taxes_dict = {}

		def update_actual_tax_dict(tax, tax_amount):
			nonlocal total_actual_tax

			if tax.get("add_deduct_tax") == "Deduct":
				tax_amount *= -1

			if tax.get("category") != "Valuation":
				total_actual_tax += tax_amount

			actual_taxes_dict[int(tax.idx)] = {
				"tax_amount": tax_amount,
				"cumulative_tax_amount": total_actual_tax,
			}

		for tax in doc.taxes:
			if tax.charge_type in ["Actual", "On Item Quantity"]:
				update_actual_tax_dict(tax, tax.tax_amount)
				continue

			if not tax.row_id:
				continue

			base_row = actual_taxes_dict.get(int(tax.row_id))
			if not base_row:
				continue

			base_tax_amount = (
				base_row["tax_amount"]
				if tax.charge_type == "On Previous Row Amount"
				else base_row["cumulative_tax_amount"]
			)
			update_actual_tax_dict(tax, base_tax_amount * tax.rate / 100)

		return getattr(self, "grand_total_for_distributing_discount", doc.grand_total) - total_actual_tax

	def calculate_total_advance(self):
		if not self.doc.docstatus.is_cancelled():
			total_allocated_amount = sum(
				flt(adv.allocated_amount, adv.precision("allocated_amount"))
				for adv in self.doc.get("advances")
			)

			self.doc.total_advance = flt(total_allocated_amount, self.doc.precision("total_advance"))

			grand_total = self.doc.rounded_total or self.doc.grand_total

			if self.doc.party_account_currency == self.doc.currency:
				invoice_total = flt(
					grand_total - flt(self.doc.write_off_amount), self.doc.precision("grand_total")
				)
			else:
				base_write_off_amount = flt(
					flt(self.doc.write_off_amount) * self.doc.conversion_rate,
					self.doc.precision("base_write_off_amount"),
				)
				invoice_total = (
					flt(grand_total * self.doc.conversion_rate, self.doc.precision("grand_total"))
					- base_write_off_amount
				)

			if invoice_total > 0 and self.doc.total_advance > invoice_total:
				frappe.throw(
					_("Advance amount cannot be greater than {0} {1}").format(
						self.doc.party_account_currency, invoice_total
					)
				)

			if self.doc.get("write_off_outstanding_amount_automatically"):
				self.doc.write_off_amount = 0

			self.calculate_outstanding_amount()
			self.calculate_write_off_amount()

	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

	def calculate_outstanding_amount(self):
		# NOTE:
		# write_off_amount is only for POS Invoice
		# total_advance is only for non POS Invoice
		if self.doc.doctype == "Sales Invoice":
			self.calculate_paid_amount()

		if (
			self.doc.is_return
			and self.doc.return_against
			and not self.doc.update_outstanding_for_self
			and not self.doc.get("is_pos")
			or self.is_internal_invoice()
		):
			# Do not calculate the outstanding amount for a return invoice if 'update_outstanding_for_self' is not enabled.
			self.doc.outstanding_amount = 0
			return

		self.doc.round_floats_in(self.doc, ["grand_total", "total_advance", "write_off_amount"])
		self._set_in_company_currency(self.doc, ["write_off_amount"])

		if self.doc.doctype in ["Sales Invoice", "Purchase Invoice"]:
			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

			if self.doc.party_account_currency == self.doc.currency:
				total_amount_to_pay = flt(
					grand_total - self.doc.total_advance - flt(self.doc.write_off_amount),
					self.doc.precision("grand_total"),
				)
			else:
				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"),
				)

			self.doc.round_floats_in(self.doc, ["paid_amount"])
			change_amount = 0

			if self.doc.doctype == "Sales Invoice" and not self.doc.get("is_return"):
				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
				)

			paid_amount = (
				self.doc.paid_amount
				if self.doc.party_account_currency == self.doc.currency
				else self.doc.base_paid_amount
			)

			self.doc.outstanding_amount = flt(
				total_amount_to_pay - flt(paid_amount) + flt(change_amount),
				self.doc.precision("outstanding_amount"),
			)

			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

			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")
			):
				self.set_total_amount_to_default_mop(total_amount_to_pay)
				self.calculate_paid_amount()

	def calculate_paid_amount(self):
		paid_amount = base_paid_amount = 0.0

		if self.doc.is_pos:
			for payment in self.doc.get("payments"):
				payment.amount = flt(payment.amount)
				payment.base_amount = payment.amount * flt(self.doc.conversion_rate)
				paid_amount += payment.amount
				base_paid_amount += payment.base_amount
		elif not self.doc.is_return:
			self.doc.set("payments", [])

		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)

		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"))

	def calculate_change_amount(self):
		self.doc.change_amount = 0.0
		self.doc.base_change_amount = 0.0
		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

		if (
			self.doc.doctype == "Sales Invoice"
			and self.doc.paid_amount > grand_total
			and not self.doc.is_return
			and any(d.type == "Cash" for d in self.doc.payments)
		):
			self.doc.change_amount = flt(
				self.doc.paid_amount - grand_total, self.doc.precision("change_amount")
			)

			self.doc.base_change_amount = flt(
				self.doc.base_paid_amount - base_grand_total, self.doc.precision("base_change_amount")
			)

	def calculate_write_off_amount(self):
		if self.doc.get("write_off_outstanding_amount_automatically"):
			self.doc.write_off_amount = flt(
				self.doc.outstanding_amount, self.doc.precision("write_off_amount")
			)
			self.doc.base_write_off_amount = flt(
				self.doc.write_off_amount * self.doc.conversion_rate,
				self.doc.precision("base_write_off_amount"),
			)

			self.calculate_outstanding_amount()

	def set_item_wise_tax_breakup(self):
		self.doc.other_charges_calculation = get_itemised_tax_breakup_html(self.doc)

	def set_total_amount_to_default_mop(self, total_amount_to_pay):
		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

		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,
			)

			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,
					},
				)


def remove_discount(item):
	item.discount_percentage = 0.0
	item.discount_amount = 0.0


def remove_margin(item):
	item.margin_type = None
	item.margin_rate_or_amount = 0.0


def get_rate_with_margin(item):
	if not item.margin_type:
		return item.price_list_rate

	if item.margin_type == "Percentage":
		return flt(
			item.price_list_rate * (1 + (item.margin_rate_or_amount / 100.0)),
			item.precision("rate_with_margin"),
		)

	return flt(item.price_list_rate + item.margin_rate_or_amount, item.precision("rate_with_margin"))


def get_itemised_tax_breakup_html(doc):
	if not doc.taxes:
		return

	# get headers
	tax_accounts = []
	for tax in doc.taxes:
		if getattr(tax, "category", None) and tax.category == "Valuation":
			continue
		if tax.description not in tax_accounts:
			tax_accounts.append(tax.description)

	with temporary_flag("company", doc.company):
		headers = get_itemised_tax_breakup_header(doc.doctype + " Item", tax_accounts)
		itemised_tax_data = get_itemised_tax_breakup_data(doc)
		get_rounded_tax_amount(itemised_tax_data, doc.precision("tax_amount", "taxes"))
		update_itemised_tax_data(doc)

	return frappe.render_template(
		"templates/includes/itemised_tax_breakup.html",
		dict(
			headers=headers,
			itemised_tax_data=itemised_tax_data,
			tax_accounts=tax_accounts,
			doc=doc,
		),
	)


@frappe.whitelist()
def get_round_off_applicable_accounts(
	company: str, account_list: list | str, doc: str | dict | Document | None = None
):
	# required to set correct region
	with temporary_flag("company", company):
		return get_regional_round_off_accounts(company, account_list, doc)


@erpnext.allow_regional
def get_regional_round_off_accounts(company, account_list, doc=None):
	pass


@erpnext.allow_regional
def update_itemised_tax_data(doc):
	# Don't delete this method, used for localization
	pass


@erpnext.allow_regional
def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
	return [_("Item"), _("Taxable Amount"), *tax_accounts]


@erpnext.allow_regional
def get_itemised_tax_breakup_data(doc):
	itemised_tax = get_itemised_tax(doc)
	itemised_tax_data = []
	for item_code, taxes in itemised_tax.items():
		taxable_amount = next(iter(taxes.values())).get("taxable_amount")
		itemised_tax_data.append(frappe._dict({"item": item_code, "taxable_amount": taxable_amount, **taxes}))

	return itemised_tax_data


def get_itemised_tax(doc, with_tax_account=False):
	itemised_tax = {}
	precision = doc.precision("tax_amount", "taxes")

	for row in doc.get("_item_wise_tax_details"):
		item = row.get("item")
		tax = row.get("tax")
		if not item or not tax:
			continue

		item_code = item.item_code or item.item_name
		if getattr(tax, "category", None) and tax.category == "Valuation":
			continue

		tax_info = itemised_tax.setdefault(item_code, frappe._dict()).setdefault(
			tax.description,
			frappe._dict(
				{
					"tax_amount": 0.0,
					"taxable_amount": 0.0,
					"tax_rate": row.rate,
				}
			),
		)

		tax_info.tax_amount += flt(row.amount, precision)
		conversion_rate = doc.conversion_rate or 1
		tax_info.taxable_amount += flt(row.taxable_amount / conversion_rate, precision)

		if with_tax_account:
			tax_info.tax_account = tax.account_head

	return itemised_tax


from erpnext.deprecation_dumpster import (
	taxes_and_totals_get_itemised_taxable_amount as get_itemised_taxable_amount,
)


def get_rounded_tax_amount(itemised_tax, precision):
	# Rounding based on tax_amount precision
	for taxes in itemised_tax:
		for row in taxes.values():
			if isinstance(row, dict) and isinstance(row["tax_amount"], float):
				row["tax_amount"] = flt(row["tax_amount"], precision)


@frappe.whitelist()
def get_rounding_tax_settings():
	return frappe.get_single_value("Accounts Settings", "round_row_wise_tax")


def ignore_item_wise_tax_details(doc):
	"""Ignore item wise tax details if the doctype does not have item_wise_tax_details field."""
	if not doc.meta.get_field("item_wise_tax_details"):
		return True

	return False


def process_item_wise_tax_details(doc):
	if ignore_item_wise_tax_details(doc):
		return

	if not (doc.get("update_item_wise_tax_details") and doc.get("_item_wise_tax_details")):
		return

	docs = []
	for row in doc.get("_item_wise_tax_details"):
		tax_details = doc.append(
			"item_wise_tax_details",
			{
				**row,
				"docstatus": doc.docstatus,
				"item_row": row.item.name,
				"tax_row": row.tax.name,
			},
		)
		tax_details.set_new_name()
		docs.append(tax_details)

	bulk_insert("Item Wise Tax Detail", docs)
	doc.update_item_wise_tax_details = False


class init_landed_taxes_and_totals:
	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:
				account_currency = frappe.get_cached_value("Account", d.expense_account, "account_currency")
				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
			elif not d.exchange_rate:
				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"))
			d.base_amount = flt(d.amount * flt(d.exchange_rate), d.precision("base_amount"))
