~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/addons/base/res/partner/wizard/wizard_ean_check.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# WARNING: This program as such is intended to be used by professional
 
6
# programmers who take the whole responsability of assessing all potential
 
7
# consequences resulting from its eventual inadequacies and bugs
 
8
# End users who are looking for a ready-to-use solution with commercial
 
9
# garantees and support are strongly adviced to contract a Free Software
 
10
# Service Company
 
11
#
 
12
# This program is Free Software; you can redistribute it and/or
 
13
# modify it under the terms of the GNU General Public License
 
14
# as published by the Free Software Foundation; either version 2
 
15
# of the License, or (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
25
#
 
26
##############################################################################
 
27
 
 
28
import wizard
 
29
import math
 
30
from osv import osv
 
31
from tools.misc import UpdateableStr
 
32
import pooler
 
33
 
 
34
def _is_pair(x):
 
35
        return not x%2
 
36
 
 
37
def _get_ean_key(string):
 
38
        if not string or string=='':
 
39
                return '0'
 
40
        if len(string)!=12:
 
41
                return '0'
 
42
        sum=0
 
43
        for i in range(12):
 
44
                if _is_pair(i):
 
45
                        sum+=int(string[i])
 
46
                else:
 
47
                        sum+=3*int(string[i])
 
48
        return str(int(math.ceil(sum/10.0)*10-sum))
 
49
 
 
50
#FIXME: this is not concurrency safe !!!!
 
51
_check_arch = UpdateableStr()
 
52
_check_fields = {}
 
53
 
 
54
def _check_key(self, cr, uid, data, context):
 
55
        partner_table=pooler.get_pool(cr.dbname).get('res.partner')
 
56
        partners = partner_table.browse(cr, uid, data['ids'])
 
57
        _check_arch_lst=['<?xml version="1.0"?>', '<form string="Check EAN13">', '<label string=""/>', '<label string=""/>','<label string="Original" />', '<label string="Computed" />']
 
58
        for partner in partners:
 
59
                if partner['ean13'] and len(partner['ean13'])>11 and len(partner['ean13'])<14:
 
60
                        _check_arch_lst.append('<label colspan="2" string="%s" />' % partner['ean13']);
 
61
                        key=_get_ean_key(partner['ean13'][:12])
 
62
                        _check_arch_lst.append('<label string=""/>')
 
63
                        if len(partner['ean13'])==12:
 
64
                                _check_arch_lst.append('<label string="" />');
 
65
                        else:
 
66
                                _check_arch_lst.append('<label string="%s" />' % partner['ean13'][12])
 
67
                        _check_arch_lst.append('<label string="%s" />' % key)
 
68
        _check_arch_lst.append('</form>')
 
69
        _check_arch.string = '\n'.join(_check_arch_lst)
 
70
        return {}
 
71
 
 
72
def _update_ean(self, cr, uid, data, context):
 
73
        partner_table = pooler.get_pool(cr.dbname).get('res.partner')
 
74
        partners = partner_table.browse(cr, uid, data['ids'])
 
75
        for partner in partners:
 
76
                partner_table.write(cr, uid, data['ids'], {
 
77
                        'ean13': "%s%s" % (partner['ean13'][:12], _get_ean_key(partner['ean13'][:12]))
 
78
                })
 
79
        return {}
 
80
 
 
81
class wiz_ean_check(wizard.interface):
 
82
        states = {
 
83
                'init': {
 
84
                        'actions': [_check_key],
 
85
                        'result': {
 
86
                                'type': 'form',
 
87
                                'arch': _check_arch,
 
88
                                'fields': _check_fields,
 
89
                                'state': (('end', 'Ignore'), ('correct', 'Correct EAN13'))
 
90
                        }
 
91
                },
 
92
                'correct' : {
 
93
                        'actions': [_update_ean],
 
94
                        'result': {
 
95
                                'type': 'state',
 
96
                                'state': 'end'
 
97
                        }
 
98
                }
 
99
        }
 
100
 
 
101
wiz_ean_check('res.partner.ean13')