diff --git a/bemade_partner_email_domain/__init__.py b/bemade_partner_email_domain/__init__.py index 9a7e03e..f7209b1 100644 --- a/bemade_partner_email_domain/__init__.py +++ b/bemade_partner_email_domain/__init__.py @@ -1 +1,2 @@ -from . import models \ No newline at end of file +from . import models +from . import controllers diff --git a/bemade_partner_email_domain/__manifest__.py b/bemade_partner_email_domain/__manifest__.py index f2b36bb..9fe7c77 100644 --- a/bemade_partner_email_domain/__manifest__.py +++ b/bemade_partner_email_domain/__manifest__.py @@ -7,10 +7,12 @@ This module allows users to automatically link a partner to a company based on the email domain. """, 'depends': [ - 'base' + 'base', + 'mail' ], 'data': [ - 'views/res_partner_views.xml' + 'views/res_partner_views.xml', + 'data/mail_template.xml' ], 'license': 'AGPL-3', 'author': 'Bemade', diff --git a/bemade_partner_email_domain/controllers/__init__.py b/bemade_partner_email_domain/controllers/__init__.py new file mode 100644 index 0000000..deec4a8 --- /dev/null +++ b/bemade_partner_email_domain/controllers/__init__.py @@ -0,0 +1 @@ +from . import main \ No newline at end of file diff --git a/bemade_partner_email_domain/controllers/main.py b/bemade_partner_email_domain/controllers/main.py new file mode 100644 index 0000000..5ed7498 --- /dev/null +++ b/bemade_partner_email_domain/controllers/main.py @@ -0,0 +1,14 @@ +from odoo import http +from odoo.http import request + +class DivisionCompanyController(http.Controller): + + @http.route('/select_division_company', type='http', auth='public', methods=['GET']) + def select_division_company(self, partner_id, access_token, division_id, **kwargs): + partner = request.env['res.partner'].sudo().search([ + ('id', '=', int(partner_id)), + ('access_token', '=', access_token) + ]) + + if not partner: + return request.render('website.404') \ No newline at end of file diff --git a/bemade_partner_email_domain/data/mail_template.xml b/bemade_partner_email_domain/data/mail_template.xml new file mode 100644 index 0000000..cdf76da --- /dev/null +++ b/bemade_partner_email_domain/data/mail_template.xml @@ -0,0 +1,41 @@ + + + + + Division Selection: Choose Your Division + + {{object.company_id.email_formatted or user.email_formatted}} + {{object.id}} + Select Your Division at {{object.company_id.name}} + +
+

+ Hello Brandon Freeman, +

+

+ We noticed that you have registered with our company. To provide you with the best possible service, we need you to confirm the division of our company that you are working with. +

+

+ Please click on the link below to select your division: +

+

+ Choose Your Division +

+

+ If you have any questions or did not register with us, please disregard this email or contact us directly. +

+

+
Best regards, +

+

+
The team.
+
+ +

+
+
+ {{ object.lang }} + +
+
+
diff --git a/bemade_partner_email_domain/models/res_partner.py b/bemade_partner_email_domain/models/res_partner.py index e4391fc..3c07918 100644 --- a/bemade_partner_email_domain/models/res_partner.py +++ b/bemade_partner_email_domain/models/res_partner.py @@ -1,10 +1,34 @@ from odoo import api, fields, models, Command +from odoo.addons.website.controllers.main import Website + class Partner(models.Model): _inherit = 'res.partner' + domain_identifier = fields.Boolean(string='Domain identifier', default=False) email_domain = fields.Char(string='Email Domain') + is_subdivision = fields.Boolean(string='Subdivision', default=False) + access_token = fields.Char(string='Access Token') + + def _generate_access_token(self): + return uuid.uuid4().hex + + def _send_selection_email(self, partner, division_companies): + # Generate a token and save it to the partner + access_token = self._generate_access_token() + partner.write({'access_token': access_token}) + + # Now include the token in the links + base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') + links = { + company.id: f'{base_url}/select_division_company?partner_id={partner.id}&access_token={access_token}&division_id={company.id}' + for company in division_companies + } + + # Render the email template and send the email + template = self.env.ref('bemade_partner_email_domain.mail_template_selection_email') + template.with_context(links=links).send_mail(partner.id, force_send=True) @api.onchange('email') def _check_parent_from_email_domain(self): @@ -26,7 +50,19 @@ class Partner(models.Model): # 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 + if company_domain.domain_identifier: + division_company = self.env['res.partner'].search([('parent_id', '=', company_domain.id), + ('is_company', '=', True)]) + # need to send an email to the new partner asking him to select his parent in the liste + # of company under domain identifier + if division_company: + # Send an email to the partner with the division company selection link + if len(division_company) > 1: + self._send_selection_email(self, division_company) + else: + rec.parent_id = division_company[0].id + else: + rec.parent_id = company_domain.id return # If not, drop the part before the first '.' to check the next level diff --git a/bemade_partner_email_domain/views/res_partner_views.xml b/bemade_partner_email_domain/views/res_partner_views.xml index 42b676b..06bb562 100644 --- a/bemade_partner_email_domain/views/res_partner_views.xml +++ b/bemade_partner_email_domain/views/res_partner_views.xml @@ -5,9 +5,14 @@ res.partner - -