Skip to content

Commit

Permalink
fix: bulk e-waybill print
Browse files Browse the repository at this point in the history
  • Loading branch information
ljain112 committed Nov 23, 2023
1 parent 22ce65b commit d6c4d8f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
40 changes: 40 additions & 0 deletions india_compliance/gst_india/client_scripts/sales_invoice_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ frappe.listview_settings[DOCTYPE].onload = function (list_view) {
);
}

if (frappe.model.can_print("e-Waybill Log")) {
add_bulk_action_for_invoices(
list_view,
__("Print e-Waybill"),
bulk_e_waybill_print
);
}

if (india_compliance.is_e_invoice_enabled())
add_bulk_action_for_invoices(
list_view,
Expand Down Expand Up @@ -76,6 +84,38 @@ function show_bulk_update_transporter_dialog(docnames) {
d.show();
}

async function bulk_e_waybill_print(docnames) {
frappe.call({
method: "india_compliance.gst_india.utils.e_waybill.get_valid_and_invalid_e_waybill_log",
args: {
doctype: DOCTYPE,
docs: JSON.stringify(docnames),
},
callback: function (r) {
if (r.message) {
if (r.message.invalid_log) {
frappe.msgprint(
__(
"Cannot print e-Waybill for following documents:<br><br><strong>{0}</strong>",
[r.message.invalid_log.join("<br>")]
)
);
}

window.open_url_post(
"/api/method/frappe.utils.print_format.download_multi_pdf",
{
doctype: "e-Waybill Log",
name: JSON.stringify(r.message.valid_log),
format: "e-Waybill",
},
true
);
}
},
});
}

async function enqueue_bulk_e_waybill_generation(docnames) {
enqueue_bulk_generation(
"india_compliance.gst_india.utils.e_waybill.enqueue_bulk_e_waybill_generation",
Expand Down
68 changes: 68 additions & 0 deletions india_compliance/gst_india/utils/e_waybill.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

import frappe
Expand Down Expand Up @@ -656,6 +657,73 @@ def get_pdf_filename(e_waybill_number):
return f"e-Waybill_{e_waybill_number}.pdf"


@frappe.whitelist()
def get_valid_and_invalid_e_waybill_log(
doctype,
docs,
):
"""
- Validate e-Waybill Log
- If not latest update
- Return Valid e-waybill no and invalid documents
"""
frappe.has_permission("e-Waybill Log", "print", throw=True)

if isinstance(docs, str):
docs = json.loads(docs)

valid_log = []
invalid_log = []

# get e-waybill nos from sales invoice
e_waybills = {
doc.name: doc.ewaybill
for doc in frappe.get_all(
"Sales Invoice", filters={"name": ["in", docs]}, fields=["name", "ewaybill"]
)
}

e_waybill_log = {
log.name: log
for log in frappe.get_all(
"e-Waybill Log",
filters={"name": ["in", e_waybills.values()]},
fields=[
"name",
"is_latest_data",
],
)
}

for docname in docs:
e_waybill_no = e_waybills.get(docname)

if not e_waybill_no:
invalid_log.append(docname)
continue

log = e_waybill_log.get(e_waybill_no)

if not log:
invalid_log.append(docname)
continue

if log.is_latest_data:
valid_log.append(e_waybill_no)
continue

try:
fetch_e_waybill_data(doctype=doctype, docname=docname, attach=False)
valid_log.append(e_waybill_no)
except Exception:
invalid_log.append(docname)

if not valid_log:
frappe.throw(_("No Valid e-Waybill log found."))

return {"valid_log": valid_log, "invalid_log": invalid_log}


#######################################################################################
### Other Utility Functions ###########################################################
#######################################################################################
Expand Down

0 comments on commit d6c4d8f

Please sign in to comment.