[MIG] account_tax_balance: Migration to 14.0

This commit is contained in:
Francisco Ivan Anton Prieto
2020-10-23 15:30:27 +02:00
committed by Borruso
parent 54339092f3
commit 9fbb756b87
13 changed files with 115 additions and 65 deletions

View File

@@ -7,14 +7,16 @@ from psycopg2 import sql
def pre_init_hook(cr):
"""Precreate move_type and fill with appropriate values to prevent
"""Precreate financial_type and fill with appropriate values to prevent
a MemoryError when the ORM attempts to call its compute method on a large
amount of preexisting moves. Note that the order of the mapping is
important as one move can have move lines on accounts of multiple types
and the move type is set in the order of precedence."""
logger = logging.getLogger(__name__)
logger.info("Add account_move.move_type column if it does not yet exist")
cr.execute("ALTER TABLE account_move ADD COLUMN IF NOT EXISTS move_type VARCHAR")
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 financial_type VARCHAR"
)
MAPPING = [
("liquidity", "liquidity", False),
("payable", "payable", "AND aml.balance < 0"),
@@ -23,22 +25,22 @@ def pre_init_hook(cr):
("receivable_refund", "receivable", "AND aml.balance >= 0"),
("other", False, False),
]
for move_type, internal_type, extra_where in MAPPING:
args = [move_type]
query = sql.SQL("UPDATE account_move am SET move_type = %s")
for financial_type, internal_type, extra_where in MAPPING:
args = [financial_type]
query = sql.SQL("UPDATE account_move am SET financial_type = %s")
if internal_type:
query += sql.SQL(
"""FROM account_move_line aml
WHERE aml.account_id IN (
SELECT id FROM account_account
WHERE internal_type = %s)
AND aml.move_id = am.id AND am.move_type IS NULL
AND aml.move_id = am.id AND am.financial_type IS NULL
"""
)
args.append(internal_type)
else:
query += sql.SQL("WHERE am.move_type IS NULL")
query += sql.SQL("WHERE am.financial_type IS NULL")
if extra_where:
query += sql.SQL(extra_where)
cr.execute(query, tuple(args))
logger.info("%s move set to type %s", move_type, cr.rowcount)
logger.info("%s move set to type %s", financial_type, cr.rowcount)