@@ -28,15 +28,15 @@ class StockLocation(models.Model):
2828 # give the unique shuttle for any location in the tree (whether it's a
2929 # shuttle, a tray or a cell)
3030 inverse_vertical_lift_shuttle_ids = fields .One2many (
31- comodel_name = "vertical.lift.shuttle" , inverse_name = "location_id "
31+ comodel_name = "vertical.lift.shuttle" , inverse_name = "shared_storage_location_id "
3232 )
3333 # compute the unique shuttle for any shuttle, tray or cell location, by
3434 # going through the parents
3535 vertical_lift_shuttle_id = fields .Many2one (
3636 comodel_name = "vertical.lift.shuttle" ,
3737 compute = "_compute_vertical_lift_shuttle_id" ,
3838 recursive = True ,
39- store = True ,
39+ store = False ,
4040 )
4141
4242 @api .depends (
@@ -51,22 +51,23 @@ def _compute_vertical_lift_kind(self):
5151 kind = tree .get (location .location_id .vertical_lift_kind , False )
5252 location .vertical_lift_kind = kind
5353
54- @api .depends (
55- "inverse_vertical_lift_shuttle_ids" , "location_id.vertical_lift_shuttle_id"
56- )
54+ @api .depends ("inverse_vertical_lift_shuttle_ids" )
55+ @api .depends_context ("shuttle_id" )
5756 def _compute_vertical_lift_shuttle_id (self ):
57+ shuttle_id = self .env .context .get ("shuttle_id" )
5858 for location in self :
59- if location .inverse_vertical_lift_shuttle_ids :
60- # we have a unique constraint on the other side
61- assert len (location .inverse_vertical_lift_shuttle_ids ) == 1
59+ if len (location .inverse_vertical_lift_shuttle_ids ) == 1 :
6260 shuttle = location .inverse_vertical_lift_shuttle_ids
6361 else :
64- shuttle = location . location_id . vertical_lift_shuttle_id
62+ shuttle = self . env [ "vertical.lift.shuttle" ]. browse ( shuttle_id )
6563 location .vertical_lift_shuttle_id = shuttle
6664
6765 def _hardware_vertical_lift_fetch_tray (self , cell_location = None ):
68- payload = self ._hardware_vertical_lift_fetch_tray_payload (cell_location )
69- return self .vertical_lift_shuttle_id ._hardware_send_message (payload )
66+ shuttle = self .vertical_lift_shuttle_id
67+ payload = self ._hardware_vertical_lift_fetch_tray_payload (
68+ cell_location = cell_location
69+ )
70+ return shuttle ._hardware_send_message (payload )
7071
7172 def _hardware_vertical_lift_fetch_tray_payload (self , cell_location = None ):
7273 """Prepare "fetch" message to be sent to the vertical lift hardware
@@ -107,7 +108,8 @@ def _hardware_vertical_lift_fetch_tray_payload(self, cell_location=None):
107108 Returns a message in bytes, that will be sent through
108109 ``VerticalLiftShuttle._hardware_send_message()``.
109110 """
110- if self .vertical_lift_shuttle_id .hardware == "simulation" :
111+ shuttle = self .vertical_lift_shuttle_id
112+ if shuttle .hardware == "simulation" :
111113 message = self .env ._ ("Opening tray %(name)s." , name = self .name )
112114 if cell_location :
113115 from_left , from_bottom = cell_location .tray_cell_center_position ()
@@ -136,13 +138,20 @@ def fetch_vertical_lift_tray(self, cell_location=None):
136138 ``_hardware_vertical_lift_fetch_tray()``.
137139 """
138140 self .ensure_one ()
139- if self .vertical_lift_kind == "cell" :
140- if cell_location :
141- raise ValueError (
142- "cell_location cannot be set when the location is a cell."
141+ if self .vertical_lift_kind == "cell" and cell_location :
142+ raise ValueError ("cell_location cannot be set when the location is a cell." )
143+ if not (shuttle := self .vertical_lift_shuttle_id ):
144+ raise exceptions .UserError (
145+ self .env ._ (
146+ "Cannot determine which shuttle to use on location %s" ,
147+ self .name ,
143148 )
149+ )
150+ if self .vertical_lift_kind == "cell" :
144151 tray = self .location_id
145- tray .fetch_vertical_lift_tray (cell_location = self )
152+ tray .with_context (shuttle_id = shuttle .id ).fetch_vertical_lift_tray (
153+ cell_location = self
154+ )
146155 elif self .vertical_lift_kind == "tray" :
147156 self ._hardware_vertical_lift_fetch_tray (cell_location = cell_location )
148157 else :
@@ -154,14 +163,63 @@ def fetch_vertical_lift_tray(self, cell_location=None):
154163 )
155164 return True
156165
166+ def _get_shuttles (self ):
167+ self .ensure_one ()
168+ # Reached the top of hierarchy without finding a shuttle
169+ if not self :
170+ return self .env ["vertical.lift.shuttle" ]
171+ # Found a location linked to a shuttle
172+ if shuttles := self .inverse_vertical_lift_shuttle_ids :
173+ return shuttles
174+ # Check the parent location
175+ return self .location_id ._get_shuttles ()
176+
157177 def button_fetch_vertical_lift_tray (self ):
158178 self .ensure_one ()
159- if self .vertical_lift_kind in ("cell" , "tray" ):
160- self .fetch_vertical_lift_tray ()
161- return True
179+ if self .vertical_lift_kind not in ("cell" , "tray" ):
180+ return True
181+
182+ # If shuttle is explicitly provided (e.g. from wizard), use it
183+ if self .vertical_lift_shuttle_id :
184+ return self .fetch_vertical_lift_tray ()
185+
186+ # Otherwise, check for a unique link
187+ shuttles = self ._get_shuttles ()
188+ if len (shuttles ) == 1 :
189+ return self .with_context (shuttle_id = shuttles .id ).fetch_vertical_lift_tray ()
190+
191+ # If shared (len > 1) or no link (len == 0), open shuttle selector
192+ return self ._open_shuttle_selector ("button_fetch_vertical_lift_tray" )
162193
163194 def button_release_vertical_lift_tray (self ):
164195 self .ensure_one ()
165- if self .vertical_lift_kind :
166- self .vertical_lift_shuttle_id .release_vertical_lift_tray ()
167- return True
196+ if not self .vertical_lift_kind :
197+ return True
198+
199+ # If shuttle is explicitly provided (e.g. from wizard), use it
200+ if shuttle := self .vertical_lift_shuttle_id :
201+ return shuttle .release_vertical_lift_tray ()
202+
203+ # Otherwise, check for a unique link
204+ shuttles = self ._get_shuttles ()
205+ if len (shuttles ) == 1 :
206+ return shuttles .release_vertical_lift_tray ()
207+
208+ # If shared (len > 1) or no link (len == 0), open shuttle selector
209+ return self ._open_shuttle_selector ("button_release_vertical_lift_tray" )
210+
211+ def _open_shuttle_selector (self , method_name ):
212+ self .ensure_one ()
213+ return {
214+ "name" : self .env ._ ("Select Shuttle for %s" , self .name ),
215+ "type" : "ir.actions.act_window" ,
216+ "res_model" : "vertical.lift.select.shuttle" ,
217+ "view_mode" : "form" ,
218+ "target" : "new" ,
219+ "context" : {
220+ "default_location_id" : self .id ,
221+ "default_res_model" : self ._name ,
222+ "default_res_id" : self .id ,
223+ "default_method_name" : method_name ,
224+ },
225+ }
0 commit comments