Merge commit '0f83da289bc3e63db70f25b68707d2f1f4d46311' into 15.0
This commit is contained in:
commit
bb191fbcc0
5 changed files with 78 additions and 0 deletions
1
bemade_partner_email_domain/__init__.py
Normal file
1
bemade_partner_email_domain/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from . import models
|
||||
20
bemade_partner_email_domain/__manifest__.py
Normal file
20
bemade_partner_email_domain/__manifest__.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
'name': 'Partner Email Domain Auto-Set',
|
||||
'version': '15.0.0.0.0',
|
||||
'category': 'Extra Tools',
|
||||
'summary': 'Allow to autolink partner based on email domain',
|
||||
'description': """
|
||||
This module allows users to automatically link a partner to a company based on the email domain.
|
||||
""",
|
||||
'depends': [
|
||||
'base'
|
||||
],
|
||||
'data': [
|
||||
'views/res_partner_views.xml'
|
||||
],
|
||||
'license': 'AGPL-3',
|
||||
'author': 'Bemade',
|
||||
'website': 'https://bemade.org/',
|
||||
'installable': True,
|
||||
'auto_install': False
|
||||
}
|
||||
1
bemade_partner_email_domain/models/__init__.py
Normal file
1
bemade_partner_email_domain/models/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from . import res_partner
|
||||
42
bemade_partner_email_domain/models/res_partner.py
Normal file
42
bemade_partner_email_domain/models/res_partner.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from odoo import api, fields, models, Command
|
||||
|
||||
class Partner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
email_domain = fields.Char(string='Email Domain')
|
||||
|
||||
@api.onchange('email')
|
||||
def _check_parent_from_email_domain(self):
|
||||
for rec in self:
|
||||
try:
|
||||
# Split the email address on '@' and get the domain part
|
||||
if rec.email:
|
||||
email_domain = rec.email.split('@')[1].strip()
|
||||
else:
|
||||
continue
|
||||
except IndexError:
|
||||
# If there's no '@' symbol, the email address is invalid
|
||||
return False
|
||||
|
||||
# Loop while there's more than one part in the domain (e.g., subdomain.domain.tld)
|
||||
while '.' in email_domain:
|
||||
# If the current email domain matches the main domain, return True
|
||||
company_domain = self.env['res.partner'].search([('email_domain', 'ilike', email_domain)])
|
||||
if company_domain:
|
||||
rec.parent_id = company_domain.id
|
||||
return
|
||||
|
||||
# If not, drop the part before the first '.' to check the next level
|
||||
email_domain = email_domain.split('.', 1)[1]
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
res = super(Partner, self).create(vals_list)
|
||||
res._check_parent_from_email_domain()
|
||||
return res
|
||||
|
||||
def write(self, vals):
|
||||
res = super(Partner, self).write(vals)
|
||||
if 'email' in vals:
|
||||
self._check_parent_from_email_domain()
|
||||
return res
|
||||
14
bemade_partner_email_domain/views/res_partner_views.xml
Normal file
14
bemade_partner_email_domain/views/res_partner_views.xml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0"?>
|
||||
<odoo>
|
||||
<record id="partner_email_domain_form" model="ir.ui.view">
|
||||
<field name="name">bemade_partner_email_domain.partner.email.domain.form</field>
|
||||
<field name="model">res.partner</field>
|
||||
<field name="inherit_id" ref="base.view_partner_form"/>
|
||||
<field name="arch" type="xml">
|
||||
<field name="email" position="after">
|
||||
<label for="email_domain" string="Domain"/>
|
||||
<field name="email_domain"/>
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
||||
Loading…
Reference in a new issue