diff --git a/india_compliance/gst_india/client_scripts/sales_invoice_list.js b/india_compliance/gst_india/client_scripts/sales_invoice_list.js index 009c71c45..619e39c11 100644 --- a/india_compliance/gst_india/client_scripts/sales_invoice_list.js +++ b/india_compliance/gst_india/client_scripts/sales_invoice_list.js @@ -7,13 +7,20 @@ frappe.listview_settings[DOCTYPE].onload = function (list_view) { if (!frappe.perm.has_perm(DOCTYPE, 0, "submit")) return; - if (gst_settings.enable_e_waybill) + if (gst_settings.enable_e_waybill) { add_bulk_action_for_submitted_invoices( list_view, __("Generate e-Waybill JSON"), generate_e_waybill_json ); + add_bulk_action_for_submitted_invoices( + list_view, + __("Enqueue Bulk e-Waybill Generation"), + enqueue_bulk_e_waybill_generation + ); + } + if (india_compliance.is_e_invoice_enabled()) add_bulk_action_for_submitted_invoices( list_view, @@ -39,14 +46,24 @@ async function generate_e_waybill_json(docnames) { trigger_file_download(ewb_data, get_e_waybill_file_name()); } -async function enqueue_bulk_e_invoice_generation(docnames) { - const now = frappe.datetime.system_datetime(); +async function enqueue_bulk_e_waybill_generation(docnames) { + enqueue_bulk_generation( + "india_compliance.gst_india.utils.e_waybill.enqueue_bulk_e_waybill_generation", + { doctype: DOCTYPE, docnames } + ); +} - const job_id = await frappe.xcall( +async function enqueue_bulk_e_invoice_generation(docnames) { + enqueue_bulk_generation( "india_compliance.gst_india.utils.e_invoice.enqueue_bulk_e_invoice_generation", { docnames } ); +} + +async function enqueue_bulk_generation(method, args) { + const job_id = await frappe.xcall(method, args); + const now = frappe.datetime.system_datetime(); const creation_filter = `[">", "${now}"]`; const api_requests_link = frappe.utils.generate_route({ type: "doctype", @@ -66,7 +83,7 @@ async function enqueue_bulk_e_invoice_generation(docnames) { frappe.msgprint( __( - `Bulk e-Invoice Generation has been queued. You can track the + `Bulk Generation has been queued. You can track the Background Job, API Request(s), and Error Log(s).`, diff --git a/india_compliance/gst_india/utils/e_invoice.py b/india_compliance/gst_india/utils/e_invoice.py index 80e652639..63bae37e2 100644 --- a/india_compliance/gst_india/utils/e_invoice.py +++ b/india_compliance/gst_india/utils/e_invoice.py @@ -87,8 +87,7 @@ def generate_e_invoices(docnames): finally: # each e-Invoice needs to be committed individually - # nosemgrep - frappe.db.commit() + frappe.db.commit() # nosemgrep @frappe.whitelist() diff --git a/india_compliance/gst_india/utils/e_waybill.py b/india_compliance/gst_india/utils/e_waybill.py index 987eab34e..3fe0660af 100644 --- a/india_compliance/gst_india/utils/e_waybill.py +++ b/india_compliance/gst_india/utils/e_waybill.py @@ -67,6 +67,54 @@ def generate_e_waybill_json(doctype: str, docnames, values=None): ####################################################################################### +@frappe.whitelist() +def enqueue_bulk_e_waybill_generation(doctype, docnames): + """ + Enqueue bulk generation of e-Waybill for the given documents. + """ + + frappe.has_permission(doctype, "submit", throw=True) + + from india_compliance.gst_india.utils import is_api_enabled + + gst_settings = frappe.get_cached_doc("GST Settings") + if not is_api_enabled(gst_settings) or not gst_settings.enable_e_waybill: + frappe.throw(_("Please enable e-Waybill in GST Settings first.")) + + docnames = frappe.parse_json(docnames) if docnames.startswith("[") else [docnames] + rq_job = frappe.enqueue( + "india_compliance.gst_india.utils.e_waybill.generate_e_waybills", + queue="long", + timeout=len(docnames) * 240, # 4 mins per e-Waybill + doctype=doctype, + docnames=docnames, + ) + + return rq_job.id + + +def generate_e_waybills(doctype, docnames): + """ + Bulk generate e-Waybill for the given documents. + """ + + for docname in docnames: + try: + doc = load_doc(doctype, docname, "submit") + _generate_e_waybill(doc) + except Exception: + frappe.log_error( + title=_("e-Waybill generation failed for {0} {1}").format( + doctype, docname + ), + message=frappe.get_traceback(), + ) + + finally: + # each e-Waybill needs to be committed individually + frappe.db.commit() # nosemgrep + + @frappe.whitelist() def generate_e_waybill(*, doctype, docname, values=None): doc = load_doc(doctype, docname, "submit")