blob: 437465a6a26006eaf833cabd1eeab2647dc6bad5 [file] [log] [blame]
Rushabh Mehtab3c8f442017-06-21 17:22:38 +05301import frappe, re
2from frappe import _
3from erpnext.regional.india import states, state_numbers
4
5def validate_gstin_for_india(doc, method):
6 if not hasattr(doc, 'gstin'):
7 return
8
9 if doc.gstin:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053010 doc.gstin = doc.gstin.upper()
Aditya Duggalf1bd39c2017-06-29 14:25:19 +053011 if doc.gstin != "NA":
12 p = re.compile("[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[1-9A-Za-z]{1}[Z]{1}[0-9a-zA-Z]{1}")
13 if not p.match(doc.gstin):
14 frappe.throw(_("Invalid GSTIN or Enter NA for Unregistered"))
Rushabh Mehtab3c8f442017-06-21 17:22:38 +053015
16 if not doc.gst_state:
17 if doc.state in states:
18 doc.gst_state = doc.state
19
Nabin Haitcd61a202017-07-08 13:52:13 +053020 if doc.gst_state:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053021 doc.gst_state_number = state_numbers[doc.gst_state]
Nabin Haitcd61a202017-07-08 13:52:13 +053022 if doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]:
Nabin Haitf3f0dfe2017-07-06 14:49:34 +053023 frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
Nabin Haitcd61a202017-07-08 13:52:13 +053024 .format(doc.gst_state_number))
Rushabh Mehta7231f292017-07-13 15:00:56 +053025
26# don't remove this function it is used in tests
27def test_method():
28 '''test function'''
29 return 'overridden'