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.

97 lines
3.7 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 MailMassMailingContact(models.Model):
  9. _inherit = 'mail.mass_mailing.contact'
  10. partner_id = fields.Many2one(comodel_name='res.partner', string="Partner",
  11. domain=[('email', '!=', False)])
  12. @api.constrains('partner_id', 'list_ids')
  13. def _check_partner_id_list_ids(self):
  14. for contact in self:
  15. if contact.partner_id:
  16. other_contact = self.search([
  17. ('partner_id', '=', contact.partner_id.id),
  18. ('id', '!=', contact.id)
  19. ])
  20. if contact.list_ids & other_contact.mapped('list_ids'):
  21. raise ValidationError(_("Partner already exists in one of "
  22. "these mailing lists"))
  23. @api.onchange('partner_id')
  24. def _onchange_partner_mass_mailing_partner(self):
  25. if self.partner_id:
  26. self.name = self.partner_id.name
  27. self.email = self.partner_id.email
  28. self.title_id = self.partner_id.title
  29. self.company_name = self.partner_id.company_id.name
  30. self.country_id = self.partner_id.country_id
  31. category_ids = self.partner_id.category_id
  32. if category_ids:
  33. self.tag_ids = category_ids
  34. @api.model
  35. def create(self, vals):
  36. record = self.new(vals)
  37. if not record.partner_id:
  38. record._set_partner()
  39. record._onchange_partner_mass_mailing_partner()
  40. new_vals = record._convert_to_write(record._cache)
  41. return super(MailMassMailingContact, self).create(new_vals)
  42. def write(self, vals):
  43. for contact in self:
  44. new_vals = contact.copy_data(vals)[0]
  45. record = self.new(new_vals)
  46. if not record.partner_id:
  47. record._set_partner()
  48. record._onchange_partner_mass_mailing_partner()
  49. new_vals = record._convert_to_write(record._cache)
  50. super(MailMassMailingContact, contact).write(new_vals)
  51. return True
  52. def _get_company(self):
  53. company_id = False
  54. if self.company_name:
  55. company_id = self.env['res.company'].search(
  56. [('name', '=', self.company_name)]).id
  57. if not company_id:
  58. company_id = self.env['res.company'].create(
  59. {'name': self.company_name}).id
  60. return company_id
  61. def _get_categories(self):
  62. ca_ids = self.tag_ids.ids + self.list_ids.mapped('partner_category.id')
  63. return [[6, 0, ca_ids]]
  64. def _prepare_partner(self):
  65. return {
  66. 'name': self.name or self.email,
  67. 'email': self.email,
  68. 'country_id': self.country_id.id,
  69. 'title': self.title_id.id,
  70. 'company_id': self._get_company(),
  71. 'category_id': self._get_categories(),
  72. }
  73. @api.multi
  74. def _set_partner(self):
  75. self.ensure_one()
  76. m_partner = self.env['res.partner']
  77. # Look for a partner with that email
  78. email = self.email.strip()
  79. partner = m_partner.search([('email', '=ilike', email)], limit=1)
  80. if partner:
  81. # Partner found
  82. self.partner_id = partner
  83. elif self.list_ids.filtered('partner_mandatory'):
  84. # Create partner
  85. self.partner_id = m_partner.sudo().create(self._prepare_partner())