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.

761 lines
29 KiB

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