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.

71 lines
2.5 KiB

  1. # Copyright 2019 Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import _, api, fields, models
  4. class Contact(models.Model):
  5. _inherit = "res.partner"
  6. company_group_id = fields.Many2one(
  7. "res.partner", "Company group", domain=[("is_company", "=", True)]
  8. )
  9. company_group_member_ids = fields.One2many(
  10. comodel_name="res.partner",
  11. inverse_name="company_group_id",
  12. string="Company group members",
  13. )
  14. def _commercial_fields(self):
  15. return super()._commercial_fields() + ["company_group_id"]
  16. @api.onchange("company_group_id")
  17. def _onchange_company_group_id(self):
  18. res = {}
  19. if (
  20. self.company_group_id
  21. and self.company_group_id.property_product_pricelist
  22. != self.property_product_pricelist
  23. ):
  24. res["warning"] = {
  25. "title": _("Warning"),
  26. "message": _(
  27. "The company group %s has the pricelist %s, that is different than"
  28. " the pricelist set on this contact"
  29. )
  30. % (
  31. self.company_group_id.display_name,
  32. self.company_group_id.property_product_pricelist.display_name,
  33. ),
  34. }
  35. return res
  36. @api.onchange("property_product_pricelist")
  37. def _onchange_property_product_pricelist(self):
  38. res = self._onchange_company_group_id()
  39. if (
  40. not res
  41. and self.company_group_member_ids
  42. # Need to check _origin because the field company_group_ids is a recordset of
  43. # NewIds that have False values on the field property_product_pricelist.
  44. and self.company_group_member_ids._origin.mapped(
  45. "property_product_pricelist"
  46. )
  47. - self.property_product_pricelist
  48. ):
  49. company_members = self.company_group_member_ids.filtered(
  50. lambda cm: cm.property_product_pricelist
  51. != self.property_product_pricelist
  52. )
  53. members_str = ""
  54. for member in company_members:
  55. members_str += "\t- %s\n" % member.display_name
  56. res["warning"] = {
  57. "title": _("Warning"),
  58. "message": _(
  59. "This contact has members of a company group with"
  60. " different pricelists, the members are:\n%s"
  61. )
  62. % members_str,
  63. }
  64. return res