[FIX] crm: base_partner_merge, search with lowercase for name,email
[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         sql_fields = []
382         for field in fields:
383             if field in ['email', 'name']:
384                 sql_fields.append('lower(%s)' % field)
385             elif field in ['vat']:
386                 sql_fields.append("replace(%s, ' ', '')" % field)
387             else:
388                 sql_fields.append(field)
389
390         group_fields = ', '.join(sql_fields)
391
392         filters = []
393         for field in fields:
394             if field in ['email', 'name', 'vat']:
395                 filters.append((field, 'IS NOT', 'NULL'))
396
397         criteria = ' AND '.join('%s %s %s' % (field, operator, value)
398                                 for field, operator, value in filters)
399
400         text = [
401             "SELECT min(id), array_agg(id)",
402             "FROM res_partner",
403         ]
404
405         if criteria:
406             text.append('WHERE %s' % criteria)
407
408         text.extend([
409             "GROUP BY %s" % group_fields,
410             "HAVING COUNT(*) >= 2",
411             "ORDER BY min(id)",
412         ])
413
414         if maximum_group:
415             text.extend([
416                 "LIMIT %s" % maximum_group,
417             ])
418
419         return ' '.join(text)
420
421     def _compute_selected_groupby(self, this):
422         group_by_str = 'group_by_'
423         group_by_len = len(group_by_str)
424
425         fields = [
426             key[group_by_len:]
427             for key in self._columns.keys()
428             if key.startswith(group_by_str)
429         ]
430
431         groups = [
432             field
433             for field in fields
434             if getattr(this, '%s%s' % (group_by_str, field), False)
435         ]
436
437         if not groups:
438             raise osv.except_osv(_('Error'),
439                                  _("You have to specify a filter for your selection"))
440
441         return groups
442
443     def next_cb(self, cr, uid, ids, context=None):
444         """
445         Don't compute any thing
446         """
447         context = dict(context or {}, active_test=False)
448         this = self.browse(cr, uid, ids[0], context=context)
449         if this.current_line_id:
450             this.current_line_id.unlink()
451         return self._next_screen(cr, uid, this, context)
452
453     def _get_ordered_partner(self, cr, uid, partner_ids, context=None):
454         partners = self.pool.get('res.partner').browse(cr, uid, list(partner_ids), context=context)
455         ordered_partners = sorted(sorted(partners,
456                             key=operator.attrgetter('create_date'), reverse=True),
457                                 key=operator.attrgetter('active'), reverse=True)
458         return ordered_partners
459
460     def _next_screen(self, cr, uid, this, context=None):
461         this.refresh()
462         values = {}
463         if this.line_ids:
464             # in this case, we try to find the next record.
465             current_line = this.line_ids[0]
466             current_partner_ids = literal_eval(current_line.aggr_ids)
467             values.update({
468                 'current_line_id': current_line.id,
469                 'partner_ids': [(6, 0, current_partner_ids)],
470                 'dst_partner_id': self._get_ordered_partner(cr, uid, current_partner_ids, context)[-1].id,
471                 'state': 'selection',
472             })
473         else:
474             values.update({
475                 'current_line_id': False,
476                 'partner_ids': [],
477                 'state': 'finished',
478             })
479
480         this.write(values)
481
482         return {
483             'type': 'ir.actions.act_window',
484             'res_model': this._name,
485             'res_id': this.id,
486             'view_mode': 'form',
487             'target': 'new',
488         }
489
490     def _model_is_installed(self, cr, uid, model, context=None):
491         proxy = self.pool.get('ir.model')
492         domain = [('model', '=', model)]
493         return proxy.search_count(cr, uid, domain, context=context) > 0
494
495     def _partner_use_in(self, cr, uid, aggr_ids, models, context=None):
496         """
497         Check if there is no occurence of this group of partner in the selected
498         model
499         """
500         for model, field in models.iteritems():
501             proxy = self.pool.get(model)
502             domain = [(field, 'in', aggr_ids)]
503             if proxy.search_count(cr, uid, domain, context=context):
504                 return True
505         return False
506
507     def compute_models(self, cr, uid, ids, context=None):
508         """
509         Compute the different models needed by the system if you want to exclude
510         some partners.
511         """
512         assert is_integer_list(ids)
513
514         this = self.browse(cr, uid, ids[0], context=context)
515
516         models = {}
517         if this.exclude_contact:
518             models['res.users'] = 'partner_id'
519
520         if self._model_is_installed(cr, uid, 'account.move.line', context=context) and this.exclude_journal_item:
521             models['account.move.line'] = 'partner_id'
522
523         return models
524
525     def _process_query(self, cr, uid, ids, query, context=None):
526         """
527         Execute the select request and write the result in this wizard
528         """
529         proxy = self.pool.get('base.partner.merge.line')
530         this = self.browse(cr, uid, ids[0], context=context)
531         models = self.compute_models(cr, uid, ids, context=context)
532         cr.execute(query)
533
534         counter = 0
535         for min_id, aggr_ids in cr.fetchall():
536             if models and self._partner_use_in(cr, uid, aggr_ids, models, context=context):
537                 continue
538             values = {
539                 'wizard_id': this.id,
540                 'min_id': min_id,
541                 'aggr_ids': aggr_ids,
542             }
543
544             proxy.create(cr, uid, values, context=context)
545             counter += 1
546
547         values = {
548             'state': 'selection',
549             'number_group': counter,
550         }
551
552         this.write(values)
553
554         _logger.info("counter: %s", counter)
555
556     def start_process_cb(self, cr, uid, ids, context=None):
557         """
558         Start the process.
559         * Compute the selected groups (with duplication)
560         * If the user has selected the 'exclude_XXX' fields, avoid the partners.
561         """
562         assert is_integer_list(ids)
563
564         context = dict(context or {}, active_test=False)
565         this = self.browse(cr, uid, ids[0], context=context)
566         groups = self._compute_selected_groupby(this)
567         query = self._generate_query(groups, this.maximum_group)
568         self._process_query(cr, uid, ids, query, context=context)
569
570         return self._next_screen(cr, uid, this, context)
571
572     def automatic_process_cb(self, cr, uid, ids, context=None):
573         assert is_integer_list(ids)
574         this = self.browse(cr, uid, ids[0], context=context)
575         this.start_process_cb()
576         this.refresh()
577
578         for line in this.line_ids:
579             partner_ids = literal_eval(line.aggr_ids)
580             self._merge(cr, uid, partner_ids, context=context)
581             line.unlink()
582             cr.commit()
583
584         this.write({'state': 'finished'})
585         return {
586             'type': 'ir.actions.act_window',
587             'res_model': this._name,
588             'res_id': this.id,
589             'view_mode': 'form',
590             'target': 'new',
591         }
592
593     def parent_migration_process_cb(self, cr, uid, ids, context=None):
594         assert is_integer_list(ids)
595
596         context = dict(context or {}, active_test=False)
597         this = self.browse(cr, uid, ids[0], context=context)
598
599         query = """
600             SELECT
601                 min(p1.id),
602                 array_agg(DISTINCT p1.id)
603             FROM
604                 res_partner as p1
605             INNER join
606                 res_partner as p2
607             ON
608                 p1.email = p2.email AND
609                 p1.name = p2.name AND
610                 (p1.parent_id = p2.id OR p1.id = p2.parent_id)
611             WHERE
612                 p2.id IS NOT NULL
613             GROUP BY
614                 p1.email,
615                 p1.name,
616                 CASE WHEN p1.parent_id = p2.id THEN p2.id
617                     ELSE p1.id
618                 END
619             HAVING COUNT(*) >= 2
620             ORDER BY
621                 min(p1.id)
622         """
623
624         self._process_query(cr, uid, ids, query, context=context)
625
626         for line in this.line_ids:
627             partner_ids = literal_eval(line.aggr_ids)
628             self._merge(cr, uid, partner_ids, context=context)
629             line.unlink()
630             cr.commit()
631
632         this.write({'state': 'finished'})
633
634         cr.execute("""
635             UPDATE
636                 res_partner
637             SET
638                 is_company = NULL,
639                 parent_id = NULL
640             WHERE
641                 parent_id = id
642         """)
643
644         return {
645             'type': 'ir.actions.act_window',
646             'res_model': this._name,
647             'res_id': this.id,
648             'view_mode': 'form',
649             'target': 'new',
650         }
651
652     def update_all_process_cb(self, cr, uid, ids, context=None):
653         assert is_integer_list(ids)
654
655         # WITH RECURSIVE cycle(id, parent_id) AS (
656         #     SELECT id, parent_id FROM res_partner
657         #   UNION
658         #     SELECT  cycle.id, res_partner.parent_id
659         #     FROM    res_partner, cycle
660         #     WHERE   res_partner.id = cycle.parent_id AND
661         #             cycle.id != cycle.parent_id
662         # )
663         # UPDATE  res_partner
664         # SET     parent_id = NULL
665         # WHERE   id in (SELECT id FROM cycle WHERE id = parent_id);
666
667         this = self.browse(cr, uid, ids[0], context=context)
668
669         self.parent_migration_process_cb(cr, uid, ids, context=None)
670
671         list_merge = [
672             {'group_by_vat': True, 'group_by_email': True, 'group_by_name': True},
673             # {'group_by_name': True, 'group_by_is_company': True, 'group_by_parent_id': True},
674             # {'group_by_email': True, 'group_by_is_company': True, 'group_by_parent_id': True},
675             # {'group_by_name': True, 'group_by_vat': True, 'group_by_is_company': True, 'exclude_journal_item': True},
676             # {'group_by_email': True, 'group_by_vat': True, 'group_by_is_company': True, 'exclude_journal_item': True},
677             # {'group_by_email': True, 'group_by_is_company': True, 'exclude_contact': True, 'exclude_journal_item': True},
678             # {'group_by_name': True, 'group_by_is_company': True, 'exclude_contact': True, 'exclude_journal_item': True}
679         ]
680
681         for merge_value in list_merge:
682             id = self.create(cr, uid, merge_value, context=context)
683             self.automatic_process_cb(cr, uid, [id], context=context)
684
685         cr.execute("""
686             UPDATE
687                 res_partner
688             SET
689                 is_company = NULL
690             WHERE
691                 parent_id IS NOT NULL AND
692                 is_company IS NOT NULL
693         """)
694
695         # cr.execute("""
696         #     UPDATE
697         #         res_partner as p1
698         #     SET
699         #         is_company = NULL,
700         #         parent_id = (
701         #             SELECT  p2.id
702         #             FROM    res_partner as p2
703         #             WHERE   p2.email = p1.email AND
704         #                     p2.parent_id != p2.id
705         #             LIMIT 1
706         #         )
707         #     WHERE
708         #         p1.parent_id = p1.id
709         # """)
710
711         return self._next_screen(cr, uid, this, context)
712
713     def merge_cb(self, cr, uid, ids, context=None):
714         assert is_integer_list(ids)
715
716         context = dict(context or {}, active_test=False)
717         this = self.browse(cr, uid, ids[0], context=context)
718
719         partner_ids = set(map(int, this.partner_ids))
720         if not partner_ids:
721             this.write({'state': 'finished'})
722             return {
723                 'type': 'ir.actions.act_window',
724                 'res_model': this._name,
725                 'res_id': this.id,
726                 'view_mode': 'form',
727                 'target': 'new',
728             }
729
730         self._merge(cr, uid, partner_ids, this.dst_partner_id, context=context)
731
732         if this.current_line_id:
733             this.current_line_id.unlink()
734
735         return self._next_screen(cr, uid, this, context)
736
737     def auto_set_parent_id(self, cr, uid, ids, context=None):
738         assert is_integer_list(ids)
739
740         # select partner who have one least invoice
741         partner_treated = ['@gmail.com']
742         cr.execute("""  SELECT p.id, p.email
743                         FROM res_partner as p 
744                         LEFT JOIN account_invoice as a 
745                         ON p.id = a.partner_id AND a.state in ('open','paid')
746                         WHERE p.grade_id is NOT NULL
747                         GROUP BY p.id
748                         ORDER BY COUNT(a.id) DESC
749                 """)
750         re_email = re.compile(r".*@")
751         for id, email in cr.fetchall():
752             # check email domain
753             email = re_email.sub("@", email or "")
754             if not email or email in partner_treated:
755                 continue
756             partner_treated.append(email)
757
758             # don't update the partners if they are more of one who have invoice
759             cr.execute("""  SELECT *
760                             FROM res_partner as p
761                             WHERE p.id != %s AND p.email LIKE '%%%s' AND
762                                 EXISTS (SELECT * FROM account_invoice as a WHERE p.id = a.partner_id AND a.state in ('open','paid'))
763                     """ % (id, email))
764
765             if len(cr.fetchall()) > 1:
766                 _logger.info("%s MORE OF ONE COMPANY", email)
767                 continue
768
769             # to display changed values
770             cr.execute("""  SELECT id,email
771                             FROM res_partner
772                             WHERE parent_id != %s AND id != %s AND email LIKE '%%%s'
773                     """ % (id, id, email))
774             _logger.info("%r", cr.fetchall())
775
776             # upgrade
777             cr.execute("""  UPDATE res_partner
778                             SET parent_id = %s
779                             WHERE id != %s AND email LIKE '%%%s'
780                     """ % (id, id, email))
781         return False