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.

49 lines
2.2 KiB

  1. # -*- encoding: 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. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  37. type="char", store=True,
  38. select=True, readonly=True,
  39. fnct_inv=_write_name),
  40. 'firstname': fields.char("Firstname"),
  41. 'lastname': fields.char("Lastname", required=True)}