refactor!: store item wise tax details as a more flexible dict

This commit is contained in:
David
2024-11-07 22:07:12 +01:00
parent 5af88a7fb1
commit 3732dd1b1f
12 changed files with 162 additions and 90 deletions

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,21 +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_data[2] + tax_data[2],
]
}
)
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], tax_data[2]]})
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,15 +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, net_amount = item_wise_tax_detail.get("_Test Item")
self.assertEqual(tax_rate, 9)
self.assertEqual(amount, 9)
self.assertEqual(net_amount, 100)
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, net_amount2 = item_wise_tax_detail.get("_Test Item 2")
self.assertEqual(tax_rate2, 5)
self.assertEqual(amount2, 5)
self.assertEqual(net_amount2, 100)
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

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

@@ -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):
@@ -520,20 +522,25 @@ class calculate_taxes_and_totals:
if frappe.flags.round_row_wise_tax:
item_wise_tax_amount = flt(item_wise_tax_amount, tax.precision("tax_amount"))
item_wise_net_amount = flt(item_wise_net_amount, tax.precision("net_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"))
item_wise_net_amount += flt(tax.item_wise_tax_detail[key][2], tax.precision("net_amount"))
tax.item_wise_tax_detail[key] = [
tax_rate,
flt(item_wise_tax_amount, tax.precision("tax_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]
item_wise_net_amount += tax.item_wise_tax_detail[key][2]
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, item_wise_net_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:
@@ -667,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:
@@ -1067,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}
)
)
@@ -1090,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
@@ -1111,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

@@ -93,8 +93,12 @@ class TestTaxesAndTotals(FrappeTestCase):
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[0], expected_values[tax.description]["tax_rate"])
self.assertAlmostEqual(tax_detail[1], expected_values[tax.description]["tax_amount"])
self.assertAlmostEqual(tax_detail[2], expected_values[tax.description]["net_amount"])
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)) {
@@ -504,15 +505,20 @@ erpnext.taxes_and_totals = class TaxesAndTotals extends erpnext.payments {
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_net_amount += flt(tax_detail[key][2], precision("net_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)), flt(item_wise_net_amount, precision("base_net_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(