Skip to content

Commit

Permalink
add check method for ean8, ean13 and upc
Browse files Browse the repository at this point in the history
  • Loading branch information
chafique-delli committed Jan 30, 2015
1 parent 762c40c commit 076022b
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions product_gtin/product_gtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,53 @@ def check_ean(eancode):
int(eancode)
except:
return False
check = True
if len(eancode) == 8:
check = check_ean8(eancode)
if len(eancode) == 11:
check = check_ean11(eancode)
if len(eancode) == 12:
check = check_upc(eancode)
if len(eancode) == 13:
check = check_ean13(eancode)
if len(eancode) == 14:
check = check_gtin14(eancode)
return check


def check_ean8(eancode):
sum = 0
ean_len = int(len(eancode))
for i in range(ean_len-1):
if is_pair(i):
sum += 3 * int(eancode[i])
else:
sum += int(eancode[i])
check = 10 - operator.mod(sum, 10)
if check == 10:
check = 0
if check != int(eancode[-1]):
return False
return True


def check_upc(eancode):
sum_pair = 0
ean_len = int(len(eancode))
for i in range(ean_len-1):
if is_pair(i):
sum_pair += int(eancode[i])
sum = sum_pair * 3
for i in range(ean_len-1):
if not is_pair(i):
sum += int(eancode[i])
check = ((sum/10 + 1) * 10) - sum
if check != int(eancode[-1]):
return False
return True


def check_ean13(eancode):
sum = 0
ean_len = int(len(eancode))
for i in range(ean_len-1):
Expand All @@ -53,6 +100,14 @@ def check_ean(eancode):
return True


def check_ean11(eancode):
pass


def check_gtin14(eancode):
pass


class product_product(orm.Model):
_inherit = "product.product"

Expand All @@ -64,12 +119,12 @@ def _check_ean_key(self, cr, uid, ids):

_columns = {
'ean13': fields.char(
'EAN', size=14,
'EAN/GTIN', size=14,
help="Code for EAN8 EAN13 UPC JPC GTIN "
"http://en.wikipedia.org/wiki/Global_Trade_Item_Number"),
}

_constraints = [(_check_ean_key, 'Error: Invalid EAN code', ['ean13'])]
_constraints = [(_check_ean_key, 'Error: Invalid EAN/GTIN code', ['ean13'])]


class product_packaging(orm.Model):
Expand Down

0 comments on commit 076022b

Please sign in to comment.