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.

92 lines
3.4 KiB

10 years ago
  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. # assign respective attachment to all statements pointing to an imported
  54. # banking file
  55. cr.execute(
  56. """with banking_id2attachment as (
  57. select distinct b.id banking_id, a.id attachment_id
  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. )
  64. update account_bank_statement s
  65. set import_file=b2a.attachment_id
  66. from banking_id2attachment b2a
  67. where b2a.banking_id=s.banking_id""",
  68. (tuple(attachment_ids),)
  69. )
  70. # now we just have to write the file's content via the orm
  71. # (to support non-db storage)
  72. cr.execute(
  73. """select distinct a.id, b.file
  74. from account_banking_imported_file b
  75. join account_bank_statement s
  76. on s.banking_id=b.id
  77. join ir_attachment a
  78. on a.id in %s and s.id=a.res_id""",
  79. (tuple(attachment_ids),)
  80. )
  81. for attachment_id, content in cr.fetchall():
  82. pool['ir.attachment'].write(
  83. cr, SUPERUSER_ID,
  84. [attachment_id],
  85. {'datas': str(content)})