|
@ -23,15 +23,18 @@ import logging |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(orm.Model): |
|
|
class ResPartner(orm.Model): |
|
|
"""Adds lastname and firstname, name become a stored function field""" |
|
|
"""Adds lastname and firstname, name become a stored function field""" |
|
|
|
|
|
|
|
|
_inherit = 'res.partner' |
|
|
_inherit = 'res.partner' |
|
|
|
|
|
|
|
|
def init(self, cursor): |
|
|
def init(self, cursor): |
|
|
cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL Limit 1') |
|
|
|
|
|
|
|
|
cursor.execute('SELECT id FROM res_partner WHERE lastname IS NOT NULL ' |
|
|
|
|
|
'Limit 1') |
|
|
if not cursor.fetchone(): |
|
|
if not cursor.fetchone(): |
|
|
cursor.execute('UPDATE res_partner set lastname = name WHERE name IS NOT NULL') |
|
|
|
|
|
|
|
|
cursor.execute('UPDATE res_partner set lastname = name WHERE name ' |
|
|
|
|
|
'IS NOT NULL') |
|
|
# Create Sql constraint if table is not empty |
|
|
# Create Sql constraint if table is not empty |
|
|
cursor.execute('SELECT id FROM res_partner Limit 1') |
|
|
cursor.execute('SELECT id FROM res_partner Limit 1') |
|
|
if cursor.fetchone(): |
|
|
if cursor.fetchone(): |
|
@ -55,18 +58,26 @@ class ResPartner(orm.Model): |
|
|
cursor, uid, partner, context=context) |
|
|
cursor, uid, partner, context=context) |
|
|
return res |
|
|
return res |
|
|
|
|
|
|
|
|
def _write_name(self, cursor, uid, partner_id, field_name, field_value, arg, context=None): |
|
|
|
|
|
|
|
|
def _write_name( |
|
|
|
|
|
self, cursor, uid, partner_id, field_name, field_value, arg, |
|
|
|
|
|
context=None |
|
|
|
|
|
): |
|
|
""" |
|
|
""" |
|
|
Try to reverse the effect of _compute_name_custom: |
|
|
Try to reverse the effect of _compute_name_custom: |
|
|
* if the partner is not a company and the firstname does not change in the new name |
|
|
|
|
|
then firstname remains untouched and lastname is updated accordingly |
|
|
|
|
|
|
|
|
* if the partner is not a company and the firstname does not change in |
|
|
|
|
|
the new name then firstname remains untouched and lastname is updated |
|
|
|
|
|
accordingly |
|
|
* otherwise lastname=new name and firstname=False |
|
|
* otherwise lastname=new name and firstname=False |
|
|
In addition an heuristic avoids to keep a firstname without a non-blank lastname |
|
|
|
|
|
|
|
|
In addition an heuristic avoids to keep a firstname without a non-blank |
|
|
|
|
|
lastname |
|
|
""" |
|
|
""" |
|
|
field_value = field_value and not field_value.isspace() and field_value or False |
|
|
|
|
|
|
|
|
field_value = ( |
|
|
|
|
|
field_value and not field_value.isspace() and field_value or False) |
|
|
vals = {'lastname': field_value, 'firstname': False} |
|
|
vals = {'lastname': field_value, 'firstname': False} |
|
|
if field_value: |
|
|
if field_value: |
|
|
flds = self.read(cursor, uid, [partner_id], ['firstname', 'is_company'], context=context)[0] |
|
|
|
|
|
|
|
|
flds = self.read( |
|
|
|
|
|
cursor, uid, [partner_id], ['firstname', 'is_company'], |
|
|
|
|
|
context=context)[0] |
|
|
if not flds['is_company']: |
|
|
if not flds['is_company']: |
|
|
to_check = ' %s' % flds['firstname'] |
|
|
to_check = ' %s' % flds['firstname'] |
|
|
if field_value.endswith(to_check): |
|
|
if field_value.endswith(to_check): |
|
@ -83,23 +94,27 @@ class ResPartner(orm.Model): |
|
|
|
|
|
|
|
|
def copy_data(self, cr, uid, _id, default=None, context=None): |
|
|
def copy_data(self, cr, uid, _id, default=None, context=None): |
|
|
""" |
|
|
""" |
|
|
Avoid to replicate the firstname into the name when duplicating a partner |
|
|
|
|
|
|
|
|
Avoid to replicate the firstname into the name when duplicating a |
|
|
|
|
|
partner |
|
|
""" |
|
|
""" |
|
|
default = default or {} |
|
|
default = default or {} |
|
|
if not default.get('lastname'): |
|
|
if not default.get('lastname'): |
|
|
default = default.copy() |
|
|
default = default.copy() |
|
|
default['lastname'] = ( |
|
|
default['lastname'] = ( |
|
|
_('%s (copy)') % self.read(cr, uid, [_id], ['lastname'], context=context)[0]['lastname'] |
|
|
|
|
|
|
|
|
_('%s (copy)') % self.read( |
|
|
|
|
|
cr, uid, [_id], ['lastname'], context=context |
|
|
|
|
|
)[0]['lastname'] |
|
|
) |
|
|
) |
|
|
if default.get('name'): |
|
|
if default.get('name'): |
|
|
del(default['name']) |
|
|
del(default['name']) |
|
|
return super(ResPartner, self).copy_data(cr, uid, _id, default, context=context) |
|
|
|
|
|
|
|
|
return super(ResPartner, self).copy_data( |
|
|
|
|
|
cr, uid, _id, default, context=context) |
|
|
|
|
|
|
|
|
def create(self, cursor, uid, vals, context=None): |
|
|
def create(self, cursor, uid, vals, context=None): |
|
|
""" |
|
|
""" |
|
|
To support data backward compatibility we have to keep this overwrite even if we |
|
|
|
|
|
use fnct_inv: otherwise we can't create entry because lastname is mandatory and module |
|
|
|
|
|
will not install if there is demo data |
|
|
|
|
|
|
|
|
To support data backward compatibility we have to keep this overwrite |
|
|
|
|
|
even if we use fnct_inv: otherwise we can't create entry because |
|
|
|
|
|
lastname is mandatory and module will not install if there is demo data |
|
|
""" |
|
|
""" |
|
|
to_use = vals |
|
|
to_use = vals |
|
|
if 'name' in vals: |
|
|
if 'name' in vals: |
|
@ -108,7 +123,8 @@ class ResPartner(orm.Model): |
|
|
corr_vals['lastname'] = corr_vals['name'] |
|
|
corr_vals['lastname'] = corr_vals['name'] |
|
|
del(corr_vals['name']) |
|
|
del(corr_vals['name']) |
|
|
to_use = corr_vals |
|
|
to_use = corr_vals |
|
|
return super(ResPartner, self).create(cursor, uid, to_use, context=context) |
|
|
|
|
|
|
|
|
return super(ResPartner, self).create( |
|
|
|
|
|
cursor, uid, to_use, context=context) |
|
|
|
|
|
|
|
|
_columns = {'name': fields.function(_compute_name_custom, string="Name", |
|
|
_columns = {'name': fields.function(_compute_name_custom, string="Name", |
|
|
type="char", store=True, |
|
|
type="char", store=True, |
|
|