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
3.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # Author: Nicolas Bessi. Copyright Camptocamp SA
  3. # Copyright (C)
  4. # 2014: Agile Business Group (<http://www.agilebg.com>)
  5. # 2015: Grupo ESOC <www.grupoesoc.es>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. from openerp import api, fields, models
  20. from . import exceptions
  21. class ResPartner(models.Model):
  22. """Adds last name and first name; name becomes a stored function field."""
  23. _inherit = 'res.partner'
  24. firstname = fields.Char("First name")
  25. lastname = fields.Char("Last name")
  26. name = fields.Char(
  27. compute="_name_compute",
  28. inverse="_name_clean_inverse",
  29. required=False,
  30. store=True)
  31. @api.one
  32. @api.depends("firstname", "lastname")
  33. def _name_compute(self):
  34. """Write the 'name' field according to splitted data."""
  35. self.name = u" ".join((p for p in (self.lastname,
  36. self.firstname) if p))
  37. @api.one
  38. def _name_clean_inverse(self):
  39. """Clean whitespace in ``name`` and call :meth:`._name_inverse`."""
  40. # Remove unneeded whitespace
  41. clean = u" ".join(self.name.split(None))
  42. # Clean name avoiding infinite recursion
  43. if self.name != clean:
  44. self.name = clean
  45. # Save name in the real fields
  46. else:
  47. self._name_inverse()
  48. @api.one
  49. def _name_inverse(self):
  50. """Try to revert the effect of :meth:`._name_compute`.
  51. - If the partner is a company, save it in the lastname.
  52. - Otherwise, make a guess.
  53. This method can be easily overriden by other submodules.
  54. When this method is called, ``self.name`` already has unified and
  55. trimmed whitespace.
  56. """
  57. # Company name goes to the lastname
  58. if self.is_company:
  59. parts = [self.name, False]
  60. # Guess name splitting
  61. else:
  62. parts = self.name.split(" ", 1)
  63. while len(parts) < 2:
  64. parts.append(False)
  65. self.lastname, self.firstname = parts
  66. @api.one
  67. @api.constrains("firstname", "lastname")
  68. def _check_name(self):
  69. """Ensure at least one name is set."""
  70. if not (self.firstname or self.lastname):
  71. raise exceptions.EmptyNamesError(self)
  72. @api.model
  73. def _firstname_install(self):
  74. """Save names correctly in the database.
  75. Before installing the module, field ``name`` contains all full names.
  76. When installing it, this method parses those names and saves them
  77. correctly into the database. This can be called later too if needed.
  78. """
  79. # Find records with empty firstname and lastname
  80. records = self.search([("firstname", "=", False),
  81. ("lastname", "=", False)])
  82. # Force calculations there
  83. records._name_inverse()