2017-06-14 20:47:41 +02:00
|
|
|
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
|
|
|
|
# Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
|
|
|
|
# Copyright 2015 Javier Iniesta <javieria@antiun.com>
|
|
|
|
# Copyright 2017 David Vidal <david.vidal@tecnativa.com>
|
2020-02-10 11:40:11 +01:00
|
|
|
# Copyright 2020 Tecnativa - Manuel Calero
|
2020-03-18 15:29:18 +01:00
|
|
|
# Copyright 2020 Hibou Corp.
|
2016-08-08 13:14:37 +02:00
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2017-06-14 20:47:41 +02:00
|
|
|
from odoo import _, api, fields, models
|
2018-05-17 22:00:43 +02:00
|
|
|
from odoo.exceptions import ValidationError
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
|
2020-02-10 11:40:11 +01:00
|
|
|
class MailingContact(models.Model):
|
|
|
|
_inherit = "mailing.contact"
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
partner_id = fields.Many2one(
|
|
|
|
comodel_name="res.partner", string="Partner", domain=[("email", "!=", False)]
|
|
|
|
)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
@api.constrains("partner_id", "list_ids")
|
2018-05-17 22:00:43 +02:00
|
|
|
def _check_partner_id_list_ids(self):
|
|
|
|
for contact in self:
|
|
|
|
if contact.partner_id:
|
2020-02-10 11:35:23 +01:00
|
|
|
other_contact = self.search(
|
|
|
|
[
|
|
|
|
("partner_id", "=", contact.partner_id.id),
|
|
|
|
("id", "!=", contact.id),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
if contact.list_ids & other_contact.mapped("list_ids"):
|
|
|
|
raise ValidationError(
|
|
|
|
_("Partner already exists in one of " "these mailing lists")
|
|
|
|
)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
@api.onchange("partner_id")
|
2018-05-17 22:00:43 +02:00
|
|
|
def _onchange_partner_mass_mailing_partner(self):
|
2015-08-28 11:15:50 +02:00
|
|
|
if self.partner_id:
|
|
|
|
self.name = self.partner_id.name
|
|
|
|
self.email = self.partner_id.email
|
2018-05-17 22:00:43 +02:00
|
|
|
self.title_id = self.partner_id.title
|
|
|
|
self.company_name = self.partner_id.company_id.name
|
|
|
|
self.country_id = self.partner_id.country_id
|
|
|
|
category_ids = self.partner_id.category_id
|
|
|
|
if category_ids:
|
|
|
|
self.tag_ids = category_ids
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
@api.model
|
|
|
|
def create(self, vals):
|
2018-05-17 22:00:43 +02:00
|
|
|
record = self.new(vals)
|
|
|
|
if not record.partner_id:
|
|
|
|
record._set_partner()
|
|
|
|
record._onchange_partner_mass_mailing_partner()
|
|
|
|
new_vals = record._convert_to_write(record._cache)
|
2018-11-09 13:24:39 +01:00
|
|
|
new_vals.update(
|
2020-03-18 15:29:18 +01:00
|
|
|
subscription_list_ids=vals.get("subscription_list_ids", []),
|
|
|
|
list_ids=vals.get("list_ids", []),
|
2018-11-09 13:24:39 +01:00
|
|
|
)
|
2020-02-10 11:40:11 +01:00
|
|
|
return super(MailingContact, self).create(new_vals)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
def write(self, vals):
|
2017-06-14 20:47:41 +02:00
|
|
|
for contact in self:
|
2018-05-17 22:00:43 +02:00
|
|
|
new_vals = contact.copy_data(vals)[0]
|
|
|
|
record = self.new(new_vals)
|
|
|
|
if not record.partner_id:
|
|
|
|
record._set_partner()
|
|
|
|
record._onchange_partner_mass_mailing_partner()
|
|
|
|
new_vals = record._convert_to_write(record._cache)
|
2018-11-09 13:24:39 +01:00
|
|
|
new_vals.update(
|
2020-03-18 15:29:18 +01:00
|
|
|
subscription_list_ids=vals.get("subscription_list_ids", []),
|
|
|
|
list_ids=vals.get("list_ids", []),
|
2018-11-09 13:24:39 +01:00
|
|
|
)
|
2020-02-10 11:40:11 +01:00
|
|
|
super(MailingContact, contact).write(new_vals)
|
2018-05-17 22:00:43 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _get_company(self):
|
|
|
|
company_id = False
|
|
|
|
if self.company_name:
|
2020-02-10 11:35:23 +01:00
|
|
|
company_id = (
|
|
|
|
self.env["res.company"].search([("name", "=", self.company_name)]).id
|
|
|
|
)
|
2018-05-17 22:00:43 +02:00
|
|
|
if not company_id:
|
2020-02-10 11:35:23 +01:00
|
|
|
company_id = (
|
|
|
|
self.env["res.company"].create({"name": self.company_name}).id
|
|
|
|
)
|
2018-05-17 22:00:43 +02:00
|
|
|
return company_id
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2018-05-17 22:00:43 +02:00
|
|
|
def _get_categories(self):
|
2020-02-10 11:35:23 +01:00
|
|
|
ca_ids = self.tag_ids.ids + self.list_ids.mapped("partner_category.id")
|
2018-05-17 22:00:43 +02:00
|
|
|
return [[6, 0, ca_ids]]
|
|
|
|
|
|
|
|
def _prepare_partner(self):
|
|
|
|
return {
|
2020-02-10 11:35:23 +01:00
|
|
|
"name": self.name or self.email,
|
|
|
|
"email": self.email,
|
|
|
|
"country_id": self.country_id.id,
|
|
|
|
"title": self.title_id.id,
|
|
|
|
"company_id": self._get_company(),
|
|
|
|
"category_id": self._get_categories(),
|
2015-08-28 11:15:50 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 22:00:43 +02:00
|
|
|
def _set_partner(self):
|
|
|
|
self.ensure_one()
|
2020-02-10 11:35:23 +01:00
|
|
|
m_partner = self.env["res.partner"]
|
2015-08-28 11:15:50 +02:00
|
|
|
# Look for a partner with that email
|
2018-05-17 22:00:43 +02:00
|
|
|
email = self.email.strip()
|
2020-02-10 11:35:23 +01:00
|
|
|
partner = m_partner.search([("email", "=ilike", email)], limit=1)
|
2018-05-17 22:00:43 +02:00
|
|
|
if partner:
|
2015-08-28 11:15:50 +02:00
|
|
|
# Partner found
|
2018-05-17 22:00:43 +02:00
|
|
|
self.partner_id = partner
|
2018-11-09 13:24:39 +01:00
|
|
|
else:
|
2020-02-10 11:35:23 +01:00
|
|
|
lts = self.subscription_list_ids.mapped("list_id") | self.list_ids
|
|
|
|
if lts.filtered("partner_mandatory"):
|
2018-11-09 13:24:39 +01:00
|
|
|
# Create partner
|
|
|
|
partner_vals = self._prepare_partner()
|
|
|
|
self.partner_id = m_partner.sudo().create(partner_vals)
|