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.

96 lines
4.5 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Nicolas Bessi. Copyright Camptocamp SA
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp.osv.orm import Model, fields
  21. from openerp.tools.translate import _
  22. class ResPartner(Model):
  23. """Adds lastname and firstname, name become a stored function field"""
  24. _inherit = 'res.partner'
  25. def init(self, cursor):
  26. cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL Limit 1')
  27. if not cursor.fetchone():
  28. cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL')
  29. # Create Sql constraint if table is not empty
  30. cursor.execute('SELECT id FROM res_partner Limit 1')
  31. if cursor.fetchone():
  32. cursor.execute('ALTER TABLE res_partner ALTER COLUMN lastname SET NOT NULL')
  33. def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
  34. res = {}
  35. partners = self.read(cursor, uid, ids,
  36. ['firstname', 'lastname'], context=context)
  37. for rec in partners:
  38. names = (rec['lastname'], rec['firstname'])
  39. fullname = " ".join([s for s in names if s])
  40. res[rec['id']] = fullname
  41. return res
  42. def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
  43. """
  44. # Try to reverse the effect of _compute_name_custom:
  45. # * if is_company is True then lastname = name and firstname False
  46. # * if firstname change in the new name: lastname is set to new name, firstname is reset
  47. # * if only lastname change in the new name: lastname is updated accordingly, firstname remains untouched
  48. """
  49. vals = {'lastname': field_value, 'firstname': False}
  50. fields = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0]
  51. if not fields['is_company']:
  52. to_check = ' %s' % fields['firstname']
  53. if field_value.endswith(to_check):
  54. vals['lastname'] = field_value[:-len(to_check)]
  55. del(vals['firstname'])
  56. return self.write(cursor, uid, partner_id, vals, context=context)
  57. def copy_data(self, cr, uid, id, default=None, context=None):
  58. """
  59. # Avoid to replicate the firstname into the name when duplicating a partner
  60. """
  61. default = default or {}
  62. if not default.get('lastname'):
  63. default = default.copy()
  64. default['lastname'] = _('%s (copy)') % self.read(cr, uid, [id], ['lastname'], context=context)[0]['lastname']
  65. if default.get('name'):
  66. del(default['name'])
  67. return super(ResPartner, self).copy_data(cr, uid, id, default, context=context)
  68. def create(self, cursor, uid, vals, context=None):
  69. """
  70. # To support data backward compatibility we have to keep this overwrite even if we
  71. # use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
  72. # will not install if there is demo data
  73. """
  74. to_use = vals
  75. if vals.get('name'):
  76. corr_vals = vals.copy()
  77. corr_vals['lastname'] = corr_vals['name']
  78. del(corr_vals['name'])
  79. to_use = corr_vals
  80. return super(ResPartner, self).create(cursor, uid, to_use, context=context)
  81. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  82. type="char", store=True,
  83. select=True, readonly=True,
  84. fnct_inv=_write_name),
  85. 'firstname': fields.char("Firstname"),
  86. 'lastname': fields.char("Lastname", required=True)}