Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][FIX] stock_location_package_restriction for backorder #1797

Merged
Merged
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
28 changes: 22 additions & 6 deletions stock_location_package_restriction/models/stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
class StockMove(models.Model):
_inherit = "stock.move"

def _check_location_package_restriction(self):
"""
Check if the current stock.move can be executed
regarding a potential package restriction
def _check_location_package_restriction(
self, new_location=None, only_qty_done=True
):
"""Check if the moves can be done regarding potential package restrictions

By default will only check move lines with a quantity done and their set
destination location.

If `new_location` is set it will be use as destination location.
If `only_qty_done` is False the check is executed on all
related move lines.

"""
Package = self.env["stock.quant.package"]
error_msgs = []
dest_location = new_location or self.move_line_ids.location_dest_id
quants_grouped = self.env["stock.quant"].read_group(
[
("location_id", "in", self.move_line_ids.location_dest_id.ids),
("location_id", "in", dest_location.ids),
("location_id.package_restriction", "!=", False),
("quantity", ">", 0),
],
Expand All @@ -31,12 +40,19 @@
location_packages = {
g["location_id"][0]: set(g["package_id"]) for g in quants_grouped
}
lines_being_processed = (
self.move_line_ids.filtered(lambda line: line.qty_done)
if only_qty_done
else self.move_line_ids
)
for location, move_lines in groupby(
self.move_line_ids, lambda m: m.location_dest_id
lines_being_processed, lambda m: m.location_dest_id
):
if not location.package_restriction:
continue

if new_location:
location = new_location

Check warning on line 55 in stock_location_package_restriction/models/stock_move.py

View check run for this annotation

Codecov / codecov/patch

stock_location_package_restriction/models/stock_move.py#L55

Added line #L55 was not covered by tests
existing_package_ids = location_packages.get(location.id, set())
existing_packages = Package.browse(existing_package_ids).exists()
new_packages = Package.browse()
Expand Down
43 changes: 43 additions & 0 deletions stock_location_package_restriction/tests/test_stock_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,49 @@ def test_03(self):
with self.assertRaises(ValidationError):
self._process_picking(picking)

def test_03_with_backorder(self):
"""
Data:
location_2 without package but with package restriction = 'single package'
a picking with two move with destination location_2 but only one move is
being processsed, backorder creation.
Test case:
Process the picking
Expected result:
One package in location no error
"""
self.location_2.package_restriction = SINGLEPACKAGE
picking = self._create_and_assign_picking(
[
ShortMoveInfo(
product=self.product_1,
location_dest=self.location_2,
qty=2,
package_id=self.pack_1,
),
ShortMoveInfo(
product=self.product_2,
location_dest=self.location_2,
qty=2,
package_id=self.pack_2,
),
],
location_dest=self.location_2,
)
picking.action_assign()
# Processing only one move out of two
line_to_process = picking.move_line_ids[0]
line_to_process.qty_done = line_to_process.product_qty
wizard_action = picking.button_validate()
wizard_context = wizard_action.get("context", {})
wizard = (
self.env[wizard_action["res_model"]]
.with_context(**wizard_context)
.create({})
)
wizard.process()
self.assertEqual(self.pack_1, self._get_package_in_location(self.location_2))

def test_04(self):
"""
Data:
Expand Down
Loading