Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0] [ADD] stock_inventory_security #2189

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions setup/stock_inventory_security/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
1 change: 0 additions & 1 deletion stock_inventory_discrepancy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

from . import models
from . import wizards
from .hooks import post_load_hook
1 change: 0 additions & 1 deletion stock_inventory_discrepancy/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"wizards/confirm_discrepancy_wiz.xml",
],
"license": "AGPL-3",
"post_load": "post_load_hook",
"installable": True,
"application": False,
}
76 changes: 0 additions & 76 deletions stock_inventory_discrepancy/hooks.py

This file was deleted.

11 changes: 11 additions & 0 deletions stock_inventory_discrepancy/models/stock_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ class StockLocation(models.Model):
)

def write(self, values):
# OVERRIDE: Allow the inventory user to set the last inventory date.
# https://github.com/odoo/odoo/blob/534220ee/addons/stock/models/stock_quant.py#L775
if (
self.env.context.get("_stock_inventory_discrepancy")
and len(values) == 1
and "last_inventory_date" in values
and self.user_has_groups(
"stock_inventory_discrepancy.group_stock_inventory_validation"
)
):
self = self.sudo()
res = super().write(values)
# Set the discrepancy threshold for all child locations
if values.get("discrepancy_threshold", False):
Expand Down
17 changes: 17 additions & 0 deletions stock_inventory_discrepancy/models/stock_quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ def _compute_has_over_discrepancy(self):
rec.discrepancy_percent > rec.discrepancy_threshold
)

def user_has_groups(self, groups):
# Most inventory adjustment operations are limited to users having
# the Inventory Manager group.
# OVERRIDE: Hijack the check to replace it with our own group.
if groups == "stock.group_stock_manager" and self.env.context.get(
"_stock_inventory_discrepancy"
):
groups = "stock_inventory_discrepancy.group_stock_inventory_validation"
return super().user_has_groups(groups)

def _apply_inventory(self):
if self.user_has_groups(
"stock_inventory_discrepancy.group_stock_inventory_validation"
):
self = self.with_context(_stock_inventory_discrepancy=True)
return super()._apply_inventory()

def action_apply_inventory(self):
if self.env.context.get("skip_exceeded_discrepancy", False):
return super().action_apply_inventory()
Expand Down
1 change: 1 addition & 0 deletions stock_inventory_security/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# To be generated by bot
1 change: 1 addition & 0 deletions stock_inventory_security/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions stock_inventory_security/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Stock Quant Inventory Security",
"summary": "Dedicated security group to apply inventory adjustments",
"version": "16.0.1.0.0",
"author": "Camptocamp, Odoo Community Association (OCA)",
"maintainers": ["ivantodorovich"],
"website": "https://github.com/OCA/stock-logistics-warehouse",
"license": "AGPL-3",
"category": "Inventory",
"depends": ["stock"],
"data": [
"security/security.xml",
"security/ir.model.access.csv",
"views/product.xml",
"views/stock_quant.xml",
],
"demo": [
"demo/demo.xml",
],
}
15 changes: 15 additions & 0 deletions stock_inventory_security/demo/demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>

<record id="base.user_demo" model="res.users">
<field
name="groups_id"
eval="[Command.link(ref('group_inventory_adjustment'))]"
/>
</record>

</odoo>
4 changes: 4 additions & 0 deletions stock_inventory_security/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import product_product
from . import stock_location
from . import stock_lot
from . import stock_quant
23 changes: 23 additions & 0 deletions stock_inventory_security/models/product_product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class ProductProduct(models.Model):
_inherit = "product.product"

def user_has_groups(self, groups):
# Most inventory adjustment operations are limited to users having
# the Inventory Manager group.
# OVERRIDE: Hijack the check to replace it with our own group.
if groups == "stock.group_stock_manager" and self.env.context.get(
"_stock_inventory_security"
):
groups = "stock_inventory_security.group_inventory_adjustment"
return super().user_has_groups(groups)

def action_open_quants(self):
if self.user_has_groups("stock_inventory_security.group_inventory_adjustment"):
ivantodorovich marked this conversation as resolved.
Show resolved Hide resolved
self = self.with_context(_stock_inventory_security=True)
return super().action_open_quants()
22 changes: 22 additions & 0 deletions stock_inventory_security/models/stock_location.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class StockLocation(models.Model):
_inherit = "stock.location"

def write(self, vals):
# OVERRIDE: Allow the inventory user to set the last inventory date.
# https://github.com/odoo/odoo/blob/534220ee/addons/stock/models/stock_quant.py#L775
if (
self.env.context.get("_stock_inventory_security")
and len(vals) == 1
and "last_inventory_date" in vals
and self.user_has_groups(
"stock_inventory_security.group_inventory_adjustment"
)
):
self = self.sudo()
return super().write(vals)
24 changes: 24 additions & 0 deletions stock_inventory_security/models/stock_lot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class StockLot(models.Model):
_inherit = "stock.lot"

def user_has_groups(self, groups):
# Most inventory adjustment operations are limited to users having
# the Inventory Manager group.
# OVERRIDE: Hijack the check to replace it with our own group.)
if groups == "stock.group_stock_manager" and self.env.context.get(
"_stock_inventory_security"
):
groups = "stock_inventory_security.group_inventory_adjustment"
return super().user_has_groups(groups)

def action_lot_open_quants(self):
# OVERRIDE: Add the inventory_mode context
if self.user_has_groups("stock_inventory_security.group_inventory_adjustment"):
self = self.with_context(_stock_inventory_security=True)
return super().action_lot_open_quants()
43 changes: 43 additions & 0 deletions stock_inventory_security/models/stock_quant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class StockQuant(models.Model):
_inherit = "stock.quant"

inventory_quantity_auto_apply = fields.Float(
# Change stock.group_stock_manager to our own group.
groups="stock_inventory_security.group_inventory_adjustment",
)

def user_has_groups(self, groups):
# Most inventory adjustment operations are limited to users having
# the Inventory Manager group.
# OVERRIDE: Hijack the check to replace it with our own group.
if groups == "stock.group_stock_manager" and self.env.context.get(
"_stock_inventory_security"
):
groups = "stock_inventory_security.group_inventory_adjustment"
return super().user_has_groups(groups)

def _get_quants_action(self, domain=None, extend=False):
# OVERRIDE: Show the editable quants view for users having the Inventory
# Adjustments group.
# The original method would only do it for Stock Managers.
if self.user_has_groups("stock_inventory_security.group_inventory_adjustment"):
self = self.with_context(_stock_inventory_security=True)
return super()._get_quants_action(domain=domain, extend=extend)

def action_view_inventory(self):
# OVERRIDE: Disable the "My count" filter for users having the Inventory
# Adjustments group.
if self.user_has_groups("stock_inventory_security.group_inventory_adjustment"):
self = self.with_context(_stock_inventory_security=True)
return super().action_view_inventory()

def _apply_inventory(self):
if self.user_has_groups("stock_inventory_security.group_inventory_adjustment"):
self = self.with_context(_stock_inventory_security=True)
return super()._apply_inventory()
1 change: 1 addition & 0 deletions stock_inventory_security/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Iván Todorovich <[email protected]>
4 changes: 4 additions & 0 deletions stock_inventory_security/readme/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
In standard, the inventory adjustments can only be applied by **Inventory / Manager** users.

This module introduces a new security group named **Stock: Inventory Adjustments**, which
grants regular stock users the ability to apply inventory adjustments.
3 changes: 3 additions & 0 deletions stock_inventory_security/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_stock_inventory_adjustment_name,stock.inventory.adjustment.name,stock.model_stock_inventory_adjustment_name,group_inventory_adjustment,1,1,1,0
access_stock_request_count,stock.request.count,stock.model_stock_request_count,group_inventory_adjustment,1,1,1,0
19 changes: 19 additions & 0 deletions stock_inventory_security/security/security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">

<record id="group_inventory_adjustment" model="res.groups">
<field name="name">Stock: Inventory Adjustments</field>
<field
name="implied_ids"
eval="[Command.link(ref('stock.group_stock_user'))]"
/>
</record>

<record id="stock.group_stock_manager" model="res.groups">
<field
name="implied_ids"
eval="[Command.link(ref('group_inventory_adjustment'))]"
/>
</record>

</odoo>
1 change: 1 addition & 0 deletions stock_inventory_security/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_stock_inventory_security
Loading
Loading