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.

95 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2015 Therp BV <http://therp.nl>.
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import SUPERUSER_ID
  22. def _post_init_hook(cr, pool):
  23. # if we install this module on a database with remains of account_banking,
  24. # migrate account.banking.imported.file
  25. cr.execute(
  26. "select 1 from pg_catalog.pg_class c "
  27. "join pg_catalog.pg_namespace n ON n.oid = c.relnamespace "
  28. "where n.nspname = 'public' and "
  29. "c.relname = 'account_banking_imported_file' and "
  30. "c.relkind = 'r'")
  31. if cr.fetchall():
  32. _post_init_hook_migrate_account_banking_imported_file(cr, pool)
  33. def _post_init_hook_migrate_account_banking_imported_file(cr, pool):
  34. # create attachments
  35. cr.execute(
  36. """insert into ir_attachment
  37. (
  38. name, create_uid, create_date, datas_fname, description,
  39. company_id, res_model, type,
  40. res_id
  41. )
  42. select
  43. coalesce(file_name, '<unknown>'), user_id, date, file_name, log,
  44. company_id, 'account.bank.statement', 'binary',
  45. (
  46. select id from account_bank_statement
  47. where banking_id=f.id
  48. limit 1
  49. )
  50. from account_banking_imported_file f
  51. returning id""")
  52. attachment_ids = [attachment_id for attachment_id, in cr.fetchall()]
  53. if not attachment_ids:
  54. return
  55. # assign respective attachment to all statements pointing to an imported
  56. # banking file
  57. cr.execute(
  58. """with banking_id2attachment as (
  59. select distinct b.id banking_id, a.id attachment_id
  60. from account_banking_imported_file b
  61. join account_bank_statement s
  62. on s.banking_id=b.id
  63. join ir_attachment a
  64. on a.id in %s and s.id=a.res_id
  65. )
  66. update account_bank_statement s
  67. set import_file=b2a.attachment_id
  68. from banking_id2attachment b2a
  69. where b2a.banking_id=s.banking_id""",
  70. (tuple(attachment_ids),)
  71. )
  72. # now we just have to write the file's content via the orm
  73. # (to support non-db storage)
  74. cr.execute(
  75. """select distinct a.id, b.file
  76. from account_banking_imported_file b
  77. join account_bank_statement s
  78. on s.banking_id=b.id
  79. join ir_attachment a
  80. on a.id in %s and s.id=a.res_id""",
  81. (tuple(attachment_ids),)
  82. )
  83. for attachment_id, content in cr.fetchall():
  84. pool['ir.attachment'].write(
  85. cr, SUPERUSER_ID,
  86. [attachment_id],
  87. {'datas': str(content)})