Skip to content
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
30 changes: 30 additions & 0 deletions stock_move_location/tests/test_move_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,33 @@ def test_delivery_order_assignation_after_transfer(self):
self.assertEqual(len(delivery_move.move_line_ids), 1)
self.assertEqual(delivery_move.move_line_ids.reserved_uom_qty, 20.0)
self.assertEqual(delivery_move.move_line_ids.location_id, wh_stock_shelf_3)

def test_picking_type_preserved_on_origin_change(self):
"""picking_type_id must not be reset when origin_location changes."""
wizard = self._create_wizard(self.internal_loc_1, self.internal_loc_2)
# Pick a picking type different from the default to assert preservation
other_picking_type = self.env["stock.picking.type"].search(
[
("code", "in", ("internal", "outgoing")),
("id", "!=", wizard.picking_type_id.id),
],
limit=1,
)
self.assertTrue(
other_picking_type,
"Test requires at least two picking types to exist",
)
wizard.picking_type_id = other_picking_type
wizard.origin_location_id = self.internal_loc_2
wizard.onchange_origin_location()
self.assertEqual(wizard.picking_type_id, other_picking_type)

def test_destination_propagated_to_lines_without_dest(self):
"""Lines without destination receive the wizard's destination on transfer."""
wizard = self._create_wizard(self.internal_loc_1, self.internal_loc_2)
wizard.onchange_origin_location()
# Simulate a line that ended up without destination
wizard.stock_move_location_line_ids[0].destination_location_id = False
wizard.action_move_location()
self.assertTrue(wizard.picking_id)
self.assertEqual(wizard.picking_id.location_dest_id, self.internal_loc_2)
45 changes: 19 additions & 26 deletions stock_move_location/wizard/stock_move_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,26 @@

from odoo import api, fields, models
from odoo.fields import first
from odoo.osv import expression


class StockMoveLocationWizard(models.TransientModel):
_name = "wiz.stock.move.location"
_description = "Wizard move location"

def _get_default_picking_type_id(self):
company_id = self.env.context.get("company_id") or self.env.company.id
return (
self.env["stock.picking.type"]
.search(
[
("code", "in", ("internal", "outgoing")),
("warehouse_id.company_id", "=", company_id),
],
limit=1,
)
.id
)

origin_location_disable = fields.Boolean(
compute="_compute_readonly_locations",
help="technical field to disable the edition of origin location.",
Expand All @@ -40,10 +53,8 @@ class StockMoveLocationWizard(models.TransientModel):
string="Move Location lines",
)
picking_type_id = fields.Many2one(
compute="_compute_picking_type_id",
comodel_name="stock.picking.type",
readonly=False,
store=True,
default=_get_default_picking_type_id,
)
picking_id = fields.Many2one(
string="Connected Picking", comodel_name="stock.picking"
Expand All @@ -64,28 +75,6 @@ def _compute_readonly_locations(self):
rec.origin_location_disable = True
rec.destination_location_disable = True

@api.depends_context("company")
@api.depends("origin_location_id")
def _compute_picking_type_id(self):
company_id = self.env.context.get("company_id") or self.env.company.id
for rec in self:
picking_type = self.env["stock.picking.type"]
base_domain = [
("code", "in", ("internal", "outgoing")),
("warehouse_id.company_id", "=", company_id),
]
if rec.origin_location_id:
location_id = rec.origin_location_id
while location_id and not picking_type:
domain = [("default_location_src_id", "=", location_id.id)]
domain = expression.AND([base_domain, domain])
picking_type = picking_type.search(domain, limit=1)
# Move up to the parent location if no picking type found
location_id = not picking_type and location_id.location_id or False
if not picking_type:
picking_type = picking_type.search(base_domain, limit=1)
rec.picking_type_id = picking_type.id

@api.model
def default_get(self, fields):
res = super().default_get(fields)
Expand Down Expand Up @@ -281,6 +270,10 @@ def _unreserve_moves(self):

def action_move_location(self):
self.ensure_one()
# Ensure all lines have a destination location
for line in self.stock_move_location_line_ids:
if not line.destination_location_id:
line.destination_location_id = self.destination_location_id
if not self.picking_id:
picking = self._create_picking()
else:
Expand Down
Loading