[MERGE] lp:~openerp-dev/openobject-addons/trunk-review_module_desc-psi
[odoo/odoo.git] / addons / point_of_sale / res_users.py
1 #!/usr/bin/env python
2 from osv import osv, fields
3 import math
4
5 def is_pair(x):
6     return not x%2
7 # This code is a duplicate of product#check_ean function
8 def check_ean(eancode):
9     if not eancode:
10         return True
11     if len(eancode) <> 13:
12         return False
13     try:
14         int(eancode)
15     except:
16         return False
17     oddsum=0
18     evensum=0
19     total=0
20     eanvalue=eancode
21     reversevalue = eanvalue[::-1]
22     finalean=reversevalue[1:]
23
24     for i in range(len(finalean)):
25         if is_pair(i):
26             oddsum += int(finalean[i])
27         else:
28             evensum += int(finalean[i])
29     total=(oddsum * 3) + evensum
30
31     check = int(10 - math.ceil(total % 10.0)) %10
32
33     if check != int(eancode[-1]):
34         return False
35     return True
36
37 class res_users(osv.osv):
38     _inherit = 'res.users'
39     _columns = {
40         'ean13' : fields.char('EAN13', size=13, help="BarCode"),
41         'pos_config' : fields.many2one('pos.config', 'Default Point of Sale', domain=[('state', '=', 'active')]),
42     }
43
44     def _check_ean(self, cr, uid, ids, context=None):
45         return all(
46             check_ean(user.ean13) == True
47             for user in self.browse(cr, uid, ids, context=context)
48         )
49
50     _constraints = [
51         (_check_ean, "Error: Invalid ean code", ['ean13'],),
52     ]
53