From 6cbbf93ac9f63434b10a24089e96abd5fc4b6d53 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Wed, 9 Oct 2024 13:22:45 +0530 Subject: [PATCH 1/2] fix: add comment in gst return log for backdated transactions --- .../gst_india/overrides/payment_entry.py | 7 +-- .../gst_india/overrides/sales_invoice.py | 3 +- .../gst_india/overrides/transaction.py | 51 +++++++++++++++++-- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/india_compliance/gst_india/overrides/payment_entry.py b/india_compliance/gst_india/overrides/payment_entry.py index c847a25cc..b3dc94806 100644 --- a/india_compliance/gst_india/overrides/payment_entry.py +++ b/india_compliance/gst_india/overrides/payment_entry.py @@ -83,8 +83,6 @@ def validate(doc, method=None): return if doc.party_type == "Customer": - validate_backdated_transaction(doc) - # Presume is export with GST if GST accounts are present doc.is_export_with_gst = 1 validate_transaction_for_advance_payment(doc, method) @@ -98,6 +96,9 @@ def validate(doc, method=None): def on_submit(doc, method=None): + if doc.party_type == "Customer": + validate_backdated_transaction(doc) + make_gst_revesal_entry_from_advance_payment(doc) @@ -112,7 +113,7 @@ def before_cancel(doc, method=None): validate_backdated_transaction(doc, action="cancel") -def validate_backdated_transaction(doc, action="create"): +def validate_backdated_transaction(doc, action="submit"): for row in doc.taxes: if row.gst_tax_type in TAX_TYPES and row.tax_amount != 0: _validate_backdated_transaction(doc, action=action) diff --git a/india_compliance/gst_india/overrides/sales_invoice.py b/india_compliance/gst_india/overrides/sales_invoice.py index 1c02e333e..e4e7fa1ae 100644 --- a/india_compliance/gst_india/overrides/sales_invoice.py +++ b/india_compliance/gst_india/overrides/sales_invoice.py @@ -58,7 +58,6 @@ def validate(doc, method=None): gst_settings = frappe.get_cached_doc("GST Settings") - validate_backdated_transaction(doc, gst_settings) validate_invoice_number(doc) validate_credit_debit_note(doc) validate_fields_and_set_status_for_e_invoice(doc, gst_settings) @@ -139,6 +138,8 @@ def is_shipping_address_in_india(doc): def on_submit(doc, method=None): + validate_backdated_transaction(doc) + if getattr(doc, "_submitted_from_ui", None) or validate_transaction(doc) is False: return diff --git a/india_compliance/gst_india/overrides/transaction.py b/india_compliance/gst_india/overrides/transaction.py index bc96e87aa..000bee8cb 100644 --- a/india_compliance/gst_india/overrides/transaction.py +++ b/india_compliance/gst_india/overrides/transaction.py @@ -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 +from frappe.utils import cint, flt, format_date, getdate from erpnext.controllers.accounts_controller import get_taxes_and_charges from india_compliance.gst_india.constants import ( @@ -20,7 +20,10 @@ 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 +from india_compliance.gst_india.doctype.gstin.gstin import ( + get_and_validate_gstin_status, + get_gstr_1_filed_upto, +) from india_compliance.gst_india.utils import ( get_all_gst_accounts, get_gst_account_gst_tax_type_map, @@ -660,10 +663,14 @@ def get_source_state_code(doc): return (doc.supplier_gstin or doc.company_gstin)[:2] -def validate_backdated_transaction(doc, gst_settings=None, action="create"): - if gstr_1_filed_upto := restrict_gstr_1_transaction_for( +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: frappe.throw( _( "You are not allowed to {0} {1} as GSTR-1 has been filed upto {2}" @@ -671,6 +678,40 @@ def validate_backdated_transaction(doc, gst_settings=None, action="create"): 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 + + doc_name = get_gst_return_log_name(doc) + if not doc_name: + return + + gst_return_log = frappe.get_doc("GST Return Log", doc_name) + + url = f"{frappe.utils.get_url()}/app/{doc.doctype.replace(' ', '-').lower()}/{doc.name}" + gst_return_log.add_comment( + "Comment", + f"{doc.doctype} : [{doc.name}]({url}) has been {action} by {frappe.session.user}", + ) + + +def get_gst_return_log_name(doc): + posting_date = getdate(doc.posting_date) + year = posting_date.year + month = f"{posting_date.month:02d}" + + doc_name = f"GSTR1-{month}{year}-{doc.company_gstin}" + if frappe.db.exists("GST Return Log", doc_name): + return doc_name + + quarter_month = (cint(month) - 1) // 3 * 3 + 3 + quarter_name = f"GSTR1-{quarter_month:02d}{year}-{doc.company_gstin}" + + if frappe.db.exists("GST Return Log", quarter_name): + return quarter_name + + return None + def validate_hsn_codes(doc, throw=False, message=None): validate_hsn_code, valid_hsn_length = get_hsn_settings() From 7566d29d9fb4af3d3638d82c5cd7b76a30a75537 Mon Sep 17 00:00:00 2001 From: Sanket322 Date: Thu, 10 Oct 2024 16:07:55 +0530 Subject: [PATCH 2/2] fix: changes as per review --- .../gst_india/overrides/transaction.py | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/india_compliance/gst_india/overrides/transaction.py b/india_compliance/gst_india/overrides/transaction.py index 000bee8cb..7992dd198 100644 --- a/india_compliance/gst_india/overrides/transaction.py +++ b/india_compliance/gst_india/overrides/transaction.py @@ -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, getdate +from frappe.utils import cint, flt, format_date, get_link_to_form, getdate from erpnext.controllers.accounts_controller import get_taxes_and_charges from india_compliance.gst_india.constants import ( @@ -682,34 +682,26 @@ def validate_backdated_transaction(doc, gst_settings=None, action="submit"): if not gstr_1_filed_upto or getdate(doc.posting_date) > gstr_1_filed_upto: return - doc_name = get_gst_return_log_name(doc) - if not doc_name: + 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", doc_name) - - url = f"{frappe.utils.get_url()}/app/{doc.doctype.replace(' ', '-').lower()}/{doc.name}" + gst_return_log = frappe.get_doc("GST Return Log", gst_return_log_name) gst_return_log.add_comment( "Comment", - f"{doc.doctype} : [{doc.name}]({url}) has been {action} by {frappe.session.user}", + f"{doc.doctype} : {get_link_to_form(doc.doctype, doc.name)} has been {action} by {frappe.session.user}", ) -def get_gst_return_log_name(doc): - posting_date = getdate(doc.posting_date) - year = posting_date.year - month = f"{posting_date.month:02d}" +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, + } - doc_name = f"GSTR1-{month}{year}-{doc.company_gstin}" - if frappe.db.exists("GST Return Log", doc_name): + if doc_name := frappe.db.exists("GST Return Log", filters): return doc_name - quarter_month = (cint(month) - 1) // 3 * 3 + 3 - quarter_name = f"GSTR1-{quarter_month:02d}{year}-{doc.company_gstin}" - - if frappe.db.exists("GST Return Log", quarter_name): - return quarter_name - return None