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 00000000..ff2ee0f5 --- /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 00000000..694c956c --- /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 00000000..a1813e65 --- /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 00000000..57b40975 --- /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 00000000..f35b069e --- /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 00000000..84bff13e --- /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 00000000..fe67d3c9 --- /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 00000000..15b8d86a --- /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 00000000..1a22a285 Binary files /dev/null and b/account_bank_statement_import_save_file/static/description/icon.png differ diff --git a/account_bank_statement_import_save_file/views/account_bank_statement.xml b/account_bank_statement_import_save_file/views/account_bank_statement.xml new file mode 100644 index 00000000..ab0ce419 --- /dev/null +++ b/account_bank_statement_import_save_file/views/account_bank_statement.xml @@ -0,0 +1,25 @@ + + + + + account.bank.statement + + + + + + + + + + + + + + + + + + + +