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.

102 lines
4.0 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. # HACK: if creating partner record is needed, `list_ids` and
  40. # `tag_ids` fields are reset in `record`, so we recreate it as ugly
  41. # workaround while no clue about Odoo glitch
  42. vals['partner_id'] = record.partner_id.id
  43. record = self.new(vals)
  44. record._onchange_partner_mass_mailing_partner()
  45. new_vals = record._convert_to_write(record._cache)
  46. return super(MailMassMailingContact, self).create(new_vals)
  47. def write(self, vals):
  48. for contact in self:
  49. new_vals = contact.copy_data(vals)[0]
  50. record = self.new(new_vals)
  51. if not record.partner_id:
  52. record._set_partner()
  53. record._onchange_partner_mass_mailing_partner()
  54. new_vals = record._convert_to_write(record._cache)
  55. super(MailMassMailingContact, contact).write(new_vals)
  56. return True
  57. def _get_company(self):
  58. company_id = False
  59. if self.company_name:
  60. company_id = self.env['res.company'].search(
  61. [('name', '=', self.company_name)]).id
  62. if not company_id:
  63. company_id = self.env['res.company'].create(
  64. {'name': self.company_name}).id
  65. return company_id
  66. def _get_categories(self):
  67. ca_ids = self.tag_ids.ids + self.list_ids.mapped('partner_category.id')
  68. return [[6, 0, ca_ids]]
  69. def _prepare_partner(self):
  70. return {
  71. 'name': self.name or self.email,
  72. 'email': self.email,
  73. 'country_id': self.country_id.id,
  74. 'title': self.title_id.id,
  75. 'company_id': self._get_company(),
  76. 'category_id': self._get_categories(),
  77. }
  78. @api.multi
  79. def _set_partner(self):
  80. self.ensure_one()
  81. m_partner = self.env['res.partner']
  82. # Look for a partner with that email
  83. email = self.email.strip()
  84. partner = m_partner.search([('email', '=ilike', email)], limit=1)
  85. if partner:
  86. # Partner found
  87. self.partner_id = partner
  88. elif self.list_ids.filtered('partner_mandatory'):
  89. # Create partner
  90. self.partner_id = m_partner.sudo().create(self._prepare_partner())