Skip to content

Commit

Permalink
feat: bulk generate e-waybill (#724)
Browse files Browse the repository at this point in the history
Co-authored-by: Smit Vora <[email protected]>
  • Loading branch information
rtdany10 and vorasmit authored Aug 5, 2023
1 parent 7239eb2 commit c2d6a41
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 7 deletions.
27 changes: 22 additions & 5 deletions india_compliance/gst_india/client_scripts/sales_invoice_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -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
<a href='{0}'>Background Job</a>,
<a href='{1}'>API Request(s)</a>,
and <a href='{2}'>Error Log(s)</a>.`,
Expand Down
3 changes: 1 addition & 2 deletions india_compliance/gst_india/utils/e_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
48 changes: 48 additions & 0 deletions india_compliance/gst_india/utils/e_waybill.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c2d6a41

Please sign in to comment.