From cc43805f25c1e3b69c18d78cf5bc6a409d95eac7 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Wed, 30 Sep 2020 19:34:09 +0530 Subject: [PATCH] feat: cancel e-way bill before cancelling IRN --- erpnext/regional/india/e_invoice_utils.py | 15 ++++++++++ erpnext/regional/india/einvoice.js | 35 ++++++++++++++++++++++- erpnext/regional/india/setup.py | 8 ++++-- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/erpnext/regional/india/e_invoice_utils.py b/erpnext/regional/india/e_invoice_utils.py index f597e329621..dc38b25bed7 100644 --- a/erpnext/regional/india/e_invoice_utils.py +++ b/erpnext/regional/india/e_invoice_utils.py @@ -192,6 +192,21 @@ def cancel_irn(irn, reason, remark=''): return res +@frappe.whitelist() +def cancel_eway_bill(eway_bill, reason, remark=''): + einv_creds = get_einv_credentials() + endpoint = 'https://einv-apisandbox.nic.in/ewaybillapi/v1.03/ewayapi' + headers = get_header(einv_creds) + + cancel_eway_bill_json = json.dumps(dict(ewbNo=eway_bill, cancelRsnCode=reason, cancelRmrk=remark)) + enc_json = aes_encrypt(cancel_eway_bill_json, einv_creds.sek) + payload = dict(action="CANEWB", Data=enc_json) + + res = make_post_request(endpoint, headers=headers, data=json.dumps(payload)) + handle_err_response(res) + + return res + def handle_irn_response(data): enc_signed_invoice = data['SignedInvoice'] enc_signed_qr_code = data['SignedQRCode'] diff --git a/erpnext/regional/india/einvoice.js b/erpnext/regional/india/einvoice.js index 05cf56e8f51..20461ea317b 100644 --- a/erpnext/regional/india/einvoice.js +++ b/erpnext/regional/india/einvoice.js @@ -28,7 +28,8 @@ erpnext.setup_einvoice_actions = (doctype) => { }) } ) - } else if (frm.doc.docstatus == 1 && frm.doc.irn && !frm.doc.irn_cancelled) { + } + if (frm.doc.docstatus == 1 && frm.doc.irn && !frm.doc.irn_cancelled) { frm.add_custom_button( "Cancel IRN", () => { @@ -59,6 +60,38 @@ erpnext.setup_einvoice_actions = (doctype) => { } ) } + 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_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(); + } + ) + } } + }) } \ No newline at end of file diff --git a/erpnext/regional/india/setup.py b/erpnext/regional/india/setup.py index 0b032019145..d5aba17a88f 100644 --- a/erpnext/regional/india/setup.py +++ b/erpnext/regional/india/setup.py @@ -369,7 +369,7 @@ def make_custom_fields(update=True): 'fieldname': 'ewaybill', 'label': 'e-Way Bill No.', 'fieldtype': 'Data', - 'depends_on': 'eval:(doc.docstatus === 1)', + 'depends_on': 'eval:((doc.docstatus === 1 || doc.ewaybill) && doc.eway_bill_cancelled === 0)', 'allow_on_submit': 1, 'insert_after': 'tax_id', 'translatable': 0 @@ -379,10 +379,12 @@ def make_custom_fields(update=True): si_einvoice_fields = [ dict(fieldname='irn', label='IRN', fieldtype='Data', read_only=1, insert_after='customer', no_copy=1, depends_on='eval:in_list(["Register Regular", "SEZ", "Overseas", "Deemed Export"], doc.gst_category) && doc.irn_cancelled === 0'), - dict(fieldname='irn_cancelled', label='IRN Cancelled', fieldtype='Check', no_copy=1 + dict(fieldname='irn_cancelled', label='IRN Cancelled', fieldtype='Check', no_copy=1, depends_on='eval:(doc.irn_cancelled === 1)', read_only=1, allow_on_submit=1, insert_after='customer'), dict(fieldname='signed_einvoice', fieldtype='Code', options='JSON', hidden=1, no_copy=1, read_only=1), - dict(fieldname='signed_qr_code', fieldtype='Code', options='JSON', hidden=1, no_copy=1, read_only=1) + dict(fieldname='signed_qr_code', fieldtype='Code', options='JSON', hidden=1, no_copy=1, read_only=1), + dict(fieldname='eway_bill_cancelled', label='E-Way Bill Cancelled', fieldtype='Check', no_copy=1, + depends_on='eval:(doc.eway_bill_cancelled === 1)', read_only=1, allow_on_submit=1, insert_after='customer'), ] custom_fields = {