Skip to content

Commit 96dcbea

Browse files
committed
[IMP] stock_archive_constraint
1 parent 56aed70 commit 96dcbea

7 files changed

Lines changed: 270 additions & 76 deletions

File tree

stock_archive_constraint/__manifest__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"development_status": "Production/Stable",
1111
"category": "Warehouse",
1212
"depends": ["stock"],
13+
"data": [
14+
"views/res_company.xml",
15+
],
1316
"installable": True,
1417
"maintainers": ["victoralmau"],
1518
}

stock_archive_constraint/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
from . import product_product
55
from . import stock_location
6+
from . import res_company

stock_archive_constraint/models/product_product.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,25 @@
33

44
from odoo import _, api, models
55
from odoo.exceptions import ValidationError
6-
from odoo.tools import config
76

87

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

12-
def _skip_check_archive_constraint_condition(self):
13-
return config["test_enable"] and not self.env.context.get(
14-
"test_stock_archive_constraint"
15-
)
16-
1711
@api.constrains("active")
1812
def _check_active_stock_archive_constraint_stock_quant(self):
19-
if self._skip_check_archive_constraint_condition():
20-
return
21-
res = self.env["stock.quant"].search(
22-
[
23-
("location_id.usage", "in", ("internal", "transit")),
24-
("product_id", "in", self.filtered(lambda x: not x.active).ids),
25-
("quantity", "!=", 0.0),
26-
],
27-
limit=1,
13+
res = (
14+
self.sudo()
15+
.env["stock.quant"]
16+
.search(
17+
[
18+
("location_id.usage", "in", ("internal", "transit")),
19+
("product_id", "in", self.filtered(lambda x: not x.active).ids),
20+
("quantity", "!=", 0.0),
21+
("company_id.active_stock_constraint", "=", True),
22+
],
23+
limit=1,
24+
)
2825
)
2926
if res:
3027
raise ValidationError(
@@ -37,14 +34,17 @@ def _check_active_stock_archive_constraint_stock_quant(self):
3734

3835
@api.constrains("active")
3936
def _check_active_stock_archive_constraint_stock_move(self):
40-
if self._skip_check_archive_constraint_condition():
41-
return
42-
res = self.env["stock.move"].search(
43-
[
44-
("product_id", "in", self.filtered(lambda x: not x.active).ids),
45-
("state", "not in", ("done", "cancel")),
46-
],
47-
limit=1,
37+
res = (
38+
self.sudo()
39+
.env["stock.move"]
40+
.search(
41+
[
42+
("product_id", "in", self.filtered(lambda x: not x.active).ids),
43+
("state", "not in", ("done", "cancel")),
44+
("company_id.active_stock_constraint", "=", True),
45+
],
46+
limit=1,
47+
)
4848
)
4949
if res:
5050
raise ValidationError(
@@ -57,14 +57,17 @@ def _check_active_stock_archive_constraint_stock_move(self):
5757

5858
@api.constrains("active")
5959
def _check_active_stock_archive_constraint_stock_move_line(self):
60-
if self._skip_check_archive_constraint_condition():
61-
return
62-
res = self.env["stock.move.line"].search(
63-
[
64-
("product_id", "in", self.filtered(lambda x: not x.active).ids),
65-
("state", "not in", ("done", "cancel")),
66-
],
67-
limit=1,
60+
res = (
61+
self.sudo()
62+
.env["stock.move.line"]
63+
.search(
64+
[
65+
("product_id", "in", self.filtered(lambda x: not x.active).ids),
66+
("state", "not in", ("done", "cancel")),
67+
("company_id.active_stock_constraint", "=", True),
68+
],
69+
limit=1,
70+
)
6871
)
6972
if res:
7073
raise ValidationError(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 Akretion France (http://www.akretion.com/)
2+
# @author: Mathieu Delva <mathieu.delva@akretion.com>
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
4+
5+
from odoo import fields, models
6+
7+
8+
class ResCompany(models.Model):
9+
_inherit = "res.company"
10+
11+
active_stock_constraint = fields.Boolean(
12+
string="Active Stock constraint on product archive action",
13+
)

stock_archive_constraint/models/stock_location.py

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33

44
from odoo import _, api, models
55
from odoo.exceptions import ValidationError
6-
from odoo.tools import config
76

87

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

12-
def _skip_check_archive_constraint_condition(self):
13-
return config["test_enable"] and not self.env.context.get(
14-
"test_stock_archive_constraint"
15-
)
16-
1711
@api.constrains("active")
1812
def _check_active_stock_archive_constraint_stock_quant(self):
19-
if self._skip_check_archive_constraint_condition():
20-
return
21-
res = self.env["stock.quant"].search(
22-
[
23-
"&",
24-
("location_id.usage", "in", ("internal", "transit")),
25-
"|",
26-
("location_id", "in", self.filtered(lambda x: not x.active).ids),
27-
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
28-
],
29-
limit=1,
13+
res = (
14+
self.sudo()
15+
.env["stock.quant"]
16+
.search(
17+
[
18+
"&",
19+
("location_id.usage", "in", ("internal", "transit")),
20+
"|",
21+
("location_id", "in", self.filtered(lambda x: not x.active).ids),
22+
(
23+
"location_id",
24+
"child_of",
25+
self.filtered(lambda x: not x.active).ids,
26+
),
27+
("company_id.active_stock_constraint", "=", True),
28+
],
29+
limit=1,
30+
)
3031
)
3132
if res:
3233
raise ValidationError(
@@ -39,17 +40,24 @@ def _check_active_stock_archive_constraint_stock_quant(self):
3940

4041
@api.constrains("active")
4142
def _check_active_stock_archive_constraint_stock_move(self):
42-
if self._skip_check_archive_constraint_condition():
43-
return
44-
res = self.env["stock.move"].search(
45-
[
46-
"&",
47-
("state", "not in", ("done", "cancel")),
48-
"|",
49-
("location_id", "in", self.filtered(lambda x: not x.active).ids),
50-
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
51-
],
52-
limit=1,
43+
res = (
44+
self.sudo()
45+
.env["stock.move"]
46+
.search(
47+
[
48+
"&",
49+
("state", "not in", ("done", "cancel")),
50+
"|",
51+
("location_id", "in", self.filtered(lambda x: not x.active).ids),
52+
(
53+
"location_id",
54+
"child_of",
55+
self.filtered(lambda x: not x.active).ids,
56+
),
57+
("company_id.active_stock_constraint", "=", True),
58+
],
59+
limit=1,
60+
)
5361
)
5462
if res:
5563
raise ValidationError(
@@ -62,17 +70,24 @@ def _check_active_stock_archive_constraint_stock_move(self):
6270

6371
@api.constrains("active")
6472
def _check_active_stock_archive_constraint_stock_move_line(self):
65-
if self._skip_check_archive_constraint_condition():
66-
return
67-
res = self.env["stock.move.line"].search(
68-
[
69-
"&",
70-
("state", "not in", ("done", "cancel")),
71-
"|",
72-
("location_id", "in", self.filtered(lambda x: not x.active).ids),
73-
("location_id", "child_of", self.filtered(lambda x: not x.active).ids),
74-
],
75-
limit=1,
73+
res = (
74+
self.sudo()
75+
.env["stock.move.line"]
76+
.search(
77+
[
78+
"&",
79+
("state", "not in", ("done", "cancel")),
80+
"|",
81+
("location_id", "in", self.filtered(lambda x: not x.active).ids),
82+
(
83+
"location_id",
84+
"child_of",
85+
self.filtered(lambda x: not x.active).ids,
86+
),
87+
("company_id.active_stock_constraint", "=", True),
88+
],
89+
limit=1,
90+
)
7691
)
7792
if res:
7893
raise ValidationError(

0 commit comments

Comments
 (0)