Skip to content

Commit

Permalink
[MIG] product_operating_unit: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidderen-nsi committed Jun 11, 2024
1 parent 7986cdc commit 88faccc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 66 deletions.
2 changes: 1 addition & 1 deletion product_operating_unit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Operating Unit in Products",
"summary": "Adds the concept of operating unit (OU) in products",
"version": "16.0.1.0.1",
"version": "17.0.1.0.0",
"author": "brain-tec AG, "
"Open Source Integrators, "
"Serpent Consulting Services Pvt. Ltd.,"
Expand Down
9 changes: 5 additions & 4 deletions product_operating_unit/models/product_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (C) 2019 Serpent Consulting Services
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
from odoo import fields, models
from odoo.models import Command


class ProductCategory(models.Model):
Expand All @@ -14,15 +15,15 @@ class ProductCategory(models.Model):
)

def write(self, vals):
res = super(ProductCategory, self).write(vals)
res = super().write(vals)
product_template_obj = self.env["product.template"]
if vals.get("operating_unit_ids"):
for rec in self:
products = product_template_obj.search(
[("categ_id", "child_of", rec.id)]
)
category_ou_ids = rec.operating_unit_ids
for product in products:
ou_ids = product.operating_unit_ids.ids
ou_ids.extend(vals.get("operating_unit_ids")[0][2])
product.operating_unit_ids = [(6, 0, ou_ids)]
ou_ids = product.operating_unit_ids | category_ou_ids
product.operating_unit_ids = [Command.set(ou_ids.ids)]
return res
79 changes: 24 additions & 55 deletions product_operating_unit/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@
# Copyright (C) 2019 Serpent Consulting Services
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).


import logging

from odoo import _, api, fields, models
from odoo.exceptions import AccessError, RedirectWarning, ValidationError

_logger = logging.getLogger(__name__)
from odoo.exceptions import ValidationError


class ProductTemplate(models.Model):
_inherit = "product.template"

operating_unit_ids = fields.Many2many(
"operating.unit",
"product_operating_unit_rel",
string="Operating Units",
compute="_compute_operating_unit_ids",
store=True,
readonly=False,
default=lambda self: self._default_operating_unit_ids(),
)
categ_id = fields.Many2one(
default=lambda self: self._get_default_category_id(),
)

@api.model
def _default_operating_unit_ids(self):
if self.categ_id and self.categ_id.operating_unit_ids:
return [(6, 0, self.categ_id.operating_unit_ids.ids)]
default_ou = self.env["res.users"].operating_unit_default_get(self.env.uid)
default_ou = self.env["res.users"]._get_default_operating_unit(self.env.uid)
if default_ou:
return [
(
Expand All @@ -29,12 +35,12 @@ def _default_operating_unit_ids(self):
)
]

operating_unit_ids = fields.Many2many(
"operating.unit",
"product_operating_unit_rel",
string="Operating Units",
default=_default_operating_unit_ids,
)
def _get_default_category_id(self):
category = self.env["product.category"].search([], limit=1)
if category:
return category.id
else:
return super()._get_default_category_id()

@api.constrains("operating_unit_ids", "categ_id")
def _check_operating_unit(self):
Expand All @@ -52,44 +58,7 @@ def _check_operating_unit(self):
)
)

@api.onchange("categ_id")
def onchange_operating_unit_ids(self):
@api.depends("categ_id")
def _compute_operating_unit_ids(self):
for record in self:
if record.categ_id.operating_unit_ids:
record.operating_unit_ids = [
(6, 0, record.categ_id.operating_unit_ids.ids)
]

def _get_default_category_id(self):
for ou_id in self.env.user.operating_unit_ids:
_logger.info("%s" % (ou_id.name))
category = self.env["product.category"].search([], limit=1)
if category:
return category.id
else:
try:
self.env.ref(
"product.product_category_all", raise_if_not_found=False
).name
except AccessError as exc:
err_msg = _(
"You must define at least one product \
category within your Operating Unit in order to be \
able to create products."
)
redir_msg = _("Go to Product Categories")
raise RedirectWarning(
err_msg,
self.env.ref("product.product_category_action_form").id,
redir_msg,
) from exc
return super()._get_default_category_id()

categ_id = fields.Many2one(
"product.category",
"Product Category",
change_default=True,
default=_get_default_category_id,
required=True,
help="Select category for the current product",
)
record.operating_unit_ids = [(6, 0, record.categ_id.operating_unit_ids.ids)]
4 changes: 2 additions & 2 deletions product_operating_unit/security/product_template_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<record id="ir_rule_product_template_allowed_operating_units" model="ir.rule">
<field name="model_id" ref="product.model_product_template" />
<field name="domain_force">['|', ('operating_unit_ids', '=', False),
('operating_unit_ids', 'in', user.operating_unit_ids.ids)]</field>
('operating_unit_ids', 'in', operating_unit_ids)]</field>
<field name="name">Product Templates from allowed operating units</field>
<field name="global" eval="True" />
<field eval="0" name="perm_unlink" />
Expand All @@ -21,7 +21,7 @@
<record id="ir_rule_product_category_allowed_operating_units" model="ir.rule">
<field name="model_id" ref="product.model_product_category" />
<field name="domain_force">['|', ('operating_unit_ids', '=', False),
('operating_unit_ids', 'in', user.operating_unit_ids.ids)]</field>
('operating_unit_ids', 'in', operating_unit_ids)]</field>
<field name="name">Product Category from allowed operating units</field>
<field name="global" eval="True" />
<field eval="0" name="perm_unlink" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def _create_user(self, login, groups, company, operating_units):
def test_po_ou_onchange(self):
with self.assertRaises(ValidationError):
self.product1.operating_unit_ids = [(6, 0, [self.b2b.id])]
self.product1.onchange_operating_unit_ids()

def test_po_ou_security(self):
"""Test Security of Product Operating Unit"""
Expand Down
1 change: 0 additions & 1 deletion product_operating_unit/views/product_category_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<field name="parent_id" position="after">
<field
name="operating_unit_ids"
domain="[('user_ids', 'in', uid)]"
groups="operating_unit.group_multi_operating_unit"
widget="many2many_tags"
/>
Expand Down
7 changes: 5 additions & 2 deletions product_operating_unit/views/product_template_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
<field name="inherit_id" ref="product.product_template_tree_view" />
<field name="arch" type="xml">
<field name="categ_id" position="after">
<field
name="company_id"
groups="!base.group_multi_company"
column_invisible="True"
/>
<field
name="operating_unit_ids"
widget="many2many_tags"
domain="[('user_ids', 'in', uid)]"
groups="operating_unit.group_multi_operating_unit"
/>
</field>
Expand All @@ -33,7 +37,6 @@
>
<field
name="operating_unit_ids"
domain="[('user_ids', 'in', uid)]"
groups="operating_unit.group_multi_operating_unit"
widget="many2many_tags"
/>
Expand Down

0 comments on commit 88faccc

Please sign in to comment.