Ported most of the old im.js to im_common.js
[odoo/odoo.git] / addons / l10n_ro / res_partner.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2012 (<http://www.erpsystems.ro>). All Rights Reserved
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from openerp.osv import fields, osv
23
24 class res_partner(osv.osv):
25     _name = "res.partner"
26     _inherit = "res.partner"
27     _columns = {
28         'nrc' : fields.char('NRC', size=16, help='Registration number at the Registry of Commerce'),
29     }
30
31     # The SQL constraints are no-ops but present only to display the right error message to the
32     # user when the partial unique indexes defined below raise errors/  
33     # The real constraints need to be implemented with PARTIAL UNIQUE INDEXES (see auto_init),
34     # due to the way accounting data is delegated by contacts to their companies in OpenERP 7.0.
35     _sql_constraints = [
36        ('vat_uniq', 'unique (id)', 'The vat of the partner must be unique !'),
37        ('nrc_uniq', 'unique (id)', 'The code of the partner must be unique !')
38     ]
39
40     def _auto_init(self, cr, context=None):
41         result = super(res_partner, self)._auto_init(cr, context=context)
42         # Real implementation of the vat/nrc constraints: only "commercial entities" need to have
43         # unique numbers, and the condition for being a commercial entity is "is_company or parent_id IS NULL".
44         # Contacts inside a company automatically have a copy of the company's commercial fields
45         # (see _commercial_fields()), so they are automatically consistent.
46         cr.execute("""
47             DROP INDEX IF EXISTS res_partner_vat_uniq_for_companies;
48             DROP INDEX IF EXISTS res_partner_nrc_uniq_for_companies;
49             CREATE UNIQUE INDEX res_partner_vat_uniq_for_companies ON res_partner (vat) WHERE is_company OR parent_id IS NULL;
50             CREATE UNIQUE INDEX res_partner_nrc_uniq_for_companies ON res_partner (nrc) WHERE is_company OR parent_id IS NULL;
51         """)
52         return result
53
54     def _commercial_fields(self, cr, uid, context=None):
55         return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['nrc']
56
57
58 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: