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.

44 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 move_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.move_type column if it does not yet exist")
  14. cr.execute("ALTER TABLE account_move ADD COLUMN IF NOT EXISTS move_type VARCHAR")
  15. MAPPING = [
  16. ("liquidity", "liquidity", False),
  17. ("payable", "payable", "AND aml.balance < 0"),
  18. ("payable_refund", "payable", "AND aml.balance >= 0"),
  19. ("receivable", "receivable", "AND aml.balance < 0"),
  20. ("receivable_refund", "receivable", "AND aml.balance >= 0"),
  21. ("other", False, False),
  22. ]
  23. for move_type, internal_type, extra_where in MAPPING:
  24. args = [move_type]
  25. query = sql.SQL("UPDATE account_move am SET move_type = %s")
  26. if internal_type:
  27. query += sql.SQL(
  28. """FROM account_move_line aml
  29. WHERE aml.account_id IN (
  30. SELECT id FROM account_account
  31. WHERE internal_type = %s)
  32. AND aml.move_id = am.id AND am.move_type IS NULL
  33. """
  34. )
  35. args.append(internal_type)
  36. else:
  37. query += sql.SQL("WHERE am.move_type IS NULL")
  38. if extra_where:
  39. query += sql.SQL(extra_where)
  40. cr.execute(query, tuple(args))
  41. logger.info("%s move set to type %s", move_type, cr.rowcount)