Skip to content

Commit

Permalink
fix: changes as per comments
Browse files Browse the repository at this point in the history
(cherry picked from commit 42d9bee)
  • Loading branch information
Sanket322 authored and mergify[bot] committed Aug 22, 2024
1 parent 64a1ff8 commit acb7a50
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 184 deletions.
1 change: 1 addition & 0 deletions india_compliance/gst_india/client_scripts/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ validate_gstin(DOCTYPE);
update_gstin_in_other_documents(DOCTYPE);
show_overseas_disabled_warning(DOCTYPE);
set_gstin_options_and_status(DOCTYPE);
set_pan_status(DOCTYPE)

frappe.ui.form.off(DOCTYPE, "make_default_tax_template");
frappe.ui.form.on(DOCTYPE, {
Expand Down
1 change: 1 addition & 0 deletions india_compliance/gst_india/client_scripts/customer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ update_gstin_in_other_documents(DOCTYPE);
show_overseas_disabled_warning(DOCTYPE);
set_gstin_options_and_status(DOCTYPE);
set_gst_category(DOCTYPE);
set_pan_status(DOCTYPE)
17 changes: 11 additions & 6 deletions india_compliance/gst_india/client_scripts/party.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ function validate_gstin(doctype) {
frm.doc.pan = pan;
frm.refresh_field("pan");
set_party_type(frm);
if(doctype != "Address"){
india_compliance.set_pan_status(frm.get_field("pan"));
}
}
},
});
Expand Down Expand Up @@ -123,15 +126,17 @@ function set_gstin_options_and_status(doctype) {
refresh(frm) {
set_gstin_options(frm);
india_compliance.set_gstin_status(frm.get_field("gstin"));
if (doctype !== "Address") {
india_compliance.set_pan_status(frm.get_field("pan"));
}
},
gstin(frm) {
india_compliance.set_gstin_status(frm.get_field("gstin"));
if (doctype !== "Address") {
india_compliance.set_pan_status(frm.get_field("pan"));
}
},
});
}

function set_pan_status(doctype) {
frappe.ui.form.on(doctype, {
refresh(frm) {
india_compliance.set_pan_status(frm.get_field("pan"));
},
pan(frm) {
india_compliance.set_pan_status(frm.get_field("pan"));
Expand Down
1 change: 1 addition & 0 deletions india_compliance/gst_india/client_scripts/supplier.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ update_gstin_in_other_documents(DOCTYPE);
show_overseas_disabled_warning(DOCTYPE);
set_gstin_options_and_status(DOCTYPE);
set_gst_category(DOCTYPE);
set_pan_status(DOCTYPE)

frappe.ui.form.on(DOCTYPE, {
gstin(frm) {
Expand Down
3 changes: 0 additions & 3 deletions india_compliance/gst_india/doctype/pan/pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ frappe.ui.form.on("Pan", {
frm.call("update_pan_status");
});
},
pan(frm) {
frm.doc.pan = frm.doc.pan.toUpperCase();
},
});
103 changes: 53 additions & 50 deletions india_compliance/gst_india/doctype/pan/pan.json
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
{
"actions": [],
"allow_rename": 1,
"creation": "2024-07-31 13:10:35.277697",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"pan",
"pan_status"
],
"fields": [
{
"fieldname": "pan",
"fieldtype": "Data",
"label": "Pan"
},
{
"fieldname": "pan_status",
"fieldtype": "Data",
"label": "Pan Status"
},
{
"fieldname": "last_updated_on",
"fieldtype": "Datetime",
"label": "Last Updated On"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-07-31 13:11:07.830805",
"modified_by": "Administrator",
"module": "GST India",
"name": "Pan",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "creation",
"sort_order": "DESC",
"states": []
"actions": [],
"allow_rename": 1,
"creation": "2024-07-31 13:10:35.277697",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"pan",
"pan_status",
"last_updated_on"
],
"fields": [
{
"fieldname": "pan",
"fieldtype": "Data",
"label": "Pan"
},
{
"fieldname": "pan_status",
"fieldtype": "Data",
"label": "Pan Status"
},
{
"fieldname": "last_updated_on",
"fieldtype": "Datetime",
"label": "Last Updated On"
}
],
"in_create": 1,
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-08-07 15:04:50.116211",
"modified_by": "Administrator",
"module": "GST India",
"name": "Pan",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"read_only": 1,
"sort_field": "creation",
"sort_order": "DESC",
"states": []
}
123 changes: 122 additions & 1 deletion india_compliance/gst_india/doctype/pan/pan.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,135 @@
# Copyright (c) 2024, Resilient Tech and contributors
# For license information, please see license.txt

import random

import requests

import frappe
from frappe.model.document import Document
from frappe.utils import now

multiplication_table = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
]

permutation_table = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
]

from india_compliance.gst_india.overrides.party import get_pancard_status
inverse_table = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9]


class Pan(Document):
@frappe.whitelist()
def update_pan_status(self):
get_pancard_status(self.pan, True)
frappe.msgprint("PAN Status Updated")

def before_save(self):
self.name = self.pan.upper()


def verhoeff_checksum(number: str) -> int:
"""Calculate the Verhoeff checksum digit."""
c = 0
n = len(number)
for i in range(n):
c = multiplication_table[c][
permutation_table[(i + 1) % 8][int(number[n - i - 1])]
]
return inverse_table[c]


def generate_aadhaar_number():
"""Generate a valid Aadhaar number using the Verhoeff algorithm."""
base_number = "".join(str(random.randint(0, 9)) for _ in range(11))
check_digit = verhoeff_checksum(base_number)
return base_number + str(check_digit)


def fetch_pan_status_from_api(aadhaar_number, pan, force_update):
"""This is an unofficial API"""
url = "https://eportal.incometax.gov.in/iec/servicesapi/getEntity"
payload = {
"aadhaarNumber": aadhaar_number, # this is random generated aadhaar_number
"pan": pan,
"preLoginFlag": "Y",
"serviceName": "linkAadhaarPreLoginService",
}

try:
response = requests.post(url, json=payload)
return response.json().get("messages", [])
except ConnectionError:
msg = "Connection error. Please retry after some time."
except Exception:
msg = "An error occurred. Please retry after some time."

if force_update:
frappe.throw(msg)
else:
return ""


def update_pan_document(pan, status):
if docname := frappe.db.exists("Pan", pan):
doc = frappe.get_doc("Pan", docname)
else:
doc = frappe.new_doc("Pan")

doc.update(
{
"pan": pan,
"pan_status": status,
"last_updated_on": now(),
}
)
doc.save()


def get_pancard_status(pan, force_update):
aadhaar_number = generate_aadhaar_number()
messages = fetch_pan_status_from_api(aadhaar_number, pan, force_update)

if not messages:
return

error_code_desc_map = {
"EF40124": "Valid", # pan linked to generated aadhar card number
"EF40026": "Valid", # pan linked but not to generated aadhar card
"EF40119": "Valid", # not an individual taxpayer : AAACS8577K
"EF40089": "Invalid", # invalid pan : OIMPS2320M
"EF40024": "Not-Linked",
"EF40077": "", # Invalid Aadhaar number
}

status = error_code_desc_map.get(messages[0].get("code", ""), "")
if not status:
return

update_pan_document(pan, status)


@frappe.whitelist()
def get_pan_status(pan, force_update=False):
if force_update or not frappe.db.exists("Pan", pan):
get_pancard_status(pan, force_update)

return frappe.db.get_value("Pan", pan, ["pan_status", "last_updated_on"])
Loading

0 comments on commit acb7a50

Please sign in to comment.