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.

82 lines
2.7 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 api, 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. required=True,
  13. )
  14. partner_names_order_changed = fields.Boolean(
  15. compute="_compute_names_order_changed",
  16. )
  17. def _partner_names_order_selection(self):
  18. return [
  19. ('last_first', 'Lastname Firstname'),
  20. ('last_first_comma', 'Lastname, Firstname'),
  21. ('first_last', 'Firstname Lastname'),
  22. ]
  23. @api.multi
  24. def _partner_names_order_default(self):
  25. return self.env['res.partner']._names_order_default()
  26. @api.model
  27. def get_values(self):
  28. res = super(ResConfigSettings, self).get_values()
  29. partner_names_order = self.env['ir.config_parameter'].sudo().get_param(
  30. 'partner_names_order',
  31. default=self._partner_names_order_default()
  32. )
  33. res.update(partner_names_order=partner_names_order)
  34. return res
  35. @api.multi
  36. @api.depends('partner_names_order')
  37. def _compute_names_order_changed(self):
  38. current = self.env['ir.config_parameter'].sudo().get_param(
  39. 'partner_names_order', default=self._partner_names_order_default()
  40. )
  41. for record in self:
  42. record.partner_names_order_changed = bool(
  43. record.partner_names_order != current
  44. )
  45. @api.multi
  46. @api.onchange('partner_names_order')
  47. def _onchange_partner_names_order(self):
  48. self._compute_names_order_changed()
  49. @api.multi
  50. def set_values(self):
  51. super(ResConfigSettings, self).set_values()
  52. self.env['ir.config_parameter'].sudo().set_param(
  53. 'partner_names_order', self.partner_names_order
  54. )
  55. @api.multi
  56. def _partners_for_recalculating(self):
  57. return self.env['res.partner'].search([
  58. ('is_company', '=', False),
  59. ('firstname', '!=', False), ('lastname', '!=', False),
  60. ])
  61. @api.multi
  62. def action_recalculate_partners_name(self):
  63. self.env['ir.config_parameter'].sudo().set_param(
  64. 'partner_names_order', self.partner_names_order
  65. )
  66. partners = self._partners_for_recalculating()
  67. _logger.info("Recalculating names for %d partners.", len(partners))
  68. partners._compute_name()
  69. _logger.info("%d partners updated.", len(partners))
  70. return True