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.

148 lines
5.5 KiB

11 years ago
11 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 = (
  65. field_value if field_value and not field_value.isspace() else False
  66. )
  67. vals = {'lastname': field_value, 'firstname': False}
  68. if field_value:
  69. flds = self.read(
  70. cursor,
  71. uid,
  72. [partner_id],
  73. ['firstname', 'is_company'],
  74. context=context
  75. )[0]
  76. if not flds['is_company']:
  77. to_check = ' %s' % flds['firstname']
  78. if field_value.endswith(to_check):
  79. ln = field_value[:-len(to_check)].strip()
  80. if ln:
  81. vals['lastname'] = ln
  82. del(vals['firstname'])
  83. else:
  84. # If the lastname is deleted from the new name
  85. # then the firstname becomes the lastname
  86. vals['lastname'] = flds['firstname']
  87. return self.write(cursor, uid, partner_id, vals, context=context)
  88. def copy_data(self, cr, uid, _id, default=None, context=None):
  89. """Avoid to replicate the firstname into the name when
  90. duplicating a partner
  91. """
  92. default = default or {}
  93. if not default.get('lastname'):
  94. default = default.copy()
  95. default['lastname'] = (
  96. _('%s (copy)') % self.read(
  97. cr,
  98. uid,
  99. [_id],
  100. ['lastname'],
  101. context=context
  102. )[0]['lastname']
  103. )
  104. if default.get('name'):
  105. del(default['name'])
  106. return super(ResPartner, self).copy_data(
  107. cr, uid, _id, default, context=context
  108. )
  109. def create(self, cursor, uid, vals, context=None):
  110. """To support data backward compatibility we have to keep this
  111. overwrite even if we use fnct_inv: otherwise we can't create
  112. entry because lastname is mandatory and module will not install
  113. if there is demo data
  114. """
  115. to_use = vals
  116. if 'name' in vals:
  117. corr_vals = vals.copy()
  118. corr_vals['lastname'] = corr_vals['name']
  119. del(corr_vals['name'])
  120. to_use = corr_vals
  121. return super(ResPartner, self).create(
  122. cursor, uid, to_use, context=context
  123. )
  124. _columns = {
  125. 'name': fields.function(
  126. _compute_name_custom,
  127. string="Name",
  128. type="char",
  129. store=True,
  130. select=True,
  131. readonly=True,
  132. fnct_inv=_write_name
  133. ),
  134. 'firstname': fields.char("Firstname"),
  135. 'lastname': fields.char("Lastname", required=True),
  136. }