1- # Copyright 2020 ForgeFlow S.L. (https://www.forgeflow.com)
1+ # Copyright 2020-24 ForgeFlow S.L. (https://www.forgeflow.com)
22# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
33
44import logging
88logger = logging .getLogger (__name__ )
99
1010
11- def post_init_hook (cr , registry ):
11+ def post_init_hook (env ):
1212 """
1313 The objective of this hook is to link existing MOs
1414 coming from a Stock Request.
1515 """
1616 logger .info ("Linking existing MOs coming from a Stock Request" )
17- link_existing_mos_to_stock_request (cr )
17+ link_existing_mos_to_stock_request (env )
1818
1919
20- def link_existing_mos_to_stock_request (cr ):
21- env = api .Environment (cr , SUPERUSER_ID , dict ())
20+ def link_existing_mos_to_stock_request (env ):
21+ env = api .Environment (env . cr , SUPERUSER_ID , dict ())
2222 stock_request_obj = env ["stock.request" ]
2323 stock_request_order_obj = env ["stock.request.order" ]
2424 stock_request_allocation_obj = env ["stock.request.allocation" ]
@@ -29,7 +29,7 @@ def link_existing_mos_to_stock_request(cr):
2929 [("name" , "in" , [mo .origin for mo in mos_with_sr ])]
3030 )
3131 for mo in mos_with_sr :
32- stock_request = stock_requests .filtered (lambda x : x .name == mo .origin )
32+ stock_request = stock_requests .filtered (lambda x , mo = mo : x .name == mo .origin )
3333 if stock_request :
3434 # Link SR to MO
3535 mo .stock_request_ids = [(6 , 0 , stock_request .ids )]
@@ -59,13 +59,13 @@ def link_existing_mos_to_stock_request(cr):
5959 for ml in mo .finished_move_line_ids .filtered (
6060 lambda m : m .exists () and m .move_id .allocation_ids
6161 ):
62- qty_done = ml .product_uom_id ._compute_quantity (
63- ml .qty_done , ml .product_id .uom_id
62+ quantity = ml .product_uom_id ._compute_quantity (
63+ ml .quantity , ml .product_id .uom_id
6464 )
65- to_allocate_qty = ml .qty_done
65+ to_allocate_qty = ml .quantity
6666 for allocation in ml .move_id .allocation_ids :
6767 if allocation .open_product_qty :
68- allocated_qty = min (allocation .open_product_qty , qty_done )
68+ allocated_qty = min (allocation .open_product_qty , quantity )
6969 allocation .allocated_product_qty += allocated_qty
7070 to_allocate_qty -= allocated_qty
7171 stock_request .check_done ()
0 commit comments