44# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
55
66from odoo import api , models
7+ from odoo .tools import float_compare , float_round
78
89
910class 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 """
0 commit comments