fix: use division consistent with Python 3 & other changes

This commit is contained in:
Sagar Vora 2019-01-10 11:07:51 +05:30
parent 2825b929c1
commit 07cf4e8f5b

View File

@ -8,44 +8,47 @@ def validate_gstin_for_india(doc, method):
if not hasattr(doc, 'gstin'): if not hasattr(doc, 'gstin'):
return return
if doc.gstin: doc.gstin = doc.gstin.upper().strip()
doc.gstin = doc.gstin.upper() if not doc.gstin or doc.gstin == 'NA':
if doc.gstin not in ["NA", "na"]: return
p = re.compile("[0-9]{2}[A-Z]{4}[0-9A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}[1-9A-Z]{1}[0-9A-Z]{1}")
if not p.match(doc.gstin) or doc.gstin != get_gstin_with_check_digit(doc.gstin[:-1]):
frappe.throw(_("Invalid GSTIN!! Check for typos or Enter NA for Unregistered"))
if not doc.gst_state: if len(doc.gstin) != 15:
if doc.state in states: frappe.throw(_("Invalid GSTIN! A GSTIN must have 15 characters."))
doc.gst_state = doc.state
if doc.gst_state: p = re.compile("^[0-9]{2}[A-Z]{4}[0-9A-Z]{1}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}[1-9A-Z]{1}[0-9A-Z]{1}$")
doc.gst_state_number = state_numbers[doc.gst_state] if not p.match(doc.gstin):
if doc.gstin and doc.gstin != "NA" and doc.gst_state_number != doc.gstin[:2]: frappe.throw(_("Invalid GSTIN! The input you've entered doesn't match the format of GSTIN."))
frappe.throw(_("First 2 digits of GSTIN should match with State number {0}")
.format(doc.gst_state_number))
def get_gstin_with_check_digit(gstin_without_check_digit): validate_gstin_check_digit(doc.gstin)
''' Function to get the check digit for the gstin.
param: gstin_without_check_digit if not doc.gst_state and doc.state:
return: GSTIN with check digit state = doc.state.lower()
''' states_lowercase = {s.lower():s for s in states}
if state in states_lowercase:
doc.gst_state = states_lowercase[state]
else:
return
doc.gst_state_number = state_numbers[doc.gst_state]
if doc.gst_state_number != doc.gstin[:2]:
frappe.throw(_("Invalid GSTIN! First 2 digits of GSTIN should match with State number {0}.")
.format(doc.gst_state_number))
def validate_gstin_check_digit(gstin):
''' Function to validate the check digit of the GSTIN.'''
factor = 1 factor = 1
total = 0 total = 0
code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' code_point_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
input_chars = gstin_without_check_digit.strip()
if not input_chars:
frappe.throw(_("GSTIN supplied for checkdigit calculation is blank"))
mod = len(code_point_chars) mod = len(code_point_chars)
input_chars = gstin[:-1]
for char in input_chars: for char in input_chars:
digit = factor * code_point_chars.find(char) digit = factor * code_point_chars.find(char)
if digit < 0: digit = (digit // mod) + (digit % mod)
frappe.throw(_("GSTIN supplied for checkdigit contains invalid character"))
digit = (digit / mod) + (digit % mod)
total += digit total += digit
factor = 2 if factor == 1 else 1 factor = 2 if factor == 1 else 1
return ''.join([gstin_without_check_digit,code_point_chars[((mod - (total % mod)) % mod)]]) if gstin[-1] != code_point_chars[((mod - (total % mod)) % mod)]:
frappe.throw(_("Invalid GSTIN! The check digit validation has failed. " +
"Please ensure you've typed the GSTIN correctly."))
def get_itemised_tax_breakup_header(item_doctype, tax_accounts): def get_itemised_tax_breakup_header(item_doctype, tax_accounts):
if frappe.get_meta(item_doctype).has_field('gst_hsn_code'): if frappe.get_meta(item_doctype).has_field('gst_hsn_code'):