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.

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