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.

92 lines
3.2 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_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 = " ".join((p for p in (self.lastname,
  36. self.firstname) if p))
  37. @api.one
  38. def _name_inverse(self):
  39. """Try to reverse the effect of :meth:`._check_name`.
  40. - If the partner is a company, save it in the first name.
  41. - Otherwise, make a guess.
  42. """
  43. # Remove unneeded whitespace
  44. clean = " ".join(self.name.split(None))
  45. # Clean name avoiding infinite recursion
  46. if self.name != clean:
  47. self.name = clean
  48. # Save name in the real fields
  49. else:
  50. # Company name goes to the lastname
  51. if self.is_company:
  52. parts = [clean, False]
  53. # Guess name splitting
  54. else:
  55. parts = clean.split(" ", 1)
  56. while len(parts) < 2:
  57. parts.append(False)
  58. self.lastname, self.firstname = parts
  59. @api.one
  60. @api.constrains("firstname", "lastname")
  61. def _check_name(self):
  62. """Ensure at least one name is set."""
  63. if not (self.firstname or self.lastname):
  64. raise exceptions.EmptyNamesError(self)
  65. @api.model
  66. def _firstname_install(self):
  67. """Save names correctly in the database.
  68. Before installing the module, field ``name`` contains all full names.
  69. When installing it, this method parses those names and saves them
  70. correctly into the database. This can be called later too if needed.
  71. """
  72. # Find records with empty firstname and lastname
  73. records = self.search([("firstname", "=", False),
  74. ("lastname", "=", False)])
  75. # Force calculations there
  76. records._name_inverse()