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.

54 lines
1.8 KiB

  1. # Copyright 2019 Open Source Integrators
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import api, models
  4. class ResPartner(models.Model):
  5. _name = "res.partner"
  6. _inherit = ["res.partner", "tier.validation"]
  7. _tier_validation_buttons_xpath = "/form/header/field[@name='state']"
  8. _state_from = ["draft"]
  9. _state_to = ["confirmed"]
  10. _cancel_state = ["inactive"]
  11. _tier_validation_manual_config = False
  12. @api.model
  13. def _tier_revalidation_fields(self, values):
  14. """
  15. Changing some Partner fields forces Tier Validation to be reevaluated.
  16. Out of the box these are is_company and parent_id.
  17. Other can be added extending this method.
  18. """
  19. return ["is_company", "parent_id"]
  20. @api.model
  21. def _get_confirmed_stage(self):
  22. return self.env["res.partner.stage"].search(
  23. [("state", "in", self._state_to)], limit=1
  24. )
  25. @api.model
  26. def create(self, vals):
  27. new = super().create(vals)
  28. # Contact not requiring validation
  29. # are created in confirmed state
  30. if not new.need_validation:
  31. confirmed_stage = self._get_confirmed_stage()
  32. new.stage_id = confirmed_stage
  33. return new
  34. def write(self, vals):
  35. # Signal state transition by adding to vals
  36. if "stage_id" in vals:
  37. stage_id = vals.get("stage_id")
  38. stage = self.env["res.partner.stage"].browse(stage_id)
  39. vals["state"] = stage.state
  40. # Changing certain fields required new validation process
  41. revalidate_fields = self._tier_revalidation_fields(vals)
  42. if any(x in revalidate_fields for x in vals.keys()):
  43. self.mapped("review_ids").unlink()
  44. vals["state"] = "draft"
  45. self.request_validation()
  46. return super().write(vals)