fulfilio / pyepc

Python GS1 EPC toolkit
MIT License
16 stars 3 forks source link

[Bug] wrong check digit sscc #14

Closed LucasCallista closed 1 year ago

LucasCallista commented 1 year ago

Your environment

Description

I was looking at this package to generate sscc barcodes but noticed that when generating multiple digits some of them where to long. This always happens when the check digit is 0 and i believe it might be setting the check digit to 10 thus making the total barcode 19 long instead of 18. example:

from pyepc import SSCC

company_prefix = '5430000001'
extension_digit = '1'
serial_ref = '123459'
sscc = SSCC(company_prefix, extension_digit, serial_ref)
print(len(sscc.sscc))
print(sscc.sscc)

output:

19 1543000000112345910

LucasCallista commented 1 year ago

I am looking further into the code on how it calculates the check_digit and nothing seems wrong: when i execute the following the utils method returns 10 but the copied local utils method that is an exact copy in this script returns 0 as intended.

from pyepc import utils
number = "15430000001123459"
print(utils.calculate_check_digit(number))
def local_check_digit(number):
    """
    Given a number without the check-digit, calculate
    the check digit and return it.

    See: https://www.gs1.org/services/how-calculate-check-digit-manually
    """
    # Step 2: Multiply alternate position by 3 and 1
    # and get the sum
    step_2 = sum([int(n) * 3 for n in number[::-1][::2]]) + sum(
        [int(n) for n in number[::-1][1::2]]
    )
    if step_2 % 10 == 0:
        return "0"

    # Subtract the sum from nearest equal or higher multiple of ten
    return str(10 - (step_2 % 10))

print(local_check_digit(number))

10 0

LucasCallista commented 1 year ago

Ah I see pip installs 0.0.3 and not 0.5.0 because of the python version.

LucasCallista commented 1 year ago

Installed on python 3.10.9 and did not encounter any issues