product_template.py 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
# -*- encoding: utf-8 -*-
##############################################################################
#
#    Product - Average Consumption Module for Odoo
#    Copyright (C) 2021-Today GRAP (http://www.grap.coop)
#    @author Damien Moulard
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api
from openerp.exceptions import ValidationError
import math

class ProductTemplate(models.Model):
    _inherit = "product.template"

30 31
    minimal_stock = fields.Integer(string="Minimal stock")

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    @api.constrains('barcode')
    def _check_barcode(self):
        """ Vérification du code barre avant de sauvegarder une fiche article"""
        for record in self:
            barcode_str = '' if not record.barcode else str(record.barcode)

            if len(barcode_str) == 0 :
              return None
            elif not barcode_str.isdigit():
                raise ValidationError("Le code-barre n'est pas au bon format : "
                        + "il doit être composé de chiffres uniquement.")
            elif ( len(barcode_str) > 0
                    and len(barcode_str) != 8
                    and len(barcode_str) != 13 ):
                raise ValidationError("Le code-barre n'est pas au bon format : "
                    + "il faut 8 ou 13 chiffres. "
                    + "Vous pouvez aussi laisser le champ vide.")
            else:
                # Vérification de la validité codebarre: calcul du dernier digit

                # Sur un code-barre composé des chiffres (sans le dernier digit):
                # N1 N2 ... Nn-1 Nn
                # On calcule la somme :
                # Nn * 3 + Nn-1 * 1 + Nn-2 * 3 + Nn-3 * 1 ...
                # On soustrait le résultat à l'arrondi à la dizaine supérieur
                # Le résultat devrait être le dernier digit

                checksum = 0
                for i in range(len(barcode_str)-2, -1, -1):
                    d = 3 if (len(barcode_str)-1 - i+1) % 2 == 0 else 1
                    checksum += int(barcode_str[i]) * d

                roundup = int(math.ceil(checksum / 10.0) * 10)
                check_digit = roundup - checksum

                if str(check_digit) != barcode_str[len(barcode_str)-1]:
                    raise ValidationError("Le code-barre n'est pas valide. "
                        + "(la validation par la clé de vérification a échoué)")