fix: Item Tax template is not working for e-commerce

(cherry picked from commit 7d3240ae3a2e8fc761d460793fbddfb2b5cffc09)
This commit is contained in:
Rohit Waghchaure 2024-01-24 23:45:06 +05:30 committed by Mergify
parent bd7e8ed230
commit 2905db19e5
2 changed files with 87 additions and 0 deletions

View File

@ -21,6 +21,7 @@ from frappe.utils import (
get_link_to_form,
getdate,
nowdate,
parse_json,
today,
)
@ -831,6 +832,37 @@ class AccountsController(TransactionBase):
self.extend("taxes", get_taxes_and_charges(tax_master_doctype, self.get("taxes_and_charges")))
def append_taxes_from_item_tax_template(self):
if not frappe.db.get_single_value("Accounts Settings", "add_taxes_from_item_tax_template"):
return
for row in self.items:
item_tax_rate = row.get("item_tax_rate")
if not item_tax_rate:
continue
if isinstance(item_tax_rate, str):
item_tax_rate = parse_json(item_tax_rate)
for account_head, rate in item_tax_rate.items():
row = self.get_tax_row(account_head)
if not row:
self.append(
"taxes",
{
"charge_type": "On Net Total",
"account_head": account_head,
"rate": 0,
"description": account_head,
},
)
def get_tax_row(self, account_head):
for row in self.taxes:
if row.account_head == account_head:
return row
def set_other_charges(self):
self.set("taxes", [])
self.set_taxes()

View File

@ -597,6 +597,61 @@ class TestQuotation(FrappeTestCase):
quotation.items[0].conversion_factor = 2.23
self.assertRaises(frappe.ValidationError, quotation.save)
def test_item_tax_template_for_quotation(self):
from erpnext.stock.doctype.item.test_item import make_item
if not frappe.db.exists("Account", {"account_name": "_Test Vat", "company": "_Test Company"}):
frappe.get_doc(
{
"doctype": "Account",
"account_name": "_Test Vat",
"company": "_Test Company",
"account_type": "Tax",
"root_type": "Asset",
"is_group": 0,
"parent_account": "Tax Assets - _TC",
"tax_rate": 10,
}
).insert()
if not frappe.db.exists("Item Tax Template", "Vat Template - _TC"):
doc = frappe.get_doc(
{
"doctype": "Item Tax Template",
"name": "Vat Template",
"title": "Vat Template",
"company": "_Test Company",
"taxes": [
{
"tax_type": "_Test Vat - _TC",
"tax_rate": 5,
}
],
}
).insert()
item_doc = make_item("_Test Item Tax Template QTN", {"is_stock_item": 1})
if not frappe.db.exists(
"Item Tax", {"parent": item_doc.name, "item_tax_template": "Vat Template - _TC"}
):
item_doc.append("taxes", {"item_tax_template": "Vat Template - _TC"})
item_doc.save()
quotation = make_quotation(
item_code="_Test Item Tax Template QTN", qty=1, rate=100, do_not_submit=1
)
self.assertFalse(quotation.taxes)
quotation.append_taxes_from_item_tax_template()
quotation.save()
self.assertTrue(quotation.taxes)
for row in quotation.taxes:
self.assertEqual(row.account_head, "_Test Vat - _TC")
self.assertAlmostEqual(row.base_tax_amount, quotation.total * 5 / 100)
item_doc.taxes = []
item_doc.save()
test_records = frappe.get_test_records("Quotation")