diff --git a/erpnext/regional/india/e_invoice/einvoice.js b/erpnext/regional/india/e_invoice/einvoice.js index 4748b265dc2b..ef24ce791c06 100644 --- a/erpnext/regional/india/e_invoice/einvoice.js +++ b/erpnext/regional/india/e_invoice/einvoice.js @@ -11,7 +11,7 @@ erpnext.setup_einvoice_actions = (doctype) => { if (!invoice_eligible) return; - const { doctype, irn, irn_cancelled, ewaybill, eway_bill_cancelled, name, __unsaved } = frm.doc; + const { doctype, irn, irn_cancelled, ewaybill, eway_bill_cancelled, name, qrcode_image, __unsaved } = frm.doc; const add_custom_button = (label, action) => { if (!frm.custom_buttons[label]) { @@ -175,27 +175,44 @@ erpnext.setup_einvoice_actions = (doctype) => { } if (irn && !irn_cancelled) { - const action = () => { - const dialog = frappe.msgprint({ - title: __("Generate QRCode"), - message: __("Generate and attach QR Code using IRN?"), - primary_action: { - action: function() { - frappe.call({ - method: 'erpnext.regional.india.e_invoice.utils.generate_qrcode', - args: { doctype, docname: name }, - freeze: true, - callback: () => frm.reload_doc() || dialog.hide(), - error: () => dialog.hide() - }); + let is_qrcode_attached = false; + if (qrcode_image && frm.attachments) { + let attachments = frm.attachments.get_attachments(); + if (attachments.length != 0) { + for (let i = 0; i < attachments.length; i++) { + if (attachments[i].file_url == qrcode_image) { + is_qrcode_attached = true; + break; } - }, + } + } + } + if (!is_qrcode_attached) { + const action = () => { + if (frm.doc.__unsaved) { + frappe.throw(__('Please save the document to generate QRCode.')); + } + const dialog = frappe.msgprint({ + title: __("Generate QRCode"), + message: __("Generate and attach QR Code using IRN?"), + primary_action: { + action: function() { + frappe.call({ + method: 'erpnext.regional.india.e_invoice.utils.generate_qrcode', + args: { doctype, docname: name }, + freeze: true, + callback: () => frm.reload_doc() || dialog.hide(), + error: () => dialog.hide() + }); + } + }, primary_action_label: __('Yes') }); dialog.show(); }; add_custom_button(__("Generate QRCode"), action); } + } } }); }; diff --git a/erpnext/regional/india/e_invoice/utils.py b/erpnext/regional/india/e_invoice/utils.py index bcb3e4fb85a9..e5a1a59e42f0 100644 --- a/erpnext/regional/india/e_invoice/utils.py +++ b/erpnext/regional/india/e_invoice/utils.py @@ -1010,13 +1010,32 @@ def bulk_generate_irn(invoices): return failed def fetch_and_attach_qrcode_from_irn(self): - qrcode = self.get_qrcode_from_irn(self.invoice.irn) - if qrcode: - qrcode_file = self.create_qr_code_file(qrcode) - frappe.db.set_value("Sales Invoice", self.invoice.name, "qrcode_image", qrcode_file.file_url) - frappe.msgprint(_("QR Code attached to the invoice"), alert=True) + is_qrcode_file_attached = self.invoice.qrcode_image and frappe.db.exists( + "File", + { + "attached_to_doctype": "Sales Invoice", + "attached_to_name": self.invoice.name, + "file_url": self.invoice.qrcode_image, + "attached_to_field": "qrcode_image", + }, + ) + if not is_qrcode_file_attached: + if self.invoice.signed_qr_code: + self.attach_qrcode_image() + frappe.db.set_value( + "Sales Invoice", self.invoice.name, "qrcode_image", self.invoice.qrcode_image + ) + frappe.msgprint(_("QR Code attached to the invoice."), alert=True) + else: + qrcode = self.get_qrcode_from_irn(self.invoice.irn) + if qrcode: + qrcode_file = self.create_qr_code_file(qrcode) + frappe.db.set_value("Sales Invoice", self.invoice.name, "qrcode_image", qrcode_file.file_url) + frappe.msgprint(_("QR Code attached to the invoice."), alert=True) + else: + frappe.msgprint(_("QR Code not found for the IRN"), alert=True) else: - frappe.msgprint(_("QR Code not found for the IRN"), alert=True) + frappe.msgprint(_("QR Code is already Attached"), indicator="green", alert=True) def get_qrcode_from_irn(self, irn): import requests @@ -1281,7 +1300,6 @@ def set_einvoice_data(self, res): def attach_qrcode_image(self): qrcode = self.invoice.signed_qr_code - qr_image = io.BytesIO() url = qrcreate(qrcode, error="L") url.png(qr_image, scale=2, quiet_zone=1)