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.

76 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Therp BV (<http://therp.nl>).
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. def _post_init_hook(cr, pool):
  5. # if we install this module on a database with remains of account_banking,
  6. # migrate account.banking.imported.file
  7. cr.execute(
  8. "select 1 from pg_catalog.pg_class c "
  9. "join pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
  10. "where n.nspname = 'public' and "
  11. "c.relname = 'account_banking_imported_file' and "
  12. "c.relkind = 'r'")
  13. if cr.fetchall():
  14. _post_init_hook_migrate_account_banking_imported_file(cr, pool)
  15. def _post_init_hook_migrate_account_banking_imported_file(cr, pool):
  16. # create attachments
  17. cr.execute(
  18. """insert into ir_attachment
  19. (
  20. name, create_uid, create_date, datas_fname, description,
  21. company_id, res_model, type,
  22. res_id
  23. )
  24. select
  25. coalesce(file_name, '<unknown>'), user_id, date, file_name, log,
  26. company_id, 'account.bank.statement', 'binary',
  27. (
  28. select id from account_bank_statement
  29. where banking_id=f.id
  30. limit 1
  31. )
  32. from account_banking_imported_file f
  33. returning id""")
  34. attachment_ids = [attachment_id for attachment_id, in cr.fetchall()]
  35. if not attachment_ids:
  36. return
  37. # assign respective attachment to all statements pointing to an imported
  38. # banking file
  39. cr.execute(
  40. """with banking_id2attachment as (
  41. select distinct b.id banking_id, a.id attachment_id
  42. from account_banking_imported_file b
  43. join account_bank_statement s
  44. on s.banking_id=b.id
  45. join ir_attachment a
  46. on a.id in %s and s.id=a.res_id
  47. )
  48. update account_bank_statement s
  49. set import_file=b2a.attachment_id
  50. from banking_id2attachment b2a
  51. where b2a.banking_id=s.banking_id""",
  52. (tuple(attachment_ids),)
  53. )
  54. # now we just have to write the file's content via the orm
  55. # (to support non-db storage)
  56. cr.execute(
  57. """select distinct a.id, b.file
  58. from account_banking_imported_file b
  59. join account_bank_statement s
  60. on s.banking_id=b.id
  61. join ir_attachment a
  62. on a.id in %s and s.id=a.res_id""",
  63. (tuple(attachment_ids),)
  64. )
  65. for attachment_id, content in cr.fetchall():
  66. pool['ir.attachment'].sudo().write(
  67. [attachment_id],
  68. {'datas': str(content)})