Skip to content

Commit

Permalink
fix: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanket322 committed Oct 21, 2024
1 parent c1fb3c4 commit d73ca45
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 44 deletions.
50 changes: 45 additions & 5 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from frappe import _
from frappe.model.document import Document
from frappe.query_builder.functions import IfNull
from frappe.utils import add_to_date, getdate
from frappe.utils import add_to_date, cint, get_link_to_form, getdate

from india_compliance.gst_india.constants import GST_ACCOUNT_FIELDS, GST_PARTY_TYPES
from india_compliance.gst_india.constants.custom_fields import (
Expand Down Expand Up @@ -496,12 +496,12 @@ def update_not_applicable_status(e_invoice_applicability_date=None, company=None
query.run()


def restrict_gstr_1_transaction_for(posting_date, company_gstin, gst_settings=None):
def restrict_gstr_1_transaction_for(doc, gst_settings=None, action="submit"):
"""
Check if the user is allowed to modify transactions before the GSTR-1 filing date
Additionally, update the `is_not_latest_gstr1_data` field in the GST Return Log
"""
posting_date = getdate(posting_date)
posting_date = getdate(doc.posting_date)

if not gst_settings:
gst_settings = frappe.get_cached_doc("GST Settings")
Expand All @@ -511,7 +511,7 @@ def restrict_gstr_1_transaction_for(posting_date, company_gstin, gst_settings=No
if not gst_settings.restrict_changes_after_gstr_1:
restrict = False

gstr_1_filed_upto = get_gstr_1_filed_upto(company_gstin)
gstr_1_filed_upto = get_gstr_1_filed_upto(doc.company_gstin)

if not gstr_1_filed_upto:
restrict = False
Expand All @@ -528,7 +528,47 @@ def restrict_gstr_1_transaction_for(posting_date, company_gstin, gst_settings=No
if restrict:
return gstr_1_filed_upto

update_is_not_latest_gstr1_data(posting_date, company_gstin)
update_is_not_latest_gstr1_data(posting_date, doc.company_gstin)

if posting_date <= getdate(gstr_1_filed_upto):
gst_return_log_name = get_gst_return_log_name(
doc.posting_date, doc.company_gstin
)
if not gst_return_log_name:
return

gst_return_log = frappe.get_doc("GST Return Log", gst_return_log_name)
gst_return_log.add_comment(
"Comment",
f"{doc.doctype} : {get_link_to_form(doc.doctype, doc.name)} has been {action} by {frappe.session.user}",
)


def get_gst_return_log_name(posting_date, company_gstin):
posting_date = getdate(posting_date)
year = posting_date.year
month = f"{posting_date.month:02d}"

# monthly
if doc_name := frappe.db.exists(
"GST Return Log",
{
"return_period": f"{month}{year}",
"gstin": company_gstin,
},
):
return doc_name

# quarterly
quarter_month = (cint(month) - 1) // 3 * 3 + 3
if doc_name := frappe.db.exists(
"GST Return Log",
{
"return_period": f"{quarter_month:02d}{year}",
"gstin": company_gstin,
},
):
return doc_name

return None

Expand Down
55 changes: 54 additions & 1 deletion india_compliance/gst_india/overrides/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import frappe
from frappe.tests import IntegrationTestCase, change_settings
from frappe.utils import today
from frappe.utils import add_days, getdate, today
from erpnext.accounts.doctype.purchase_invoice.purchase_invoice import (
make_regional_gl_entries,
)
Expand Down Expand Up @@ -972,6 +972,59 @@ def test_copy_e_waybill_fields_from_si_to_return(self):

self.assertEqual(si_return.vehicle_no, None)

@change_settings("GST Settings", {"restrict_changes_after_gstr_1": 1})
def test_backdated_transaction(self):
si = create_transaction(doctype="Sales Invoice", do_not_submit=True)

gstin_doc = frappe.new_doc(
"GSTIN",
gstin=si.company_gstin,
status="Active",
gstr_1_filed_upto=add_days(today(), 1),
)
gstin_doc.save(ignore_permissions=True)

test_user = frappe.get_doc("User", {"email": "test@example.com"})
test_user.add_roles("Accounts User")
frappe.set_user(test_user.name)

self.assertRaisesRegex(
frappe.exceptions.ValidationError,
re.compile(r"You are not allowed to submit Sales Invoice"),
si.submit,
)

def test_backdated_transaction_with_comment(self):
si = create_transaction(doctype="Sales Invoice", do_not_submit=True)

gstin_doc = frappe.new_doc(
"GSTIN",
gstin=si.company_gstin,
status="Active",
gstr_1_filed_upto=add_days(today(), 1),
)
gstin_doc.save()

posting_date = getdate(si.posting_date)
year = posting_date.year
month = f"{posting_date.month:02d}"
gst_return_log = frappe.new_doc(
"GST Return Log",
return_period=f"{month}{year}",
gstin=si.company_gstin,
return_type="GSTR1",
)
gst_return_log.save()

si.submit()

comment = frappe.get_value(
"Comment",
{"comment_type": "Comment", "reference_name": gst_return_log.name},
["content"],
)
self.assertTrue(si.name in comment)


def create_cess_accounts():
input_cess_non_advol_account = create_tax_accounts("Input Tax Cess Non Advol")
Expand Down
41 changes: 3 additions & 38 deletions india_compliance/gst_india/overrides/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from frappe import _, bold
from frappe.contacts.doctype.address.address import get_default_address
from frappe.model.utils import get_fetch_values
from frappe.utils import cint, flt, format_date, get_link_to_form, getdate
from frappe.utils import cint, flt, format_date
from erpnext.controllers.accounts_controller import get_taxes_and_charges

from india_compliance.gst_india.constants import (
Expand All @@ -20,10 +20,7 @@
from india_compliance.gst_india.doctype.gst_settings.gst_settings import (
restrict_gstr_1_transaction_for,
)
from india_compliance.gst_india.doctype.gstin.gstin import (
get_and_validate_gstin_status,
get_gstr_1_filed_upto,
)
from india_compliance.gst_india.doctype.gstin.gstin import get_and_validate_gstin_status
from india_compliance.gst_india.utils import (
get_all_gst_accounts,
get_gst_account_gst_tax_type_map,
Expand Down Expand Up @@ -664,46 +661,14 @@ def get_source_state_code(doc):


def validate_backdated_transaction(doc, gst_settings=None, action="submit"):
gst_settings = gst_settings or frappe.get_cached_doc("GST Settings")

gstr_1_filed_upto = restrict_gstr_1_transaction_for(
doc.posting_date, doc.company_gstin, gst_settings
)

if gstr_1_filed_upto:
if gstr_1_filed_upto := restrict_gstr_1_transaction_for(doc, gst_settings, action):
frappe.throw(
_(
"You are not allowed to {0} {1} as GSTR-1 has been filed upto {2}"
).format(action, doc.doctype, frappe.bold(format_date(gstr_1_filed_upto))),
title=_("Restricted Changes"),
)

gstr_1_filed_upto = get_gstr_1_filed_upto(doc.company_gstin)
if not gstr_1_filed_upto or getdate(doc.posting_date) > gstr_1_filed_upto:
return

gst_return_log_name = get_gst_return_log_name(doc.company_gstin, gstr_1_filed_upto)
if not gst_return_log_name:
return

gst_return_log = frappe.get_doc("GST Return Log", gst_return_log_name)
gst_return_log.add_comment(
"Comment",
f"{doc.doctype} : {get_link_to_form(doc.doctype, doc.name)} has been {action} by {frappe.session.user}",
)


def get_gst_return_log_name(company_gstin, gstr_1_filed_upto):
filters = {
"return_period": f"{gstr_1_filed_upto.month:02d}{gstr_1_filed_upto.year}",
"gstin": company_gstin,
}

if doc_name := frappe.db.exists("GST Return Log", filters):
return doc_name

return None


def validate_hsn_codes(doc, throw=False, message=None):
validate_hsn_code, valid_hsn_length = get_hsn_settings()
Expand Down

0 comments on commit d73ca45

Please sign in to comment.