You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.5 KiB

  1. # Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  2. # Copyright 2015 Antonio Espinosa <antonio.espinosa@tecnativa.com>
  3. # Copyright 2015 Javier Iniesta <javieria@antiun.com>
  4. # Copyright 2017 David Vidal <david.vidal@tecnativa.com>
  5. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  6. from odoo import _, api, fields, models
  7. from odoo.exceptions import ValidationError
  8. class ResPartner(models.Model):
  9. _inherit = 'res.partner'
  10. mass_mailing_contact_ids = fields.One2many(
  11. string="Mailing contacts",
  12. oldname="mass_mailing_contacts",
  13. domain=[('opt_out', '=', False)],
  14. comodel_name='mail.mass_mailing.contact', inverse_name='partner_id')
  15. mass_mailing_contacts_count = fields.Integer(
  16. string='Mailing contacts number',
  17. compute='_compute_mass_mailing_contacts_count', store=True,
  18. compute_sudo=True)
  19. mass_mailing_stats_ids = fields.One2many(
  20. string="Mass mailing stats",
  21. comodel_name='mail.mail.statistics', inverse_name='partner_id')
  22. mass_mailing_stats_count = fields.Integer(
  23. string='Mass mailing stats number',
  24. compute='_compute_mass_mailing_stats_count', store=True)
  25. @api.constrains('email')
  26. def _check_email_mass_mailing_contacts(self):
  27. for partner in self:
  28. if not partner.email and self.sudo().mass_mailing_contact_ids:
  29. raise ValidationError(_(
  30. "This partner '%s' is linked to one or more mass "
  31. "mailing contact. Email must be assigned."
  32. ) % partner.name)
  33. @api.depends('mass_mailing_contact_ids',
  34. 'mass_mailing_contact_ids.opt_out')
  35. def _compute_mass_mailing_contacts_count(self):
  36. contact_data = self.env['mail.mass_mailing.contact'].read_group(
  37. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  38. mapped_data = dict(
  39. [(contact['partner_id'][0], contact['partner_id_count'])
  40. for contact in contact_data])
  41. for partner in self:
  42. partner.mass_mailing_contacts_count = mapped_data.get(partner.id,
  43. 0)
  44. @api.depends('mass_mailing_stats_ids')
  45. def _compute_mass_mailing_stats_count(self):
  46. contact_data = self.env['mail.mail.statistics'].read_group(
  47. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  48. mapped_data = dict(
  49. [(contact['partner_id'][0], contact['partner_id_count'])
  50. for contact in contact_data])
  51. for partner in self:
  52. partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
  53. def write(self, vals):
  54. res = super(ResPartner, self).write(vals)
  55. mm_vals = {}
  56. if vals.get('name'):
  57. mm_vals['name'] = vals['name']
  58. if vals.get('email'):
  59. mm_vals['email'] = vals['email']
  60. if vals.get('title'):
  61. mm_vals['title_id'] = vals['title']
  62. if vals.get('company_id'):
  63. company = self.env['res.company'].browse(vals.get('company_id'))
  64. mm_vals['company_name'] = company.name
  65. if vals.get('country_id'):
  66. mm_vals['country_id'] = vals['country_id']
  67. if vals.get('category_id'):
  68. mm_vals['tag_ids'] = vals['category_id']
  69. if mm_vals:
  70. # Using sudo because ACLs shouldn't produce data inconsistency
  71. self.env["mail.mass_mailing.contact"].sudo().search([
  72. ("partner_id", "in", self.ids),
  73. ]).write(mm_vals)
  74. return res