Daniel Reis
3 years ago
7 changed files with 60 additions and 74 deletions
-
2partner_tier_validation/__init__.py
-
4partner_tier_validation/__manifest__.py
-
6partner_tier_validation/data/tier_definition.xml
-
18partner_tier_validation/hooks.py
-
41partner_tier_validation/models/res_partner.py
-
47partner_tier_validation/tests/test_tier_validation.py
-
16partner_tier_validation/views/res_partner_view.xml
@ -1,4 +1,2 @@ |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
|
|||
from . import models |
|||
from .hooks import post_init_hook |
@ -1,13 +1,11 @@ |
|||
<odoo noupdate="1"> |
|||
<record id="partner_tier_definition_company_only" model="tier.definition"> |
|||
<field name="name">Partner Validation (Company)</field> |
|||
<field name="name">Validate New Company</field> |
|||
<field name="model_id" ref="base.model_res_partner" /> |
|||
<field name="review_type">group</field> |
|||
<field name="reviewer_group_id" ref="base.group_user" /> |
|||
<field name="definition_type">domain</field> |
|||
<field name="active" eval="False" /> |
|||
<field |
|||
name="definition_domain" |
|||
>["&",["is_company","=",True],"|",["active","=",True],["active","=",False]]</field> |
|||
<field name="definition_domain">[["is_company","=",True]]</field> |
|||
</record> |
|||
</odoo> |
@ -1,18 +0,0 @@ |
|||
# Copyright 2021 Tecnativa - Víctor Martínez |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). |
|||
|
|||
from odoo import SUPERUSER_ID |
|||
from odoo.api import Environment |
|||
from odoo.tools import config |
|||
|
|||
|
|||
def post_init_hook(cr, pool): |
|||
""" |
|||
We need to activate the rule only if we are not in a test environment. |
|||
""" |
|||
if not config["test_enable"]: |
|||
env = Environment(cr, SUPERUSER_ID, {}) |
|||
tier_partner = env.ref( |
|||
"partner_tier_validation.partner_tier_definition_company_only" |
|||
) |
|||
tier_partner.write({"active": True}) |
@ -1,47 +1,54 @@ |
|||
# Copyright 2019 Open Source Integrators |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
|
|||
from odoo import api, fields, models |
|||
from odoo import api, models |
|||
|
|||
|
|||
class ResPartner(models.Model): |
|||
_name = "res.partner" |
|||
_inherit = ["res.partner", "tier.validation"] |
|||
_tier_validation_manual_config = False |
|||
|
|||
state = fields.Selection( |
|||
[("draft", "Draft"), ("confirmed", "Active"), ("cancel", "Archived")], |
|||
string="Status", |
|||
default="draft", |
|||
) |
|||
_tier_validation_buttons_xpath = "/form/header/field[@name='state']" |
|||
_state_from = ["draft"] |
|||
_state_to = ["confirmed"] |
|||
_cancel_state = ["inactive"] |
|||
_tier_validation_manual_config = False |
|||
|
|||
@api.model |
|||
def _tier_revalidation_fields(self, values): |
|||
""" |
|||
Changing some Partner fields forces Tier Validation to be reevaluated. |
|||
Out of the box these are is_company and parent_id. |
|||
Other can be added extenting this method. |
|||
Other can be added extending this method. |
|||
""" |
|||
return ["is_company", "parent_id"] |
|||
|
|||
@api.model |
|||
def _get_confirmed_stage(self): |
|||
return self.env["res.partner.stage"].search( |
|||
[("state", "in", self._state_to)], limit=1 |
|||
) |
|||
|
|||
@api.model |
|||
def create(self, vals): |
|||
new = super().create(vals) |
|||
if new.need_validation and new.state != "confirmed": |
|||
new.active = False |
|||
else: |
|||
new.active = True |
|||
new.state = "confirmed" |
|||
# Contact not requiring validation |
|||
# are created in confirmed state |
|||
if not new.need_validation: |
|||
confirmed_stage = self._get_confirmed_stage() |
|||
new.stage_id = confirmed_stage |
|||
return new |
|||
|
|||
def write(self, vals): |
|||
# Signal state transition by adding to vals |
|||
if "stage_id" in vals: |
|||
stage_id = vals.get("stage_id") |
|||
stage = self.env["res.partner.stage"].browse(stage_id) |
|||
vals["state"] = stage.state |
|||
# Changing certain fields required new validation process |
|||
revalidate_fields = self._tier_revalidation_fields(vals) |
|||
if any(x in revalidate_fields for x in vals.keys()): |
|||
self.mapped("review_ids").unlink() |
|||
vals["state"] = "draft" |
|||
|
|||
# Automatically update active flag depending on state |
|||
if "state" in vals: |
|||
vals["active"] = vals["state"] == "confirmed" |
|||
self.request_validation() |
|||
return super().write(vals) |
Write
Preview
Loading…
Cancel
Save
Reference in new issue