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.

113 lines
3.9 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. # © 2015 Antiun Ingenieria S.L. - Antonio Espinosa
  5. from openerp import api, fields, models
  6. from openerp.addons.partner_firstname.models import exceptions
  7. class ResPartner(models.Model):
  8. """Adds a second last name."""
  9. _inherit = "res.partner"
  10. lastname2 = fields.Char("Second last name", oldname="lastname_second")
  11. @api.model
  12. def _get_computed_name(self, lastname, firstname, lastname2=None):
  13. """Compute the name combined with the second lastname too.
  14. We have 2 lastnames, so lastnames and firstname will be separated by a
  15. comma.
  16. """
  17. order = self._get_names_order()
  18. names = list()
  19. if order == 'first_last':
  20. if firstname:
  21. names.append(firstname)
  22. if lastname:
  23. names.append(lastname)
  24. if lastname2:
  25. names.append(lastname2)
  26. else:
  27. if lastname:
  28. names.append(lastname)
  29. if lastname2:
  30. names.append(lastname2)
  31. if names and firstname and order == 'last_first_comma':
  32. names[-1] = names[-1] + ","
  33. if firstname:
  34. names.append(firstname)
  35. return u" ".join(names)
  36. @api.one
  37. @api.depends("firstname", "lastname", "lastname2")
  38. def _compute_name(self):
  39. """Write :attr:`~.name` according to splitted data."""
  40. self.name = self._get_computed_name(
  41. self.lastname, self.firstname, self.lastname2)
  42. @api.one
  43. def _inverse_name(self):
  44. """Try to revert the effect of :meth:`._compute_name`."""
  45. parts = self._get_inverse_name(self.name, self.is_company)
  46. # Avoid to hit :meth:`~._check_name` with all 3 fields being ``False``
  47. before, after = dict(), dict()
  48. for key, value in parts.iteritems():
  49. (before if value else after)[key] = value
  50. if any([before[k] != self[k] for k in before.keys()]):
  51. self.update(before)
  52. if any([after[k] != self[k] for k in after.keys()]):
  53. self.update(after)
  54. @api.model
  55. def _get_inverse_name(self, name, is_company=False):
  56. """Compute the inverted name.
  57. - If the partner is a company, save it in the lastname.
  58. - Otherwise, make a guess.
  59. """
  60. # Company name goes to the lastname
  61. result = {
  62. 'firstname': False,
  63. 'lastname': name or False,
  64. 'lastname2': False,
  65. }
  66. if not is_company and name:
  67. order = self._get_names_order()
  68. result = super(ResPartner, self)._get_inverse_name(
  69. name, is_company)
  70. parts = []
  71. if order == 'last_first':
  72. if result['firstname']:
  73. parts = result['firstname'].split(" ", 1)
  74. while len(parts) < 2:
  75. parts.append(False)
  76. result['lastname2'] = parts[0]
  77. result['firstname'] = parts[1]
  78. else:
  79. if result['lastname']:
  80. parts = result['lastname'].split(" ", 1)
  81. while len(parts) < 2:
  82. parts.append(False)
  83. result['lastname'] = parts[0]
  84. result['lastname2'] = parts[1]
  85. return result
  86. @api.one
  87. @api.constrains("firstname", "lastname", "lastname2")
  88. def _check_name(self):
  89. """Ensure at least one name is set."""
  90. try:
  91. super(ResPartner, self)._check_name()
  92. except exceptions.EmptyNamesError as error:
  93. if not self.lastname2:
  94. raise error
  95. @api.one
  96. @api.onchange("firstname", "lastname", "lastname2")
  97. def _onchange_subnames(self):
  98. """Trigger onchange with :attr:`~.lastname2` too."""
  99. super(ResPartner, self)._onchange_subnames()