-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
hooks.py
45 lines (36 loc) · 1.43 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright 2023 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo.tools.sql import column_exists, create_column
_logger = logging.getLogger(__name__)
def _create_and_init_nbr_picking_lines_column(cr):
"""Create and initialize the column nbr_picking_lines in the table
stock_picking
It's used to create and initialize the column nbr_picking_lines in the
table stock_picking. To avoid to freeze the database during the
installation of the module, this column is initialized only for the pickings
that are not in state done or cancel
"""
_logger.info("Create the column nbr_picking_lines in the table stock_picking")
if not column_exists(cr, "stock_picking", "nbr_picking_lines"):
create_column(cr, "stock_picking", "nbr_picking_lines", "integer")
_logger.info("Initialize the column nbr_picking_lines")
cr.execute(
"""
UPDATE stock_picking
SET nbr_picking_lines = (
SELECT count(1)
FROM stock_move_line
WHERE picking_id = stock_picking.id
)
Returning id
"""
)
updated_pickings = cr.fetchall()
_logger.info(
"The column nbr_picking_lines has been initialized for %s pickings",
len(updated_pickings),
)
def pre_init_hook(cr):
"""This method is called before the module is installed"""
_create_and_init_nbr_picking_lines_column(cr)