Skip to content

Commit

Permalink
[FIX] sale_partner_approval: Fixed Can Sell is lazy updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikul-OSI committed Mar 25, 2022
1 parent 89806d6 commit c78f684
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
1 change: 1 addition & 0 deletions sale_partner_approval/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"data": [
"data/exception_rule.xml",
"data/init_sale_ok.xml",
"views/res_partner_stage.xml",
"views/res_partner.xml",
],
Expand Down
9 changes: 9 additions & 0 deletions sale_partner_approval/data/init_sale_ok.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<odoo noupdate="1">
<!--
To not cause disruption upon install, set pre-existing Partners as Can Sale
-->
<function model="res.partner" name="write">
<value model="res.partner" search="[]" />
<value eval="{'sale_ok': True, 'candidate_sale': True}" />
</function>
</odoo>
52 changes: 35 additions & 17 deletions sale_partner_approval/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,42 @@ class Partner(models.Model):
_inherit = "res.partner"

sale_ok = fields.Boolean(
compute="_compute_sale_ok_product",
string="Can Sell To",
default=False,
store=True,
)
candidate_sale = fields.Boolean(
string="Candidate to Sell To",
default=True,
copy=False,
readonly=True,
)
candidate_sale = fields.Boolean(string="Candidate to Sell To")

@api.model
def default_get(self, fields):
res = super().default_get(fields)
if "candidate_sale" in fields and "candidate_sale" not in res:
# Set default when creating from Sale App menu
res["candidate_sale"] = bool(res.get("customer_rank"))
return res

@api.depends("candidate_sale", "stage_id.approved_sale", "parent_id.sale_ok")
def _compute_sale_ok_product(self):
for partner in self:
# Can Sell To if is candidate and current stage allows it
# Child contacts inherit that same sale_ok value
if partner == partner.commercial_partner_id:
partner.sale_ok = (
partner.candidate_sale and partner.stage_id.approved_sale
)
def _set_sale_ok(self):
# Candidate Sale is set/changed only in draft state
# Can Sale is set only when not in draft state
# This allows for Candidate Sale changes to be effective only after approval
for partner in self.filtered(lambda x: x.stage_id.state != "draft"):
if partner.parent_id:
# Child contacts inherit that same sale_ok value
ok = partner.parent_id.sale_ok
else:
partner.sale_ok = partner.parent_id.sale_ok
ok = partner.candidate_sale and partner.stage_id.approved_sale
super(Partner, partner).write({"sale_ok": ok})

@api.model
def create(self, vals):
new = super().create(vals)
new._set_sale_ok()
return new

def write(self, vals):
res = super().write(vals)
# Do not set ok flag when moving to draft state
if "stage_id" in vals:
to_state = self.env["res.partner.stage"].browse(vals["stage_id"]).state
to_state != "draft" and self._set_sale_ok()
return res
6 changes: 6 additions & 0 deletions sale_partner_approval/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Require partners to be approved before thay can be used on Sales Orders.

This does not require, but was designed to be used with
Partner Tier Validation (partner_tier_validation module).

Using that module, rules can be configured on who must approve,
before the Partner Stage can be changed to an apptoved one.
5 changes: 5 additions & 0 deletions sale_partner_approval/readme/USAGE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ To approve a Partner to be used in sales:
If this is the case, the Partner will automatically have enabled the "Can Sell To" checkbox.
found next to the "Candidate to Sell" checkbox.

The "Candidate to Sell" checkbox is only available in a draft/to approve Stage.
The "Can Sell To" will only be set when moving to a Stage that is not draft/to approve.
Moving from an approved Stage to a draft one will not automatically reset the "Can Sell To".
This means that removing from Can Sale state also needs to go through an approval.

On a Sales Order:

* The "Customer" and "Invoice Address" selection lists
Expand Down
3 changes: 2 additions & 1 deletion sale_partner_approval/views/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
expr="//group[@name='sale']/field[@name='user_id']"
position="before"
>
<field name="candidate_sale" />
<field name="state" invisible="1" />
<field name="candidate_sale" states="draft" />
<field name="sale_ok" />
</xpath>

Expand Down

0 comments on commit c78f684

Please sign in to comment.