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.

149 lines
5.5 KiB

10 years ago
10 years ago
  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 import orm, fields
  21. from openerp.tools.translate import _
  22. class ResPartner(orm.Model):
  23. """Adds lastname and firstname, name become a stored function field"""
  24. _inherit = 'res.partner'
  25. def init(self, cursor):
  26. cursor.execute(
  27. 'SELECT id FROM res_partner WHERE lastname IS NOT NULL Limit 1'
  28. )
  29. if not cursor.fetchone():
  30. cursor.execute(
  31. 'UPDATE res_partner set lastname = name WHERE name IS NOT NULL'
  32. )
  33. # Create Sql constraint if table is not empty
  34. cursor.execute('SELECT id FROM res_partner Limit 1')
  35. if cursor.fetchone():
  36. cursor.execute(
  37. 'ALTER TABLE res_partner '
  38. 'ALTER COLUMN lastname SET NOT NULL'
  39. )
  40. def _prepare_name_custom(self, cursor, uid, partner, context=None):
  41. """
  42. This function is designed to be inherited in a custom module
  43. """
  44. names = (partner.lastname, partner.firstname)
  45. fullname = " ".join([s for s in names if s])
  46. return fullname
  47. def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
  48. res = {}
  49. for partner in self.browse(cursor, uid, ids, context=context):
  50. res[partner.id] = self._prepare_name_custom(
  51. cursor, uid, partner, context=context)
  52. return res
  53. def _write_name(
  54. self, cursor, uid, partner_id, field_name, field_value, arg,
  55. context=None):
  56. """Try to reverse the effect of _compute_name_custom:
  57. * if the partner is not a company and the firstname does not
  58. change in the new name then firstname remains untouched and
  59. lastname is updated accordingly
  60. * otherwise lastname=new name and firstname=False
  61. In addition an heuristic avoids to keep a firstname without a
  62. non-blank lastname
  63. """
  64. field_value = (field_value
  65. and not field_value.isspace()
  66. and field_value
  67. or False)
  68. vals = {'lastname': field_value, 'firstname': False}
  69. if field_value:
  70. flds = self.read(
  71. cursor,
  72. uid,
  73. [partner_id],
  74. ['firstname', 'is_company'],
  75. context=context
  76. )[0]
  77. if not flds['is_company']:
  78. to_check = ' %s' % flds['firstname']
  79. if field_value.endswith(to_check):
  80. ln = field_value[:-len(to_check)].strip()
  81. if ln:
  82. vals['lastname'] = ln
  83. del(vals['firstname'])
  84. else:
  85. # If the lastname is deleted from the new name
  86. # then the firstname becomes the lastname
  87. vals['lastname'] = flds['firstname']
  88. return self.write(cursor, uid, partner_id, vals, context=context)
  89. def copy_data(self, cr, uid, _id, default=None, context=None):
  90. """Avoid to replicate the firstname into the name when
  91. duplicating a partner
  92. """
  93. default = default or {}
  94. if not default.get('lastname'):
  95. default = default.copy()
  96. default['lastname'] = (
  97. _('%s (copy)') % self.read(
  98. cr,
  99. uid,
  100. [_id],
  101. ['lastname'],
  102. context=context
  103. )[0]['lastname']
  104. )
  105. if default.get('name'):
  106. del(default['name'])
  107. return super(ResPartner, self).copy_data(
  108. cr, uid, _id, default, context=context
  109. )
  110. def create(self, cursor, uid, vals, context=None):
  111. """To support data backward compatibility we have to keep this
  112. overwrite even if we use fnct_inv: otherwise we can't create
  113. entry because lastname is mandatory and module will not install
  114. if there is demo data
  115. """
  116. to_use = vals
  117. if 'name' in vals:
  118. corr_vals = vals.copy()
  119. corr_vals['lastname'] = corr_vals['name']
  120. del(corr_vals['name'])
  121. to_use = corr_vals
  122. return super(ResPartner, self).create(
  123. cursor, uid, to_use, context=context
  124. )
  125. _columns = {
  126. 'name': fields.function(
  127. _compute_name_custom,
  128. string="Name",
  129. type="char",
  130. store=True,
  131. select=True,
  132. readonly=True,
  133. fnct_inv=_write_name
  134. ),
  135. 'firstname': fields.char("Firstname"),
  136. 'lastname': fields.char("Lastname", required=True),
  137. }