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.

80 lines
2.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Antiun Ingenieria S.L. - Antonio Espinosa
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from odoo import api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class ResConfigSettings(models.TransientModel):
  8. _inherit = 'res.config.settings'
  9. partner_names_order = fields.Selection(
  10. string="Partner names order",
  11. selection="_partner_names_order_selection",
  12. help="Order to compose partner fullname",
  13. required=True,
  14. )
  15. partner_names_order_changed = fields.Boolean(
  16. compute="_compute_names_order_changed",
  17. )
  18. def _partner_names_order_selection(self):
  19. return [
  20. ('last_first', 'Lastname Firstname'),
  21. ('last_first_comma', 'Lastname, Firstname'),
  22. ('first_last', 'Firstname Lastname'),
  23. ]
  24. @api.multi
  25. def _partner_names_order_default(self):
  26. return self.env['res.partner']._names_order_default()
  27. @api.model
  28. def get_values(self):
  29. res = super(ResConfigSettings, self).get_values()
  30. partner_names_order = self.env['ir.config_parameter'].sudo().get_param(
  31. 'partner_names_order',
  32. default=self._partner_names_order_default()
  33. )
  34. res.update(partner_names_order=partner_names_order)
  35. return res
  36. @api.multi
  37. @api.depends('partner_names_order')
  38. def _compute_names_order_changed(self):
  39. current = self.env['ir.config_parameter'].sudo().get_param(
  40. 'partner_names_order', default=self._partner_names_order_default()
  41. )
  42. for record in self:
  43. record.partner_names_order_changed = bool(
  44. record.partner_names_order != current
  45. )
  46. @api.multi
  47. @api.onchange('partner_names_order')
  48. def _onchange_partner_names_order(self):
  49. self._compute_names_order_changed()
  50. @api.multi
  51. def set_values(self):
  52. super(ResConfigSettings, self).set_values()
  53. self.env['ir.config_parameter'].sudo().set_param(
  54. 'partner_names_order', self.partner_names_order
  55. )
  56. @api.multi
  57. def _partners_for_recalculating(self):
  58. return self.env['res.partner'].search([
  59. ('is_company', '=', False),
  60. ('firstname', '!=', False), ('lastname', '!=', False),
  61. ])
  62. @api.multi
  63. def action_recalculate_partners_name(self):
  64. partners = self._partners_for_recalculating()
  65. _logger.info("Recalculating names for %d partners.", len(partners))
  66. partners._compute_name()
  67. _logger.info("%d partners updated.", len(partners))
  68. return True