event_registration.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from collections import defaultdict
  4. from markupsafe import Markup
  5. from odoo import api, fields, models, tools, _
  6. from odoo.addons.phone_validation.tools import phone_validation
  7. class EventRegistration(models.Model):
  8. _inherit = 'event.registration'
  9. lead_ids = fields.Many2many(
  10. 'crm.lead', string='Leads', copy=False, readonly=True,
  11. groups='sales_team.group_sale_salesman')
  12. lead_count = fields.Integer(
  13. '# Leads', compute='_compute_lead_count', compute_sudo=True)
  14. @api.depends('lead_ids')
  15. def _compute_lead_count(self):
  16. for record in self:
  17. record.lead_count = len(record.lead_ids)
  18. @api.model_create_multi
  19. def create(self, vals_list):
  20. """ Trigger rules based on registration creation, and check state for
  21. rules based on confirmed / done attendees. """
  22. registrations = super(EventRegistration, self).create(vals_list)
  23. # handle triggers based on creation, then those based on confirm and done
  24. # as registrations can be automatically confirmed, or even created directly
  25. # with a state given in values
  26. if not self.env.context.get('event_lead_rule_skip'):
  27. self.env['event.lead.rule'].search([('lead_creation_trigger', '=', 'create')]).sudo()._run_on_registrations(registrations)
  28. open_registrations = registrations.filtered(lambda reg: reg.state == 'open')
  29. if open_registrations:
  30. self.env['event.lead.rule'].search([('lead_creation_trigger', '=', 'confirm')]).sudo()._run_on_registrations(open_registrations)
  31. done_registrations = registrations.filtered(lambda reg: reg.state == 'done')
  32. if done_registrations:
  33. self.env['event.lead.rule'].search([('lead_creation_trigger', '=', 'done')]).sudo()._run_on_registrations(done_registrations)
  34. return registrations
  35. def write(self, vals):
  36. """ Update the lead values depending on fields updated in registrations.
  37. There are 2 main use cases
  38. * first is when we update the partner_id of multiple registrations. It
  39. happens when a public user fill its information when they register to
  40. an event;
  41. * second is when we update specific values of one registration like
  42. updating question answers or a contact information (email, phone);
  43. Also trigger rules based on confirmed and done attendees (state written
  44. to open and done).
  45. """
  46. to_update, event_lead_rule_skip = False, self.env.context.get('event_lead_rule_skip')
  47. if not event_lead_rule_skip:
  48. to_update = self.filtered(lambda reg: reg.lead_count)
  49. if to_update:
  50. lead_tracked_vals = to_update._get_lead_tracked_values()
  51. res = super(EventRegistration, self).write(vals)
  52. if not event_lead_rule_skip and to_update:
  53. self.env.flush_all() # compute notably partner-based fields if necessary
  54. to_update.sudo()._update_leads(vals, lead_tracked_vals)
  55. # handle triggers based on state
  56. if not event_lead_rule_skip:
  57. if vals.get('state') == 'open':
  58. self.env['event.lead.rule'].search([('lead_creation_trigger', '=', 'confirm')]).sudo()._run_on_registrations(self)
  59. elif vals.get('state') == 'done':
  60. self.env['event.lead.rule'].search([('lead_creation_trigger', '=', 'done')]).sudo()._run_on_registrations(self)
  61. return res
  62. def _load_records_create(self, values):
  63. """ In import mode: do not run rules those are intended to run when customers
  64. buy tickets, not when bootstrapping a database. """
  65. return super(EventRegistration, self.with_context(event_lead_rule_skip=True))._load_records_create(values)
  66. def _load_records_write(self, values):
  67. """ In import mode: do not run rules those are intended to run when customers
  68. buy tickets, not when bootstrapping a database. """
  69. return super(EventRegistration, self.with_context(event_lead_rule_skip=True))._load_records_write(values)
  70. def _update_leads(self, new_vals, lead_tracked_vals):
  71. """ Update leads linked to some registrations. Update is based depending
  72. on updated fields, see ``_get_lead_contact_fields()`` and ``_get_lead_
  73. description_fields()``. Main heuristic is
  74. * check attendee-based leads, for each registration recompute contact
  75. information if necessary (changing partner triggers the whole contact
  76. computation); update description if necessary;
  77. * check order-based leads, for each existing group-based lead, only
  78. partner change triggers a contact and description update. We consider
  79. that group-based rule works mainly with the main contact and less
  80. with further details of registrations. Those can be found in stat
  81. button if necessary.
  82. :param new_vals: values given to write. Used to determine updated fields;
  83. :param lead_tracked_vals: dict(registration_id, registration previous values)
  84. based on new_vals;
  85. """
  86. for registration in self:
  87. leads_attendee = registration.lead_ids.filtered(
  88. lambda lead: lead.event_lead_rule_id.lead_creation_basis == 'attendee'
  89. )
  90. if not leads_attendee:
  91. continue
  92. old_vals = lead_tracked_vals[registration.id]
  93. # if partner has been updated -> update registration contact information
  94. # as they are computed (and therefore not given to write values)
  95. if 'partner_id' in new_vals:
  96. new_vals.update(**dict(
  97. (field, registration[field])
  98. for field in self._get_lead_contact_fields()
  99. if field != 'partner_id')
  100. )
  101. lead_values = {}
  102. # update contact fields: valid for all leads of registration
  103. upd_contact_fields = [field for field in self._get_lead_contact_fields() if field in new_vals.keys()]
  104. if any(new_vals[field] != old_vals[field] for field in upd_contact_fields):
  105. lead_values = registration._get_lead_contact_values()
  106. # update description fields: each lead has to be updated, otherwise
  107. # update in batch
  108. upd_description_fields = [field for field in self._get_lead_description_fields() if field in new_vals.keys()]
  109. if any(new_vals[field] != old_vals[field] for field in upd_description_fields):
  110. for lead in leads_attendee:
  111. lead_values['description'] = "%s<br/>%s" % (
  112. lead.description,
  113. registration._get_lead_description(_("Updated registrations"), line_counter=True)
  114. )
  115. lead.write(lead_values)
  116. elif lead_values:
  117. leads_attendee.write(lead_values)
  118. leads_order = self.lead_ids.filtered(lambda lead: lead.event_lead_rule_id.lead_creation_basis == 'order')
  119. for lead in leads_order:
  120. lead_values = {}
  121. if new_vals.get('partner_id'):
  122. lead_values.update(lead.registration_ids._get_lead_contact_values())
  123. if not lead.partner_id:
  124. lead_values['description'] = lead.registration_ids._get_lead_description(_("Participants"), line_counter=True)
  125. elif new_vals['partner_id'] != lead.partner_id.id:
  126. lead_values['description'] = lead.description + "<br/>" + lead.registration_ids._get_lead_description(_("Updated registrations"), line_counter=True, line_suffix=_("(updated)"))
  127. if lead_values:
  128. lead.write(lead_values)
  129. def _get_lead_values(self, rule):
  130. """ Get lead values from registrations. Self can contain multiple records
  131. in which case first found non void value is taken. Note that all
  132. registrations should belong to the same event.
  133. :return dict lead_values: values used for create / write on a lead
  134. """
  135. lead_values = {
  136. # from rule
  137. 'type': rule.lead_type,
  138. 'user_id': rule.lead_user_id.id,
  139. 'team_id': rule.lead_sales_team_id.id,
  140. 'tag_ids': rule.lead_tag_ids.ids,
  141. 'event_lead_rule_id': rule.id,
  142. # event and registration
  143. 'event_id': self.event_id.id,
  144. 'referred': self.event_id.name,
  145. 'registration_ids': self.ids,
  146. 'campaign_id': self._find_first_notnull('utm_campaign_id'),
  147. 'source_id': self._find_first_notnull('utm_source_id'),
  148. 'medium_id': self._find_first_notnull('utm_medium_id'),
  149. }
  150. lead_values.update(self._get_lead_contact_values())
  151. lead_values['description'] = self._get_lead_description(_("Participants"), line_counter=True)
  152. return lead_values
  153. def _get_lead_contact_values(self):
  154. """ Specific management of contact values. Rule creation basis has some
  155. effect on contact management
  156. * in attendee mode: keep registration partner only if partner phone and
  157. email match. Indeed lead are synchronized with their contact and it
  158. would imply rewriting on partner, and therefore on other documents;
  159. * in batch mode: if a customer is found use it as main contact. Registrations
  160. details are included in lead description;
  161. :return dict: values used for create / write on a lead
  162. """
  163. valid_partner = next(
  164. (reg.partner_id for reg in self if reg.partner_id != self.env.ref('base.public_partner')),
  165. self.env['res.partner']
  166. ) # CHECKME: broader than just public partner
  167. # mono registration mode: keep partner only if email and phone matches;
  168. # otherwise registration > partner. Note that email format and phone
  169. # formatting have to taken into account in comparison
  170. if len(self) == 1 and valid_partner:
  171. # compare emails: email_normalized or raw
  172. if self.email and valid_partner.email:
  173. if valid_partner.email_normalized and tools.email_normalize(self.email) != valid_partner.email_normalized:
  174. valid_partner = self.env['res.partner']
  175. elif not valid_partner.email_normalized and valid_partner.email != self.email:
  176. valid_partner = self.env['res.partner']
  177. # compare phone, taking into account formatting
  178. if valid_partner and self.phone and valid_partner.phone:
  179. phone_formatted = phone_validation.phone_format(
  180. self.phone,
  181. valid_partner.country_id.code or None,
  182. valid_partner.country_id.phone_code or None,
  183. force_format='E164',
  184. raise_exception=False
  185. )
  186. partner_phone_formatted = valid_partner._phone_format(valid_partner.phone)
  187. if phone_formatted and partner_phone_formatted and phone_formatted != partner_phone_formatted:
  188. valid_partner = self.env['res.partner']
  189. if (not phone_formatted or not partner_phone_formatted) and self.phone != valid_partner.phone:
  190. valid_partner = self.env['res.partner']
  191. if valid_partner:
  192. contact_vals = self.env['crm.lead']._prepare_values_from_partner(valid_partner)
  193. # force email_from / phone only if not set on partner because those fields are now synchronized automatically
  194. if not valid_partner.email:
  195. contact_vals['email_from'] = self._find_first_notnull('email')
  196. if not valid_partner.phone:
  197. contact_vals['phone'] = self._find_first_notnull('phone')
  198. else:
  199. # don't force email_from + partner_id because those fields are now synchronized automatically
  200. contact_vals = {
  201. 'contact_name': self._find_first_notnull('name'),
  202. 'email_from': self._find_first_notnull('email'),
  203. 'phone': self._find_first_notnull('phone'),
  204. 'lang_id': False,
  205. }
  206. contact_vals.update({
  207. 'name': "%s - %s" % (self.event_id.name, valid_partner.name or self._find_first_notnull('name') or self._find_first_notnull('email')),
  208. 'partner_id': valid_partner.id,
  209. 'mobile': valid_partner.mobile or self._find_first_notnull('mobile'),
  210. })
  211. return contact_vals
  212. def _get_lead_description(self, prefix='', line_counter=True, line_suffix=''):
  213. """ Build the description for the lead using a prefix for all generated
  214. lines. For example to enumerate participants or inform of an update in
  215. the information of a participant.
  216. :return string description: complete description for a lead taking into
  217. account all registrations contained in self
  218. """
  219. reg_lines = [
  220. registration._get_lead_description_registration(
  221. line_suffix=line_suffix
  222. ) for registration in self
  223. ]
  224. description = (prefix if prefix else '') + Markup("<br/>")
  225. if line_counter:
  226. description += Markup("<ol>") + Markup('').join(reg_lines) + Markup("</ol>")
  227. else:
  228. description += Markup("<ul>") + Markup('').join(reg_lines) + Markup("</ul>")
  229. return description
  230. def _get_lead_description_registration(self, line_suffix=''):
  231. """ Build the description line specific to a given registration. """
  232. self.ensure_one()
  233. return Markup("<li>") + "%s (%s)%s" % (
  234. self.name or self.partner_id.name or self.email,
  235. " - ".join(self[field] for field in ('email', 'phone') if self[field]),
  236. f" {line_suffix}" if line_suffix else "",
  237. ) + Markup("</li>")
  238. def _get_lead_tracked_values(self):
  239. """ Tracked values are based on two subset of fields to track in order
  240. to fill or update leads. Two main use cases are
  241. * description fields: registration contact fields: email, phone, ...
  242. on registration. Other fields are added by inheritance like
  243. question answers;
  244. * contact fields: registration contact fields + partner_id field as
  245. contact of a lead is managed specifically. Indeed email and phone
  246. synchronization of lead / partner_id implies paying attention to
  247. not rewrite partner values from registration values.
  248. Tracked values are therefore the union of those two field sets. """
  249. tracked_fields = list(set(self._get_lead_contact_fields()) or set(self._get_lead_description_fields()))
  250. return dict(
  251. (registration.id,
  252. dict((field, self._convert_value(registration[field], field)) for field in tracked_fields)
  253. ) for registration in self
  254. )
  255. def _get_lead_grouping(self, rules, rule_to_new_regs):
  256. """ Perform grouping of registrations in order to enable order-based
  257. lead creation and update existing groups with new registrations.
  258. Heuristic in event is the following. Registrations created in multi-mode
  259. are grouped by event. Customer use case: website_event flow creates
  260. several registrations in a create-multi.
  261. Update is not supported as there is no way to determine if a registration
  262. is part of an existing batch.
  263. :param rules: lead creation rules to run on registrations given by self;
  264. :param rule_to_new_regs: dict: for each rule, subset of self matching
  265. rule conditions. Used to speedup batch computation;
  266. :return dict: for each rule, rule (key of dict) gives a list of groups.
  267. Each group is a tuple (
  268. existing_lead: existing lead to update;
  269. group_record: record used to group;
  270. registrations: sub record set of self, containing registrations
  271. belonging to the same group;
  272. )
  273. """
  274. event_to_reg_ids = defaultdict(lambda: self.env['event.registration'])
  275. for registration in self:
  276. event_to_reg_ids[registration.event_id] += registration
  277. return dict(
  278. (rule, [(False, event, (registrations & rule_to_new_regs[rule]).sorted('id'))
  279. for event, registrations in event_to_reg_ids.items()])
  280. for rule in rules
  281. )
  282. # ------------------------------------------------------------
  283. # TOOLS
  284. # ------------------------------------------------------------
  285. @api.model
  286. def _get_lead_contact_fields(self):
  287. """ Get registration fields linked to lead contact. Those are used notably
  288. to see if an update of lead is necessary or to fill contact values
  289. in ``_get_lead_contact_values())`` """
  290. return ['name', 'email', 'phone', 'mobile', 'partner_id']
  291. @api.model
  292. def _get_lead_description_fields(self):
  293. """ Get registration fields linked to lead description. Those are used
  294. notablyto see if an update of lead is necessary or to fill description
  295. in ``_get_lead_description())`` """
  296. return ['name', 'email', 'phone']
  297. def _find_first_notnull(self, field_name):
  298. """ Small tool to extract the first not nullvalue of a field: its value
  299. or the ids if this is a relational field. """
  300. value = next((reg[field_name] for reg in self if reg[field_name]), False)
  301. return self._convert_value(value, field_name)
  302. def _convert_value(self, value, field_name):
  303. """ Small tool because convert_to_write is touchy """
  304. if isinstance(value, models.BaseModel) and self._fields[field_name].type in ['many2many', 'one2many']:
  305. return value.ids
  306. if isinstance(value, models.BaseModel) and self._fields[field_name].type == 'many2one':
  307. return value.id
  308. return value