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.

76 lines
2.5 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 openerp import api, fields, models
  6. _logger = logging.getLogger(__name__)
  7. class BaseConfigSettings(models.TransientModel):
  8. _inherit = 'base.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_default_partner_names_order(self, fields):
  29. return {
  30. 'partner_names_order': self.env['ir.config_parameter'].get_param(
  31. 'partner_names_order', self._partner_names_order_default(),
  32. ),
  33. }
  34. @api.multi
  35. @api.depends('partner_names_order')
  36. def _compute_names_order_changed(self):
  37. current = self.env['ir.config_parameter'].get_param(
  38. 'partner_names_order', self._partner_names_order_default(),
  39. )
  40. for record in self:
  41. record.partner_names_order_changed = bool(
  42. record.partner_names_order != current
  43. )
  44. @api.multi
  45. @api.onchange('partner_names_order')
  46. def _onchange_partner_names_order(self):
  47. self.partner_names_order_changed = self._compute_names_order_changed()
  48. @api.multi
  49. def set_partner_names_order(self):
  50. self.env['ir.config_parameter'].set_param(
  51. 'partner_names_order', self.partner_names_order)
  52. @api.multi
  53. def _partners_for_recalculating(self):
  54. return self.env['res.partner'].search([
  55. ('is_company', '=', False),
  56. ('firstname', '!=', False), ('lastname', '!=', False),
  57. ])
  58. @api.multi
  59. def action_recalculate_partners_name(self):
  60. partners = self._partners_for_recalculating()
  61. _logger.info("Recalculating names for %d partners.", len(partners))
  62. partners._compute_name()
  63. _logger.info("%d partners updated.", len(partners))
  64. return True