fix(india): duplicate qrcode and hide button (#31100)

This commit is contained in:
maharshivpatel 2022-05-27 11:48:55 +05:30 committed by GitHub
parent e3c0d0134e
commit 935e5b1dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 22 deletions

View File

@ -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);
}
}
}
});
};

View File

@ -1010,13 +1010,32 @@ class GSPConnector:
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 @@ class GSPConnector:
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)