fulfilio / pyepc

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

Encoding does not work with some EANs #8

Closed ionescu77 closed 2 years ago

ionescu77 commented 2 years ago

Description

Hi, I am trying to encode epc's, however with some EANs it does not work.

What I Did

def generate_epc_code(ean, serial_number): new_ean = '0'+ean my_tag = SGTIN.from_sgtin(new_ean, serial_number=serial_number) encoded = my_tag.encode() return encoded


- I am adding a `0` otherwise I get this error:

Error creating EPCs with product 2100000109869. GTIN must be 14 digits


- with ean `6422705714596` the code works
- with ean `2100000109869` I get this error

Error creating EPCs with product 2100000109869. GS1 EPC Tag Data Standard only support prefixes between 6 and 12 digits. Got 0 instead



Thanks for writing this library.

Any hint would be very much appreciated.

Raz
sharoonthomas commented 2 years ago

Can you try explicitly setting the company prefix length?

my_tag = SGTIN.from_sgtin(new_ean, serial_number=serial_number, company_prefix_len=6)

When the length is not specified, the library attempts to guess [1] the company prefix part of the UPC/EAN. This is done using the lookup table from GS1 [2] and is not the most reliable method.

[1] https://github.com/fulfilio/pyepc/blob/4b41debef3a4434117a18b9189af45e5b4f40163/pyepc/epc.py#L576-L579 [2] https://github.com/fulfilio/pyepc/blob/4b41debef3a4434117a18b9189af45e5b4f40163/pyepc/utils.py#L163-L194

ionescu77 commented 2 years ago

@sharoonthomas thank you for the hint.

Indeed using a company_prefix_len it works. Somehow the company was using some code in the past and now they were using null which was messing things up!

Possibly the look-up was for 000000 and was returning null also (with the "wrong" EANs like 2100000109869).

from pyepc import SGTIN

def generate_epc_code(ean, serial_number):
    new_ean = '0'+ean
    company_prefix_len = len('1111111')
    my_tag = SGTIN.from_sgtin(new_ean, serial_number, company_prefix_len)
    print(my_tag)
    encoded = my_tag.encode()
    print(encoded)

Have a great day!