parent
1442b4e3b8
commit
fe858f0f4d
|
@ -0,0 +1,75 @@
|
|||
from gs1_gtin_validation import gtin_check #The GTIN validation module
|
||||
from expiry_date_validation import expiry_date_check #The Expiry validation module
|
||||
|
||||
result = {'SCHEME': 'GTIN'}
|
||||
|
||||
|
||||
def gs1_gtin(barcode: str) -> dict:
|
||||
|
||||
if chr(29) in barcode:
|
||||
|
||||
while barcode:
|
||||
|
||||
if barcode[:2] == '01':
|
||||
result['GTIN'] = barcode[2:16]
|
||||
if len(barcode) > 16:
|
||||
barcode = barcode[16:]
|
||||
else:
|
||||
barcode = None
|
||||
|
||||
elif barcode[:2] == '17':
|
||||
result['EXPIRY'] = barcode[2:8]
|
||||
barcode = barcode[8:]
|
||||
|
||||
elif barcode[:2] == '10':
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['BATCH'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['BATCH'] = barcode[2:]
|
||||
barcode = None
|
||||
|
||||
elif barcode[:2] == '21':
|
||||
if chr(29) in barcode:
|
||||
#print("In the serial GS check")
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['SERIAL'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['SERIAL'] = barcode[2:]
|
||||
barcode = None
|
||||
|
||||
|
||||
elif barcode[:3] in ['710', '711', '712', '713', '714']:
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['NHRN'] = barcode[3:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['NHRN'] = barcode[3:]
|
||||
barcode = None
|
||||
|
||||
else:
|
||||
return f"INVALID BARCODE"
|
||||
|
||||
else:
|
||||
return f"INVALID BARCODE"
|
||||
|
||||
if 'GTIN' and 'BATCH' and 'EXPIRY' and 'SERIAL' in result.keys():
|
||||
if gtin_check(result['GTIN']) == False and expiry_date_check(result['EXPIRY']) == False:
|
||||
return f'INVALID GTIN & EXPIRY DATE'
|
||||
elif expiry_date_check(result['EXPIRY']) == False:
|
||||
return f'INVALID EXPIRY DATE'
|
||||
elif gtin_check(result['GTIN']) == False:
|
||||
return f'INVALID GTIN'
|
||||
else:
|
||||
return result
|
||||
|
||||
|
|
@ -8,5 +8,6 @@ def gtin_check(g: str):
|
|||
digit_sum = (int(g[0]) * 3) + int(g[1]) + (int(g[2]) * 3) + int(g[3]) + (int(g[4]) * 3) + int(g[5]) + (int(g[6]) * 3) + int(g[7]) + (int(g[8]) * 3) + int(g[9]) + (int(g[10]) * 3) + int(g[11]) + (int(g[12]) * 3)
|
||||
nearest_ten = round(digit_sum/10) * 10
|
||||
check_sum_digit = nearest_ten - digit_sum
|
||||
if check_sum_digit < 0:
|
||||
check_sum_digit = 10 + check_sum_digit
|
||||
return check_sum_digit == int(g[-1])
|
||||
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
from expiry_date_validation import expiry_date_check #The Expiry validation module
|
||||
|
||||
result = {'SCHEME': 'PPN'}
|
||||
|
||||
def ifa_ppn(barcode: str) -> dict:
|
||||
|
||||
while barcode:
|
||||
|
||||
if barcode[:2] == '9N':
|
||||
result['PPN'] = barcode[2:14]
|
||||
if len(barcode) > 14:
|
||||
barcode = barcode[15:]
|
||||
else:
|
||||
barcode = None
|
||||
|
||||
elif barcode[:1] == 'D':
|
||||
result['EXPIRY'] = barcode[1:7]
|
||||
if len(barcode) > 7:
|
||||
barcode = barcode[8:]
|
||||
else:
|
||||
barcode = None
|
||||
|
||||
elif barcode[:2] == '1T':
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['BATCH'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 30:
|
||||
result['BATCH'] = barcode[2:i]
|
||||
barcode = None
|
||||
|
||||
elif barcode[:1] == 'S':
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['SERIAL'] = barcode[1:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 30:
|
||||
result['SERIAL'] = barcode[1:i]
|
||||
barcode = None
|
||||
|
||||
elif barcode[:2] == '8P':
|
||||
result['GTIN'] = barcode[2:16]
|
||||
if len(barcode) > 16:
|
||||
barcode = barcode[17:]
|
||||
else:
|
||||
barcode = None
|
||||
|
||||
else:
|
||||
return f"INVALID BARCODE"
|
||||
|
||||
if 'PPN' and 'BATCH' and 'EXPIRY' and 'SERIAL' in result.keys():
|
||||
if expiry_date_check(result['EXPIRY']) == False:
|
||||
return f'INVALID EXPIRY DATE'
|
||||
else:
|
||||
return result
|
||||
else:
|
||||
return f'INVALID BARCODE'
|
||||
|
||||
|
|
@ -11,8 +11,9 @@ The barcode scanners in the default setup outputs the scan is in below format:
|
|||
The symbology identifier ]d2 and for the second FNC1, when used as a separator character is <GS> Group-Separator.
|
||||
|
||||
"""
|
||||
from .gs1_gtin_validation import gtin_check #The GTIN validation module
|
||||
from .expiry_date_validation import expiry_date_check #The Expiry validation module
|
||||
|
||||
from gs1_gtin import gs1_gtin
|
||||
from ifa_ppn import ifa_ppn
|
||||
|
||||
def pharma_datamatrix(barcode: str, validation: bool = False) -> dict:
|
||||
"""
|
||||
|
@ -23,67 +24,19 @@ def pharma_datamatrix(barcode: str, validation: bool = False) -> dict:
|
|||
Returns:
|
||||
dict: Returns dictionary object with GTIN, EXPIRY, BATCH, SERIAL & NHRN as keys.
|
||||
"""
|
||||
result = dict()
|
||||
result['NHRN'] = None
|
||||
if barcode[:3] == ']d2': #Most barcode scanners prepend ']d2' identifier for the GS1 datamatrix. This senction removes the identifier.
|
||||
barcode = barcode[3:]
|
||||
while barcode:
|
||||
barcode = barcode[3:]
|
||||
result = gs1_gtin(barcode)
|
||||
|
||||
elif barcode[:2] in ['01', '21', '17', '10', '71']:
|
||||
result = gs1_gtin(barcode)
|
||||
|
||||
if barcode[:2] == '01':
|
||||
result['GTIN'] = barcode[2:16]
|
||||
barcode = barcode[16:]
|
||||
|
||||
elif barcode[:2] == '17':
|
||||
result['EXPIRY'] = barcode[2:8]
|
||||
barcode = barcode[8:]
|
||||
|
||||
elif barcode[:2] == '10':
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['BATCH'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['BATCH'] = barcode[2:]
|
||||
barcode = None
|
||||
|
||||
elif barcode[:2] == '21':
|
||||
if chr(29) in barcode:
|
||||
#print("In the serial GS check")
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['SERIAL'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['SERIAL'] = barcode[2:]
|
||||
barcode = None
|
||||
|
||||
|
||||
elif barcode[:3] in ['710', '711', '712', '713', '714']:
|
||||
if chr(29) in barcode:
|
||||
for i, c in enumerate(barcode):
|
||||
if ord(c) == 29:
|
||||
result['NHRN'] = barcode[2:i]
|
||||
barcode = barcode[i+1:]
|
||||
break
|
||||
else:
|
||||
result['NHRN'] = barcode[2:]
|
||||
barcode = None
|
||||
|
||||
else:
|
||||
return f"INVALID BARCODE"
|
||||
|
||||
# If the validtion is set to "True", below section is processed. Perform validation checks on GTIN and Expriry Date
|
||||
if validation:
|
||||
if gtin_check(result['GTIN']) == False and expiry_date_check(result['EXPIRY']) == False:
|
||||
return f'INVALID GTIN & EXPIRY DATE'
|
||||
elif expiry_date_check(result['EXPIRY']) == False:
|
||||
return f'INVALID EXPIRY DATE'
|
||||
elif gtin_check(result['GTIN']) == False:
|
||||
return f'INVALID GTIN'
|
||||
else:
|
||||
return result
|
||||
elif barcode[:6] == '[)>'+chr(30)+'06':
|
||||
barcode = barcode[7:] # Removes the leading ASCII seperator after the scheme identifier.
|
||||
result = ifa_ppn(barcode)
|
||||
|
||||
else:
|
||||
return result
|
||||
result = f"01 INVALID BARCODE"
|
||||
|
||||
return result
|
||||
|
||||
|
|
Loading…
Reference in New Issue