Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP][16.0] Add hooks to account_invoice_pricelist #1511

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions account_invoice_pricelist/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from . import models
from .hooks import pre_init_hook
from .hooks import post_init_hook
2 changes: 2 additions & 0 deletions account_invoice_pricelist/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
34 changes: 34 additions & 0 deletions account_invoice_pricelist/hooks.py
Original file line number Diff line number Diff line change
@@ -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,
)
Loading