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.

134 lines
5.5 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Nicolas Bessi. Copyright Camptocamp SA
  5. # Copyright (C) 2014 Agile Business Group (<http://www.agilebg.com>)
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp.osv import orm, fields
  22. from openerp.tools.translate import _
  23. import logging
  24. _logger = logging.getLogger(__name__)
  25. class ResPartner(orm.Model):
  26. """Adds lastname and firstname, name become a stored function field"""
  27. _inherit = 'res.partner'
  28. def _set_default_value_on_column(self, cr, column_name, context=None):
  29. res = super(ResPartner, self)._set_default_value_on_column(
  30. cr, column_name, context=context)
  31. if column_name == 'lastname':
  32. cr.execute('UPDATE res_partner SET lastname = name WHERE name '
  33. 'IS NOT NULL AND lastname IS NULL')
  34. cr.execute('ALTER TABLE res_partner ALTER COLUMN lastname '
  35. 'SET NOT NULL')
  36. _logger.info("NOT NULL constraint for "
  37. "res_partner.lastname correctly set")
  38. return res
  39. def _prepare_name_custom(self, cursor, uid, partner, context=None):
  40. """
  41. This function is designed to be inherited in a custom module
  42. """
  43. names = (partner.lastname, partner.firstname)
  44. fullname = " ".join([s for s in names if s])
  45. return fullname
  46. def _compute_name_custom(self, cursor, uid, ids, fname, arg, context=None):
  47. res = {}
  48. for partner in self.browse(cursor, uid, ids, context=context):
  49. res[partner.id] = self._prepare_name_custom(
  50. cursor, uid, partner, context=context)
  51. return res
  52. def _write_name(
  53. self, cursor, uid, partner_id, field_name, field_value, arg,
  54. context=None
  55. ):
  56. """
  57. Try to reverse the effect of _compute_name_custom:
  58. * if the partner is not a company and the firstname does not change in
  59. the new name then firstname remains untouched and lastname is updated
  60. accordingly
  61. * otherwise lastname=new name and firstname=False
  62. In addition an heuristic avoids to keep a firstname without a non-blank
  63. lastname
  64. """
  65. field_value = (
  66. field_value and not field_value.isspace() and field_value or False)
  67. vals = {'lastname': field_value, 'firstname': False}
  68. if field_value:
  69. flds = self.read(
  70. cursor, uid, [partner_id], ['firstname', 'is_company'],
  71. context=context)[0]
  72. if not flds['is_company']:
  73. to_check = ' %s' % flds['firstname']
  74. if field_value.endswith(to_check):
  75. ln = field_value[:-len(to_check)].strip()
  76. if ln:
  77. vals['lastname'] = ln
  78. del(vals['firstname'])
  79. else:
  80. # If the lastname is deleted from the new name
  81. # then the firstname becomes the lastname
  82. vals['lastname'] = flds['firstname']
  83. return self.write(cursor, uid, partner_id, vals, context=context)
  84. def copy_data(self, cr, uid, id, default=None, context=None):
  85. """
  86. Avoid to replicate the firstname into the name when duplicating a
  87. partner
  88. """
  89. default = default or {}
  90. if not default.get('lastname'):
  91. default = default.copy()
  92. default['lastname'] = (
  93. _('%s (copy)') % self.read(
  94. cr, uid, [id], ['lastname'], context=context
  95. )[0]['lastname']
  96. )
  97. if default.get('name'):
  98. del(default['name'])
  99. return super(ResPartner, self).copy_data(
  100. cr, uid, id, default, context=context)
  101. def create(self, cursor, uid, vals, context=None):
  102. """
  103. To support data backward compatibility we have to keep this overwrite
  104. even if we use fnct_inv: otherwise we can't create entry because
  105. lastname is mandatory and module will not install if there is demo data
  106. """
  107. to_use = vals
  108. if 'name' in vals:
  109. corr_vals = vals.copy()
  110. if vals.get('name'):
  111. corr_vals['lastname'] = corr_vals['name']
  112. del(corr_vals['name'])
  113. to_use = corr_vals
  114. return super(ResPartner, self).create(
  115. cursor, uid, to_use, context=context)
  116. _columns = {'name': fields.function(_compute_name_custom, string="Name",
  117. type="char", store=True,
  118. select=True, readonly=True,
  119. fnct_inv=_write_name),
  120. 'firstname': fields.char("Firstname"),
  121. 'lastname': fields.char("Lastname", required=True)}