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.

108 lines
4.2 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. new_vals.update(
  42. subscription_list_ids=vals.get('subscription_list_ids', False),
  43. list_ids=vals.get('list_ids', False)
  44. )
  45. return super(MailMassMailingContact, self).create(new_vals)
  46. def write(self, vals):
  47. for contact in self:
  48. new_vals = contact.copy_data(vals)[0]
  49. record = self.new(new_vals)
  50. if not record.partner_id:
  51. record._set_partner()
  52. record._onchange_partner_mass_mailing_partner()
  53. new_vals = record._convert_to_write(record._cache)
  54. new_vals.update(
  55. subscription_list_ids=vals.get('subscription_list_ids', False),
  56. list_ids=vals.get('list_ids', False)
  57. )
  58. super(MailMassMailingContact, contact).write(new_vals)
  59. return True
  60. def _get_company(self):
  61. company_id = False
  62. if self.company_name:
  63. company_id = self.env['res.company'].search(
  64. [('name', '=', self.company_name)]).id
  65. if not company_id:
  66. company_id = self.env['res.company'].create(
  67. {'name': self.company_name}).id
  68. return company_id
  69. def _get_categories(self):
  70. ca_ids = self.tag_ids.ids + self.list_ids.mapped('partner_category.id')
  71. return [[6, 0, ca_ids]]
  72. def _prepare_partner(self):
  73. return {
  74. 'name': self.name or self.email,
  75. 'email': self.email,
  76. 'country_id': self.country_id.id,
  77. 'title': self.title_id.id,
  78. 'company_id': self._get_company(),
  79. 'category_id': self._get_categories(),
  80. }
  81. @api.multi
  82. def _set_partner(self):
  83. self.ensure_one()
  84. m_partner = self.env['res.partner']
  85. # Look for a partner with that email
  86. email = self.email.strip()
  87. partner = m_partner.search([('email', '=ilike', email)], limit=1)
  88. if partner:
  89. # Partner found
  90. self.partner_id = partner
  91. else:
  92. lts = self.subscription_list_ids.mapped('list_id') | self.list_ids
  93. if lts.filtered('partner_mandatory'):
  94. # Create partner
  95. partner_vals = self._prepare_partner()
  96. self.partner_id = m_partner.sudo().create(partner_vals)