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.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. # © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U.
  4. from openerp import api, fields, models
  5. from openerp.addons.partner_firstname import exceptions
  6. class ResPartner(models.Model):
  7. """Adds a second last name."""
  8. _inherit = "res.partner"
  9. lastname2 = fields.Char("Second last name", oldname="lastname_second")
  10. @api.model
  11. def _get_computed_name(self, lastname, firstname, lastname2=None):
  12. """Compute the name combined with the second lastname too.
  13. We have 2 lastnames, so lastnames and firstname will be separated by a
  14. comma.
  15. """
  16. names = list()
  17. if lastname:
  18. names.append(lastname)
  19. if lastname2:
  20. names.append(lastname2)
  21. if names and firstname:
  22. names[-1] = names[-1] + ","
  23. if firstname:
  24. names.append(firstname)
  25. return u" ".join(names)
  26. @api.one
  27. @api.depends("firstname", "lastname", "lastname2")
  28. def _compute_name(self):
  29. """Write :attr:`~.name` according to splitted data."""
  30. self.name = self._get_computed_name(self.lastname,
  31. self.firstname,
  32. self.lastname2)
  33. @api.one
  34. def _inverse_name(self):
  35. """Try to revert the effect of :meth:`._compute_name`."""
  36. parts = self._get_inverse_name(self.name, self.is_company)
  37. # Avoid to hit :meth:`~._check_name` with all 3 fields being ``False``
  38. before, after = dict(), dict()
  39. for key, value in parts.iteritems():
  40. (before if value else after)[key] = value
  41. self.update(before)
  42. self.update(after)
  43. @api.model
  44. def _get_inverse_name(self, name, is_company=False):
  45. """Compute the inverted name.
  46. - If the partner is a company, save it in the lastname.
  47. - Otherwise, make a guess.
  48. """
  49. # Company name goes to the lastname
  50. if is_company or not name:
  51. parts = [False, name or False, False]
  52. # The comma separates the firstname
  53. elif "," in name:
  54. lastnames, firstname = name.split(",", 1)
  55. parts = [firstname.strip()] + lastnames.split(" ", 1)
  56. # Without comma, the user wrote the firstname first
  57. else:
  58. parts = name.split(" ", 2)
  59. while len(parts) < 3:
  60. parts.append(False)
  61. return {"firstname": parts[0],
  62. "lastname": parts[1],
  63. "lastname2": parts[2]}
  64. @api.one
  65. @api.constrains("firstname", "lastname", "lastname2")
  66. def _check_name(self):
  67. """Ensure at least one name is set."""
  68. try:
  69. super(ResPartner, self)._check_name()
  70. except exceptions.EmptyNamesError as error:
  71. if not self.lastname2:
  72. raise error
  73. @api.one
  74. @api.onchange("firstname", "lastname", "lastname2")
  75. def _onchange_subnames(self):
  76. """Trigger onchange with :attr:`~.lastname2` too."""
  77. super(ResPartner, self)._onchange_subnames()