forked from OCA/purchase-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
37 lines (31 loc) · 1.01 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
# Copyright 2024-Today - Sylvain Le GAL (GRAP)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
_logger = logging.getLogger(__name__)
def pre_init_hook(cr):
_logger.info("Initializing column main_seller_id on table product_template")
cr.execute(
"""
ALTER TABLE product_template
ADD COLUMN IF NOT EXISTS main_seller_id integer;
"""
)
cr.execute(
"""
WITH numbered_supplierinfos as (
SELECT *, ROW_number() over (
partition BY product_tmpl_id
ORDER BY sequence, min_qty desc, price
) as row_number
FROM product_supplierinfo
),
first_supplierinfos as (
SELECT * from numbered_supplierinfos
WHERE row_number = 1
)
UPDATE product_template pt
SET main_seller_id = first_supplierinfos.partner_id
FROM first_supplierinfos
WHERE pt.id = first_supplierinfos.product_tmpl_id;
"""
)