Skip to content

Commit

Permalink
[FIX] stock_picking_auto_create_lot_qty: Immediate Transfer create lo…
Browse files Browse the repository at this point in the history
…t for every n qty
  • Loading branch information
dessanhemrayev committed Sep 8, 2024
1 parent cd75e77 commit 4eacb70
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
61 changes: 51 additions & 10 deletions stock_picking_auto_create_lot_qty/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,66 @@ def _set_auto_lot(self):
"""
pickings = self.filtered(lambda p: p.picking_type_id.auto_create_lot)
immediate_pickings = self.filtered(lambda p: p.immediate_transfer)
new_lines = self.env["stock.move.line"].browse()
lines, new_lines = self._split_stock_move_lines(pickings)
if immediate_pickings:
return super()._set_auto_lot()
if immediate_pickings and lines:
return self._create_lots_for_immediate_pickings(lines)
if new_lines or not lines:
return
for line in lines:
product_id = line.product_id
product_qty = line.reserved_uom_qty
current_product_qty = product_id.uom_id._compute_quantity(
product_qty,
product_id.batch_uom_id or product_id.uom_id,
round=False,
)

if product_id.create_lot_every_n:
product_qty = line.reserved_uom_qty
current_product_qty = product_id.uom_id._compute_quantity(
product_qty,
product_id.batch_uom_id or product_id.uom_id,
round=False,
)
new_lines |= self._prepare_stock_move_lines_for_lots(
product_id, line, current_product_qty
)
else:
new_lines |= line
for line in new_lines:

self._set_lotname(new_lines)

@api.model
def _create_lots_for_immediate_pickings(self, lines):
"""
Create stock move lines for immediate pickings,
handling products that require lot creation.
Args:
lines (stock.move.line): A recordset of stock move lines to process.
"""
new_lines = self.env["stock.move.line"].browse()
old_lines = self.env["stock.move.line"].browse()
for line in lines:
product_id = line.product_id

if product_id.create_lot_every_n:
product_qty = line.reserved_uom_qty
current_product_qty = product_id.uom_id._compute_quantity(
product_qty,
product_id.batch_uom_id or product_id.uom_id,
round=False,
)
old_lines |= line
new_lines |= self._prepare_stock_move_lines_for_lots(
product_id, line, current_product_qty
)
else:
new_lines |= line

Check warning on line 159 in stock_picking_auto_create_lot_qty/models/stock_picking.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_auto_create_lot_qty/models/stock_picking.py#L159

Added line #L159 was not covered by tests
old_lines.update({"qty_done": 0})
self._set_lotname(new_lines)

@api.model
def _set_lotname(self, lines):
"""
Set the lot name for each stock move line in the provided recordset.
Args:
lines (stock.move.line): A recordset of stock move lines to update with lot names.
"""
for line in lines:
line.lot_name = line._get_lot_sequence()
12 changes: 9 additions & 3 deletions stock_picking_auto_create_lot_qty/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _create_product(
)

@classmethod
def _create_picking(cls, multicompany=False):
def _create_picking(cls, multicompany=False, immediate_transfer=False):
"""
Create a new picking and set the following properties:
Expand All @@ -68,7 +68,10 @@ def _create_picking(cls, multicompany=False):
)
cls.picking = (
cls.env["stock.picking"]
.with_context(default_picking_type_id=picking_type_in.id)
.with_context(
default_picking_type_id=picking_type_in.id,
default_immediate_transfer=immediate_transfer,
)
.with_company(company)
.create(
{
Expand All @@ -80,7 +83,9 @@ def _create_picking(cls, multicompany=False):
)

@classmethod
def _create_move(cls, product=None, qty=1.0, multicompany=False):
def _create_move(
cls, product=None, qty=1.0, multicompany=False, immediate_transfer=False
):
"""Create a new stock move for the given product and quantity
Args:
Expand All @@ -104,6 +109,7 @@ def _create_move(cls, product=None, qty=1.0, multicompany=False):
"product_uom": product.uom_id.id,
"location_id": cls.supplier_location.id,
"location_dest_id": location_dest.id,
"quantity_done": qty if immediate_transfer else 0,
"move_line_ids": [
(
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,38 @@ def test_prepare_stock_move_lines_for_lots_3(self):
5,
msg="The number of created lots should be equal to 5",
)

def test_multiples_allowed_false_immediate_transfer(self):
"""
Test that the serial number is created if the quantity
received is not a multiple of the selected value (Immediate Transfer)
"""
self._create_picking(immediate_transfer=True)
self._create_move(
product=self.product_serial_auto_qty_5_multiples_allowed_false,
qty=8.0,
immediate_transfer=True,
)
self.picking.button_validate()
# Check the display field
move = self.picking.move_ids.filtered(
lambda m: m.product_id
== self.product_serial_auto_qty_5_multiples_allowed_false
)
self.assertFalse(
move.display_assign_serial, msg="Do not display the assigned serial number"
)

# Search for serials
lot = self.lot_obj.search(
[
(
"product_id",
"=",
self.product_serial_auto_qty_5_multiples_allowed_false.id,
)
]
)
self.assertEqual(
len(lot), 2, msg="The number of created lots should be equal to 2"
)

0 comments on commit 4eacb70

Please sign in to comment.