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.

72 lines
2.5 KiB

6 years ago
6 years ago
  1. # Copyright 2015 Antiun Ingenieria S.L. - Antonio Espinosa
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. import logging
  4. from odoo import fields, models
  5. _logger = logging.getLogger(__name__)
  6. class ResConfigSettings(models.TransientModel):
  7. _inherit = "res.config.settings"
  8. partner_names_order = fields.Selection(
  9. string="Partner names order",
  10. selection="_partner_names_order_selection",
  11. help="Order to compose partner fullname",
  12. config_parameter="partner_names_order",
  13. default=lambda a: a._partner_names_order_default(),
  14. required=True,
  15. inverse="_inverse_partner_names_order",
  16. )
  17. partner_names_order_changed = fields.Boolean(
  18. config_parameter="partner_names_order_changed"
  19. )
  20. def _partner_names_order_selection(self):
  21. return [
  22. ("last_first", "Lastname Firstname"),
  23. ("last_first_comma", "Lastname, Firstname"),
  24. ("first_last", "Firstname Lastname"),
  25. ]
  26. def _partner_names_order_default(self):
  27. return self.env["res.partner"]._names_order_default()
  28. def _inverse_partner_names_order(self):
  29. current = (
  30. self.env["ir.config_parameter"]
  31. .sudo()
  32. .get_param(
  33. "partner_names_order", default=self._partner_names_order_default()
  34. )
  35. )
  36. for record in self:
  37. record.partner_names_order_changed = bool(
  38. record.partner_names_order != current
  39. )
  40. def _partners_for_recalculating(self):
  41. return self.env["res.partner"].search(
  42. [
  43. ("is_company", "=", False),
  44. ("firstname", "!=", False),
  45. ("lastname", "!=", False),
  46. ]
  47. )
  48. def action_recalculate_partners_name(self):
  49. self.env["ir.config_parameter"].sudo().set_param(
  50. "partner_names_order", self.partner_names_order
  51. )
  52. partners = self._partners_for_recalculating()
  53. _logger.info("Recalculating names for %d partners.", len(partners))
  54. # Use add_to_compute instead of _compute_name to avoid triggering
  55. # _inverse_name_after_cleaning_whitespace, which can
  56. # modify a partner's firstname, lastname and lastname2
  57. self.env.add_to_compute(self.env["res.partner"]._fields["name"], partners)
  58. self.partner_names_order_changed = False
  59. self.execute()
  60. _logger.info("%d partners updated.", len(partners))
  61. return True