crm_lead_to_opportunity_mass.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class Lead2OpportunityMassConvert(models.TransientModel):
  5. _name = 'crm.lead2opportunity.partner.mass'
  6. _description = 'Convert Lead to Opportunity (in mass)'
  7. _inherit = 'crm.lead2opportunity.partner'
  8. lead_id = fields.Many2one(required=False)
  9. lead_tomerge_ids = fields.Many2many(
  10. 'crm.lead', 'crm_convert_lead_mass_lead_rel',
  11. string='Active Leads', context={'active_test': False},
  12. default=lambda self: self.env.context.get('active_ids', []),
  13. )
  14. user_ids = fields.Many2many('res.users', string='Salespersons')
  15. deduplicate = fields.Boolean('Apply deduplication', default=True, help='Merge with existing leads/opportunities of each partner')
  16. action = fields.Selection(selection_add=[
  17. ('each_exist_or_create', 'Use existing partner or create'),
  18. ], string='Related Customer', ondelete={
  19. 'each_exist_or_create': lambda recs: recs.write({'action': 'exist'}),
  20. })
  21. force_assignment = fields.Boolean(default=False)
  22. @api.depends('duplicated_lead_ids')
  23. def _compute_name(self):
  24. for convert in self:
  25. convert.name = 'convert'
  26. @api.depends('lead_tomerge_ids')
  27. def _compute_action(self):
  28. for convert in self:
  29. convert.action = 'each_exist_or_create'
  30. @api.depends('lead_tomerge_ids')
  31. def _compute_partner_id(self):
  32. for convert in self:
  33. convert.partner_id = False
  34. @api.depends('user_ids')
  35. def _compute_team_id(self):
  36. """ When changing the user, also set a team_id or restrict team id
  37. to the ones user_id is member of. """
  38. for convert in self:
  39. # setting user as void should not trigger a new team computation
  40. if not convert.user_id and not convert.user_ids and convert.team_id:
  41. continue
  42. user = convert.user_id or convert.user_ids and convert.user_ids[0] or self.env.user
  43. if convert.team_id and user in convert.team_id.member_ids | convert.team_id.user_id:
  44. continue
  45. team = self.env['crm.team']._get_default_team_id(user_id=user.id, domain=None)
  46. convert.team_id = team.id
  47. @api.depends('lead_tomerge_ids')
  48. def _compute_duplicated_lead_ids(self):
  49. for convert in self:
  50. duplicated = self.env['crm.lead']
  51. for lead in convert.lead_tomerge_ids:
  52. duplicated_leads = self.env['crm.lead']._get_lead_duplicates(
  53. partner=lead.partner_id,
  54. email=lead.partner_id and lead.partner_id.email or lead.email_from,
  55. include_lost=False)
  56. if len(duplicated_leads) > 1:
  57. duplicated += lead
  58. convert.duplicated_lead_ids = duplicated.ids
  59. def _convert_and_allocate(self, leads, user_ids, team_id=False):
  60. """ When "massively" (more than one at a time) converting leads to
  61. opportunities, check the salesteam_id and salesmen_ids and update
  62. the values before calling super.
  63. """
  64. self.ensure_one()
  65. salesmen_ids = []
  66. if self.user_ids:
  67. salesmen_ids = self.user_ids.ids
  68. return super(Lead2OpportunityMassConvert, self)._convert_and_allocate(leads, salesmen_ids, team_id=team_id)
  69. def action_mass_convert(self):
  70. self.ensure_one()
  71. if self.name == 'convert' and self.deduplicate:
  72. # TDE CLEANME: still using active_ids from context
  73. active_ids = self._context.get('active_ids', [])
  74. merged_lead_ids = set()
  75. remaining_lead_ids = set()
  76. for lead in self.lead_tomerge_ids:
  77. if lead not in merged_lead_ids:
  78. duplicated_leads = self.env['crm.lead']._get_lead_duplicates(
  79. partner=lead.partner_id,
  80. email=lead.partner_id.email or lead.email_from,
  81. include_lost=False
  82. )
  83. if len(duplicated_leads) > 1:
  84. lead = duplicated_leads.merge_opportunity()
  85. merged_lead_ids.update(duplicated_leads.ids)
  86. remaining_lead_ids.add(lead.id)
  87. # rebuild list of lead IDS to convert, following given order
  88. final_ids = [lead_id for lead_id in active_ids if lead_id not in merged_lead_ids]
  89. final_ids += [lead_id for lead_id in remaining_lead_ids if lead_id not in final_ids]
  90. self = self.with_context(active_ids=final_ids) # only update active_ids when there are set
  91. return self.action_apply()
  92. def _convert_handle_partner(self, lead, action, partner_id):
  93. if self.action == 'each_exist_or_create':
  94. partner_id = lead._find_matching_partner(email_only=True).id
  95. action = 'create'
  96. return super(Lead2OpportunityMassConvert, self)._convert_handle_partner(lead, action, partner_id)