diff --git a/erpnext/regional/india/e_invoice/e_invoice_utils.py b/erpnext/regional/india/e_invoice/e_invoice_utils.py index 044c2c88da2..fe48bbe0af4 100644 --- a/erpnext/regional/india/e_invoice/e_invoice_utils.py +++ b/erpnext/regional/india/e_invoice/e_invoice_utils.py @@ -162,7 +162,7 @@ def generate_irn(doctype, name): json_str = aes_decrypt(enc_json, credentials.sek) signed_einvoice = json.loads(json_str) - handle_irn_response(signed_einvoice) + decrypt_irn_response(signed_einvoice) update_einvoice_fields(doctype, name, signed_einvoice) @@ -218,7 +218,7 @@ def cancel_eway_bill(doctype, name, eway_bill, reason, remark=''): return res -def handle_irn_response(data): +def decrypt_irn_response(data): enc_signed_invoice = data['SignedInvoice'] enc_signed_qr_code = data['SignedQRCode'] signed_invoice = jwt_decrypt(enc_signed_invoice)['data'] @@ -473,7 +473,7 @@ def make_einvoice(doctype, name): else: frappe.throw(error_msgs[0], title=_('E Invoice Validation Failed')) - return {'einvoice': json.dumps([einvoice])} + return json.dumps(einvoice) def validate_einvoice(validations, einvoice, error_msgs=[]): type_map = { 'string': 'str', 'number': 'int', 'object': 'dict', 'array': 'list' } diff --git a/erpnext/regional/india/e_invoice/einvoice.js b/erpnext/regional/india/e_invoice/einvoice.js index 87e2031f883..bcdd6ef02ff 100644 --- a/erpnext/regional/india/e_invoice/einvoice.js +++ b/erpnext/regional/india/e_invoice/einvoice.js @@ -3,24 +3,157 @@ erpnext.setup_einvoice_actions = (doctype) => { refresh(frm) { const einvoicing_enabled = frappe.db.get_value("E Invoice Settings", "E Invoice Settings", "enable"); const supply_type = frm.doc.gst_category; - if (!einvoicing_enabled - || !['Registered Regular', 'SEZ', 'Overseas', 'Deemed Export'].includes(supply_type)) { - return; + const valid_supply_type = ['Registered Regular', 'SEZ', 'Overseas', 'Deemed Export'].includes(supply_type) + + if (!einvoicing_enabled || !valid_supply_type) return; + + const { docstatus, irn, irn_cancelled, ewaybill, eway_bill_cancelled, doctype, name, __unsaved } = frm.doc; + + if (docstatus == 0 && !irn && !__unsaved) { + frm.add_custom_button( + _("Generate IRN"), + () => { + frappe.call({ + method: 'erpnext.regional.india.e_invoice.e_invoice_utils.generate_irn', + args: { doctype: doctype, name: name }, + freeze: true, + callback: () => frm.reload_doc() + }) + }, + __("E Invoicing") + ); } + + if (docstatus == 1 && irn && !irn_cancelled) { + frm.add_custom_button( + __("Cancel IRN"), + () => { + const fields = [ + { + "label" : "Reason", + "fieldname": "reason", + "fieldtype": "Select", + "reqd": 1, + "default": "1-Duplicate", + "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] + }, + { + "label": "Remark", + "fieldname": "remark", + "fieldtype": "Data", + "reqd": 1 + } + ]; + const d = new frappe.ui.Dialog({ + title: __("Cancel IRN"), + fields: fields, + primary_action: function() { + const data = d.get_values(); + frappe.call({ + method: 'erpnext.regional.india.e_invoice.e_invoice_utils.cancel_irn', + args: { + doctype: doctype, + name: name, + irn: irn, + reason: data.reason.split('-')[0], + remark: data.remark + }, + freeze: true, + callback: () => frm.reload_doc() || d.hide(), + error: () => d.hide() + }); + }, + primary_action_label: __('Submit') + }); + d.show(); + }, + __("E Invoicing") + ) + } + + if (docstatus == 1 && irn && !irn_cancelled && !eway_bill_cancelled) { + frm.add_custom_button( + __("Cancel E-Way Bill"), + () => { + const fields = [ + { + "label" : "Reason", + "fieldname": "reason", + "fieldtype": "Select", + "reqd": 1, + "default": "1-Duplicate", + "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] + }, + { + "label": "Remark", + "fieldname": "remark", + "fieldtype": "Data", + "reqd": 1 + } + ] + const d = new frappe.ui.Dialog({ + title: __('Cancel E-Way Bill'), + fields: fields, + primary_action: function() { + const data = d.get_values(); + frappe.call({ + method: 'erpnext.regional.india.e_invoice.e_invoice_utils.cancel_eway_bill', + args: { + doctype: doctype, + name: name, + eway_bill: ewaybill, + reason: data.reason.split('-')[0], + remark: data.remark + }, + freeze: true, + callback: () => frm.reload_doc() || d.hide(), + error: () => d.hide() + }) + }, + primary_action_label: __('Submit') + }); + d.show(); + }, + __("E Invoicing") + ); + } + // if (frm.doc.docstatus == 0 && !frm.doc.irn && !frm.doc.__unsaved) { // frm.add_custom_button( - // "Generate IRN", + // "Download E-Invoice", // () => { // frappe.call({ - // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.generate_irn', + // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.make_einvoice', // args: { doctype: frm.doc.doctype, name: frm.doc.name }, // freeze: true, - // callback: () => frm.reload_doc() + // callback: (res) => { + // if (!res.exc) { + // const args = { + // cmd: 'erpnext.regional.india.e_invoice.e_invoice_utils.download_einvoice', + // einvoice: res.message.einvoice, + // name: frm.doc.name + // }; + // open_url_post(frappe.request.url, args); + // } + // } // }) - // } - // ) + // }, "E-Invoicing"); + // frm.add_custom_button( + // "Upload Signed E-Invoice", + // () => { + // new frappe.ui.FileUploader({ + // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.upload_einvoice', + // allow_multiple: 0, + // doctype: frm.doc.doctype, + // docname: frm.doc.name, + // on_success: (attachment, r) => { + // if (!r.exc) { + // frm.reload_doc(); + // } + // } + // }); + // }, "E-Invoicing"); // } - // if (frm.doc.docstatus == 1 && frm.doc.irn && !frm.doc.irn_cancelled) { // frm.add_custom_button( // "Cancel IRN", @@ -28,147 +161,45 @@ erpnext.setup_einvoice_actions = (doctype) => { // const d = new frappe.ui.Dialog({ // title: __('Cancel IRN'), // fields: [ - // { "label" : "Reason", "fieldname": "reason", "fieldtype": "Select", "reqd": 1, "default": "1-Duplicate", - // "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] }, - // { "label": "Remark", "fieldname": "remark", "fieldtype": "Data", "reqd": 1 } + // { + // "label" : "Reason", "fieldname": "reason", + // "fieldtype": "Select", "reqd": 1, "default": "1-Duplicate", + // "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] + // }, + // { + // "label": "Remark", "fieldname": "remark", "fieldtype": "Data", "reqd": 1 + // } // ], // primary_action: function() { // const data = d.get_values(); - // frappe.call({ - // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.cancel_irn', - // args: { - // doctype: frm.doc.doctype, - // name: frm.doc.name, - // irn: frm.doc.irn, - // reason: data.reason.split('-')[0], - // remark: data.remark - // }, - // freeze: true, - // callback: () => frm.reload_doc() || d.hide(), - // error: () => d.hide() - // }) + // const args = { + // cmd: 'erpnext.regional.india.e_invoice.e_invoice_utils.download_cancel_einvoice', + // irn: frm.doc.irn, reason: data.reason.split('-')[0], remark: data.remark, name: frm.doc.name + // }; + // open_url_post(frappe.request.url, args); + // d.hide(); // }, - // primary_action_label: __('Submit') + // primary_action_label: __('Download JSON') // }); // d.show(); - // } - // ) - // } - - // if (frm.doc.docstatus == 1 && frm.doc.irn && !frm.doc.irn_cancelled && !frm.doc.eway_bill_cancelled) { - // frm.add_custom_button( - // "Cancel E-Way Bill", - // () => { - // const d = new frappe.ui.Dialog({ - // title: __('Cancel E-Way Bill'), - // fields: [ - // { "label" : "Reason", "fieldname": "reason", "fieldtype": "Select", "reqd": 1, "default": "1-Duplicate", - // "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] }, - // { "label": "Remark", "fieldname": "remark", "fieldtype": "Data", "reqd": 1 } - // ], - // primary_action: function() { - // const data = d.get_values(); - // frappe.call({ - // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.cancel_eway_bill', - // args: { eway_bill: frm.doc.ewaybill, reason: data.reason.split('-')[0], remark: data.remark }, - // freeze: true, - // callback: () => { - // frm.set_value('eway_bill_cancelled', 1); - // frm.save("Update"); - // d.hide() - // }, - // error: () => d.hide() - // }) - // }, - // primary_action_label: __('Submit') - // }); - // d.show(); - // } - // ) - // } - - if (frm.doc.docstatus == 0 && !frm.doc.irn && !frm.doc.__unsaved) { - frm.add_custom_button( - "Download E-Invoice", - () => { - frappe.call({ - method: 'erpnext.regional.india.e_invoice.e_invoice_utils.make_einvoice', - args: { doctype: frm.doc.doctype, name: frm.doc.name }, - freeze: true, - callback: (res) => { - if (!res.exc) { - const args = { - cmd: 'erpnext.regional.india.e_invoice.e_invoice_utils.download_einvoice', - einvoice: res.message.einvoice, - name: frm.doc.name - }; - open_url_post(frappe.request.url, args); - } - } - }) - }, "E-Invoicing"); - frm.add_custom_button( - "Upload Signed E-Invoice", - () => { - new frappe.ui.FileUploader({ - method: 'erpnext.regional.india.e_invoice.e_invoice_utils.upload_einvoice', - allow_multiple: 0, - doctype: frm.doc.doctype, - docname: frm.doc.name, - on_success: (attachment, r) => { - if (!r.exc) { - frm.reload_doc(); - } - } - }); - }, "E-Invoicing"); - } - if (frm.doc.docstatus == 1 && frm.doc.irn && !frm.doc.irn_cancelled) { - frm.add_custom_button( - "Cancel IRN", - () => { - const d = new frappe.ui.Dialog({ - title: __('Cancel IRN'), - fields: [ - { - "label" : "Reason", "fieldname": "reason", - "fieldtype": "Select", "reqd": 1, "default": "1-Duplicate", - "options": ["1-Duplicate", "2-Data Entry Error", "3-Order Cancelled", "4-Other"] - }, - { - "label": "Remark", "fieldname": "remark", "fieldtype": "Data", "reqd": 1 - } - ], - primary_action: function() { - const data = d.get_values(); - const args = { - cmd: 'erpnext.regional.india.e_invoice.e_invoice_utils.download_cancel_einvoice', - irn: frm.doc.irn, reason: data.reason.split('-')[0], remark: data.remark, name: frm.doc.name - }; - open_url_post(frappe.request.url, args); - d.hide(); - }, - primary_action_label: __('Download JSON') - }); - d.show(); - }, "E-Invoicing"); + // }, "E-Invoicing"); - frm.add_custom_button( - "Upload Cancel JSON", - () => { - new frappe.ui.FileUploader({ - method: 'erpnext.regional.india.e_invoice.e_invoice_utils.upload_cancel_ack', - allow_multiple: 0, - doctype: frm.doc.doctype, - docname: frm.doc.name, - on_success: (attachment, r) => { - if (!r.exc) { - frm.reload_doc(); - } - } - }); - }, "E-Invoicing"); - } + // frm.add_custom_button( + // "Upload Cancel JSON", + // () => { + // new frappe.ui.FileUploader({ + // method: 'erpnext.regional.india.e_invoice.e_invoice_utils.upload_cancel_ack', + // allow_multiple: 0, + // doctype: frm.doc.doctype, + // docname: frm.doc.name, + // on_success: (attachment, r) => { + // if (!r.exc) { + // frm.reload_doc(); + // } + // } + // }); + // }, "E-Invoicing"); + // } } }) } \ No newline at end of file