Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fetch e-invoice data for manually generated IRN #2978

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions india_compliance/gst_india/client_scripts/e_invoice_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,12 @@ frappe.ui.form.on("Sales Invoice", {
) {
frm.add_custom_button(
__("Generate"),
() => {
frappe.call({
async () => {
await taxpayer_api.call({
method: "india_compliance.gst_india.utils.e_invoice.generate_e_invoice",
args: { docname: frm.doc.name, force: true },
callback: async (r) => {
if (r.message?.error_type == "otp_requested") {
await india_compliance.authenticate_otp(frm.doc.company_gstin);
await frappe.call({
method: "india_compliance.gst_india.utils.e_invoice.handle_duplicate_irn_error",
args: r.message
});
}
frm.refresh();
},
});
frm.refresh();
},
"e-Invoice"
);
Expand Down Expand Up @@ -204,19 +195,17 @@ function show_mark_e_invoice_as_generated_dialog(frm) {
title: __("Update e-Invoice Details"),
fields: get_generated_e_invoice_dialog_fields(),
primary_action_label: __("Update"),
primary_action(values) {
frappe.call({
async primary_action(values) {
await taxpayer_api.call({
method: "india_compliance.gst_india.utils.e_invoice.mark_e_invoice_as_generated",
args: {
doctype: frm.doctype,
docname: frm.doc.name,
values,
},
callback: () => {
d.hide();
frm.refresh();
},
}
});
d.hide();
frm.refresh();
},
});

Expand All @@ -230,19 +219,7 @@ function get_generated_e_invoice_dialog_fields() {
fieldname: "irn",
fieldtype: "Data",
reqd: 1,
},
{
label: "Acknowledgement Number",
fieldname: "ack_no",
fieldtype: "Data",
reqd: 1,
},
{
label: "Acknowledged On",
fieldname: "ack_dt",
fieldtype: "Datetime",
reqd: 1,
},
}
];
return fields;
}
Expand Down
34 changes: 25 additions & 9 deletions india_compliance/gst_india/utils/e_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_datetime,
getdate,
random_string,
rounded,
)

from india_compliance.exceptions import GSPServerError
Expand Down Expand Up @@ -137,7 +138,7 @@ def generate_e_invoice(docname, throw=True, force=False):
current_gstin = data.get("BuyerDtls").get("Gstin")
current_invoice_amount = data.get("ValDtls").get("TotInvVal")

return handle_duplicate_irn_error(
return fetch_irn_details_compare_invoice(
irn_data=result.Desc,
current_gstin=current_gstin,
current_invoice_amount=current_invoice_amount,
Expand Down Expand Up @@ -184,15 +185,16 @@ def generate_e_invoice(docname, throw=True, force=False):


@frappe.whitelist()
def handle_duplicate_irn_error(
def fetch_irn_details_compare_invoice(
irn_data,
current_gstin,
current_invoice_amount,
doc=None,
docname=None,
message=None,
):
"""
Handle Duplicate IRN errors by fetching the IRN details and comparing with the current invoice.
Handle Duplicate IRN errors and Manually Generated by fetching the IRN details and comparing with the current invoice.

Steps:
1. Fetch IRN details using the IRN number using e-Invoice API.
Expand Down Expand Up @@ -236,7 +238,12 @@ def handle_duplicate_irn_error(
if response.error_code:
response = irn_data

return log_and_process_e_invoice_generation(doc, response, api.sandbox_mode)
if irn_data.einvoice_status == "Manually Generated":
response.einvoice_status = "Manually Generated"

return log_and_process_e_invoice_generation(
doc, response, api.sandbox_mode, message=message
)


def verify_e_invoice_details(current_gstin, current_invoice_amount, signed_data):
Expand Down Expand Up @@ -389,17 +396,26 @@ def mark_e_invoice_as_generated(doctype, docname, values):
doc = load_doc(doctype, docname, "submit")

values = frappe.parse_json(values)
result = frappe._dict(
irn_data = frappe._dict(
{
"Irn": values.irn,
"AckDt": values.ack_dt,
"AckNo": values.ack_no,
"einvoice_status": "Manually Generated",
}
)

return log_and_process_e_invoice_generation(
doc, result, message="e-Invoice updated successfully"
grand_total_fieldname = (
"base_grand_total"
if doc.get("disable_rounded_total", 1)
else "base_rounded_total"
)
grand_total = abs(rounded(doc.get(grand_total_fieldname), 2))

return fetch_irn_details_compare_invoice(
irn_data,
doc.billing_address_gstin,
grand_total,
doc,
message="e-Invoice updated successfully",
)


Expand Down