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.

50 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", "cancel"]
  9. _state_to = ["confirmed"]
  10. _cancel_state = ["inactive"]
  11. _tier_validation_manual_config = False
  12. @api.model
  13. def _partner_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. # IDEA: make it a System Parameter?
  20. return [
  21. "company_type",
  22. "parent_id",
  23. "vat",
  24. "state_id",
  25. "country_id",
  26. "property_account_position_id",
  27. "property_account_receivable_id",
  28. "property_account_payable_id",
  29. ]
  30. def write(self, vals):
  31. # Changing certain fields requires a new validation process
  32. revalidate_fields = self._partner_tier_revalidation_fields(vals)
  33. if any(x in revalidate_fields for x in vals.keys()):
  34. vals["stage_id"] = self._get_default_stage_id().id
  35. # Tier Validation does not work with Stages, only States :-(
  36. # Workaround is to signal state transition adding it to the write values
  37. if "stage_id" in vals:
  38. stage_id = vals.get("stage_id")
  39. stage = self.env["res.partner.stage"].browse(stage_id)
  40. vals["state"] = stage.state
  41. res = super().write(vals)
  42. if "stage_id" in vals and vals.get("stage_id") in self._state_from:
  43. self.restart_validation()
  44. return res