diff --git a/account_invoice_pricelist/__init__.py b/account_invoice_pricelist/__init__.py index 0650744f6bc..f84dcd0429d 100644 --- a/account_invoice_pricelist/__init__.py +++ b/account_invoice_pricelist/__init__.py @@ -1 +1,3 @@ from . import models +from .hooks import pre_init_hook +from .hooks import post_init_hook diff --git a/account_invoice_pricelist/__manifest__.py b/account_invoice_pricelist/__manifest__.py index 86a60c4cef9..5a033422fd6 100644 --- a/account_invoice_pricelist/__manifest__.py +++ b/account_invoice_pricelist/__manifest__.py @@ -11,4 +11,6 @@ "depends": ["account"], "data": ["views/account_invoice_view.xml"], "installable": True, + "pre_init_hook": "pre_init_hook", + "post_init_hook": "post_init_hook", } diff --git a/account_invoice_pricelist/hooks.py b/account_invoice_pricelist/hooks.py new file mode 100644 index 00000000000..47d3c063d78 --- /dev/null +++ b/account_invoice_pricelist/hooks.py @@ -0,0 +1,34 @@ +import logging + +from psycopg2.extras import execute_values + +from odoo import SUPERUSER_ID, api + + +def pre_init_hook(cr): + """Precreate pricelist_id column to prevent tracked computation.""" + logger = logging.getLogger(__name__) + logger.info("Add account_move.financial_type column if it does not yet exist") + cr.execute("ALTER TABLE account_move ADD COLUMN IF NOT EXISTS pricelist_id int4") + + +def post_init_hook(cr, registry): + """Fill pricelist_id without tracking.""" + env = api.Environment(cr, SUPERUSER_ID, {}) + moves = env["account.move"].search( + [("move_type", "in", ("out_invoice", "out_refund"))] + ) + vals = [] + for invoice in moves: + if invoice.partner_id and invoice.partner_id.property_product_pricelist: + vals.append((invoice.id, invoice.partner_id.property_product_pricelist.id)) + if vals: + execute_values( + cr, + """ + UPDATE account_move SET pricelist_id = vals.p + FROM (VALUES %s) AS vals (id, p) + WHERE account_move.id = vals.id + """, + vals, + )