parent
86a8376291
commit
1442b4e3b8
48
README.md
48
README.md
|
@ -1,2 +1,46 @@
|
|||
# pharma_datamatrix
|
||||
The Python library parse the EU medicine pack (Falsified Medicine Directive) GS1 datamatrix barcode string into GTIN, BATCH, EXPIRY, SERIAL & NHRN.
|
||||
# pharma_datamatrix package
|
||||
The package allow users to pass the string produced by the 2D barcode scanners and parse the string into GTIN, EXPIRY, SERIAL, BATCH and NHRN.
|
||||
Optionally, user can pass validation=True, to validate the GTIN and Expiry date.
|
||||
|
||||
# Files
|
||||
pharma_datamatrix.py: Primary file with the parsing logic. Is the string is valid, it will return a dict of the values. Otherwise return invalid string.
|
||||
|
||||
expiry_date_validation.py: This files contains the logic that validates the Expiry date. This file checks if the YYMMDD contains valid digits. Ex: MM to be between 01 & 12. And if the expiry date greater than the date of the scan: i.e. not is valid past date.
|
||||
|
||||
gs1_gtin_validation.py: This file contains the logic that validates the GS1 - GTIN(14).
|
||||
|
||||
# Installation
|
||||
To install the package please run the below command:
|
||||
pip install pharma_datamatrix
|
||||
|
||||
# Usage
|
||||
The function pharma_datamatrix(barcode: str, Validation: bool = False) takes two parameters:
|
||||
The first parameter 'barcode' is of 'string' type and its mandatory. The string should contain the <GS> seperator as per GS1 guidelines.
|
||||
The second parameter 'validation' is of bool type and default to 'False'. Users can pass 'True' for the package to perform validation on GTIN and Expiry Date.
|
||||
|
||||
Initial Import:
|
||||
from pharma_datamatrix import pharma_datamatrix
|
||||
|
||||
Example 1: Usage without validation (Expiry date is invalid but no validation is performed)
|
||||
pharma_datamatrix('01085860077038511724113110HB5R121587E4QA11R')
|
||||
Output: {'NHRN': None, 'GTIN': '08586007703851', 'EXPIRY': '241131', 'BATCH': 'HB5R1', 'SERIAL': '21587E4QA11R'}
|
||||
|
||||
Example 2: Usage without validation (Invalid barcode)
|
||||
pharma_datamatrix('085860077038511724113110HB5R21587E4QA11R')
|
||||
Output: INVALID BARCODE
|
||||
|
||||
Example 3: Usage with validation (valid barcode)
|
||||
pharma_datamatrix('01085860077038511724123110HB5R21587E4QA11R', True)
|
||||
Output: {'NHRN': None, 'GTIN': '08586007703851', 'EXPIRY': '241231', 'BATCH': 'HB5R1', 'SERIAL': '21587E4QA11R'}
|
||||
|
||||
Example 4: Usage with validation (valid barcode format but Invalid expiry date (31 Nov 24))
|
||||
pharma_datamatrix('01085860077038511724113110HB5R21587E4QA11R', True)
|
||||
Output: INVALID EXPIRY DATE
|
||||
|
||||
Example 5: Usage with validation (valid barcode format but Invalid GTIN)
|
||||
pharma_datamatrix('01083860077038511724113110HB5R21587E4QA11R', True)
|
||||
Output: INVALID GTIN
|
||||
|
||||
# Source
|
||||
Github link: https://github.com/PinchofLogic/pharma_datamatrix
|
||||
|
||||
|
|
|
@ -1,3 +1,12 @@
|
|||
"""
|
||||
The expiry date is in the YYMMDD format in the EU FMD regulated barcodes. The program validates the expiry date.
|
||||
MM to be between valid values of 01 and 12.
|
||||
DD to be between valid values of 00 and 31. Taking account of months with date upto 30 days and Feb with 28 & 29 days.
|
||||
The logic also allows 00 for DD, this refers to the last day of the month.
|
||||
|
||||
"""
|
||||
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
|
@ -5,6 +14,7 @@ def expiry_date_check(e:str):
|
|||
my_year = e[:2]
|
||||
my_month = e[2:4]
|
||||
my_date = e[4:]
|
||||
|
||||
if (int(my_month) not in range(1,13)):
|
||||
return False
|
||||
elif (int(my_date) not in range(32)):
|
||||
|
@ -19,6 +29,17 @@ def expiry_date_check(e:str):
|
|||
my_date = '31'
|
||||
else:
|
||||
my_date = '30'
|
||||
|
||||
if my_month == '02' and int(my_year) % 4 == 0 and my_date <= '29':
|
||||
pass
|
||||
elif my_month == '02' and int(my_year) % 4 != 0 and my_date <= '28':
|
||||
pass
|
||||
elif my_month in ['01', '03', '05', '07', '08', '10', '12'] and my_date <= '31':
|
||||
pass
|
||||
elif my_month in ['04', '06', '09', '11'] and my_date <= '30':
|
||||
pass
|
||||
else:
|
||||
return False
|
||||
|
||||
actual_date = my_year + my_month + my_date
|
||||
if actual_date > datetime.today().strftime('%y%m%d'):
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"""
|
||||
The function perform validation on the GS1-GTIN(14)
|
||||
The function perform validation on the GS1-GTIN(14) based on the check digit detailed in the link below:
|
||||
https://www.gs1.org/services/how-calculate-check-digit-manually
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
@ -1,22 +1,31 @@
|
|||
"""
|
||||
The function parse the GS1 Datamatrix barcode used for medicine packs.
|
||||
The barcodes output is in below format:
|
||||
The barcode scanners in the default setup outputs the scan is in below format:
|
||||
]d201034531200000111719112510ABCD1234<GS>2110EW354EWER
|
||||
(01)03453120000011(17)191125(10)ABCD1234<GS>(21)10EW354EWER
|
||||
(01) = GTIN Identifier - Fixed 14 chars
|
||||
(17) = Expiry Date Identifer - Fixed 6 Chars
|
||||
(10) = Batch Identifier - Variable length. (upto 20 chars usually between 4 - 10 Chars)
|
||||
(21) = Serial Number - Variable length. (upto 20 chars - Usually between 12 - 20 Chars)
|
||||
(710 or 711 or 712 or 713 or 714) = Optional NHRN identifiers.
|
||||
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
|
||||
from expiry_date_validation import expiry_date_check
|
||||
from .gs1_gtin_validation import gtin_check #The GTIN validation module
|
||||
from .expiry_date_validation import expiry_date_check #The Expiry validation module
|
||||
|
||||
def pharma_datamatrix(barcode: str, validation: bool = False):
|
||||
def pharma_datamatrix(barcode: str, validation: bool = False) -> dict:
|
||||
"""
|
||||
The function takes barcode string and return parsed objects in a `dict`. Optionally user can pass bool value to validate the barcode
|
||||
Parameters:
|
||||
barcode: string type. This is the barcode string with the <GS> seperators.
|
||||
validation: bool type. If 'True' the function performs validation on GTIN and Expiry Date.
|
||||
Returns:
|
||||
dict: Returns dictionary object with GTIN, EXPIRY, BATCH, SERIAL & NHRN as keys.
|
||||
"""
|
||||
result = dict()
|
||||
result['NHRN'] = None
|
||||
if barcode[:3] == ']d2':
|
||||
if barcode[:3] == ']d2': #Most barcode scanners prepend ']d2' identifier for the GS1 datamatrix. This senction removes the identifier.
|
||||
barcode = barcode[3:]
|
||||
while barcode:
|
||||
|
||||
|
@ -64,8 +73,9 @@ def pharma_datamatrix(barcode: str, validation: bool = False):
|
|||
barcode = None
|
||||
|
||||
else:
|
||||
return f"Not a valid barcode"
|
||||
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'
|
||||
|
@ -77,15 +87,3 @@ def pharma_datamatrix(barcode: str, validation: bool = False):
|
|||
return result
|
||||
else:
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
result = pharma_datamatrix("01083860077038511724013110HN5R21587E4RT10P")
|
||||
|
||||
print(result)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue