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.

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