From 41d1fc8512bf06a1b3ce3cc4a5d30054a0ea1ddc Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Thu, 28 May 2015 16:43:23 +0200 Subject: [PATCH] [ADD] migration script and a module that takes care of saving imported files (+migration) --- .../migrations/8.0.1.0/post-migrate.py | 33 +++++++ .../README.rst | 30 ++++++ .../__init__.py | 22 +++++ .../__openerp__.py | 45 +++++++++ .../hooks.py | 92 ++++++++++++++++++ .../models/__init__.py | 22 +++++ .../models/account_bank_statement.py | 34 +++++++ .../models/account_bank_statement_import.py | 60 ++++++++++++ .../static/description/icon.png | Bin 0 -> 6907 bytes .../views/account_bank_statement.xml | 25 +++++ 10 files changed, 363 insertions(+) create mode 100644 account_bank_statement_import/migrations/8.0.1.0/post-migrate.py create mode 100644 account_bank_statement_import_save_file/README.rst create mode 100644 account_bank_statement_import_save_file/__init__.py create mode 100644 account_bank_statement_import_save_file/__openerp__.py create mode 100644 account_bank_statement_import_save_file/hooks.py create mode 100644 account_bank_statement_import_save_file/models/__init__.py create mode 100644 account_bank_statement_import_save_file/models/account_bank_statement.py create mode 100644 account_bank_statement_import_save_file/models/account_bank_statement_import.py create mode 100644 account_bank_statement_import_save_file/static/description/icon.png create mode 100644 account_bank_statement_import_save_file/views/account_bank_statement.xml diff --git a/account_bank_statement_import/migrations/8.0.1.0/post-migrate.py b/account_bank_statement_import/migrations/8.0.1.0/post-migrate.py new file mode 100644 index 0000000..ff2ee0f --- /dev/null +++ b/account_bank_statement_import/migrations/8.0.1.0/post-migrate.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +def migrate(cr, version): + # if we end up here, we migrate from 7.0's account_banking + # set transaction ids, taking care to enforce uniqueness + cr.execute( + """update account_bank_statement_line l set unique_import_id=l1.trans + from ( + select distinct + first_value(id) over (partition by trans) id, trans + from account_bank_statement_line + ) l1 + where l.id=l1.id""") diff --git a/account_bank_statement_import_save_file/README.rst b/account_bank_statement_import_save_file/README.rst new file mode 100644 index 0000000..694c956 --- /dev/null +++ b/account_bank_statement_import_save_file/README.rst @@ -0,0 +1,30 @@ +Save imported statement file +============================ + +This module saves the original file of an imported bank statement for further reference/processing and maintains a link between bank statements and those imported files. + +Usage +===== + +On a successful import, the generated statement(s) link to an attachment containing the original file. + +Credits +======= + +Contributors +------------ + +* Holger Brunn + +Maintainer +---------- + +.. image:: http://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: http://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. + +To contribute to this module, please visit http://odoo-community.org. diff --git a/account_bank_statement_import_save_file/__init__.py b/account_bank_statement_import_save_file/__init__.py new file mode 100644 index 0000000..a1813e6 --- /dev/null +++ b/account_bank_statement_import_save_file/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import models +from .hooks import _post_init_hook diff --git a/account_bank_statement_import_save_file/__openerp__.py b/account_bank_statement_import_save_file/__openerp__.py new file mode 100644 index 0000000..57b4097 --- /dev/null +++ b/account_bank_statement_import_save_file/__openerp__.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + "name": "Save imported bank statements", + "version": "1.0", + "author": "Therp BV", + "license": "AGPL-3", + "category": "Accounting & Finance", + "summary": "Keep imported bank statements as raw data", + "depends": [ + 'account_bank_statement_import', + ], + "data": [ + "views/account_bank_statement.xml", + ], + "qweb": [ + ], + "test": [ + ], + "post_init_hook": '_post_init_hook', + "auto_install": False, + "installable": True, + "application": False, + "external_dependencies": { + 'python': [], + }, +} diff --git a/account_bank_statement_import_save_file/hooks.py b/account_bank_statement_import_save_file/hooks.py new file mode 100644 index 0000000..f35b069 --- /dev/null +++ b/account_bank_statement_import_save_file/hooks.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import SUPERUSER_ID, api + + +def _post_init_hook(cr, pool): + # if we install this module on a database with remains of account_banking, + # migrate account.banking.imported.file + cr.execute( + "select 1 from pg_catalog.pg_class c " + "join pg_catalog.pg_namespace n ON n.oid = c.relnamespace " + "where n.nspname = 'public' and " + "c.relname = 'account_banking_imported_file' and " + "c.relkind = 'r'") + if cr.fetchall(): + _post_init_hook_migrate_account_banking_imported_file(cr, pool) + + +def _post_init_hook_migrate_account_banking_imported_file(cr, pool): + # create attachments + cr.execute( + """insert into ir_attachment + ( + name, create_uid, create_date, datas_fname, description, + company_id, res_model, type, + res_id + ) + select + coalesce(file_name, ''), user_id, date, file_name, log, + company_id, 'account.bank.statement', 'binary', + ( + select id from account_bank_statement + where banking_id=f.id + limit 1 + ) + from account_banking_imported_file f + returning id""") + + attachment_ids = [attachment_id for attachment_id, in cr.fetchall()] + + # assign respective attachment to all statements pointing to an imported + # banking file + cr.execute( + """with banking_id2attachment as ( + select distinct b.id banking_id, a.id attachment_id + from account_banking_imported_file b + join account_bank_statement s + on s.banking_id=b.id + join ir_attachment a + on a.id in %s and s.id=a.res_id + ) + update account_bank_statement s + set import_file=b2a.attachment_id + from banking_id2attachment b2a + where b2a.banking_id=s.banking_id""", + (tuple(attachment_ids),) + ) + + # now we just have to write the file's content via the orm + # (to support non-db storage) + cr.execute( + """select distinct a.id, b.file + from account_banking_imported_file b + join account_bank_statement s + on s.banking_id=b.id + join ir_attachment a + on a.id in %s and s.id=a.res_id""", + (tuple(attachment_ids),) + ) + for attachment_id, content in cr.fetchall(): + pool['ir.attachment'].write( + cr, SUPERUSER_ID, + [attachment_id], + {'datas': str(content)}) diff --git a/account_bank_statement_import_save_file/models/__init__.py b/account_bank_statement_import_save_file/models/__init__.py new file mode 100644 index 0000000..84bff13 --- /dev/null +++ b/account_bank_statement_import_save_file/models/__init__.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV . +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from . import account_bank_statement +from . import account_bank_statement_import diff --git a/account_bank_statement_import_save_file/models/account_bank_statement.py b/account_bank_statement_import_save_file/models/account_bank_statement.py new file mode 100644 index 0000000..fe67d3c --- /dev/null +++ b/account_bank_statement_import_save_file/models/account_bank_statement.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +from openerp import models, fields, api + + +class AccountBankStatement(models.Model): + _inherit = 'account.bank.statement' + + import_file = fields.Many2one( + 'ir.attachment', 'Import file', readonly=True) + import_date = fields.Datetime( + related=['import_file', 'create_date'], readonly=True) + import_user = fields.Many2one( + related=['import_file', 'create_uid'], readonly=True) + import_log = fields.Text( + related=['import_file', 'description'], readonly=True) diff --git a/account_bank_statement_import_save_file/models/account_bank_statement_import.py b/account_bank_statement_import_save_file/models/account_bank_statement_import.py new file mode 100644 index 0000000..15b8d86 --- /dev/null +++ b/account_bank_statement_import_save_file/models/account_bank_statement_import.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# This module copyright (C) 2015 Therp BV (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +import base64 +import inspect +from openerp import models, fields, api + + +class AccountBankStatementImport(models.Model): + _inherit = 'account.bank.statement.import' + + @api.model + def _import_statement(self, statement): + (statement_id, notifications) = \ + super(AccountBankStatementImport, self)._import_statement( + statement) + if statement_id: + # get raw file data from the stack + def get_data_file(frame): + if 'data_file' in frame.f_locals: + return frame.f_locals['data_file'] + if frame.f_back: + return get_data_file(frame.f_back) + return None + data_file = get_data_file(inspect.currentframe()) + self.env['account.bank.statement'].browse([statement_id]).write({ + 'import_file': self.env['ir.attachment'].create( + self._create_import_file_attachment_data( + data_file, statement_id, notifications)).id, + }) + return (statement_id, notifications) + + @api.model + def _create_import_file_attachment_data(self, data_file, statement_id, + notifications): + return { + 'name': '', + 'res_model': 'account.bank.statement', + 'res_id': statement_id, + 'type': 'binary', + 'datas': base64.b64encode(data_file), + 'description': notifications, + } diff --git a/account_bank_statement_import_save_file/static/description/icon.png b/account_bank_statement_import_save_file/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..1a22a285caee435c1257b688f5b45cc3c6682a33 GIT binary patch literal 6907 zcmaiZ^SO5P=;Lea0v~svG%bY-RdqSQx+QY@W@uf_?WXNKX!mN(KRW$ap9; z_^0pK(^ADz#Q_)B*ZT^krQ!zS!=-O?NlMc|5^qC$3yC-Lwc9>(--A%3GQ5q9Li#ibk8kt5Y31TwXXE#No_f7xjYzK-dGto!9C8eXrxWf3mWEW#b?vq(%-%O8GA)> z-(yF^o>zy=6q*6_0rrHk+b~ZxZ?|wR_}Pe+ajereAcOaB7`zM)7kehnvFvBjNs`ET! z!pRIF*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