Skip to content

Commit

Permalink
fix(india): duplicate qrcode and hide button (#31100)
Browse files Browse the repository at this point in the history
  • Loading branch information
maharshivpatel committed May 27, 2022
1 parent e3c0d01 commit 935e5b1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 22 deletions.
47 changes: 32 additions & 15 deletions erpnext/regional/india/e_invoice/einvoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
}
}
}
});
};
Expand Down
32 changes: 25 additions & 7 deletions erpnext/regional/india/e_invoice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 935e5b1

Please sign in to comment.