33
44from odoo import _ , api , models
55from odoo .exceptions import ValidationError
6- from odoo .tools import config
76
87
98class 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