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
|
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
|
|
|
|
from odoo.exceptions import ValidationError
|
2015-08-28 11:15:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
2020-02-10 11:35:23 +01:00
|
|
|
_inherit = "res.partner"
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2016-09-12 08:43:32 +02:00
|
|
|
mass_mailing_contact_ids = fields.One2many(
|
2018-05-17 22:00:43 +02:00
|
|
|
string="Mailing contacts",
|
2020-02-10 11:40:11 +01:00
|
|
|
comodel_name="mailing.contact",
|
2020-02-10 11:35:23 +01:00
|
|
|
inverse_name="partner_id",
|
|
|
|
)
|
2015-08-28 11:15:50 +02:00
|
|
|
mass_mailing_contacts_count = fields.Integer(
|
2020-02-10 11:35:23 +01:00
|
|
|
string="Mailing contacts number",
|
|
|
|
compute="_compute_mass_mailing_contacts_count",
|
2018-11-18 00:37:27 +01:00
|
|
|
store=True,
|
2020-02-10 11:35:23 +01:00
|
|
|
compute_sudo=True,
|
|
|
|
)
|
2018-05-17 22:00:43 +02:00
|
|
|
mass_mailing_stats_ids = fields.One2many(
|
2016-08-08 13:14:37 +02:00
|
|
|
string="Mass mailing stats",
|
2020-02-10 11:40:11 +01:00
|
|
|
comodel_name="mailing.trace",
|
2020-02-10 11:35:23 +01:00
|
|
|
inverse_name="partner_id",
|
|
|
|
)
|
2016-08-08 13:14:37 +02:00
|
|
|
mass_mailing_stats_count = fields.Integer(
|
2020-02-10 11:35:23 +01:00
|
|
|
string="Mass mailing stats number",
|
|
|
|
compute="_compute_mass_mailing_stats_count",
|
|
|
|
store=True,
|
|
|
|
)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
@api.constrains("email")
|
2015-08-28 11:15:50 +02:00
|
|
|
def _check_email_mass_mailing_contacts(self):
|
2017-06-14 20:47:41 +02:00
|
|
|
for partner in self:
|
2019-11-13 09:32:28 +01:00
|
|
|
if not partner.email and partner.sudo().mass_mailing_contact_ids:
|
2020-02-10 11:35:23 +01:00
|
|
|
raise ValidationError(
|
|
|
|
_(
|
|
|
|
"This partner '%s' is linked to one or more mass "
|
|
|
|
"mailing contact. Email must be assigned."
|
|
|
|
)
|
|
|
|
% partner.name
|
|
|
|
)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
@api.depends("mass_mailing_contact_ids")
|
2016-08-08 13:14:37 +02:00
|
|
|
def _compute_mass_mailing_contacts_count(self):
|
2020-02-10 11:40:11 +01:00
|
|
|
contact_data = self.env["mailing.contact"].read_group(
|
2020-02-10 11:35:23 +01:00
|
|
|
[("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
|
|
|
|
)
|
|
|
|
mapped_data = {
|
2020-02-10 11:40:11 +01:00
|
|
|
contact["partner_id"][0]: contact["partner_id_count"]
|
|
|
|
for contact in contact_data
|
2020-02-10 11:35:23 +01:00
|
|
|
}
|
2018-05-14 16:18:11 +02:00
|
|
|
for partner in self:
|
2020-02-10 11:35:23 +01:00
|
|
|
partner.mass_mailing_contacts_count = mapped_data.get(partner.id, 0)
|
2015-08-28 11:15:50 +02:00
|
|
|
|
2020-02-10 11:35:23 +01:00
|
|
|
@api.depends("mass_mailing_stats_ids")
|
2016-08-08 13:14:37 +02:00
|
|
|
def _compute_mass_mailing_stats_count(self):
|
2020-02-10 11:40:11 +01:00
|
|
|
contact_data = self.env["mailing.trace"].read_group(
|
2020-02-10 11:35:23 +01:00
|
|
|
[("partner_id", "in", self.ids)], ["partner_id"], ["partner_id"]
|
|
|
|
)
|
|
|
|
mapped_data = {
|
2020-02-10 11:40:11 +01:00
|
|
|
contact["partner_id"][0]: contact["partner_id_count"]
|
|
|
|
for contact in contact_data
|
2020-02-10 11:35:23 +01:00
|
|
|
}
|
2018-05-14 16:18:11 +02:00
|
|
|
for partner in self:
|
|
|
|
partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
|
2016-08-08 13:14:37 +02:00
|
|
|
|
2015-08-28 11:15:50 +02:00
|
|
|
def write(self, vals):
|
|
|
|
res = super(ResPartner, self).write(vals)
|
2018-05-17 22:00:43 +02:00
|
|
|
mm_vals = {}
|
2020-02-10 11:35:23 +01:00
|
|
|
if vals.get("name"):
|
|
|
|
mm_vals["name"] = vals["name"]
|
|
|
|
if vals.get("email"):
|
|
|
|
mm_vals["email"] = vals["email"]
|
|
|
|
if vals.get("title"):
|
|
|
|
mm_vals["title_id"] = vals["title"]
|
|
|
|
if vals.get("company_id"):
|
|
|
|
company = self.env["res.company"].browse(vals.get("company_id"))
|
|
|
|
mm_vals["company_name"] = company.name
|
|
|
|
if vals.get("country_id"):
|
|
|
|
mm_vals["country_id"] = vals["country_id"]
|
|
|
|
if vals.get("category_id"):
|
|
|
|
mm_vals["tag_ids"] = vals["category_id"]
|
2018-05-17 22:00:43 +02:00
|
|
|
if mm_vals:
|
2018-01-16 11:05:09 +01:00
|
|
|
# Using sudo because ACLs shouldn't produce data inconsistency
|
2020-02-10 11:40:11 +01:00
|
|
|
self.env["mailing.contact"].sudo().search(
|
2020-02-10 11:35:23 +01:00
|
|
|
[("partner_id", "in", self.ids)]
|
|
|
|
).write(mm_vals)
|
2015-08-28 11:15:50 +02:00
|
|
|
return res
|