From bf9809d7b1b3d78dcd1c9b35d830a26e881d1d03 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Thu, 4 Aug 2016 18:07:03 +0200 Subject: [PATCH] [8.0] Add a GLN field to manage GS1 Global Location Number on partners --- base_partner_gln/README.rst | 66 ++++++++++++++++ base_partner_gln/__init__.py | 5 ++ base_partner_gln/__openerp__.py | 24 ++++++ base_partner_gln/models/__init__.py | 1 + base_partner_gln/models/partner.py | 44 +++++++++++ base_partner_gln/static/description/icon.svg | 79 ++++++++++++++++++++ base_partner_gln/tests/__init__.py | 5 ++ base_partner_gln/tests/test_gln.py | 35 +++++++++ base_partner_gln/views/partner_view.xml | 15 ++++ 9 files changed, 274 insertions(+) create mode 100644 base_partner_gln/README.rst create mode 100644 base_partner_gln/__init__.py create mode 100644 base_partner_gln/__openerp__.py create mode 100644 base_partner_gln/models/__init__.py create mode 100644 base_partner_gln/models/partner.py create mode 100644 base_partner_gln/static/description/icon.svg create mode 100644 base_partner_gln/tests/__init__.py create mode 100644 base_partner_gln/tests/test_gln.py create mode 100644 base_partner_gln/views/partner_view.xml diff --git a/base_partner_gln/README.rst b/base_partner_gln/README.rst new file mode 100644 index 000000000..549ac2fcf --- /dev/null +++ b/base_partner_gln/README.rst @@ -0,0 +1,66 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================ +Base Partner Gln +================ + +Adds a field GLN on partners (which are companies) to manage GLN (EAN) code. + +see: http://www.gs1.org/1/glnrules/en/ + +Installation +============ + +This module depends on 'stdnum' python module + +Usage +===== + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/{repo_id}/{branch} + +.. repo_id is available in https://github.com/OCA/maintainer-tools/blob/master/tools/repos_with_ids.txt +.. branch is "8.0" for example + +Known issues / Roadmap +====================== + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Denis Roussel + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/base_partner_gln/__init__.py b/base_partner_gln/__init__.py new file mode 100644 index 000000000..c1f7b81c0 --- /dev/null +++ b/base_partner_gln/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Acsone S.A. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/base_partner_gln/__openerp__.py b/base_partner_gln/__openerp__.py new file mode 100644 index 000000000..7a64f591b --- /dev/null +++ b/base_partner_gln/__openerp__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Acsone S.A. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Base Partner Gln', + 'summary': """ + Adds a field GLN on partners to manage GLN (EAN) code.""", + 'version': '8.0.1.0.0', + 'license': 'AGPL-3', + 'author': 'Acsone S.A.,Odoo Community Association (OCA)', + 'contributors': [ + 'Denis Roussel ', + ], + 'website': 'https://www.acsone.eu', + 'depends': ['base' + ], + 'python': ['stdnum' + ], + 'data': ['views/partner_view.xml' + ], + 'demo': [ + ], +} diff --git a/base_partner_gln/models/__init__.py b/base_partner_gln/models/__init__.py new file mode 100644 index 000000000..4da81fa31 --- /dev/null +++ b/base_partner_gln/models/__init__.py @@ -0,0 +1 @@ +from . import partner diff --git a/base_partner_gln/models/partner.py b/base_partner_gln/models/partner.py new file mode 100644 index 000000000..a0d0655dd --- /dev/null +++ b/base_partner_gln/models/partner.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Acsone S.A. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from stdnum import ean +from stdnum.exceptions import InvalidChecksum +from openerp import api, fields, models, _ +from openerp.exceptions import ValidationError + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + gln = fields.Char(size=13, string='GLN', + help='This is the company GLN identification number.') + + @api.multi + @api.constrains('gln') + def _check_gln(self): + + for partner in self: + if partner.gln: + try: + ean.validate(partner.gln) + except InvalidChecksum, e: + raise ValidationError(_('The GLN field for the partner' + ' %s is not valid! %s') + % (partner.name, e.message)) + + @api.multi + @api.constrains('gln') + def _check_unique_gln(self): + + for partner in self: + if partner.gln: + duplicate_partners = self.search([('gln', '!=', False), + ('gln', '=', partner.gln), + ('id', '!=', partner.id)]) + + if duplicate_partners: + raise ValidationError(_('GLN code is already used by ' + 'existing partners : %s') + % ','.join([p.name for p in + duplicate_partners])) diff --git a/base_partner_gln/static/description/icon.svg b/base_partner_gln/static/description/icon.svg new file mode 100644 index 000000000..a7a26d093 --- /dev/null +++ b/base_partner_gln/static/description/icon.svg @@ -0,0 +1,79 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/base_partner_gln/tests/__init__.py b/base_partner_gln/tests/__init__.py new file mode 100644 index 000000000..a064df9b0 --- /dev/null +++ b/base_partner_gln/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Acsone S.A. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_gln diff --git a/base_partner_gln/tests/test_gln.py b/base_partner_gln/tests/test_gln.py new file mode 100644 index 000000000..10578e3ab --- /dev/null +++ b/base_partner_gln/tests/test_gln.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Acsone S.A. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.exceptions import ValidationError +from openerp.tests.common import TransactionCase + + +class TestGLN(TransactionCase): + def setUp(self): + super(TestGLN, self).setUp() + self.partner = self.env['res.partner'].create({'name': 'TestGLN'}) + self.partner2 = self.env['res.partner'].create({'name': 'TestGLN2'}) + + def test_gln(self): + # Good GLN + self.partner.write({'gln': '5450534001717'}) + self.assertEqual(self.partner.gln, '5450534001717') + + # Duplicate GLN + try: + self.partner2.write({'gln': '5450534001717'}) + except ValidationError, e: + self.assertEqual( + e.value, + "GLN code is already used by existing partners : TestGLN") + + # Bad GLN + try: + self.partner.gln = '5450534001716' + except ValidationError, e: + self.assertEqual( + e.value, + "The GLN field for the partner TestGLN is not valid! " + "The number's checksum or check digit is invalid.") diff --git a/base_partner_gln/views/partner_view.xml b/base_partner_gln/views/partner_view.xml new file mode 100644 index 000000000..6fa73e8a7 --- /dev/null +++ b/base_partner_gln/views/partner_view.xml @@ -0,0 +1,15 @@ + + + + + res.partner.gln.form + res.partner + + + + + + + + + \ No newline at end of file