Merge pull request #43372 from blaggacao/feat/log-net-total-on-taxes-and-charges

fix: item wise tax details and net amounts
This commit is contained in:
David Arnold
2024-11-14 12:40:59 +01:00
committed by GitHub
18 changed files with 367 additions and 96 deletions

View File

@@ -20,11 +20,13 @@
"rate",
"section_break_9",
"currency",
"net_amount",
"tax_amount",
"total",
"allocated_amount",
"column_break_13",
"base_tax_amount",
"base_net_amount",
"base_total"
],
"fields": [
@@ -174,12 +176,29 @@
"label": "Account Currency",
"options": "Currency",
"read_only": 1
},
{
"columns": 2,
"fieldname": "net_amount",
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Net Amount",
"options": "currency"
},
{
"fieldname": "base_net_amount",
"fieldtype": "Currency",
"label": "Net Amount (Company Currency)",
"oldfieldname": "tax_amount",
"oldfieldtype": "Currency",
"options": "Company:company:default_currency",
"read_only": 1
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-03-27 13:05:58.437605",
"modified": "2024-09-24 06:51:07.417348",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Advance Taxes and Charges",
@@ -188,4 +207,4 @@
"sort_field": "creation",
"sort_order": "ASC",
"states": []
}
}

View File

@@ -18,6 +18,7 @@ class AdvanceTaxesandCharges(Document):
account_head: DF.Link
add_deduct_tax: DF.Literal["Add", "Deduct"]
allocated_amount: DF.Currency
base_net_amount: DF.Currency
base_tax_amount: DF.Currency
base_total: DF.Currency
charge_type: DF.Literal[
@@ -27,6 +28,7 @@ class AdvanceTaxesandCharges(Document):
currency: DF.Link | None
description: DF.SmallText
included_in_paid_amount: DF.Check
net_amount: DF.Currency
parent: DF.Data
parentfield: DF.Data
parenttype: DF.Data

View File

@@ -13,6 +13,7 @@ from frappe.utils.background_jobs import enqueue, is_job_enqueued
from frappe.utils.scheduler import is_scheduler_inactive
from erpnext.accounts.doctype.pos_profile.pos_profile import required_accounting_dimensions
from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail
class POSInvoiceMergeLog(Document):
@@ -336,15 +337,14 @@ def update_item_wise_tax_detail(consolidate_tax_row, tax_row):
consolidated_tax_detail = {}
for item_code, tax_data in tax_row_detail.items():
tax_data = ItemWiseTaxDetail(**tax_data)
if consolidated_tax_detail.get(item_code):
consolidated_tax_data = consolidated_tax_detail.get(item_code)
consolidated_tax_detail.update(
{item_code: [consolidated_tax_data[0], consolidated_tax_data[1] + tax_data[1]]}
)
consolidated_tax_detail[item_code]["tax_amount"] += tax_data.tax_amount
consolidated_tax_detail[item_code]["net_amount"] += tax_data.net_amount
else:
consolidated_tax_detail.update({item_code: [tax_data[0], tax_data[1]]})
consolidated_tax_detail.update({item_code: tax_data})
consolidate_tax_row.item_wise_tax_detail = json.dumps(consolidated_tax_detail, separators=(",", ":"))
consolidate_tax_row.item_wise_tax_detail = json.dumps(consolidated_tax_detail)
def get_all_unconsolidated_invoices():

View File

@@ -158,13 +158,15 @@ class TestPOSInvoiceMergeLog(IntegrationTestCase):
consolidated_invoice = frappe.get_doc("Sales Invoice", inv.consolidated_invoice)
item_wise_tax_detail = json.loads(consolidated_invoice.get("taxes")[0].item_wise_tax_detail)
tax_rate, amount = item_wise_tax_detail.get("_Test Item")
self.assertEqual(tax_rate, 9)
self.assertEqual(amount, 9)
tax_data = item_wise_tax_detail.get("_Test Item")
self.assertEqual(tax_data.get("tax_rate"), 9)
self.assertEqual(tax_data.get("tax_amount"), 9)
self.assertEqual(tax_data.get("net_amount"), 100)
tax_rate2, amount2 = item_wise_tax_detail.get("_Test Item 2")
self.assertEqual(tax_rate2, 5)
self.assertEqual(amount2, 5)
tax_data = item_wise_tax_detail.get("_Test Item 2")
self.assertEqual(tax_data.get("tax_rate"), 5)
self.assertEqual(tax_data.get("tax_amount"), 5)
self.assertEqual(tax_data.get("net_amount"), 100)
finally:
frappe.set_user("Administrator")
frappe.db.sql("delete from `tabPOS Profile`")

View File

@@ -24,10 +24,12 @@
"dimension_col_break",
"section_break_9",
"account_currency",
"net_amount",
"tax_amount",
"tax_amount_after_discount_amount",
"total",
"column_break_14",
"base_net_amount",
"base_tax_amount",
"base_total",
"base_tax_amount_after_discount_amount",
@@ -233,12 +235,30 @@
"fieldtype": "Check",
"label": "Is Tax Withholding Account",
"read_only": 1
},
{
"columns": 2,
"fieldname": "net_amount",
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Net Amount",
"oldfieldname": "tax_amount",
"oldfieldtype": "Currency",
"options": "currency"
},
{
"fieldname": "base_net_amount",
"fieldtype": "Currency",
"label": "Net Amount (Company Currency)",
"options": "Company:company:default_currency",
"print_hide": 1,
"read_only": 1
}
],
"idx": 1,
"istable": 1,
"links": [],
"modified": "2024-04-08 19:51:36.678551",
"modified": "2024-09-24 06:47:25.129901",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Purchase Taxes and Charges",

View File

@@ -17,6 +17,7 @@ class PurchaseTaxesandCharges(Document):
account_currency: DF.Link | None
account_head: DF.Link
add_deduct_tax: DF.Literal["Add", "Deduct"]
base_net_amount: DF.Currency
base_tax_amount: DF.Currency
base_tax_amount_after_discount_amount: DF.Currency
base_total: DF.Currency
@@ -35,6 +36,7 @@ class PurchaseTaxesandCharges(Document):
included_in_print_rate: DF.Check
is_tax_withholding_account: DF.Check
item_wise_tax_detail: DF.Code | None
net_amount: DF.Currency
parent: DF.Data
parentfield: DF.Data
parenttype: DF.Data

View File

@@ -2071,12 +2071,12 @@ class TestSalesInvoice(IntegrationTestCase):
{
"item": "_Test Item",
"taxable_amount": 10000.0,
"Service Tax": {"tax_rate": 10.0, "tax_amount": 1000.0},
"Service Tax": {"tax_rate": 10.0, "tax_amount": 1000.0, "net_amount": 10000.0},
},
{
"item": "_Test Item 2",
"taxable_amount": 5000.0,
"Service Tax": {"tax_rate": 10.0, "tax_amount": 500.0},
"Service Tax": {"tax_rate": 10.0, "tax_amount": 500.0, "net_amount": 5000.0},
},
]

View File

@@ -20,10 +20,12 @@
"rate",
"section_break_9",
"account_currency",
"net_amount",
"tax_amount",
"total",
"tax_amount_after_discount_amount",
"column_break_13",
"base_net_amount",
"base_tax_amount",
"base_total",
"base_tax_amount_after_discount_amount",
@@ -212,13 +214,30 @@
"label": "Account Currency",
"options": "Currency",
"read_only": 1
},
{
"columns": 2,
"fieldname": "net_amount",
"fieldtype": "Currency",
"in_list_view": 1,
"label": "Net Amount",
"options": "currency"
},
{
"fieldname": "base_net_amount",
"fieldtype": "Currency",
"label": "Net Amount (Company Currency)",
"oldfieldname": "tax_amount",
"oldfieldtype": "Currency",
"options": "Company:company:default_currency",
"read_only": 1
}
],
"idx": 1,
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-03-27 13:10:38.190993",
"modified": "2024-09-24 06:49:32.034074",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Sales Taxes and Charges",

View File

@@ -16,6 +16,7 @@ class SalesTaxesandCharges(Document):
account_currency: DF.Link | None
account_head: DF.Link
base_net_amount: DF.Currency
base_tax_amount: DF.Currency
base_tax_amount_after_discount_amount: DF.Currency
base_total: DF.Currency
@@ -33,6 +34,7 @@ class SalesTaxesandCharges(Document):
included_in_paid_amount: DF.Check
included_in_print_rate: DF.Check
item_wise_tax_detail: DF.Code | None
net_amount: DF.Currency
parent: DF.Data
parentfield: DF.Data
parenttype: DF.Data

View File

@@ -11,6 +11,7 @@ from pypika import Order
from erpnext.accounts.report.sales_register.sales_register import get_mode_of_payments
from erpnext.accounts.report.utils import get_query_columns, get_values_for_columns
from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail
from erpnext.selling.report.item_wise_sales_history.item_wise_sales_history import (
get_customer_details,
)
@@ -596,14 +597,10 @@ def get_tax_accounts(
for item_code, tax_data in item_wise_tax_detail.items():
itemised_tax.setdefault(item_code, frappe._dict())
if isinstance(tax_data, list):
tax_rate, tax_amount = tax_data
else:
tax_rate = tax_data
tax_amount = 0
tax_data = ItemWiseTaxDetail(**tax_data)
if charge_type == "Actual" and not tax_rate:
tax_rate = "NA"
if charge_type == "Actual" and not tax_data.tax_rate:
tax_data.tax_rate = "NA"
item_net_amount = sum(
[flt(d.base_net_amount) for d in item_row_map.get(parent, {}).get(item_code, [])]
@@ -611,7 +608,9 @@ def get_tax_accounts(
for d in item_row_map.get(parent, {}).get(item_code, []):
item_tax_amount = (
flt((tax_amount * d.base_net_amount) / item_net_amount) if item_net_amount else 0
flt((tax_data.tax_amount * d.base_net_amount) / item_net_amount)
if item_net_amount
else 0
)
if item_tax_amount:
tax_value = flt(item_tax_amount, tax_amount_precision)
@@ -623,7 +622,7 @@ def get_tax_accounts(
itemised_tax.setdefault(d.name, {})[description] = frappe._dict(
{
"tax_rate": tax_rate,
"tax_rate": tax_data.tax_rate,
"tax_amount": tax_value,
"is_other_charges": 0 if tuple([account_head]) in tax_accounts else 1,
}

View File

@@ -21,6 +21,8 @@ from erpnext.controllers.accounts_controller import (
from erpnext.stock.get_item_details import _get_item_tax_template
from erpnext.utilities.regional import temporary_flag
ItemWiseTaxDetail = frappe._dict
class calculate_taxes_and_totals:
def __init__(self, doc: Document):
@@ -238,6 +240,7 @@ class calculate_taxes_and_totals:
tax.item_wise_tax_detail = {}
tax_fields = [
"net_amount",
"total",
"tax_amount_after_discount_amount",
"tax_amount_for_current_item",
@@ -380,9 +383,12 @@ class calculate_taxes_and_totals:
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)
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":
@@ -395,6 +401,7 @@ class calculate_taxes_and_totals:
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'
@@ -419,7 +426,9 @@ class calculate_taxes_and_totals:
# set precision in the last item iteration
if n == len(self._items) - 1:
self.round_off_totals(tax)
self._set_in_company_currency(tax, ["tax_amount", "tax_amount_after_discount_amount"])
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)
@@ -462,9 +471,10 @@ class calculate_taxes_and_totals:
else:
tax.total = flt(self.doc.get("taxes")[row_idx - 1].total + tax_amount, tax.precision("total"))
def get_current_tax_amount(self, item, tax, item_tax_map):
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.charge_type == "Actual":
# distribute the tax amount proportionally to each item row
@@ -473,48 +483,64 @@ class calculate_taxes_and_totals:
if tax.get("is_tax_withholding_account") and item.meta.get_field("apply_tds"):
if not item.get("apply_tds") or not self.doc.tax_withholding_net_total:
current_tax_amount = 0.0
current_net_amount = 0.0
else:
current_tax_amount = item.net_amount * actual / self.doc.tax_withholding_net_total
current_net_amount = item.net_amount
current_tax_amount = current_net_amount * actual / self.doc.tax_withholding_net_total
else:
current_net_amount = item.net_amount
current_tax_amount = (
item.net_amount * actual / self.doc.net_total if self.doc.net_total else 0.0
current_net_amount * actual / self.doc.net_total if self.doc.net_total else 0.0
)
elif tax.charge_type == "On Net Total":
current_tax_amount = (tax_rate / 100.0) * item.net_amount
current_net_amount = item.net_amount
current_tax_amount = (tax_rate / 100.0) * current_net_amount
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
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_tax_amount = (tax_rate / 100.0) * self.doc.get("taxes")[
cint(tax.row_id) - 1
].grand_total_for_current_item
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
if not (self.doc.get("is_consolidated") or tax.get("dont_recompute_tax")):
self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount)
self.set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount)
return current_tax_amount
return current_net_amount, current_tax_amount
def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount):
def set_item_wise_tax(self, item, tax, tax_rate, current_tax_amount, current_net_amount):
# store tax breakup for each item
key = item.item_code or item.item_name
item_wise_tax_amount = current_tax_amount * self.doc.conversion_rate
if tax.charge_type != "On Item Quantity":
item_wise_net_amount = current_net_amount * self.doc.conversion_rate
else:
item_wise_net_amount = 0.0
if frappe.flags.round_row_wise_tax:
item_wise_tax_amount = flt(item_wise_tax_amount, tax.precision("tax_amount"))
if tax.item_wise_tax_detail.get(key):
item_wise_tax_amount += flt(tax.item_wise_tax_detail[key][1], tax.precision("tax_amount"))
tax.item_wise_tax_detail[key] = [
tax_rate,
flt(item_wise_tax_amount, tax.precision("tax_amount")),
]
item_wise_net_amount = flt(item_wise_net_amount, tax.precision("net_amount"))
if tax_data := tax.item_wise_tax_detail.get(key):
item_wise_tax_amount += flt(tax_data.tax_amount, tax.precision("tax_amount"))
item_wise_net_amount += flt(tax_data.net_amount, tax.precision("net_amount"))
else:
tax.item_wise_tax_detail[key] = ItemWiseTaxDetail(
tax_rate=tax_rate,
tax_amount=flt(item_wise_tax_amount, tax.precision("tax_amount")),
net_amount=flt(item_wise_net_amount, tax.precision("net_amount")),
)
else:
if tax.item_wise_tax_detail.get(key):
item_wise_tax_amount += tax.item_wise_tax_detail[key][1]
if tax_data := tax.item_wise_tax_detail.get(key):
item_wise_tax_amount += tax_data.tax_amount
item_wise_net_amount += tax_data.net_amount
tax.item_wise_tax_detail[key] = [tax_rate, item_wise_tax_amount]
tax.item_wise_tax_detail[key] = ItemWiseTaxDetail(
tax_rate=tax_rate,
tax_amount=item_wise_tax_amount,
net_amount=item_wise_net_amount,
)
def round_off_totals(self, tax):
if tax.account_head in frappe.flags.round_off_applicable_accounts:
@@ -522,6 +548,7 @@ class calculate_taxes_and_totals:
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")
)
@@ -647,7 +674,7 @@ class calculate_taxes_and_totals:
if not self.doc.get("is_consolidated"):
for tax in self.doc.get("taxes"):
if not tax.get("dont_recompute_tax"):
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail, separators=(",", ":"))
tax.item_wise_tax_detail = json.dumps(tax.item_wise_tax_detail)
def set_discount_amount(self):
if self.doc.additional_discount_percentage:
@@ -1047,14 +1074,11 @@ def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
@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)
itemised_tax_data = []
for item_code, taxes in itemised_tax.items():
itemised_tax_data.append(
frappe._dict(
{"item": item_code, "taxable_amount": itemised_taxable_amount.get(item_code, 0), **taxes}
{"item": item_code, "taxable_amount": sum(tax.net_amount for tax in taxes.values()), **taxes}
)
)
@@ -1070,20 +1094,9 @@ def get_itemised_tax(taxes, with_tax_account=False):
item_tax_map = json.loads(tax.item_wise_tax_detail) if tax.item_wise_tax_detail else {}
if item_tax_map:
for item_code, tax_data in item_tax_map.items():
tax_data = ItemWiseTaxDetail(**tax_data)
itemised_tax.setdefault(item_code, frappe._dict())
tax_rate = 0.0
tax_amount = 0.0
if isinstance(tax_data, list):
tax_rate = flt(tax_data[0])
tax_amount = flt(tax_data[1])
else:
tax_rate = flt(tax_data)
itemised_tax[item_code][tax.description] = frappe._dict(
dict(tax_rate=tax_rate, tax_amount=tax_amount)
)
itemised_tax[item_code][tax.description] = tax_data
if with_tax_account:
itemised_tax[item_code][tax.description].tax_account = tax.account_head
@@ -1091,14 +1104,9 @@ def get_itemised_tax(taxes, with_tax_account=False):
return itemised_tax
def get_itemised_taxable_amount(items):
itemised_taxable_amount = frappe._dict()
for item in items:
item_code = item.item_code or item.item_name
itemised_taxable_amount.setdefault(item_code, 0)
itemised_taxable_amount[item_code] += item.net_amount
return itemised_taxable_amount
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):

View File

@@ -0,0 +1,104 @@
import json
import frappe
from frappe.tests.utils import FrappeTestCase
from erpnext.controllers.taxes_and_totals import calculate_taxes_and_totals
class TestTaxesAndTotals(FrappeTestCase):
def setUp(self):
self.doc = frappe.get_doc(
{
"doctype": "Sales Invoice",
"customer": "_Test Customer",
"company": "_Test Company",
"currency": "INR",
"conversion_rate": 1,
"items": [
{
"item_code": "_Test Item",
"qty": 1,
"rate": 100,
"income_account": "Sales - _TC",
"expense_account": "Cost of Goods Sold - _TC",
"cost_center": "_Test Cost Center - _TC",
}
],
"taxes": [],
}
)
def test_item_wise_tax_detail(self):
# Test On Net Total
self.doc.append(
"taxes",
{
"charge_type": "On Net Total",
"account_head": "_Test Account VAT - _TC",
"cost_center": "_Test Cost Center - _TC",
"description": "VAT",
"rate": 10,
},
)
# Test On Previous Row Amount
self.doc.append(
"taxes",
{
"charge_type": "On Previous Row Amount",
"account_head": "_Test Account Service Tax - _TC",
"cost_center": "_Test Cost Center - _TC",
"description": "Service Tax",
"rate": 14,
"row_id": 1,
},
)
# Test On Previous Row Total
self.doc.append(
"taxes",
{
"charge_type": "On Previous Row Total",
"account_head": "_Test Account Customs Duty - _TC",
"cost_center": "_Test Cost Center - _TC",
"description": "Customs Duty",
"rate": 5,
"row_id": 2,
},
)
# Test On Item Quantity
self.doc.append(
"taxes",
{
"charge_type": "On Item Quantity",
"account_head": "_Test Account Shipping - _TC",
"cost_center": "_Test Cost Center - _TC",
"description": "Shipping",
"rate": 50,
},
)
calculate_taxes_and_totals(self.doc)
expected_values = {
"VAT": {"tax_rate": 10, "tax_amount": 10, "net_amount": 100},
"Service Tax": {"tax_rate": 14, "tax_amount": 1.4, "net_amount": 10},
"Customs Duty": {"tax_rate": 5, "tax_amount": 5.57, "net_amount": 111.4},
"Shipping": {"tax_rate": 50, "tax_amount": 50, "net_amount": 0.0}, # net_amount: here qty
}
for tax in self.doc.taxes:
self.assertIn(tax.description, expected_values)
item_wise_tax_detail = json.loads(tax.item_wise_tax_detail)
tax_detail = item_wise_tax_detail[self.doc.items[0].item_code]
self.assertAlmostEqual(tax_detail.get("tax_rate"), expected_values[tax.description]["tax_rate"])
self.assertAlmostEqual(
tax_detail.get("tax_amount"), expected_values[tax.description]["tax_amount"]
)
self.assertAlmostEqual(
tax_detail.get("net_amount"), expected_values[tax.description]["net_amount"]
)
# Check if net_total is set for each tax
self.assertEqual(tax.net_amount, expected_values[tax.description]["net_amount"])

View File

@@ -109,3 +109,19 @@ def deprecation_warning(marked: str, graduation: str, msg: str):
### Party starts here
@deprecated(
"erpnext.controllers.taxes_and_totals.get_itemised_taxable_amount",
"2024-11-07",
"v17",
"The field item_wise_tax_detail now already contains the net_amount per tax.",
)
def taxes_and_totals_get_itemised_taxable_amount(items):
import frappe
itemised_taxable_amount = frappe._dict()
for item in items:
item_code = item.item_code or item.item_name
itemised_taxable_amount.setdefault(item_code, 0)
itemised_taxable_amount[item_code] += item.net_amount
return itemised_taxable_amount

View File

@@ -388,3 +388,4 @@ erpnext.patches.v15_0.migrate_to_utm_analytics
erpnext.patches.v15_0.update_sub_voucher_type_in_gl_entries
erpnext.patches.v15_0.update_task_assignee_email_field_in_asset_maintenance_log
erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter
erpnext.patches.v15_0.migrate_old_item_wise_tax_data_format

View File

@@ -0,0 +1,59 @@
import json
import frappe
from frappe.utils import flt
from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail
def execute():
# Get all DocTypes that have the 'item_wise_tax_details' field
doctypes_with_tax_details = frappe.get_all(
"DocField", filters={"fieldname": "item_wise_tax_details"}, fields=["parent"], pluck="parent"
)
for doctype in doctypes_with_tax_details:
# Get all documents of this DocType that have data in 'item_wise_tax_details'
docs = frappe.get_all(
doctype,
filters={"item_wise_tax_details": ["is", "set"]},
fields=["name", "item_wise_tax_details"],
)
for doc in docs:
if not doc.item_wise_tax_details:
continue
updated_tax_details = {}
needs_update = False
for item, tax_data in json.loads(doc.item_wise_tax_details).items():
if isinstance(tax_data, list) and len(tax_data) == 2:
updated_tax_details[item] = ItemWiseTaxDetail(
tax_rate=tax_data[0],
tax_amount=tax_data[1],
# can't be reliably reconstructed since it depends on the tax type
# (actual, net, previous line total, previous line net, etc)
net_amount=0.0,
)
needs_update = True
elif isinstance(tax_data, str):
updated_tax_details[item] = ItemWiseTaxDetail(
tax_rate=flt(tax_data),
tax_amount=0.0,
net_amount=0.0,
)
needs_update = True
else:
updated_tax_details[item] = tax_data
if needs_update:
frappe.db.set_value(
doctype,
doc.name,
"item_wise_tax_details",
json.dumps(updated_tax_details),
update_modified=False,
)
frappe.db.commit()
print("Migration of old item-wise tax data format completed for all relevant DocTypes.")

View File

@@ -447,6 +447,7 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
get_current_tax_amount(item, tax, item_tax_map) {
var tax_rate = this._get_tax_rate(tax, item_tax_map);
var current_tax_amount = 0.0;
var current_net_amount = 0.0;
// To set row_id by default as previous row.
if(["On Previous Row Amount", "On Previous Row Total"].includes(tax.charge_type)) {
@@ -461,16 +462,20 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
if(tax.charge_type == "Actual") {
// distribute the tax amount proportionally to each item row
var actual = flt(tax.tax_amount, precision("tax_amount", tax));
current_net_amount = item.net_amount
current_tax_amount = this.frm.doc.net_total ?
((item.net_amount / this.frm.doc.net_total) * actual) : 0.0;
} else if(tax.charge_type == "On Net Total") {
current_net_amount = item.net_amount
current_tax_amount = (tax_rate / 100.0) * item.net_amount;
} else if(tax.charge_type == "On Previous Row Amount") {
current_net_amount = this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item
current_tax_amount = (tax_rate / 100.0) *
this.frm.doc["taxes"][cint(tax.row_id) - 1].tax_amount_for_current_item;
} else if(tax.charge_type == "On Previous Row Total") {
current_net_amount = this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item
current_tax_amount = (tax_rate / 100.0) *
this.frm.doc["taxes"][cint(tax.row_id) - 1].grand_total_for_current_item;
} else if (tax.charge_type == "On Item Quantity") {
@@ -478,13 +483,13 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
}
if (!tax.dont_recompute_tax) {
this.set_item_wise_tax(item, tax, tax_rate, current_tax_amount);
this.set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount);
}
return current_tax_amount;
}
set_item_wise_tax(item, tax, tax_rate, current_tax_amount) {
set_item_wise_tax(item, tax, tax_rate, current_tax_amount, current_net_amount) {
// store tax breakup for each item
let tax_detail = tax.item_wise_tax_detail;
let key = item.item_code || item.item_name;
@@ -495,17 +500,25 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
}
let item_wise_tax_amount = current_tax_amount * this.frm.doc.conversion_rate;
let item_wise_net_amount = current_net_amount * this.frm.doc.conversion_rate;
if (frappe.flags.round_row_wise_tax) {
item_wise_tax_amount = flt(item_wise_tax_amount, precision("tax_amount", tax));
item_wise_net_amount = flt(item_wise_net_amount, precision("net_amount", tax));
if (tax_detail && tax_detail[key]) {
item_wise_tax_amount += flt(tax_detail[key][1], precision("tax_amount", tax));
item_wise_tax_amount += flt(tax_detail[key].tax_amount, precision("tax_amount", tax));
item_wise_net_amount += flt(tax_detail[key].net_amount, precision("net_amount", tax));
}
} else {
if (tax_detail && tax_detail[key])
item_wise_tax_amount += tax_detail[key][1];
item_wise_tax_amount += tax_detail[key].tax_amount;
item_wise_net_amount += tax_detail[key].net_amount;
}
tax_detail[key] = [tax_rate, flt(item_wise_tax_amount, precision("base_tax_amount", tax))];
tax_detail[key] = {
tax_rate: tax_rate,
tax_amount: flt(item_wise_tax_amount, precision("base_tax_amount", tax)),
net_amount: flt(item_wise_net_amount, precision("base_net_amount", tax)),
};
}
round_off_totals(tax) {

View File

@@ -6,7 +6,7 @@ from frappe import _
from frappe.utils import cstr, flt
from frappe.utils.file_manager import remove_file
from erpnext.controllers.taxes_and_totals import get_itemised_tax
from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail, get_itemised_tax
from erpnext.regional.italy import state_codes
@@ -214,16 +214,16 @@ def get_invoice_summary(items, taxes):
else:
item_wise_tax_detail = json.loads(tax.item_wise_tax_detail)
for rate_item in [
tax_item for tax_item in item_wise_tax_detail.items() if tax_item[1][0] == tax.rate
]:
# TODO: with net_amount stored inside item_wise_tax_detail, this entire block seems obsolete and redundant
for _item_code, tax_data in item_wise_tax_detail.items():
tax_data = ItemWiseTaxDetail(**tax_data)
if tax_data.tax_rate != tax.rate:
continue
key = cstr(tax.rate)
if not summary_data.get(key):
summary_data.setdefault(key, {"tax_amount": 0.0, "taxable_amount": 0.0})
summary_data[key]["tax_amount"] += rate_item[1][1]
summary_data[key]["taxable_amount"] += sum(
[item.net_amount for item in items if item.item_code == rate_item[0]]
)
summary_data[key]["tax_amount"] += tax_data.tax_amount
summary_data[key]["taxable_amount"] += tax_data.net_amount
for item in items:
key = cstr(tax.rate)

View File

@@ -8,6 +8,8 @@ import frappe
from frappe import _
from frappe.utils import formatdate, get_link_to_form
from erpnext.controllers.taxes_and_totals import ItemWiseTaxDetail
def execute(filters=None):
return VATAuditReport(filters).run()
@@ -125,12 +127,13 @@ class VATAuditReport:
item_wise_tax_detail = json.loads(item_wise_tax_detail)
else:
continue
for item_code, taxes in item_wise_tax_detail.items():
for item_code, tax_data in item_wise_tax_detail.items():
tax_data = ItemWiseTaxDetail(**tax_data)
is_zero_rated = self.invoice_items.get(parent).get(item_code).get("is_zero_rated")
# to skip items with non-zero tax rate in multiple rows
if taxes[0] == 0 and not is_zero_rated:
if tax_data.tax_rate == 0 and not is_zero_rated:
continue
tax_rate = self.get_item_amount_map(parent, item_code, taxes)
tax_rate = self.get_item_amount_map(parent, item_code, tax_data)
if tax_rate is not None:
rate_based_dict = self.items_based_on_tax_rate.setdefault(parent, {}).setdefault(
@@ -141,10 +144,12 @@ class VATAuditReport:
except ValueError:
continue
def get_item_amount_map(self, parent, item_code, taxes):
# TODO: now that tax_data holds net_amount, this method seems almost obsolete and can be removactored,
# gross_amount can be calculated on the file as a list comprehension
def get_item_amount_map(self, parent, item_code, tax_data):
net_amount = self.invoice_items.get(parent).get(item_code).get("net_amount")
tax_rate = taxes[0]
tax_amount = taxes[1]
tax_rate = tax_data.tax_rate
tax_amount = tax_data.tax_amount
gross_amount = net_amount + tax_amount
self.item_tax_rate.setdefault(parent, {}).setdefault(