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.

106 lines
4.9 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 the partner is not a company and the firstname does not change in the new name
  46. then firstname remains untouched and lastname is updated accordingly
  47. * otherwise lastname=new name and firstname=False
  48. In addition an heuristic avoids to keep a firstname without a non-blank lastname
  49. """
  50. field_value = field_value and not field_value.isspace() and field_value or False
  51. vals = {'lastname': field_value, 'firstname': False}
  52. if field_value:
  53. flds = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0]
  54. if not flds['is_company']:
  55. to_check = ' %s' % flds['firstname']
  56. if field_value.endswith(to_check):
  57. ln = field_value[:-len(to_check)].strip()
  58. if ln:
  59. vals['lastname'] = ln
  60. del(vals['firstname'])
  61. else:
  62. # If the lastname is deleted from the new name
  63. # then the firstname becomes the lastname
  64. vals['lastname'] = flds['firstname']
  65. return self.write(cursor, uid, partner_id, vals, context=context)
  66. def copy_data(self, cr, uid, _id, default=None, context=None):
  67. """
  68. Avoid to replicate the firstname into the name when duplicating a partner
  69. """
  70. default = default or {}
  71. if not default.get('lastname'):
  72. default = default.copy()
  73. default['lastname'] = _('%s (copy)') % self.read(cr, uid, [_id], ['lastname'], context=context)[0]['lastname']
  74. if default.get('name'):
  75. del(default['name'])
  76. return super(ResPartner, self).copy_data(cr, uid, _id, default, context=context)
  77. def create(self, cursor, uid, vals, context=None):
  78. """
  79. To support data backward compatibility we have to keep this overwrite even if we
  80. use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
  81. will not install if there is demo data
  82. """
  83. to_use = vals
  84. if 'name' in vals:
  85. corr_vals = vals.copy()
  86. corr_vals['lastname'] = corr_vals['name']
  87. del(corr_vals['name'])
  88. to_use = corr_vals
  89. return super(ResPartner, self).create(cursor, uid, to_use, context=context)
  90. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  91. type="char", store=True,
  92. select=True, readonly=True,
  93. fnct_inv=_write_name),
  94. 'firstname': fields.char("Firstname"),
  95. 'lastname': fields.char("Lastname", required=True)}