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.

47 lines
1.5 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, fields, models
  4. class ResPartner(models.Model):
  5. _name = "res.partner"
  6. _inherit = ["res.partner", "tier.validation"]
  7. _tier_validation_manual_config = False
  8. state = fields.Selection(
  9. [("draft", "Draft"), ("confirmed", "Active"), ("cancel", "Archived")],
  10. string="Status",
  11. default="draft",
  12. )
  13. @api.model
  14. def _tier_revalidation_fields(self, values):
  15. """
  16. Changing some Partner fields forces Tier Validation to be reevaluated.
  17. Out of the box these are is_company and parent_id.
  18. Other can be added extenting this method.
  19. """
  20. return ["is_company", "parent_id"]
  21. @api.model
  22. def create(self, vals):
  23. new = super().create(vals)
  24. if new.need_validation and new.state != "confirmed":
  25. new.active = False
  26. else:
  27. new.active = True
  28. new.state = "confirmed"
  29. return new
  30. def write(self, vals):
  31. # Changing certain fields required new validation process
  32. revalidate_fields = self._tier_revalidation_fields(vals)
  33. if any(x in revalidate_fields for x in vals.keys()):
  34. self.mapped("review_ids").unlink()
  35. vals["state"] = "draft"
  36. # Automatically update active flag depending on state
  37. if "state" in vals:
  38. vals["active"] = vals["state"] == "confirmed"
  39. return super().write(vals)