c07a32579f055f3dfddfbe375b63aaf02b9c2bed
[odoo/odoo.git] / addons / crm / base_partner_merge.py
1 #!/usr/bin/env python
2 from __future__ import absolute_import
3 from email.utils import parseaddr
4 import functools
5 import htmlentitydefs
6 import itertools
7 import logging
8 import operator
9 import re
10 from ast import literal_eval
11 from openerp.tools import mute_logger
12
13 # Validation Library https://pypi.python.org/pypi/validate_email/1.1
14 from .validate_email import validate_email
15
16 import openerp
17 from openerp.osv import osv, orm
18 from openerp.osv import fields
19 from openerp.osv.orm import browse_record
20 from openerp.tools.translate import _
21
22 pattern = re.compile("&(\w+?);")
23
24 _logger = logging.getLogger('base.partner.merge')
25
26
27 # http://www.php2python.com/wiki/function.html-entity-decode/
28 def html_entity_decode_char(m, defs=htmlentitydefs.entitydefs):
29     try:
30         return defs[m.group(1)]
31     except KeyError:
32         return m.group(0)
33
34
35 def html_entity_decode(string):
36     return pattern.sub(html_entity_decode_char, string)
37
38
39 def sanitize_email(email):
40     assert isinstance(email, basestring) and email
41
42     result = re.subn(r';|/|:', ',',
43                      html_entity_decode(email or ''))[0].split(',')
44
45     emails = [parseaddr(email)[1]
46               for item in result
47               for email in item.split()]
48
49     return [email.lower()
50             for email in emails
51             if validate_email(email)]
52
53
54 def is_integer_list(ids):
55     return all(isinstance(i, (int, long)) for i in ids)
56
57
58 class ResPartner(osv.Model):
59     _inherit = 'res.partner'
60
61     _columns = {
62         'id': fields.integer('Id', readonly=True),
63         'create_date': fields.datetime('Create Date', readonly=True),
64     }
65
66 class MergePartnerLine(osv.TransientModel):
67     _name = 'base.partner.merge.line'
68
69     _columns = {
70         'wizard_id': fields.many2one('base.partner.merge.automatic.wizard',
71                                      'Wizard'),
72         'min_id': fields.integer('MinID'),
73         'aggr_ids': fields.char('Ids', required=True),
74     }
75
76     _order = 'min_id asc'
77
78
79 class MergePartnerAutomatic(osv.TransientModel):
80     """
81         The idea behind this wizard is to create a list of potential partners to
82         merge. We use two objects, the first one is the wizard for the end-user.
83         And the second will contain the partner list to merge.
84     """
85     _name = 'base.partner.merge.automatic.wizard'
86
87     _columns = {
88         # Group by
89         'group_by_email': fields.boolean('Email'),
90         'group_by_name': fields.boolean('Name'),
91         'group_by_is_company': fields.boolean('Is Company'),
92         'group_by_vat': fields.boolean('VAT'),
93         'group_by_parent_id': fields.boolean('Parent Company'),
94
95         'state': fields.selection([('option', 'Option'),
96                                    ('selection', 'Selection'),
97                                    ('finished', 'Finished')],
98                                   'State',
99                                   readonly=True,
100                                   required=True),
101         'number_group': fields.integer("Group of Contacts", readonly=True),
102         'current_line_id': fields.many2one('base.partner.merge.line', 'Current Line'),
103         'line_ids': fields.one2many('base.partner.merge.line', 'wizard_id', 'Lines'),
104         'partner_ids': fields.many2many('res.partner', string='Contacts'),
105         'dst_partner_id': fields.many2one('res.partner', string='Destination Contact'),
106
107         'exclude_contact': fields.boolean('A user associated to the contact'),
108         'exclude_journal_item': fields.boolean('Journal Items associated to the contact'),
109         'maximum_group': fields.integer("Maximum of Group of Contacts"),
110     }
111
112     def default_get(self, cr, uid, fields, context=None):
113         if context is None:
114             context = {}
115         res = super(MergePartnerAutomatic, self).default_get(cr, uid, fields, context)
116         if context.get('active_model') == 'res.partner' and context.get('active_ids'):
117             partner_ids = context['active_ids']
118             res['state'] = 'selection'
119             res['partner_ids'] = partner_ids
120             res['dst_partner_id'] = self._get_ordered_partner(cr, uid, partner_ids, context=context)[-1].id
121         return res
122
123     _defaults = {
124         'state': 'option'
125     }
126
127     def get_fk_on(self, cr, table):
128         q = """  SELECT cl1.relname as table,
129                         att1.attname as column
130                    FROM pg_constraint as con, pg_class as cl1, pg_class as cl2,
131                         pg_attribute as att1, pg_attribute as att2
132                   WHERE con.conrelid = cl1.oid
133                     AND con.confrelid = cl2.oid
134                     AND array_lower(con.conkey, 1) = 1
135                     AND con.conkey[1] = att1.attnum
136                     AND att1.attrelid = cl1.oid
137                     AND cl2.relname = %s
138                     AND att2.attname = 'id'
139                     AND array_lower(con.confkey, 1) = 1
140                     AND con.confkey[1] = att2.attnum
141                     AND att2.attrelid = cl2.oid
142                     AND con.contype = 'f'
143         """
144         return cr.execute(q, (table,))
145
146     def _update_foreign_keys(self, cr, uid, src_partners, dst_partner, context=None):
147         _logger.debug('_update_foreign_keys for dst_partner: %s for src_partners: %r', dst_partner.id, list(map(operator.attrgetter('id'), src_partners)))
148
149         # find the many2one relation to a partner
150         proxy = self.pool.get('res.partner')
151         self.get_fk_on(cr, 'res_partner')
152
153         # ignore two tables
154
155         for table, column in cr.fetchall():
156             if 'base_partner_merge_' in table:
157                 continue
158             partner_ids = tuple(map(int, src_partners))
159
160             query = "SELECT column_name FROM information_schema.columns WHERE table_name LIKE '%s'" % (table)
161             cr.execute(query, ())
162             columns = []
163             for data in cr.fetchall():
164                 if data[0] != column:
165                     columns.append(data[0])
166
167             query_dic = {
168                 'table': table,
169                 'column': column,
170                 'value': columns[0],
171             }
172             if len(columns) <= 1:
173                 # unique key treated
174                 query = """
175                     UPDATE "%(table)s" as ___tu
176                     SET %(column)s = %%s
177                     WHERE
178                         %(column)s = %%s AND
179                         NOT EXISTS (
180                             SELECT 1
181                             FROM "%(table)s" as ___tw
182                             WHERE
183                                 %(column)s = %%s AND
184                                 ___tu.%(value)s = ___tw.%(value)s
185                         )""" % query_dic
186                 for partner_id in partner_ids:
187                     cr.execute(query, (dst_partner.id, partner_id, dst_partner.id))
188             else:
189                 cr.execute("SAVEPOINT recursive_partner_savepoint")
190                 try:
191                     query = 'UPDATE "%(table)s" SET %(column)s = %%s WHERE %(column)s IN %%s' % query_dic
192                     cr.execute(query, (dst_partner.id, partner_ids,))
193
194                     if column == proxy._parent_name and table == 'res_partner':
195                         query = """
196                             WITH RECURSIVE cycle(id, parent_id) AS (
197                                     SELECT id, parent_id FROM res_partner
198                                 UNION
199                                     SELECT  cycle.id, res_partner.parent_id
200                                     FROM    res_partner, cycle
201                                     WHERE   res_partner.id = cycle.parent_id AND
202                                             cycle.id != cycle.parent_id
203                             )
204                             SELECT id FROM cycle WHERE id = parent_id AND id = %s
205                         """
206                         cr.execute(query, (dst_partner.id,))
207                         if cr.fetchall():
208                             cr.execute("ROLLBACK TO SAVEPOINT recursive_partner_savepoint")
209                 finally:
210                     cr.execute("RELEASE SAVEPOINT recursive_partner_savepoint")
211
212     def _update_reference_fields(self, cr, uid, src_partners, dst_partner, context=None):
213         _logger.debug('_update_reference_fields for dst_partner: %s for src_partners: %r', dst_partner.id, list(map(operator.attrgetter('id'), src_partners)))
214
215         def update_records(model, src, field_model='model', field_id='res_id', context=None):
216             proxy = self.pool.get(model)
217             if proxy is None:
218                 return
219             domain = [(field_model, '=', 'res.partner'), (field_id, '=', src.id)]
220             ids = proxy.search(cr, openerp.SUPERUSER_ID, domain, context=context)
221             return proxy.write(cr, openerp.SUPERUSER_ID, ids, {field_id: dst_partner.id}, context=context)
222
223         update_records = functools.partial(update_records, context=context)
224
225         for partner in src_partners:
226             update_records('calendar', src=partner, field_model='model_id.model')
227             update_records('ir.attachment', src=partner, field_model='res_model')
228             update_records('mail.followers', src=partner, field_model='res_model')
229             update_records('mail.message', src=partner)
230             update_records('marketing.campaign.workitem', src=partner, field_model='object_id.model')
231             update_records('ir.model.data', src=partner)
232
233         proxy = self.pool['ir.model.fields']
234         domain = [('ttype', '=', 'reference')]
235         record_ids = proxy.search(cr, openerp.SUPERUSER_ID, domain, context=context)
236
237         for record in proxy.browse(cr, openerp.SUPERUSER_ID, record_ids, context=context):
238             try:
239                 proxy_model = self.pool[record.model]
240                 field_type = proxy_model._columns[record.name].__class__._type
241             except KeyError:
242                 # unknown model or field => skip
243                 continue
244
245             if field_type == 'function':
246                 continue
247
248             for partner in src_partners:
249                 domain = [
250                     (record.name, '=', 'res.partner,%d' % partner.id)
251                 ]
252                 model_ids = proxy_model.search(cr, openerp.SUPERUSER_ID, domain, context=context)
253                 values = {
254                     record.name: 'res.partner,%d' % dst_partner.id,
255                 }
256                 proxy_model.write(cr, openerp.SUPERUSER_ID, model_ids, values, context=context)
257
258     def _update_values(self, cr, uid, src_partners, dst_partner, context=None):
259         _logger.debug('_update_values for dst_partner: %s for src_partners: %r', dst_partner.id, list(map(operator.attrgetter('id'), src_partners)))
260
261         columns = dst_partner._columns
262         def write_serializer(column, item):
263             if isinstance(item, browse_record):
264                 return item.id
265             else:
266                 return item
267
268         values = dict()
269         for column, field in columns.iteritems():
270             if field._type not in ('many2many', 'one2many') and not isinstance(field, fields.function):
271                 for item in itertools.chain(src_partners, [dst_partner]):
272                     if item[column]:
273                         values[column] = write_serializer(column, item[column])
274
275         values.pop('id', None)
276         parent_id = values.pop('parent_id', None)
277         dst_partner.write(values)
278         if parent_id and parent_id != dst_partner.id:
279             try:
280                 dst_partner.write({'parent_id': parent_id})
281             except (osv.except_osv, orm.except_orm):
282                 _logger.info('Skip recursive partner hierarchies for parent_id %s of partner: %s', parent_id, dst_partner.id)
283
284     @mute_logger('openerp.osv.expression', 'openerp.osv.orm')
285     def _merge(self, cr, uid, partner_ids, dst_partner=None, context=None):
286         proxy = self.pool.get('res.partner')
287
288         partner_ids = proxy.exists(cr, uid, list(partner_ids), context=context)
289         if len(partner_ids) < 2:
290             return
291
292         if len(partner_ids) > 3:
293             raise osv.except_osv(_('Error'), _("For safety reasons, you cannot merge more than 3 contacts together. You can re-open the wizard several times if needed."))
294
295         if openerp.SUPERUSER_ID != uid and len(set(partner.email for partner in proxy.browse(cr, uid, partner_ids, context=context))) > 1:
296             raise osv.except_osv(_('Error'), _("All contacts must have the same email. Only the Administrator can merge contacts with different emails."))
297
298         if dst_partner and dst_partner.id in partner_ids:
299             src_partners = proxy.browse(cr, uid, [id for id in partner_ids if id != dst_partner.id], context=context)
300         else:
301             ordered_partners = self._get_ordered_partner(cr, uid, partner_ids, context)
302             dst_partner = ordered_partners[-1]
303             src_partners = ordered_partners[:-1]
304         _logger.info("dst_partner: %s", dst_partner.id)
305
306         if openerp.SUPERUSER_ID != uid and self._model_is_installed(cr, uid, 'account.move.line', context=context) and \
307                 self.pool.get('account.move.line').search(cr, openerp.SUPERUSER_ID, [('partner_id', 'in', [partner.id for partner in src_partners])], context=context):
308             raise osv.except_osv(_('Error'), _("Only the destination contact may be linked to existing Journal Items. Please ask the Administrator if you need to merge several contacts linked to existing Journal Items."))
309
310         call_it = lambda function: function(cr, uid, src_partners, dst_partner,
311                                             context=context)
312
313         call_it(self._update_foreign_keys)
314         call_it(self._update_reference_fields)
315         call_it(self._update_values)
316
317         _logger.info('(uid = %s) merged the partners %r with %s', uid, list(map(operator.attrgetter('id'), src_partners)), dst_partner.id)
318         dst_partner.message_post(body='%s %s'%(_("Merged with the following partners:"), ", ".join('%s<%s>(ID %s)' % (p.name, p.email or 'n/a', p.id) for p in src_partners)))
319         
320         for partner in src_partners:
321             partner.unlink()
322
323     def clean_emails(self, cr, uid, context=None):
324         """
325         Clean the email address of the partner, if there is an email field with
326         a mimum of two addresses, the system will create a new partner, with the
327         information of the previous one and will copy the new cleaned email into
328         the email field.
329         """
330         if context is None:
331             context = {}
332
333         proxy_model = self.pool['ir.model.fields']
334         field_ids = proxy_model.search(cr, uid, [('model', '=', 'res.partner'),
335                                                  ('ttype', 'like', '%2many')],
336                                        context=context)
337         fields = proxy_model.read(cr, uid, field_ids, context=context)
338         reset_fields = dict((field['name'], []) for field in fields)
339
340         proxy_partner = self.pool['res.partner']
341         context['active_test'] = False
342         ids = proxy_partner.search(cr, uid, [], context=context)
343
344         fields = ['name', 'var' 'partner_id' 'is_company', 'email']
345         partners = proxy_partner.read(cr, uid, ids, fields, context=context)
346
347         partners.sort(key=operator.itemgetter('id'))
348         partners_len = len(partners)
349
350         _logger.info('partner_len: %r', partners_len)
351
352         for idx, partner in enumerate(partners):
353             if not partner['email']:
354                 continue
355
356             percent = (idx / float(partners_len)) * 100.0
357             _logger.info('idx: %r', idx)
358             _logger.info('percent: %r', percent)
359             try:
360                 emails = sanitize_email(partner['email'])
361                 head, tail = emails[:1], emails[1:]
362                 email = head[0] if head else False
363
364                 proxy_partner.write(cr, uid, [partner['id']],
365                                     {'email': email}, context=context)
366
367                 for email in tail:
368                     values = dict(reset_fields, email=email)
369                     proxy_partner.copy(cr, uid, partner['id'], values,
370                                        context=context)
371
372             except Exception:
373                 _logger.exception("There is a problem with this partner: %r", partner)
374                 raise
375         return True
376
377     def close_cb(self, cr, uid, ids, context=None):
378         return {'type': 'ir.actions.act_window_close'}
379
380     def _generate_query(self, fields, maximum_group=100):
381         group_fields = ', '.join(fields)
382
383         filters = []
384         for field in fields:
385             if field in ['email', 'name']:
386                 filters.append((field, 'IS NOT', 'NULL'))
387
388         criteria = ' AND '.join('%s %s %s' % (field, operator, value)
389                                 for field, operator, value in filters)
390
391         text = [
392             "SELECT min(id), array_agg(id)",
393             "FROM res_partner",
394         ]
395
396         if criteria:
397             text.append('WHERE %s' % criteria)
398
399         text.extend([
400             "GROUP BY %s" % group_fields,
401             "HAVING COUNT(*) >= 2",
402             "ORDER BY min(id)",
403         ])
404
405         if maximum_group:
406             text.extend([
407                 "LIMIT %s" % maximum_group,
408             ])
409
410         return ' '.join(text)
411
412     def _compute_selected_groupby(self, this):
413         group_by_str = 'group_by_'
414         group_by_len = len(group_by_str)
415
416         fields = [
417             key[group_by_len:]
418             for key in self._columns.keys()
419             if key.startswith(group_by_str)
420         ]
421
422         groups = [
423             field
424             for field in fields
425             if getattr(this, '%s%s' % (group_by_str, field), False)
426         ]
427
428         if not groups:
429             raise osv.except_osv(_('Error'),
430                                  _("You have to specify a filter for your selection"))
431
432         return groups
433
434     def next_cb(self, cr, uid, ids, context=None):
435         """
436         Don't compute any thing
437         """
438         context = dict(context or {}, active_test=False)
439         this = self.browse(cr, uid, ids[0], context=context)
440         if this.current_line_id:
441             this.current_line_id.unlink()
442         return self._next_screen(cr, uid, this, context)
443
444     def _get_ordered_partner(self, cr, uid, partner_ids, context=None):
445         partners = self.pool.get('res.partner').browse(cr, uid, list(partner_ids), context=context)
446         ordered_partners = sorted(sorted(partners,
447                             key=operator.attrgetter('create_date'), reverse=True),
448                                 key=operator.attrgetter('active'), reverse=True)
449         return ordered_partners
450
451     def _next_screen(self, cr, uid, this, context=None):
452         this.refresh()
453         values = {}
454         if this.line_ids:
455             # in this case, we try to find the next record.
456             current_line = this.line_ids[0]
457             current_partner_ids = literal_eval(current_line.aggr_ids)
458             values.update({
459                 'current_line_id': current_line.id,
460                 'partner_ids': [(6, 0, current_partner_ids)],
461                 'dst_partner_id': self._get_ordered_partner(cr, uid, current_partner_ids, context)[-1].id,
462                 'state': 'selection',
463             })
464         else:
465             values.update({
466                 'current_line_id': False,
467                 'partner_ids': [],
468                 'state': 'finished',
469             })
470
471         this.write(values)
472
473         return {
474             'type': 'ir.actions.act_window',
475             'res_model': this._name,
476             'res_id': this.id,
477             'view_mode': 'form',
478             'target': 'new',
479         }
480
481     def _model_is_installed(self, cr, uid, model, context=None):
482         proxy = self.pool.get('ir.model')
483         domain = [('model', '=', model)]
484         return proxy.search_count(cr, uid, domain, context=context) > 0
485
486     def _partner_use_in(self, cr, uid, aggr_ids, models, context=None):
487         """
488         Check if there is no occurence of this group of partner in the selected
489         model
490         """
491         for model, field in models.iteritems():
492             proxy = self.pool.get(model)
493             domain = [(field, 'in', aggr_ids)]
494             if proxy.search_count(cr, uid, domain, context=context):
495                 return True
496         return False
497
498     def compute_models(self, cr, uid, ids, context=None):
499         """
500         Compute the different models needed by the system if you want to exclude
501         some partners.
502         """
503         assert is_integer_list(ids)
504
505         this = self.browse(cr, uid, ids[0], context=context)
506
507         models = {}
508         if this.exclude_contact:
509             models['res.users'] = 'partner_id'
510
511         if self._model_is_installed(cr, uid, 'account.move.line', context=context) and this.exclude_journal_item:
512             models['account.move.line'] = 'partner_id'
513
514         return models
515
516     def _process_query(self, cr, uid, ids, query, context=None):
517         """
518         Execute the select request and write the result in this wizard
519         """
520         proxy = self.pool.get('base.partner.merge.line')
521         this = self.browse(cr, uid, ids[0], context=context)
522         models = self.compute_models(cr, uid, ids, context=context)
523         cr.execute(query)
524
525         counter = 0
526         for min_id, aggr_ids in cr.fetchall():
527             if models and self._partner_use_in(cr, uid, aggr_ids, models, context=context):
528                 continue
529             values = {
530                 'wizard_id': this.id,
531                 'min_id': min_id,
532                 'aggr_ids': aggr_ids,
533             }
534
535             proxy.create(cr, uid, values, context=context)
536             counter += 1
537
538         values = {
539             'state': 'selection',
540             'number_group': counter,
541         }
542
543         this.write(values)
544
545         _logger.info("counter: %s", counter)
546
547     def start_process_cb(self, cr, uid, ids, context=None):
548         """
549         Start the process.
550         * Compute the selected groups (with duplication)
551         * If the user has selected the 'exclude_XXX' fields, avoid the partners.
552         """
553         assert is_integer_list(ids)
554
555         context = dict(context or {}, active_test=False)
556         this = self.browse(cr, uid, ids[0], context=context)
557         groups = self._compute_selected_groupby(this)
558         query = self._generate_query(groups, this.maximum_group)
559         self._process_query(cr, uid, ids, query, context=context)
560
561         return self._next_screen(cr, uid, this, context)
562
563     def automatic_process_cb(self, cr, uid, ids, context=None):
564         assert is_integer_list(ids)
565         this = self.browse(cr, uid, ids[0], context=context)
566         this.start_process_cb()
567         this.refresh()
568
569         for line in this.line_ids:
570             partner_ids = literal_eval(line.aggr_ids)
571             self._merge(cr, uid, partner_ids, context=context)
572             line.unlink()
573             cr.commit()
574
575         this.write({'state': 'finished'})
576         return {
577             'type': 'ir.actions.act_window',
578             'res_model': this._name,
579             'res_id': this.id,
580             'view_mode': 'form',
581             'target': 'new',
582         }
583
584     def parent_migration_process_cb(self, cr, uid, ids, context=None):
585         assert is_integer_list(ids)
586
587         context = dict(context or {}, active_test=False)
588         this = self.browse(cr, uid, ids[0], context=context)
589
590         query = """
591             SELECT
592                 min(p1.id),
593                 array_agg(DISTINCT p1.id)
594             FROM
595                 res_partner as p1
596             INNER join
597                 res_partner as p2
598             ON
599                 p1.email = p2.email AND
600                 p1.name = p2.name AND
601                 (p1.parent_id = p2.id OR p1.id = p2.parent_id)
602             WHERE
603                 p2.id IS NOT NULL
604             GROUP BY
605                 p1.email,
606                 p1.name,
607                 CASE WHEN p1.parent_id = p2.id THEN p2.id
608                     ELSE p1.id
609                 END
610             HAVING COUNT(*) >= 2
611             ORDER BY
612                 min(p1.id)
613         """
614
615         self._process_query(cr, uid, ids, query, context=context)
616
617         for line in this.line_ids:
618             partner_ids = literal_eval(line.aggr_ids)
619             self._merge(cr, uid, partner_ids, context=context)
620             line.unlink()
621             cr.commit()
622
623         this.write({'state': 'finished'})
624
625         cr.execute("""
626             UPDATE
627                 res_partner
628             SET
629                 is_company = NULL,
630                 parent_id = NULL
631             WHERE
632                 parent_id = id
633         """)
634
635         return {
636             'type': 'ir.actions.act_window',
637             'res_model': this._name,
638             'res_id': this.id,
639             'view_mode': 'form',
640             'target': 'new',
641         }
642
643     def update_all_process_cb(self, cr, uid, ids, context=None):
644         assert is_integer_list(ids)
645
646         # WITH RECURSIVE cycle(id, parent_id) AS (
647         #     SELECT id, parent_id FROM res_partner
648         #   UNION
649         #     SELECT  cycle.id, res_partner.parent_id
650         #     FROM    res_partner, cycle
651         #     WHERE   res_partner.id = cycle.parent_id AND
652         #             cycle.id != cycle.parent_id
653         # )
654         # UPDATE  res_partner
655         # SET     parent_id = NULL
656         # WHERE   id in (SELECT id FROM cycle WHERE id = parent_id);
657
658         this = self.browse(cr, uid, ids[0], context=context)
659
660         self.parent_migration_process_cb(cr, uid, ids, context=None)
661
662         list_merge = [
663             {'group_by_vat': True, 'group_by_email': True, 'group_by_name': True},
664             # {'group_by_name': True, 'group_by_is_company': True, 'group_by_parent_id': True},
665             # {'group_by_email': True, 'group_by_is_company': True, 'group_by_parent_id': True},
666             # {'group_by_name': True, 'group_by_vat': True, 'group_by_is_company': True, 'exclude_journal_item': True},
667             # {'group_by_email': True, 'group_by_vat': True, 'group_by_is_company': True, 'exclude_journal_item': True},
668             # {'group_by_email': True, 'group_by_is_company': True, 'exclude_contact': True, 'exclude_journal_item': True},
669             # {'group_by_name': True, 'group_by_is_company': True, 'exclude_contact': True, 'exclude_journal_item': True}
670         ]
671
672         for merge_value in list_merge:
673             id = self.create(cr, uid, merge_value, context=context)
674             self.automatic_process_cb(cr, uid, [id], context=context)
675
676         cr.execute("""
677             UPDATE
678                 res_partner
679             SET
680                 is_company = NULL
681             WHERE
682                 parent_id IS NOT NULL AND
683                 is_company IS NOT NULL
684         """)
685
686         # cr.execute("""
687         #     UPDATE
688         #         res_partner as p1
689         #     SET
690         #         is_company = NULL,
691         #         parent_id = (
692         #             SELECT  p2.id
693         #             FROM    res_partner as p2
694         #             WHERE   p2.email = p1.email AND
695         #                     p2.parent_id != p2.id
696         #             LIMIT 1
697         #         )
698         #     WHERE
699         #         p1.parent_id = p1.id
700         # """)
701
702         return self._next_screen(cr, uid, this, context)
703
704     def merge_cb(self, cr, uid, ids, context=None):
705         assert is_integer_list(ids)
706
707         context = dict(context or {}, active_test=False)
708         this = self.browse(cr, uid, ids[0], context=context)
709
710         partner_ids = set(map(int, this.partner_ids))
711         if not partner_ids:
712             this.write({'state': 'finished'})
713             return {
714                 'type': 'ir.actions.act_window',
715                 'res_model': this._name,
716                 'res_id': this.id,
717                 'view_mode': 'form',
718                 'target': 'new',
719             }
720
721         self._merge(cr, uid, partner_ids, this.dst_partner_id, context=context)
722
723         if this.current_line_id:
724             this.current_line_id.unlink()
725
726         return self._next_screen(cr, uid, this, context)
727
728     def auto_set_parent_id(self, cr, uid, ids, context=None):
729         assert is_integer_list(ids)
730
731         # select partner who have one least invoice
732         partner_treated = ['@gmail.com']
733         cr.execute("""  SELECT p.id, p.email
734                         FROM res_partner as p 
735                         LEFT JOIN account_invoice as a 
736                         ON p.id = a.partner_id AND a.state in ('open','paid')
737                         WHERE p.grade_id is NOT NULL
738                         GROUP BY p.id
739                         ORDER BY COUNT(a.id) DESC
740                 """)
741         re_email = re.compile(r".*@")
742         for id, email in cr.fetchall():
743             # check email domain
744             email = re_email.sub("@", email or "")
745             if not email or email in partner_treated:
746                 continue
747             partner_treated.append(email)
748
749             # don't update the partners if they are more of one who have invoice
750             cr.execute("""  SELECT *
751                             FROM res_partner as p
752                             WHERE p.id != %s AND p.email LIKE '%%%s' AND
753                                 EXISTS (SELECT * FROM account_invoice as a WHERE p.id = a.partner_id AND a.state in ('open','paid'))
754                     """ % (id, email))
755
756             if len(cr.fetchall()) > 1:
757                 _logger.info("%s MORE OF ONE COMPANY", email)
758                 continue
759
760             # to display changed values
761             cr.execute("""  SELECT id,email
762                             FROM res_partner
763                             WHERE parent_id != %s AND id != %s AND email LIKE '%%%s'
764                     """ % (id, id, email))
765             _logger.info("%r", cr.fetchall())
766
767             # upgrade
768             cr.execute("""  UPDATE res_partner
769                             SET parent_id = %s
770                             WHERE id != %s AND email LIKE '%%%s'
771                     """ % (id, id, email))
772         return False