You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

906 lines
33 KiB

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