7
7
8
8
from odoo import api , fields , models
9
9
from odoo .fields import first
10
+ from odoo .osv import expression
10
11
11
12
12
13
class StockMoveLocationWizard (models .TransientModel ):
13
14
_name = "wiz.stock.move.location"
14
15
_description = "Wizard move location"
15
16
16
- def _get_default_picking_type_id (self ):
17
- company_id = self .env .context .get ("company_id" ) or self .env .user .company_id .id
18
- return (
19
- self .env ["stock.picking.type" ]
20
- .search (
21
- [
22
- ("code" , "=" , "internal" ),
23
- ("warehouse_id.company_id" , "=" , company_id ),
24
- ],
25
- limit = 1 ,
26
- )
27
- .id
28
- )
29
-
30
17
origin_location_disable = fields .Boolean (
31
18
compute = "_compute_readonly_locations" ,
32
19
help = "technical field to disable the edition of origin location." ,
@@ -53,7 +40,10 @@ def _get_default_picking_type_id(self):
53
40
string = "Move Location lines" ,
54
41
)
55
42
picking_type_id = fields .Many2one (
56
- comodel_name = "stock.picking.type" , default = _get_default_picking_type_id
43
+ compute = "_compute_picking_type_id" ,
44
+ comodel_name = "stock.picking.type" ,
45
+ readonly = False ,
46
+ store = True ,
57
47
)
58
48
picking_id = fields .Many2one (
59
49
string = "Connected Picking" , comodel_name = "stock.picking"
@@ -74,6 +64,28 @@ def _compute_readonly_locations(self):
74
64
rec .origin_location_disable = True
75
65
rec .destination_location_disable = True
76
66
67
+ @api .depends_context ("company" )
68
+ @api .depends ("origin_location_id" )
69
+ def _compute_picking_type_id (self ):
70
+ company_id = self .env .context .get ("company_id" ) or self .env .user .company_id .id
71
+ for rec in self :
72
+ picking_type = self .env ["stock.picking.type" ]
73
+ base_domain = [
74
+ ("code" , "=" , "internal" ),
75
+ ("warehouse_id.company_id" , "=" , company_id ),
76
+ ]
77
+ if rec .origin_location_id :
78
+ location_id = rec .origin_location_id
79
+ while location_id and not picking_type :
80
+ domain = [("default_location_src_id" , "=" , location_id .id )]
81
+ domain = expression .AND ([base_domain , domain ])
82
+ picking_type = picking_type .search (domain , limit = 1 )
83
+ # Move up to the parent location if no picking type found
84
+ location_id = not picking_type and location_id .location_id or False
85
+ if not picking_type :
86
+ picking_type = picking_type .search (base_domain , limit = 1 )
87
+ rec .picking_type_id = picking_type .id
88
+
77
89
@api .model
78
90
def default_get (self , fields ):
79
91
res = super ().default_get (fields )
0 commit comments