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.

83 lines
3.4 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. comodel_name='mail.mass_mailing.contact',
  13. inverse_name='partner_id')
  14. mass_mailing_contacts_count = fields.Integer(
  15. string='Mailing contacts number',
  16. compute='_compute_mass_mailing_contacts_count',
  17. store=True,
  18. compute_sudo=True)
  19. mass_mailing_stats_ids = fields.One2many(
  20. string="Mass mailing stats",
  21. comodel_name='mail.mail.statistics',
  22. inverse_name='partner_id')
  23. mass_mailing_stats_count = fields.Integer(
  24. string='Mass mailing stats number',
  25. compute='_compute_mass_mailing_stats_count',
  26. store=True)
  27. @api.constrains('email')
  28. def _check_email_mass_mailing_contacts(self):
  29. for partner in self:
  30. if not partner.email and self.sudo().mass_mailing_contact_ids:
  31. raise ValidationError(_(
  32. "This partner '%s' is linked to one or more mass "
  33. "mailing contact. Email must be assigned."
  34. ) % partner.name)
  35. @api.depends('mass_mailing_contact_ids')
  36. def _compute_mass_mailing_contacts_count(self):
  37. contact_data = self.env['mail.mass_mailing.contact'].read_group(
  38. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  39. mapped_data = dict(
  40. [(contact['partner_id'][0], contact['partner_id_count'])
  41. for contact in contact_data])
  42. for partner in self:
  43. partner.mass_mailing_contacts_count = mapped_data.get(partner.id,
  44. 0)
  45. @api.depends('mass_mailing_stats_ids')
  46. def _compute_mass_mailing_stats_count(self):
  47. contact_data = self.env['mail.mail.statistics'].read_group(
  48. [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id'])
  49. mapped_data = dict(
  50. [(contact['partner_id'][0], contact['partner_id_count'])
  51. for contact in contact_data])
  52. for partner in self:
  53. partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0)
  54. def write(self, vals):
  55. res = super(ResPartner, self).write(vals)
  56. mm_vals = {}
  57. if vals.get('name'):
  58. mm_vals['name'] = vals['name']
  59. if vals.get('email'):
  60. mm_vals['email'] = vals['email']
  61. if vals.get('title'):
  62. mm_vals['title_id'] = vals['title']
  63. if vals.get('company_id'):
  64. company = self.env['res.company'].browse(vals.get('company_id'))
  65. mm_vals['company_name'] = company.name
  66. if vals.get('country_id'):
  67. mm_vals['country_id'] = vals['country_id']
  68. if vals.get('category_id'):
  69. mm_vals['tag_ids'] = vals['category_id']
  70. if mm_vals:
  71. # Using sudo because ACLs shouldn't produce data inconsistency
  72. self.env["mail.mass_mailing.contact"].sudo().search([
  73. ("partner_id", "in", self.ids),
  74. ]).write(mm_vals)
  75. return res