You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.9 KiB

  1. # Copyright 2020 Opener B.V. <https://opener.amsterdam>
  2. # Copyright 2020 Tecnativa - Pedro M. Baeza
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import logging
  5. from psycopg2 import sql
  6. def pre_init_hook(cr):
  7. """Precreate financial_type and fill with appropriate values to prevent
  8. a MemoryError when the ORM attempts to call its compute method on a large
  9. amount of preexisting moves. Note that the order of the mapping is
  10. important as one move can have move lines on accounts of multiple types
  11. and the move type is set in the order of precedence."""
  12. logger = logging.getLogger(__name__)
  13. logger.info("Add account_move.financial_type column if it does not yet exist")
  14. cr.execute(
  15. "ALTER TABLE account_move ADD COLUMN IF NOT EXISTS financial_type VARCHAR"
  16. )
  17. MAPPING = [
  18. ("liquidity", "liquidity", False),
  19. ("payable", "payable", "AND aml.balance < 0"),
  20. ("payable_refund", "payable", "AND aml.balance >= 0"),
  21. ("receivable", "receivable", "AND aml.balance < 0"),
  22. ("receivable_refund", "receivable", "AND aml.balance >= 0"),
  23. ("other", False, False),
  24. ]
  25. for financial_type, internal_type, extra_where in MAPPING:
  26. args = [financial_type]
  27. query = sql.SQL("UPDATE account_move am SET financial_type = %s")
  28. if internal_type:
  29. query += sql.SQL(
  30. """FROM account_move_line aml
  31. WHERE aml.account_id IN (
  32. SELECT id FROM account_account
  33. WHERE internal_type = %s)
  34. AND aml.move_id = am.id AND am.financial_type IS NULL
  35. """
  36. )
  37. args.append(internal_type)
  38. else:
  39. query += sql.SQL("WHERE am.financial_type IS NULL")
  40. if extra_where:
  41. query += sql.SQL(extra_where)
  42. cr.execute(query, tuple(args))
  43. logger.info("%s move set to type %s", financial_type, cr.rowcount)