Skip to content

Commit

Permalink
[IMP] product_abc_classification: add product smart button in profile
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow authored and rousseldenis committed Nov 10, 2022
1 parent ece4791 commit b56a5bf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
37 changes: 37 additions & 0 deletions product_abc_classification/models/abc_classification_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class AbcClassificationProfile(models.Model):
required=True,
)

product_variant_ids = fields.Many2many(
comodel_name="product.product",
relation="abc_classification_profile_product_rel",
column1="profile_id",
column2="product_id",
index=True,
)
product_count = fields.Integer(compute="_compute_product_count", readonly=True)

auto_apply_computed_value = fields.Boolean(default=False)

_sql_constraints = [("name_uniq", "UNIQUE(name)", _("Profile name must be unique"))]
Expand Down Expand Up @@ -60,6 +69,34 @@ def _check_levels(self):
def _compute_abc_classification(self):
raise NotImplementedError()

@api.depends("product_variant_ids")
def _compute_product_count(self):
for profile in self:
profile.product_count = len(profile.product_variant_ids)

def action_view_products(self):
products = self.mapped("product_variant_ids")
action = self.env["ir.actions.act_window"].for_xml_id(
"product", "product_variant_action"
)
del action["context"]
if len(products) > 1:
action["domain"] = [("id", "in", products.ids)]
elif len(products) == 1:
form_view = [
(self.env.ref("product.product_variant_easy_edit_view").id, "form")
]
if "views" in action:
action["views"] = form_view + [
(state, view) for state, view in action["views"] if view != "form"
]
else:
action["views"] = form_view
action["res_id"] = products.id
else:
action = {"type": "ir.actions.act_window_close"}
return action

@api.model
def _cron_compute_abc_classification(self):
self.search([])._compute_abc_classification()
Expand Down
10 changes: 10 additions & 0 deletions product_abc_classification/views/abc_classification_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
<field name="arch" type="xml">
<form string="ABC Profile">
<sheet>
<div class="oe_button_box" name="button_box">
<button
name="action_view_products"
type="object"
class="oe_stat_button"
icon="fa-cubes"
>
<field name="product_count" widget="statinfo" string="Products" />
</button>
</div>
<group>
<field name="name" />
<field name="level_ids">
Expand Down

0 comments on commit b56a5bf

Please sign in to comment.