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.

61 lines
2.8 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. class ResPartner(Model):
  22. """Adds lastname and firstname, name become a stored function field"""
  23. _inherit = 'res.partner'
  24. def init(self, cursor):
  25. cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL Limit 1')
  26. if not cursor.fetchone():
  27. cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL')
  28. def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
  29. res = {}
  30. for rec in self.read(cursor, uid, ids, ['firstname', 'lastname']):
  31. name = rec['lastname'] + (u" " + rec['firstname'] if rec['firstname'] else u"")
  32. res[rec['id']] = name
  33. return res
  34. def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
  35. return self.write(cursor, uid, partner_id, {'lastname': field_value})
  36. def create(self, cursor, uid, vals, context=None):
  37. """To support data backward compatibility we have to keep this overwrite even if we
  38. use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
  39. will not install if there is demo data"""
  40. to_use = vals
  41. if vals.get('name'):
  42. corr_vals = vals.copy()
  43. corr_vals['lastname'] = corr_vals['name']
  44. del(corr_vals['name'])
  45. to_use = corr_vals
  46. return super(ResPartner, self).create(cursor, uid, to_use, context=context)
  47. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  48. type="char", store=True,
  49. select=True, readonly=True,
  50. fnct_inv=_write_name),
  51. 'firstname': fields.char("Firstname"),
  52. 'lastname': fields.char("Lastname", required=True)}