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

[IMP] stock_archive_constraint #40

Open
wants to merge 1 commit into
base: 16.0-mig-stock_archive_constraint
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
3 changes: 3 additions & 0 deletions stock_archive_constraint/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"development_status": "Production/Stable",
"category": "Warehouse",
"depends": ["stock"],
"data": [
"views/res_company.xml",
],
"installable": True,
"maintainers": ["victoralmau"],
}
1 change: 1 addition & 0 deletions stock_archive_constraint/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

from . import product_product
from . import stock_location
from . import res_company
65 changes: 34 additions & 31 deletions stock_archive_constraint/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,25 @@

from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo.tools import config


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

def _skip_check_archive_constraint_condition(self):
return config["test_enable"] and not self.env.context.get(
"test_stock_archive_constraint"
)

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_quant(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.quant"].search(
[
("location_id.usage", "in", ("internal", "transit")),
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("quantity", "!=", 0.0),
],
limit=1,
res = (
self.sudo()
.env["stock.quant"]
.search(
[
("location_id.usage", "in", ("internal", "transit")),
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("quantity", "!=", 0.0),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand All @@ -37,14 +34,17 @@ def _check_active_stock_archive_constraint_stock_quant(self):

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_move(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.move"].search(
[
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("state", "not in", ("done", "cancel")),
],
limit=1,
res = (
self.sudo()
.env["stock.move"]
.search(
[
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("state", "not in", ("done", "cancel")),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand All @@ -57,14 +57,17 @@ def _check_active_stock_archive_constraint_stock_move(self):

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_move_line(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.move.line"].search(
[
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("state", "not in", ("done", "cancel")),
],
limit=1,
res = (
self.sudo()
.env["stock.move.line"]
.search(
[
("product_id", "in", self.filtered(lambda x: not x.active).ids),
("state", "not in", ("done", "cancel")),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand Down
13 changes: 13 additions & 0 deletions stock_archive_constraint/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Akretion France (http://www.akretion.com/)
# @author: Mathieu Delva <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

active_stock_constraint = fields.Boolean(
string="Active Stock constraint on product archive action",
)
93 changes: 54 additions & 39 deletions stock_archive_constraint/models/stock_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@

from odoo import _, api, models
from odoo.exceptions import ValidationError
from odoo.tools import config


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

def _skip_check_archive_constraint_condition(self):
return config["test_enable"] and not self.env.context.get(
"test_stock_archive_constraint"
)

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_quant(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.quant"].search(
[
"&",
("location_id.usage", "in", ("internal", "transit")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
],
limit=1,
res = (
self.sudo()
.env["stock.quant"]
.search(
[
"&",
("location_id.usage", "in", ("internal", "transit")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
(
"location_id",
"child_of",
self.filtered(lambda x: not x.active).ids,
),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand All @@ -39,17 +40,24 @@ def _check_active_stock_archive_constraint_stock_quant(self):

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_move(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.move"].search(
[
"&",
("state", "not in", ("done", "cancel")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
],
limit=1,
res = (
self.sudo()
.env["stock.move"]
.search(
[
"&",
("state", "not in", ("done", "cancel")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
(
"location_id",
"child_of",
self.filtered(lambda x: not x.active).ids,
),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand All @@ -62,17 +70,24 @@ def _check_active_stock_archive_constraint_stock_move(self):

@api.constrains("active")
def _check_active_stock_archive_constraint_stock_move_line(self):
if self._skip_check_archive_constraint_condition():
return
res = self.env["stock.move.line"].search(
[
"&",
("state", "not in", ("done", "cancel")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
],
limit=1,
res = (
self.sudo()
.env["stock.move.line"]
.search(
[
"&",
("state", "not in", ("done", "cancel")),
"|",
("location_id", "in", self.filtered(lambda x: not x.active).ids),
(
"location_id",
"child_of",
self.filtered(lambda x: not x.active).ids,
),
("company_id.active_stock_constraint", "=", True),
],
limit=1,
)
)
if res:
raise ValidationError(
Expand Down
Loading