Skip to content

Commit

Permalink
fix: correct validation for rcm returns
Browse files Browse the repository at this point in the history
(cherry picked from commit 3dc1070)

# Conflicts:
#	india_compliance/gst_india/overrides/test_transaction.py
  • Loading branch information
vorasmit authored and mergify[bot] committed Jul 13, 2024
1 parent f19f034 commit a49a742
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 28 deletions.
69 changes: 69 additions & 0 deletions india_compliance/gst_india/overrides/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
update_child_qty_rate,
update_gl_dict_with_regional_fields,
)
from erpnext.controllers.sales_and_purchase_return import make_return_doc
from erpnext.controllers.taxes_and_totals import get_regional_round_off_accounts
from erpnext.stock.doctype.delivery_note.delivery_note import make_sales_invoice
from erpnext.stock.doctype.purchase_receipt.purchase_receipt import (
Expand Down Expand Up @@ -132,6 +133,74 @@ def test_transaction_with_rcm_to_unregistered_supplier(self):
doc.taxes[0],
)

<<<<<<< HEAD
=======
def test_rcm_transaction_with_returns(self):
"Make sure RCM is not applied on Sales Return"
if self.doctype not in [
"Delivery Note",
"Sales Invoice",
"Purchase Receipt",
"Purchase Invoice",
]:
return

doc = create_transaction(
**self.transaction_details, is_reverse_charge=1, is_in_state_rcm=1
)
return_doc = make_return_doc(self.doctype, doc.name)
return_doc.save().submit()

self.assertEqual(return_doc.is_reverse_charge, 1)

def test_non_taxable_items_with_tax(self):
doc = create_transaction(
**self.transaction_details,
is_in_state=True,
item_tax_template="GST 28% - _TIRC",
do_not_submit=True,
)

for item in doc.items:
item.gst_treatment = "Nil-Rated"

self.assertRaisesRegex(
frappe.exceptions.ValidationError,
re.compile(r"^(Cannot charge GST on Non-Taxable Items.*)$"),
validate_item_tax_template,
doc,
)

def test_validate_item_tax_template(self):
item_tax_template = frappe.get_doc("Item Tax Template", "GST 28% - _TIRC")
tax_accounts = item_tax_template.get("taxes")

# Invalidate item tax template
item_tax_template.taxes = []
item_tax_template.flags.ignore_mandatory = True
item_tax_template.save()

doc = create_transaction(
**self.transaction_details,
is_in_state=True,
item_tax_template="GST 28% - _TIRC",
do_not_submit=True,
)

for tax in doc.taxes:
tax.rate = 0

self.assertRaisesRegex(
frappe.exceptions.ValidationError,
re.compile(r"^(No GST is being charged on Taxable Items.*)$"),
doc.save,
)

# Restore item tax template
item_tax_template.taxes = tax_accounts
item_tax_template.save()

>>>>>>> 3dc10704 (fix: correct validation for rcm returns)
def test_transaction_for_items_with_duplicate_taxes(self):
# Should not allow same item in invoice with multiple taxes
doc = create_transaction(**self.transaction_details, do_not_save=True)
Expand Down
59 changes: 31 additions & 28 deletions india_compliance/gst_india/overrides/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,46 +1067,49 @@ def validate_reverse_charge_transaction(doc):
if not doc.get("is_reverse_charge"):
return

is_return = doc.get("is_return", False)

def _throw_tax_error(is_positive, tax, comment_suffix=""):
expected = "positive" if is_positive else "negative"
if is_return:
expected = "negative" if is_positive else "positive"

frappe.throw(
_("Row #{0}: Tax amount should be {1} for GST Account {2}{3}").format(
tax.idx, expected, tax.account_head, comment_suffix
)
)

for tax in doc.get("taxes"):
if not tax.gst_tax_type:
if not tax.gst_tax_type or not tax.tax_amount:
continue

tax_amount = tax.base_tax_amount_after_discount_amount
if is_return:
tax_amount = -tax_amount

is_positive = tax_amount > 0

if "rcm" not in tax.gst_tax_type:
# NON RCM should be positive
if (
tax.get("add_deduct_tax", "Add") != "Add"
or tax.base_tax_amount_after_discount_amount < 0
):
frappe.throw(
_(
"Row #{0}: Tax amount should be positive for GST Account {1}"
).format(tax.idx, tax.account_head)
)
# NON RCM logic
if tax.get("add_deduct_tax", "Add") != "Add" or not is_positive:
_throw_tax_error(True, tax)

base_gst_tax += tax.base_tax_amount_after_discount_amount
base_gst_tax += tax_amount

elif "rcm" in tax.gst_tax_type:
# Using Deduct for RCM
# RCM logic
if tax.get("add_deduct_tax") == "Deduct":
if tax.base_tax_amount_after_discount_amount < 0:
frappe.throw(
_(
"Row #{0}: Tax amount should be positive for GST Account {1}"
" as you are Deducting Tax"
).format(tax.idx, tax.account_head)
)
if not is_positive:
_throw_tax_error(True, tax, " as you are Deducting Tax")

base_reverse_charge_booked -= tax.base_tax_amount_after_discount_amount
base_reverse_charge_booked -= tax_amount

else:
if tax.base_tax_amount_after_discount_amount > 0:
frappe.throw(
_(
"Row #{0}: Tax amount should be negative for GST Account {1}"
).format(tax.idx, tax.account_head)
)
if is_positive:
_throw_tax_error(False, tax)

base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount
base_reverse_charge_booked += tax_amount

condition = flt(base_gst_tax + base_reverse_charge_booked, 2) == 0

Expand Down

0 comments on commit a49a742

Please sign in to comment.