1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Author: Nicolas Bessi. Copyright Camptocamp SA
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.
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.
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/>.
19
##############################################################################
20
from openerp.osv.orm import Model, fields
23
class ResPartner(Model):
24
"""Adds lastname and firstname, name become a stored function field"""
26
_inherit = 'res.partner'
28
def init(self, cursor):
29
cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL')
30
if not cursor.fetchone():
31
cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL')
33
def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
35
for rec in self.read(cursor, uid, ids, ['firstname', 'lastname']):
36
name = rec['lastname'] + (u" " + rec['firstname'] if rec['firstname'] else u"")
40
_columns = {'name': fields.function(_compute_name_custom, string="Name",
41
type="char", store=True,
42
select=True, readonly=True),
44
'firstname': fields.char("Firstname"),
45
'lastname': fields.char("Lastname", required=True)}
47
def create(self, cursor, uid, vals, context=None):
48
"""To support data backward compatibility"""
51
corr_vals = vals.copy()
52
corr_vals['lastname'] = corr_vals['name']
53
del(corr_vals['name'])
55
return super(ResPartner, self).create(cursor, uid, to_use, context=context)
57
def write(self, cursor, uid, ids, vals, context=None):
58
"""To support data backward compatibility"""
61
corr_vals = vals.copy()
62
corr_vals['lastname'] = corr_vals['name']
63
del(corr_vals['name'])
65
return super(ResPartner, self).write(cursor, uid, ids, to_use, context=context)