2021-03-23 19:13:18 +00:00
|
|
|
# Copyright 2019 Open Source Integrators
|
|
|
|
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
|
|
|
|
2022-06-29 18:38:32 +00:00
|
|
|
from flectra import api, models
|
2021-03-23 19:13:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ResPartner(models.Model):
|
|
|
|
_name = "res.partner"
|
2021-04-28 15:43:20 +00:00
|
|
|
_inherit = ["res.partner", "tier.validation"]
|
2021-03-23 19:13:18 +00:00
|
|
|
|
2022-06-29 18:38:32 +00:00
|
|
|
_tier_validation_buttons_xpath = "/form/header/field[@name='state']"
|
|
|
|
_state_from = ["draft", "cancel"]
|
|
|
|
_state_to = ["confirmed"]
|
|
|
|
_cancel_state = ["inactive"]
|
|
|
|
_tier_validation_manual_config = False
|
2021-04-28 15:43:20 +00:00
|
|
|
|
2021-05-30 02:14:14 +00:00
|
|
|
@api.model
|
2022-06-29 18:38:32 +00:00
|
|
|
def _partner_tier_revalidation_fields(self, values):
|
2021-05-30 02:14:14 +00:00
|
|
|
"""
|
|
|
|
Changing some Partner fields forces Tier Validation to be reevaluated.
|
|
|
|
Out of the box these are is_company and parent_id.
|
2022-06-29 18:38:32 +00:00
|
|
|
Other can be added extending this method.
|
2021-05-30 02:14:14 +00:00
|
|
|
"""
|
2022-06-29 18:38:32 +00:00
|
|
|
# IDEA: make it a System Parameter?
|
|
|
|
return [
|
|
|
|
"company_type",
|
|
|
|
"parent_id",
|
|
|
|
"vat",
|
|
|
|
"state_id",
|
|
|
|
"country_id",
|
|
|
|
"property_account_position_id",
|
|
|
|
"property_account_receivable_id",
|
|
|
|
"property_account_payable_id",
|
|
|
|
]
|
2021-04-28 15:43:20 +00:00
|
|
|
|
|
|
|
def write(self, vals):
|
2022-06-29 18:38:32 +00:00
|
|
|
# Changing certain fields requires a new validation process
|
|
|
|
revalidate_fields = self._partner_tier_revalidation_fields(vals)
|
2021-05-30 02:14:14 +00:00
|
|
|
if any(x in revalidate_fields for x in vals.keys()):
|
2022-06-29 18:38:32 +00:00
|
|
|
vals["stage_id"] = self._get_default_stage_id().id
|
|
|
|
# Tier Validation does not work with Stages, only States :-(
|
|
|
|
# Workaround is to signal state transition adding it to the write values
|
|
|
|
if "stage_id" in vals:
|
|
|
|
stage_id = vals.get("stage_id")
|
|
|
|
stage = self.env["res.partner.stage"].browse(stage_id)
|
|
|
|
vals["state"] = stage.state
|
|
|
|
res = super().write(vals)
|
|
|
|
if "stage_id" in vals and vals.get("stage_id") in self._state_from:
|
|
|
|
self.restart_validation()
|
|
|
|
return res
|