diff --git a/product_usability/__manifest__.py b/product_usability/__manifest__.py index c7c1194..1444537 100644 --- a/product_usability/__manifest__.py +++ b/product_usability/__manifest__.py @@ -21,11 +21,14 @@ The usability enhancements include: * hide description field on product (description_sale must be use instead of description) +* add a field barcode_type in product form view + This module has been written by Alexis de Lattre from Akretion . """, 'author': 'Akretion', 'website': 'http://www.akretion.com', 'depends': ['product'], + "external_dependencies": {"python": ["stdnum"]}, 'data': [ 'views/product_supplierinfo_view.xml', 'views/product_pricelist_view.xml', diff --git a/product_usability/models/product_product.py b/product_usability/models/product_product.py index c0f219b..4fc551c 100644 --- a/product_usability/models/product_product.py +++ b/product_usability/models/product_product.py @@ -4,6 +4,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, models, fields +from stdnum.ean import is_valid class ProductProduct(models.Model): @@ -13,9 +14,7 @@ class ProductProduct(models.Model): barcode = fields.Char(tracking=20) weight = fields.Float(tracking=30) active = fields.Boolean(tracking=40) - barcode_code128 = fields.Char( - compute='_compute_barcode_code128', - help="Barcode in Code128-B with start char, checksum and stop char") + barcode_type = fields.Char(compute='_compute_barcode_type') _sql_constraints = [( # Maybe it could be better to have a constrain per company @@ -28,30 +27,17 @@ class ProductProduct(models.Model): 'This internal reference already exists!')] @api.model - def _compute_code128_checksum(self, code): - # This is NOT a full implementation of code128 checksum - csum = 104 # Start B - i = 0 - for char in code: - i += 1 - char_val = ord(char) - 32 - csum += char_val * i - remainder = csum % 103 - checksum = chr(remainder + 32) - return checksum + def _get_barcode_type(self, barcode): + barcode_type = False + if barcode: + size = len(barcode) + if size == 13 and is_valid(barcode): + barcode_type = 'EAN13' + elif size == 8 and is_valid(barcode): + barcode_type = 'EAN8' + return barcode_type @api.depends('barcode') - def _compute_barcode_code128(self): - # We use Code128-B. Useful info on code128: - # https://boowiki.info/art/codes-a-barres/code-128.html - # Use code128.ttf and copy it in /usr/local/share/fonts/ - startb = chr(209) - stop = chr(211) + def _compute_barcode_type(self): for product in self: - code128 = False - barcode = product.barcode - if barcode and all([32 <= ord(x) <= 127 for x in barcode]): - checksum = self._compute_code128_checksum(barcode) - if checksum: - code128 = startb + barcode + checksum + stop - product.barcode_code128 = code128 + product.barcode_type = self._get_barcode_type(product.barcode) diff --git a/product_usability/models/product_template.py b/product_usability/models/product_template.py index 91c8460..ca8238e 100644 --- a/product_usability/models/product_template.py +++ b/product_usability/models/product_template.py @@ -3,7 +3,7 @@ # @author Raphaƫl Valyi # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import models, fields +from odoo import api, models, fields class ProductTemplate(models.Model): @@ -31,3 +31,14 @@ class ProductTemplate(models.Model): purchase_ok = fields.Boolean(tracking=90) active = fields.Boolean(tracking=100) company_id = fields.Many2one(tracking=110) + barcode_type = fields.Char(compute='_compute_template_barcode_type') + + @api.depends('product_variant_ids.barcode') + def _compute_template_barcode_type(self): + ppo = self.env['product.product'] + for template in self: + barcode_type = False + if len(template.product_variant_ids) == 1: + barcode = template.product_variant_ids.barcode + barcode_type = ppo._get_barcode_type(barcode) + template.barcode_type = barcode_type diff --git a/product_usability/views/product_product.xml b/product_usability/views/product_product.xml index dfbafbc..f099b17 100644 --- a/product_usability/views/product_product.xml +++ b/product_usability/views/product_product.xml @@ -21,4 +21,16 @@ + + usability.product.product.form + product.product + + + + + + + + + diff --git a/product_usability/views/product_template_view.xml b/product_usability/views/product_template_view.xml index 4e29fa9..d473bf9 100644 --- a/product_usability/views/product_template_view.xml +++ b/product_usability/views/product_template_view.xml @@ -19,4 +19,17 @@ + + + usability.product.template.ONLY.form + product.template + + + + + + + + +