Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle gov api error #2897

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ def generate_gstr1_data(self, filters, callback=None):
gov_data_field = "unfiled"

# Get Data
gov_data, is_enqueued = self.get_gov_gstr1_data()
try:
gov_data, is_enqueued = self.get_gov_gstr1_data()
except frappe.ValidationError as error:
return self.handle_gstr1_gov_failure(data, filters, error)

books_data = self.get_books_gstr1_data(filters)

Expand Down Expand Up @@ -592,6 +595,9 @@ def generate_only_books_data(self, data, filters, callback=None):

# GET DATA
def get_gov_gstr1_data(self):
if frappe.get_cached_value("GST Settings", None, "compare_for_pre_upload") != 1:
return frappe._dict(), False

if self.filing_status == "Filed":
data_field = "filed"
else:
Expand All @@ -607,6 +613,24 @@ def get_gov_gstr1_data(self):
# download data
return download_gstr1_json_data(self)

def handle_gstr1_gov_failure(self, data, filters, error):
self.generate_only_books_data(data, filters)
error_log = frappe.log_error(
title="GSTR-1 Generation Failed",
message=str(error),
reference_doctype="GSTR-1 Beta",
)
self.update_status("Failed", commit=True)
frappe.publish_realtime(
"gstr1_data_prepared",
message={
"filters": filters,
"error_log": error_log.name,
},
user=frappe.session.user,
doctype=self.doctype,
)

def get_books_gstr1_data(self, filters, aggregate=False):
from india_compliance.gst_india.doctype.gstr_1_beta.gstr_1_beta import (
get_gstr_1_from_and_to_date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ def update_status(self, status, commit=False):
self.db_set("generation_status", status, commit=commit)

# FILE UTILITY
def load_data(self, file_field=None):
def load_data(self, *file_field):
data = {}

if file_field:
file_fields = [file_field]
file_fields = list(file_field)
else:
file_fields = self.get_applicable_file_fields()

Expand Down Expand Up @@ -184,6 +184,9 @@ def get_return_status(self):

def get_applicable_file_fields(self, settings=None):
# Books aggregated data stored in filed (as to file)
if not settings:
settings = frappe.get_cached_doc("GST Settings")

fields = ["books", "books_summary"]

if self.is_gstr1_api_enabled(settings):
Expand All @@ -192,7 +195,8 @@ def get_applicable_file_fields(self, settings=None):
if self.filing_status == "Filed":
fields.extend(["filed", "filed_summary"])
else:
fields.extend(["unfiled", "unfiled_summary"])
if settings.compare_for_pre_upload:
fields.extend(["unfiled", "unfiled_summary"])

return fields

Expand Down
12 changes: 10 additions & 2 deletions india_compliance/gst_india/doctype/gst_settings/gst_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"e_invoice_applicable_from",
"e_invoice_applicable_companies",
"gstr_1_section_break",
"compare_for_pre_upload",
"compare_gstr_1_data",
"filing_frequency",
"column_break_cxmn",
Expand Down Expand Up @@ -613,7 +614,7 @@
{
"default": "0",
"depends_on": "eval: india_compliance.is_api_enabled(doc)",
"description": "Use APIs to compare records with GST Portal Data Before and After Filing",
"description": "Use APIs to compare records with GST Portal After Filing",
"fieldname": "compare_gstr_1_data",
"fieldtype": "Check",
"label": "Compare Data with GST Portal"
Expand Down Expand Up @@ -668,12 +669,19 @@
"label": "Default Reason for e-Invoice Cancellation",
"mandatory_depends_on": "eval: doc.auto_cancel_e_invoice",
"options": "Duplicate\nOrder Cancelled\nData Entry Mistake"
},
{
"default": "1",
"description": "Use API to compare records with GST Portal Before Filing and upload Invoices ",
"fieldname": "compare_for_pre_upload",
"fieldtype": "Check",
"label": "Compare Data with GST Portal and Upload Invoices"
Sanket322 marked this conversation as resolved.
Show resolved Hide resolved
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2024-12-12 19:09:07.185191",
"modified": "2024-12-24 11:00:01.686079",
"modified_by": "Administrator",
"module": "GST India",
"name": "GST Settings",
Expand Down
14 changes: 12 additions & 2 deletions india_compliance/gst_india/doctype/gstr_1_beta/gstr_1_beta.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ frappe.ui.form.on(DOCTYPE, {
});

frappe.realtime.on("gstr1_data_prepared", message => {
const { filters } = message;
const { filters, error_log } = message;

if (
frm.doc.company_gstin !== filters.company_gstin ||
Expand All @@ -167,7 +167,17 @@ frappe.ui.form.on(DOCTYPE, {
)
return;

frm.taxpayer_api_call("generate_gstr1").then(r => {
const only_books_data = error_log != undefined ;
if (error_log) {
frappe.msgprint({
message: __("Error while preparing GSTR-1 data, Please Check {0} for more deatils",
[`<a href='/app/error-log/${error_log}' class='variant-click'>Error Log</a>`]),
title: "GSTR-1 Download Failed",
indicator: "red",
})
}

frm.taxpayer_api_call("generate_gstr1", {only_books_data}).then(r => {
frm.doc.__gst_data = r.message;
frm.trigger("load_gstr1_data");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def mark_as_filed(self):

@frappe.whitelist()
@otp_handler
def generate_gstr1(self, sync_for=None, recompute_books=False, message=None):
def generate_gstr1(
self, sync_for=None, recompute_books=False, only_books_data=None, message=None
):
period = get_period(self.month_or_quarter, self.year)

# get gstr1 log
Expand Down Expand Up @@ -99,6 +101,11 @@ def generate_gstr1(self, sync_for=None, recompute_books=False, message=None):
gstr1_log.remove_json_for("books")

# files are already present
if only_books_data:
data = gstr1_log.load_data("books", "books_summary")
data["status"] = "Error While Generation"
return data

if gstr1_log.has_all_files(settings):
data = gstr1_log.get_gstr1_data()

Expand Down
Loading