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.

172 lines
5.9 KiB

FIX pylint (cherry picked from commit eadcae217103fd8f2f8d3db87c6fb659e048812b) Conflicts: partner_identification/__openerp__.py Set version to 8.0.1.0.0. Remove dependency on sales_team, as the relevant change is not in 8.0. Change emails to the new ones (cherry picked from commit 3455ae614e28d7807fc19d7be54512cbe59d44ec) Update new name (cherry picked from commit 5b592d7562fddac0cf48c71e6607cf17c009e993) [FIX] try me on runbot link (cherry picked from commit bd587b6058a17814cee5496a0ed3c126600f6fd6) Conflicts: partner_identification/README.rst Changed runbot to 8.0 [IMP] partner_identification: Add context override (#373) Allow for context override of validations using ``id_no_validate`` (cherry picked from commit 76c2e7b784916cdca0753a46a7b2be75edc1d70d) [10.0][IMP] partner_identification: Add field computation and inverses (#419) * [IMP] partner_identification: Add field computation and inverses * Add methods to allow for computation and inverse of an ID field of a specific category type * [IMP] partner_identification: Add search option (cherry picked from commit 19c5fb6de2a710dd50248fd843465f454de887bf) [FIX] partner_identification: Infinite loop in search (#436) (cherry picked from commit fa9b390dc62f66ef33acd7aacdfb3b79912ebc28) [FIX] partner-contact CI interactions (cherry picked from commit bc93e7bbc3e0f059b228970f0a05e57f0efba310) [ADD][8.0] Backport of the 9.0 module. (cherry picked from commit a42540381d448c3a62fabf69043ed23bf1aeca3e) [8.0][MIG] partner_identification backport
8 years ago
FIX pylint (cherry picked from commit eadcae217103fd8f2f8d3db87c6fb659e048812b) Conflicts: partner_identification/__openerp__.py Set version to 8.0.1.0.0. Remove dependency on sales_team, as the relevant change is not in 8.0. Change emails to the new ones (cherry picked from commit 3455ae614e28d7807fc19d7be54512cbe59d44ec) Update new name (cherry picked from commit 5b592d7562fddac0cf48c71e6607cf17c009e993) [FIX] try me on runbot link (cherry picked from commit bd587b6058a17814cee5496a0ed3c126600f6fd6) Conflicts: partner_identification/README.rst Changed runbot to 8.0 [IMP] partner_identification: Add context override (#373) Allow for context override of validations using ``id_no_validate`` (cherry picked from commit 76c2e7b784916cdca0753a46a7b2be75edc1d70d) [10.0][IMP] partner_identification: Add field computation and inverses (#419) * [IMP] partner_identification: Add field computation and inverses * Add methods to allow for computation and inverse of an ID field of a specific category type * [IMP] partner_identification: Add search option (cherry picked from commit 19c5fb6de2a710dd50248fd843465f454de887bf) [FIX] partner_identification: Infinite loop in search (#436) (cherry picked from commit fa9b390dc62f66ef33acd7aacdfb3b79912ebc28) [FIX] partner-contact CI interactions (cherry picked from commit bc93e7bbc3e0f059b228970f0a05e57f0efba310) [ADD][8.0] Backport of the 9.0 module. (cherry picked from commit a42540381d448c3a62fabf69043ed23bf1aeca3e) [8.0][MIG] partner_identification backport
8 years ago
FIX pylint (cherry picked from commit eadcae217103fd8f2f8d3db87c6fb659e048812b) Conflicts: partner_identification/__openerp__.py Set version to 8.0.1.0.0. Remove dependency on sales_team, as the relevant change is not in 8.0. Change emails to the new ones (cherry picked from commit 3455ae614e28d7807fc19d7be54512cbe59d44ec) Update new name (cherry picked from commit 5b592d7562fddac0cf48c71e6607cf17c009e993) [FIX] try me on runbot link (cherry picked from commit bd587b6058a17814cee5496a0ed3c126600f6fd6) Conflicts: partner_identification/README.rst Changed runbot to 8.0 [IMP] partner_identification: Add context override (#373) Allow for context override of validations using ``id_no_validate`` (cherry picked from commit 76c2e7b784916cdca0753a46a7b2be75edc1d70d) [10.0][IMP] partner_identification: Add field computation and inverses (#419) * [IMP] partner_identification: Add field computation and inverses * Add methods to allow for computation and inverse of an ID field of a specific category type * [IMP] partner_identification: Add search option (cherry picked from commit 19c5fb6de2a710dd50248fd843465f454de887bf) [FIX] partner_identification: Infinite loop in search (#436) (cherry picked from commit fa9b390dc62f66ef33acd7aacdfb3b79912ebc28) [FIX] partner-contact CI interactions (cherry picked from commit bc93e7bbc3e0f059b228970f0a05e57f0efba310) [ADD][8.0] Backport of the 9.0 module. (cherry picked from commit a42540381d448c3a62fabf69043ed23bf1aeca3e) [8.0][MIG] partner_identification backport
8 years ago
  1. # -*- coding: utf-8 -*-
  2. #
  3. # © 2004-2010 Tiny SPRL http://tiny.be
  4. # © 2010-2012 ChriCar Beteiligungs- und Beratungs- GmbH
  5. # http://www.camptocamp.at
  6. # © 2015 Antiun Ingenieria, SL (Madrid, Spain)
  7. # http://www.antiun.com
  8. # Antonio Espinosa <antonioea@antiun.com>
  9. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  10. from openerp import api, models, fields, _
  11. from openerp.exceptions import ValidationError
  12. class ResPartner(models.Model):
  13. _inherit = 'res.partner' # pylint: disable=R7980
  14. id_numbers = fields.One2many(
  15. comodel_name='res.partner.id_number',
  16. inverse_name='partner_id',
  17. string="Identification Numbers",
  18. )
  19. @api.multi
  20. @api.depends('id_numbers')
  21. def _compute_identification(self, field_name, category_code):
  22. """ Compute a field that indicates a certain ID type.
  23. Use this on a field that represents a certain ID type. It will compute
  24. the desired field as that ID(s).
  25. This ID can be worked with as if it were a Char field, but it will
  26. be relating back to a ``res.partner.id_number`` instead.
  27. Example:
  28. .. code-block:: python
  29. social_security = fields.Char(
  30. compute=lambda s: s._compute_identification(
  31. 'social_security', 'SSN',
  32. ),
  33. inverse=lambda s: s._inverse_identification(
  34. 'social_security', 'SSN',
  35. ),
  36. search=lambda s, *a: s._search_identification(
  37. 'SSN', *a
  38. ),
  39. )
  40. Args:
  41. field_name (str): Name of field to set.
  42. category_code (str): Category code of the Identification type.
  43. """
  44. for record in self:
  45. id_numbers = record.id_numbers.filtered(
  46. lambda r: r.category_id.code == category_code
  47. )
  48. if not id_numbers:
  49. continue
  50. value = id_numbers[0].name
  51. record[field_name] = value
  52. @api.multi
  53. def _inverse_identification(self, field_name, category_code):
  54. """ Inverse for an identification field.
  55. This method will create a new record, or modify the existing one
  56. in order to allow for the associated field to work like a Char.
  57. If a category does not exist of the correct code, it will be created
  58. using `category_code` as both the `name` and `code` values.
  59. If the value of the target field is unset, the associated ID will
  60. be deactivated in order to preserve history.
  61. Example:
  62. .. code-block:: python
  63. social_security = fields.Char(
  64. compute=lambda s: s._compute_identification(
  65. 'social_security', 'SSN',
  66. ),
  67. inverse=lambda s: s._inverse_identification(
  68. 'social_security', 'SSN',
  69. ),
  70. search=lambda s, *a: s._search_identification(
  71. 'SSN', *a
  72. ),
  73. )
  74. Args:
  75. field_name (str): Name of field to set.
  76. category_code (str): Category code of the Identification type.
  77. """
  78. for record in self:
  79. id_number = record.id_numbers.filtered(
  80. lambda r: r.category_id.code == category_code
  81. )
  82. record_len = len(id_number)
  83. # Record for category is not existent.
  84. if record_len == 0:
  85. name = record[field_name]
  86. if not name:
  87. # No value to set
  88. continue
  89. category = self.env['res.partner.id_category'].search([
  90. ('code', '=', category_code),
  91. ])
  92. if not category:
  93. category = self.env['res.partner.id_category'].create({
  94. 'code': category_code,
  95. 'name': category_code,
  96. })
  97. self.env['res.partner.id_number'].create({
  98. 'partner_id': record.id,
  99. 'category_id': category.id,
  100. 'name': name,
  101. })
  102. # There was an identification record singleton found.
  103. elif record_len == 1:
  104. value = record[field_name]
  105. if value:
  106. id_number.name = value
  107. else:
  108. id_number.active = False
  109. # Guard against writing wrong records.
  110. else:
  111. raise ValidationError(_(
  112. 'This %s has multiple IDs of this type (%s), so a write '
  113. 'via the %s field is not possible. In order to fix this, '
  114. 'please use the IDs tab.',
  115. ) % (
  116. record._name, category_code, field_name,
  117. ))
  118. @api.model
  119. def _search_identification(self, category_code, operator, value):
  120. """ Search method for an identification field.
  121. Example:
  122. .. code-block:: python
  123. social_security = fields.Char(
  124. compute=lambda s: s._compute_identification(
  125. 'social_security', 'SSN',
  126. ),
  127. inverse=lambda s: s._inverse_identification(
  128. 'social_security', 'SSN',
  129. ),
  130. search=lambda s, *a: s._search_identification(
  131. 'SSN', *a
  132. ),
  133. )
  134. Args:
  135. category_code (str): Category code of the Identification type.
  136. operator (str): Operator of domain.
  137. value (str): Value to search for.
  138. Returns:
  139. list: Domain to search with.
  140. """
  141. id_numbers = self.env['res.partner.id_number'].search([
  142. ('name', operator, value),
  143. ('category_id.code', '=', category_code),
  144. ])
  145. return [
  146. ('id_numbers.id', 'in', id_numbers.ids),
  147. ]