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.

113 lines
5.1 KiB

10 years ago
  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 import orm, fields
  21. from openerp.tools.translate import _
  22. class ResPartner(orm.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 _prepare_name_custom(self, cursor, uid, partner, context=None):
  34. """
  35. This function is designed to be inherited in a custom module
  36. """
  37. names = (partner.lastname, partner.firstname)
  38. fullname = " ".join([s for s in names if s])
  39. return fullname
  40. def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
  41. res = {}
  42. for partner in self.browse(cursor, uid, ids, context=context):
  43. res[partner.id] = self._prepare_name_custom(
  44. cursor, uid, partner, context=context)
  45. return res
  46. def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
  47. """
  48. Try to reverse the effect of _compute_name_custom:
  49. * if the partner is not a company and the firstname does not change in the new name
  50. then firstname remains untouched and lastname is updated accordingly
  51. * otherwise lastname=new name and firstname=False
  52. In addition an heuristic avoids to keep a firstname without a non-blank lastname
  53. """
  54. field_value = field_value and not field_value.isspace() and field_value or False
  55. vals = {'lastname': field_value, 'firstname': False}
  56. if field_value:
  57. flds = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0]
  58. if not flds['is_company']:
  59. to_check = ' %s' % flds['firstname']
  60. if field_value.endswith(to_check):
  61. ln = field_value[:-len(to_check)].strip()
  62. if ln:
  63. vals['lastname'] = ln
  64. del(vals['firstname'])
  65. else:
  66. # If the lastname is deleted from the new name
  67. # then the firstname becomes the lastname
  68. vals['lastname'] = flds['firstname']
  69. return self.write(cursor, uid, partner_id, vals, context=context)
  70. def copy_data(self, cr, uid, _id, default=None, context=None):
  71. """
  72. Avoid to replicate the firstname into the name when duplicating a partner
  73. """
  74. default = default or {}
  75. if not default.get('lastname'):
  76. default = default.copy()
  77. default['lastname'] = (
  78. _('%s (copy)') % self.read(cr, uid, [_id], ['lastname'], context=context)[0]['lastname']
  79. )
  80. if default.get('name'):
  81. del(default['name'])
  82. return super(ResPartner, self).copy_data(cr, uid, _id, default, context=context)
  83. def create(self, cursor, uid, vals, context=None):
  84. """
  85. To support data backward compatibility we have to keep this overwrite even if we
  86. use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
  87. will not install if there is demo data
  88. """
  89. to_use = vals
  90. if 'name' in vals:
  91. corr_vals = vals.copy()
  92. corr_vals['lastname'] = corr_vals['name']
  93. del(corr_vals['name'])
  94. to_use = corr_vals
  95. return super(ResPartner, self).create(cursor, uid, to_use, context=context)
  96. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  97. type="char", store=True,
  98. select=True, readonly=True,
  99. fnct_inv=_write_name),
  100. 'firstname': fields.char("Firstname"),
  101. 'lastname': fields.char("Lastname", required=True)}