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.

65 lines
2.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. 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. partners = self.read(cursor, uid, ids,
  31. ['firstname', 'lastname'], context=context)
  32. for rec in partners:
  33. names = (rec['lastname'], rec['firstname'])
  34. fullname = " ".join([s for s in names if s])
  35. res[rec['id']] = fullname
  36. return res
  37. def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None):
  38. return self.write(cursor, uid, partner_id,
  39. {'lastname': field_value}, context=context)
  40. def create(self, cursor, uid, vals, context=None):
  41. """To support data backward compatibility we have to keep this overwrite even if we
  42. use fnct_inv: otherwise we can't create entry because lastname is mandatory and module
  43. will not install if there is demo data"""
  44. to_use = vals
  45. if vals.get('name'):
  46. corr_vals = vals.copy()
  47. corr_vals['lastname'] = corr_vals['name']
  48. del(corr_vals['name'])
  49. to_use = corr_vals
  50. return super(ResPartner, self).create(cursor, uid, to_use, context=context)
  51. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  52. type="char", store=True,
  53. select=True, readonly=True,
  54. fnct_inv=_write_name),
  55. 'firstname': fields.char("Firstname"),
  56. 'lastname': fields.char("Lastname", required=True)}