Skip to content

Commit 32ca896

Browse files
[MIG] stock_picking_auto_create_package: migration to 18.0
1 parent 8b71c86 commit 32ca896

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

stock_picking_auto_create_package/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "Stock Picking Auto Create Package",
77
"summary": """
88
Put all move lines in packs on validation.""",
9-
"version": "16.0.1.0.0",
9+
"version": "18.0.1.0.0",
1010
"license": "AGPL-3",
1111
"author": "BCIM,MT Software,Raumschmiede,Odoo Community Association (OCA)",
1212
"website": "https://github.com/OCA/stock-logistics-workflow",

stock_picking_auto_create_package/models/stock_picking.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
55

66
from odoo import api, models
7+
from odoo.tools import float_compare, float_round
78

89

910
class StockPicking(models.Model):
@@ -24,31 +25,63 @@ def _auto_create_delivery_package_filter(self, move_lines):
2425
return move_lines.filtered(
2526
lambda ml: not ml.result_package_id
2627
and ml.state not in ("cancel", "done")
27-
and ml.qty_done
28+
and ml.quantity
2829
)
2930

31+
def _split_move_line_package_qty(self, ml, package_qty):
32+
"""
33+
Split move line if the package quantity is less than the move line
34+
quantity.
35+
"""
36+
if (
37+
float_compare(
38+
package_qty, ml.quantity, precision_rounding=ml.product_uom_id.rounding
39+
)
40+
>= 0
41+
):
42+
return False
43+
quantity_left_todo = float_round(
44+
ml.quantity - package_qty,
45+
precision_rounding=ml.product_uom_id.rounding,
46+
rounding_method="HALF-UP",
47+
)
48+
new_move_line = ml.copy(default={"quantity": quantity_left_todo})
49+
vals = {}
50+
if self.picking_type_id.code == "incoming":
51+
if ml.lot_id:
52+
vals["lot_id"] = False
53+
if ml.lot_name:
54+
vals["lot_name"] = False
55+
new_move_line.write(vals)
56+
return new_move_line
57+
3058
def _auto_create_delivery_package_per_smallest_packaging(self) -> None:
3159
"""
3260
Put each done smallest product packaging in a package
3361
"""
3462
for picking in self:
35-
for move_line in picking.move_line_ids:
63+
picking_move_lines = picking.move_line_ids
64+
for move_line in picking_move_lines:
3665
move_line = self._auto_create_delivery_package_filter(move_line)
3766
if not move_line:
3867
continue
39-
qty_to_pack = move_line.qty_done
68+
qty_to_pack = move_line.quantity
4069
max_pack_qty = 1
4170
packagings = move_line.product_id.packaging_ids.filtered(
4271
lambda pack: pack.qty > 0
4372
)
4473
if packagings:
4574
smallest_packaging = packagings.sorted("qty")[0]
4675
max_pack_qty = smallest_packaging.qty
47-
while qty_to_pack:
76+
current_line = move_line
77+
new_line = None
78+
while qty_to_pack and current_line:
4879
pack_qty = min(qty_to_pack, max_pack_qty)
80+
new_line = self._split_move_line_package_qty(current_line, pack_qty)
4981
qty_to_pack -= pack_qty
50-
move_line.qty_done = pack_qty
51-
move_line.picking_id._put_in_pack(move_line)
82+
current_line.quantity = pack_qty
83+
current_line.picking_id._put_in_pack(current_line)
84+
current_line = new_line
5285

5386
def _auto_create_delivery_package_single(self) -> None:
5487
"""

stock_picking_auto_create_package/tests/test_automatic_package.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
# Copyright 2023 ACSONE SA/NV
33
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
44

5-
from odoo.tests import TransactionCase
5+
from odoo.addons.base.tests.common import BaseCommon
66

77

8-
class TestAutomaticPackage(TransactionCase):
8+
class TestAutomaticPackage(BaseCommon):
99
@classmethod
1010
def setUpClass(cls):
1111
super().setUpClass()
1212
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
1313
cls.product = cls.env["product.product"].create(
1414
{
1515
"name": "Product Test",
16-
"type": "product",
16+
"type": "consu",
17+
"is_storable": True,
1718
}
1819
)
1920
cls.product_packaging = cls.env["product.packaging"].create(
@@ -37,7 +38,7 @@ def setUpClass(cls):
3738
10,
3839
)
3940
cls.picking = cls._create_picking()
40-
cls.picking.move_ids.update({"quantity_done": 5.0})
41+
cls.picking.move_ids.update({"quantity": 5.0})
4142

4243
@classmethod
4344
def _create_picking(cls):

0 commit comments

Comments
 (0)