From 556e4e76c37419c810bfe0da6b13f2a19c4f0a72 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Mon, 15 Apr 2013 19:52:58 +0200 Subject: [PATCH] [FIX] l10n_ro: unique constraints on `vat` and `nrc` columns need to be updated for 7.0 model With the 7.0 model all accounting-related fields are synchronized from company to contacts (both are res.partner records), so the unique check is not a trivial unique constraint. It must be updated to use a *partial unique index* that applies only to "commercial entities" and not to contacts. The condition for being a commercial entity is "is_company is true or parent_id is NULL". If companies have unique vat/nrc numbers, then their contacts will be consistent because they are automatically synchronized, so the uniqueness between contacts does not have to be checked. (The auto-sync is performed by res.partner automatically and covered by tests in the "base" module) bzr revid: odo@openerp.com-20130415175258-l15meqin80sbhggu --- addons/l10n_ro/res_partner.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/addons/l10n_ro/res_partner.py b/addons/l10n_ro/res_partner.py index 95867f8e883..4720c6f8157 100755 --- a/addons/l10n_ro/res_partner.py +++ b/addons/l10n_ro/res_partner.py @@ -28,11 +28,30 @@ class res_partner(osv.osv): _columns = { 'nrc' : fields.char('NRC', size=16, help='Registration number at the Registry of Commerce'), } + + # The SQL constraints are no-ops but present only to display the right error message to the + # user when the partial unique indexes defined below raise errors/ + # The real constraints need to be implemented with PARTIAL UNIQUE INDEXES (see auto_init), + # due to the way accounting data is delegated by contacts to their companies in OpenERP 7.0. _sql_constraints = [ - ('vat_uniq', 'unique (vat)', 'The vat of the partner must be unique !'), - ('nrc_uniq', 'unique (nrc)', 'The code of the partner must be unique !') + ('vat_uniq', 'unique (id)', 'The vat of the partner must be unique !'), + ('nrc_uniq', 'unique (id)', 'The code of the partner must be unique !') ] + def _auto_init(self, cr, context=None): + result = super(res_partner, self)._auto_init(cr, context=context) + # Real implementation of the vat/nrc constraints: only "commercial entities" need to have + # unique numbers, and the condition for being a commercial entity is "is_company or parent_id IS NULL". + # Contacts inside a company automatically have a copy of the company's commercial fields + # (see _commercial_fields()), so they are automatically consistent. + cr.execute(""" + DROP INDEX IF EXISTS res_partner_vat_uniq_for_companies; + DROP INDEX IF EXISTS res_partner_nrc_uniq_for_companies; + CREATE UNIQUE INDEX res_partner_vat_uniq_for_companies ON res_partner (vat) WHERE is_company OR parent_id IS NULL; + CREATE UNIQUE INDEX res_partner_nrc_uniq_for_companies ON res_partner (nrc) WHERE is_company OR parent_id IS NULL; + """) + return result + def _commercial_fields(self, cr, uid, context=None): return super(res_partner, self)._commercial_fields(cr, uid, context=context) + ['nrc']