Skip to content

Commit

Permalink
fix: resolve conflicts and refactor gst account validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vorasmit committed Jun 28, 2024
1 parent b93cccd commit 07e86b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 303 deletions.
94 changes: 33 additions & 61 deletions india_compliance/gst_india/overrides/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from erpnext.controllers.accounts_controller import get_taxes_and_charges

from india_compliance.gst_india.constants import (
GST_RCM_TAX_TYPES,
GST_TAX_TYPES,
SALES_DOCTYPES,
STATE_NUMBERS,
Expand Down Expand Up @@ -156,7 +157,9 @@ def validate_item_wise_tax_detail(doc):
# Sales Invoice is created with manual tax amount. So, when a sales return is created,
# the tax amount is not recalculated, causing the issue.

is_cess_non_advol = "cess_non_advol" in row.gst_tax_type
is_cess_non_advol = (
row.gst_tax_type and "cess_non_advol" in row.gst_tax_type
)
multiplier = (
item_qty_map.get(item_name, 0)
if is_cess_non_advol
Expand Down Expand Up @@ -324,21 +327,21 @@ def validate(self, doc, is_sales_transaction=False):
if not self.doc.taxes:
return

if self.has_no_gst_accounts():
if not self.has_gst_tax_rows():
return

self.setup_defaults()

self.validate_for_invalid_accounts()
self.validate_same_party_gstin()
self.validate_invalid_account_for_transaction() # Sales / Purchase
self.validate_for_same_party_gstin()
self.validate_reverse_charge_accounts()
self.validate_sales_transaction()
self.validate_purchase_transaction()
self.validate_for_invalid_account_type()
self.validate_for_invalid_account_type() # CGST / SGST / IGST
self.validate_for_charge_type()
self.validate_missing_accounts_in_item_tax_template()

return self.all_valid_accounts
return

def setup_defaults(self):
(
Expand All @@ -351,22 +354,17 @@ def setup_defaults(self):
for_purchase=not self.is_sales_transaction,
)

self.first_gst_idx = self._get_matched_idx(
self.gst_tax_rows, self.all_valid_accounts
)
self.first_gst_idx = self._get_matched_idx(self.gst_tax_rows, TAX_TYPES)
self.used_accounts = set(row.account_head for row in self.gst_tax_rows)

def has_no_gst_accounts(self):
def has_gst_tax_rows(self):
self.gst_tax_rows = [
row
for row in self.doc.taxes
if row.tax_amount
and row.account_head in get_all_gst_accounts(self.doc.company)
row for row in self.doc.taxes if row.tax_amount and row.gst_account_type
]

return not self.gst_tax_rows
return self.gst_tax_rows

def validate_for_invalid_accounts(self):
def validate_invalid_account_for_transaction(self):
"""
- Only Valid Accounts should be allowed.
eg: Output accounts not allowed in Purchase Invoice
Expand All @@ -381,7 +379,7 @@ def validate_for_invalid_accounts(self):
).format(row.idx, bold(row.account_head))
)

def validate_same_party_gstin(self):
def validate_for_same_party_gstin(self):
party_gstin = (
self.doc.billing_address_gstin
if self.is_sales_transaction
Expand All @@ -405,17 +403,7 @@ def validate_reverse_charge_accounts(self):
if self.doc.doctype == "Payment Entry" or self.doc.is_reverse_charge:
return

rcm_account_type = (
"Sales Reverse Charge"
if self.is_sales_transaction
else "Purchase Reverse Charge"
)

reverse_charge_accounts = get_gst_accounts_by_type(
self.doc.company, rcm_account_type
).values()

if idx := self._get_matched_idx(self.gst_tax_rows, reverse_charge_accounts):
if idx := self._get_matched_idx(self.gst_tax_rows, GST_RCM_TAX_TYPES):
self._throw(
_(
"Cannot use Reverse Charge Account in Row #{0} since transaction is"
Expand Down Expand Up @@ -519,6 +507,9 @@ def validate_for_charge_type(self):

@staticmethod
def validate_charge_type_for_cess_non_advol_accounts(tax_row):
if not tax_row.gst_tax_type:
return

if (
tax_row.charge_type == "On Item Quantity"
and "cess_non_advol" not in tax_row.gst_tax_type
Expand Down Expand Up @@ -560,14 +551,9 @@ def validate_missing_accounts_in_item_tax_template(self):
indicator="orange",
)

def _get_matched_idx(self, rows_to_search, account_head_list):
def _get_matched_idx(self, rows_to_search, tax_types):
return next(
(
row.idx
for row in rows_to_search
if row.account_head in account_head_list
),
None,
(row.idx for row in rows_to_search if row.gst_tax_type in tax_types), None
)

def _throw(self, message, title=None):
Expand Down Expand Up @@ -846,7 +832,7 @@ def get_gst_details(party_details, doctype, company, *, update_place_of_supply=F
# Taxes Not Applicable
if (
(destination_gstin and destination_gstin == source_gstin) # Internal transfer
or (is_sales_transaction and (is_export_without_payment_of_gst(party_details)))
or (is_sales_transaction and is_export_without_payment_of_gst(party_details))
or (
not is_sales_transaction
and (
Expand Down Expand Up @@ -971,23 +957,11 @@ def validate_reverse_charge_transaction(doc, is_sales_transaction):
if not doc.get("is_reverse_charge"):
return

if is_sales_transaction:
rcm_account_type = "Sales Reverse Charge"
non_rcm_account_type = "Output"
else:
rcm_account_type = "Purchase Reverse Charge"
non_rcm_account_type = "Input"

reverse_charge_accounts = get_gst_accounts_by_type(
doc.company, rcm_account_type
).values()

non_rcm_accounts = get_gst_accounts_by_type(
doc.company, non_rcm_account_type
).values()

for tax in doc.get("taxes"):
if tax.account_head in non_rcm_accounts:
if not tax.gst_tax_type:
continue

if "rcm" not in tax.gst_tax_type:
# NON RCM should be positive
if (
tax.get("add_deduct_tax", "Add") != "Add"
Expand All @@ -1001,8 +975,7 @@ def validate_reverse_charge_transaction(doc, is_sales_transaction):

base_gst_tax += tax.base_tax_amount_after_discount_amount

elif tax.account_head in reverse_charge_accounts:
print("here")
elif "rcm" in tax.account_head:
# Using Deduct for RCM
if tax.get("add_deduct_tax") == "Deduct":
if tax.base_tax_amount_after_discount_amount < 0:
Expand All @@ -1025,7 +998,6 @@ def validate_reverse_charge_transaction(doc, is_sales_transaction):

base_reverse_charge_booked += tax.base_tax_amount_after_discount_amount

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

if not condition:
Expand Down Expand Up @@ -1484,10 +1456,10 @@ def validate_transaction(doc, method=None):

validate_gst_category(doc.gst_category, gstin)

valid_accounts = GSTAccounts().validate(doc, is_sales_transaction) or ()
GSTAccounts().validate(doc, is_sales_transaction)
validate_reverse_charge_transaction(doc, is_sales_transaction)
update_taxable_values(doc, valid_accounts)
validate_item_wise_tax_detail(doc, valid_accounts)
update_taxable_values(doc)
validate_item_wise_tax_detail(doc)


def before_print(doc, method=None, print_settings=None):
Expand Down Expand Up @@ -1616,9 +1588,9 @@ def before_update_after_submit(doc, method=None):
if is_sales_transaction := doc.doctype in SALES_DOCTYPES:
validate_hsn_codes(doc)

valid_accounts = GSTAccounts().validate(doc, is_sales_transaction) or ()
update_taxable_values(doc, valid_accounts)
validate_item_wise_tax_detail(doc, valid_accounts)
GSTAccounts().validate(doc, is_sales_transaction) or ()
update_taxable_values(doc)
validate_item_wise_tax_detail(doc)
update_gst_details(doc)


Expand Down
3 changes: 1 addition & 2 deletions india_compliance/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ india_compliance.patches.post_install.rename_import_of_capital_goods
execute:from india_compliance.audit_trail.setup import create_custom_fields, CUSTOM_FIELDS; create_custom_fields(CUSTOM_FIELDS)
india_compliance.patches.post_install.update_hsn_code
execute:from india_compliance.gst_india.setup import map_default_uoms; map_default_uoms()
india_compliance.patches.v14.rename_reverse_charge_to_purchase_reverse_charge
india_compliance.patches.v14.set_correct_root_account_for_rcm
india_compliance.patches.v14.set_autogenerate_e_waybill_with_e_invoice
execute:import frappe; frappe.db.set_value("GST Settings", None, "archive_party_info_days", 7)
Expand Down Expand Up @@ -51,8 +52,6 @@ india_compliance.patches.v14.update_default_gstr1_settings
india_compliance.patches.v14.add_match_found_in_purchase_reconciliation_status
india_compliance.patches.v14.unset_inward_supply_link_for_cancelled_purchase
india_compliance.patches.v14.delete_not_generated_gstr_import_log
india_compliance.patches.v14.rename_reverse_charge_to_purchase_reverse_charge
india_compliance.patches.post_install.update_sales_rcm_accounts
india_compliance.patches.v14.enable_sales_through_ecommerce_operator
india_compliance.patches.post_install.set_gst_tax_type
execute:from india_compliance.gst_india.setup import set_default_print_settings; set_default_print_settings()
Loading

0 comments on commit 07e86b8

Please sign in to comment.