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.

156 lines
6.3 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  2. from odoo import api, fields, models
  3. from odoo.osv import expression
  4. class ResPartner(models.Model):
  5. _inherit = "res.partner"
  6. contact_type = fields.Selection(
  7. [
  8. ("standalone", "Standalone Contact"),
  9. ("attached", "Attached to existing Contact"),
  10. ],
  11. compute="_compute_contact_type",
  12. store=True,
  13. index=True,
  14. default="standalone")
  15. contact_id = fields.Many2one(
  16. "res.partner",
  17. string="Main Contact",
  18. domain=[("is_company", "=", False), ("contact_type", "=", "standalone"), ],
  19. )
  20. other_contact_ids = fields.One2many(
  21. "res.partner", "contact_id", string="Others Positions",
  22. )
  23. @api.depends("contact_id")
  24. def _compute_contact_type(self):
  25. for rec in self:
  26. rec.contact_type = "attached" if rec.contact_id else "standalone"
  27. def _basecontact_check_context(self, mode):
  28. """ Remove "search_show_all_positions" for non-search mode.
  29. Keeping it in context can result in unexpected behaviour (ex: reading
  30. one2many might return wrong result - i.e with "attached contact"
  31. removed even if it"s directly linked to a company).
  32. Actually, is easier to override a dictionary value to indicate it
  33. should be ignored...
  34. """
  35. if mode != "search" and "search_show_all_positions" in self.env.context:
  36. result = self.with_context(search_show_all_positions={"is_set": False})
  37. else:
  38. result = self
  39. return result
  40. @api.model
  41. def search(self, args, offset=0, limit=None, order=None, count=False):
  42. """ Display only standalone contact matching ``args`` or having
  43. attached contact matching ``args`` """
  44. ctx = self.env.context
  45. if (
  46. ctx.get("search_show_all_positions", {}).get("is_set")
  47. and not ctx["search_show_all_positions"]["set_value"]
  48. ):
  49. args = expression.normalize_domain(args)
  50. attached_contact_args = expression.AND(
  51. (args, [("contact_type", "=", "attached")])
  52. )
  53. attached_contacts = super(ResPartner, self).search(attached_contact_args)
  54. args = expression.OR(
  55. (
  56. expression.AND(([("contact_type", "=", "standalone")], args)),
  57. [("other_contact_ids", "in", attached_contacts.ids)],
  58. )
  59. )
  60. return super(ResPartner, self).search(
  61. args, offset=offset, limit=limit, order=order, count=count
  62. )
  63. @api.model
  64. def create(self, vals):
  65. """ When creating, use a modified self to alter the context (see
  66. comment in _basecontact_check_context). Also, we need to ensure
  67. that the name on an attached contact is the same as the name on the
  68. contact it is attached to."""
  69. modified_self = self._basecontact_check_context("create")
  70. if not vals.get("name") and vals.get("contact_id"):
  71. vals["name"] = modified_self.browse(vals["contact_id"]).name
  72. return super(ResPartner, modified_self).create(vals)
  73. def read(self, fields=None, load="_classic_read"):
  74. modified_self = self._basecontact_check_context("read")
  75. return super(ResPartner, modified_self).read(fields=fields, load=load)
  76. def write(self, vals):
  77. modified_self = self._basecontact_check_context("write")
  78. return super(ResPartner, modified_self).write(vals)
  79. def unlink(self):
  80. modified_self = self._basecontact_check_context("unlink")
  81. return super(ResPartner, modified_self).unlink()
  82. def _compute_commercial_partner(self):
  83. """ Returns the partner that is considered the commercial
  84. entity of this partner. The commercial entity holds the master data
  85. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  86. result = super(ResPartner, self)._compute_commercial_partner()
  87. for partner in self:
  88. if partner.contact_type == "attached" and not partner.parent_id:
  89. partner.commercial_partner_id = partner.contact_id
  90. return result
  91. def _contact_fields(self):
  92. """ Returns the list of contact fields that are synced from the parent
  93. when a partner is attached to him. """
  94. return ["name", "title"]
  95. def _contact_sync_from_parent(self):
  96. """ Handle sync of contact fields when a new parent contact entity
  97. is set, as if they were related fields
  98. """
  99. self.ensure_one()
  100. if self.contact_id:
  101. contact_fields = self._contact_fields()
  102. sync_vals = self.contact_id._update_fields_values(contact_fields)
  103. self.write(sync_vals)
  104. def update_contact(self, vals):
  105. if self.env.context.get("__update_contact_lock"):
  106. return
  107. contact_fields = self._contact_fields()
  108. contact_vals = {
  109. field: vals[field] for field in contact_fields if field in vals
  110. }
  111. if contact_vals:
  112. self.with_context(__update_contact_lock=True).write(contact_vals)
  113. def _fields_sync(self, update_values):
  114. """Sync commercial fields and address fields from company and to
  115. children, contact fields from contact and to attached contact
  116. after create/update, just as if those were all modeled as
  117. fields.related to the parent
  118. """
  119. self.ensure_one()
  120. super(ResPartner, self)._fields_sync(update_values)
  121. contact_fields = self._contact_fields()
  122. # 1. From UPSTREAM: sync from parent contact
  123. if update_values.get("contact_id"):
  124. self._contact_sync_from_parent()
  125. # 2. To DOWNSTREAM: sync contact fields to parent or related
  126. elif any(field in contact_fields for field in update_values):
  127. update_ids = self.other_contact_ids.filtered(lambda p: not p.is_company)
  128. if self.contact_id:
  129. update_ids |= self.contact_id
  130. update_ids.update_contact(update_values)
  131. @api.onchange("contact_id")
  132. def _onchange_contact_id(self):
  133. if self.contact_id:
  134. self.name = self.contact_id.name
  135. @api.onchange("contact_type")
  136. def _onchange_contact_type(self):
  137. if self.contact_type == "standalone":
  138. self.contact_id = False