Reduce friction submitting a CRM contact us form while linking the lead to the portal contact.This facilitates the flow generating leads from the contact us form. The partner_id of the lead created from form is set to the one of the logged visitor if model is crm.lead and there is a logged_visitor and if the prefilled email and phone number of form match the logged partner's (otherwise it would change the partner when setting it on lead In details : there must be an email, the same in input and in partner, after normalization. Only then: - If there are both a phone in values and a phone on partner, propagate partner on lead only if they are the same. - Else, that is if there is no phone in partner or no phone in values, propagate partner on lead. If no phone in input, it will not propagate. on the model. If no phone in partner, then if there is one in input, it will write that additional info on partner, completing its profile. Concerning phone comparison: extending current logic, the numbers are still formatted before comparison. That is: - Before this comit, the form value was formatted according to country in record or geoip. Now, if there is no country_id in record, uses the logged visitor partner's country or env company country, if there is a logged user. Finally, it uses session geoip as last option. (changes in _get_country()) - The partner's number is formatted through its model function _phone_format, according to the partner country (or env company). - Both formattings are done in international format. Adds tours to check that partner is linked to lead only if phone and email are not changed from prefilled data (from logged visitor's partner on form. Task-ID : 2241639 PR : #61126 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
99 lines
5.4 KiB
Python
99 lines
5.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import tools
|
|
from odoo.addons.phone_validation.tools import phone_validation
|
|
from odoo.addons.website_form.controllers import main
|
|
from odoo.http import request
|
|
|
|
|
|
class WebsiteForm(main.WebsiteForm):
|
|
|
|
def _get_country(self):
|
|
visitor_partner = request.env['website.visitor']._get_visitor_from_request().partner_id
|
|
if visitor_partner:
|
|
# match same behaviour as in partner._phone_format()
|
|
country = visitor_partner.country_id or request.env.company.country_id
|
|
if country:
|
|
return country
|
|
country_code = request.session.geoip and request.session.geoip.get('country_code') or False
|
|
if country_code:
|
|
return request.env['res.country'].sudo().search([('code', '=', country_code)], limit=1)
|
|
return request.env['res.country']
|
|
|
|
def _get_phone_fields_to_validate(self):
|
|
return ['phone', 'mobile']
|
|
|
|
# Check and insert values from the form on the model <model> + validation phone fields
|
|
def _handle_website_form(self, model_name, **kwargs):
|
|
model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), ('website_form_access', '=', True)])
|
|
if model_record and hasattr(request.env[model_name], '_phone_format') or hasattr(request.env[model_name], 'phone_get_sanitized_number'):
|
|
# filter on either custom _phone_format method, either phone_get_sanitized_number but directly
|
|
# call phone_format from phone validation herebelow to simplify things as we don't have real
|
|
# records but a dictionary of value at this point (record.phone_get_sanitized_number would
|
|
# not work)
|
|
try:
|
|
data = self.extract_data(model_record, request.params)
|
|
except:
|
|
# no specific management, super will do it
|
|
pass
|
|
else:
|
|
record = data.get('record', {})
|
|
phone_fields = self._get_phone_fields_to_validate()
|
|
country = request.env['res.country'].browse(record.get('country_id'))
|
|
contact_country = country if country.exists() else self._get_country()
|
|
for phone_field in phone_fields:
|
|
if not record.get(phone_field):
|
|
continue
|
|
number = record[phone_field]
|
|
fmt_number = phone_validation.phone_format(
|
|
number, contact_country.code if contact_country else None,
|
|
contact_country.phone_code if contact_country else None,
|
|
force_format='INTERNATIONAL',
|
|
raise_exception=False
|
|
)
|
|
request.params.update({phone_field: fmt_number})
|
|
|
|
if model_name == 'crm.lead' and not request.params.get('state_id'):
|
|
geoip_country_code = request.session.get('geoip', {}).get('country_code')
|
|
geoip_state_code = request.session.get('geoip', {}).get('region')
|
|
if geoip_country_code and geoip_state_code:
|
|
state = request.env['res.country.state'].search([('code', '=', geoip_state_code), ('country_id.code', '=', geoip_country_code)])
|
|
if state:
|
|
request.params['state_id'] = state.id
|
|
return super(WebsiteForm, self)._handle_website_form(model_name, **kwargs)
|
|
|
|
def insert_record(self, request, model, values, custom, meta=None):
|
|
is_lead_model = model.model == 'crm.lead'
|
|
if is_lead_model:
|
|
values_email_normalized = tools.email_normalize(values.get('email_from'))
|
|
visitor_sudo = request.env['website.visitor']._get_visitor_from_request()
|
|
visitor_partner = visitor_sudo.partner_id
|
|
if values_email_normalized and visitor_partner and visitor_partner.email_normalized == values_email_normalized:
|
|
# Here, 'phone' in values has already been formatted, see _handle_website_form.
|
|
values_phone = values.get('phone')
|
|
# We write partner id on crm only if no phone exists on partner or in input,
|
|
# or if both numbers (after formating) are the same. This way we get additional phone
|
|
# if possible, without modifying an existing one. (see inverse function on model crm.lead)
|
|
if values_phone and visitor_partner.phone:
|
|
if visitor_partner._phone_format(visitor_partner.phone) == values_phone:
|
|
values['partner_id'] = visitor_partner.id
|
|
else:
|
|
values['partner_id'] = visitor_partner.id
|
|
if 'company_id' not in values:
|
|
values['company_id'] = request.website.company_id.id
|
|
lang = request.context.get('lang', False)
|
|
lang_id = request.env["res.lang"].sudo().search([('code', '=', lang)], limit=1).id
|
|
values['lang_id'] = lang_id
|
|
|
|
result = super(WebsiteForm, self).insert_record(request, model, values, custom, meta=meta)
|
|
|
|
if is_lead_model and visitor_sudo and result:
|
|
lead_sudo = request.env['crm.lead'].browse(result).sudo()
|
|
if lead_sudo.exists():
|
|
vals = {'lead_ids': [(4, result)]}
|
|
if not visitor_sudo.lead_ids and not visitor_sudo.partner_id:
|
|
vals['name'] = lead_sudo.contact_name
|
|
visitor_sudo.write(vals)
|
|
return result
|