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.

125 lines
4.1 KiB

  1. # Copyright 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  2. # Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa
  3. # Copyright 2017 Tecnativa - Pedro M. Baeza
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. from odoo import api, fields, models
  6. from odoo.addons.partner_firstname import exceptions
  7. class ResPartner(models.Model):
  8. """Adds a second last name."""
  9. _inherit = "res.partner"
  10. lastname2 = fields.Char(
  11. "Second last name",
  12. )
  13. @api.model
  14. def _get_computed_name(self, lastname, firstname, lastname2=None):
  15. """Compute the name combined with the second lastname too.
  16. We have 2 lastnames, so lastnames and firstname will be separated by a
  17. comma.
  18. """
  19. order = self._get_names_order()
  20. names = list()
  21. if order == "first_last":
  22. if firstname:
  23. names.append(firstname)
  24. if lastname:
  25. names.append(lastname)
  26. if lastname2:
  27. names.append(lastname2)
  28. else:
  29. if lastname:
  30. names.append(lastname)
  31. if lastname2:
  32. names.append(lastname2)
  33. if names and firstname and order == "last_first_comma":
  34. names[-1] = names[-1] + ","
  35. if firstname:
  36. names.append(firstname)
  37. return " ".join(names)
  38. @api.depends("firstname", "lastname", "lastname2")
  39. def _compute_name(self):
  40. """Write :attr:`~.name` according to splitted data."""
  41. for partner in self:
  42. partner.name = self._get_computed_name(
  43. partner.lastname,
  44. partner.firstname,
  45. partner.lastname2,
  46. )
  47. def _inverse_name(self):
  48. """Try to revert the effect of :meth:`._compute_name`."""
  49. self.ensure_one()
  50. parts = self._get_inverse_name(self.name, self.is_company)
  51. # Avoid to hit :meth:`~._check_name` with all 3 fields being ``False``
  52. before, after = {}, {}
  53. for key, value in parts.items():
  54. (before if value else after)[key] = value
  55. if any([before[k] != self[k] for k in list(before.keys())]):
  56. self.update(before)
  57. if any([after[k] != self[k] for k in list(after.keys())]):
  58. self.update(after)
  59. @api.model
  60. def _get_inverse_name(self, name, is_company=False):
  61. """Compute the inverted name.
  62. - If the partner is a company, save it in the lastname.
  63. - Otherwise, make a guess.
  64. """
  65. result = {
  66. "firstname": False,
  67. "lastname": name or False,
  68. "lastname2": False,
  69. }
  70. # Company name goes to the lastname
  71. if not name or is_company:
  72. return result
  73. order = self._get_names_order()
  74. result.update(super(ResPartner, self)._get_inverse_name(name, is_company))
  75. if order in ("first_last", "last_first_comma"):
  76. parts = self._split_part("lastname", result)
  77. if parts:
  78. result.update({"lastname": parts[0], "lastname2": " ".join(parts[1:])})
  79. else:
  80. parts = self._split_part("firstname", result)
  81. if parts:
  82. result.update(
  83. {"firstname": parts[-1], "lastname2": " ".join(parts[:-1])}
  84. )
  85. return result
  86. def _split_part(self, name_part, name_split):
  87. """Split a given part of a name.
  88. :param name_split: The parts of the name
  89. :type dict
  90. :param name_part: The part to split
  91. :type str
  92. """
  93. name = name_split.get(name_part, False)
  94. parts = name.split(" ", 1) if name else []
  95. if not name or len(parts) < 2:
  96. return False
  97. return parts
  98. @api.constrains("firstname", "lastname", "lastname2")
  99. def _check_name(self):
  100. """Ensure at least one name is set."""
  101. try:
  102. super(ResPartner, self)._check_name()
  103. except exceptions.EmptyNamesError:
  104. for partner in self:
  105. if not partner.lastname2:
  106. raise