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..37ff0bf --- /dev/null +++ b/account_bank_statement_import_save_file/README.rst @@ -0,0 +1,35 @@ +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 + +Icon +---- + +* https://commons.wikimedia.org/wiki/File:Paper_clip_font_awesome.svg + +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..908c225 --- /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 + + +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..9b7073b --- /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 + + +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..875e2c7 --- /dev/null +++ b/account_bank_statement_import_save_file/models/account_bank_statement_import.py @@ -0,0 +1,50 @@ +# -*- 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 +from openerp import models, api + + +class AccountBankStatementImport(models.TransientModel): + _inherit = 'account.bank.statement.import' + + @api.model + def _import_file(self, data_file): + (statement_ids, notifications) = \ + super(AccountBankStatementImport, self)._import_file(data_file) + if statement_ids: + self.env['account.bank.statement'].browse(statement_ids).write({ + 'import_file': self.env['ir.attachment'].create( + self._create_import_file_attachment_data( + data_file, statement_ids[0], notifications)).id, + }) + return (statement_ids, 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 0000000..49dedcf 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/tests/__init__.py b/account_bank_statement_import_save_file/tests/__init__.py new file mode 100644 index 0000000..f06dd21 --- /dev/null +++ b/account_bank_statement_import_save_file/tests/__init__.py @@ -0,0 +1,21 @@ +# -*- 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 test_save_file diff --git a/account_bank_statement_import_save_file/tests/test_save_file.py b/account_bank_statement_import_save_file/tests/test_save_file.py new file mode 100644 index 0000000..7e5b186 --- /dev/null +++ b/account_bank_statement_import_save_file/tests/test_save_file.py @@ -0,0 +1,78 @@ +# -*- 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 +from openerp import api, models +from openerp.tests.common import TransactionCase + + +acc_number = 'BE1234567890' + + +class HelloWorldParser(models.TransientModel): + _inherit = 'account.bank.statement.import' + + @api.model + def _parse_file(self, data_file): + return ( + 'EUR', + acc_number, + [{ + 'name': '000000123', + 'date': '2013-06-26', + 'transactions': [{ + 'name': 'KBC-INVESTERINGSKREDIET 787-5562831-01', + 'date': '2013-06-26', + 'amount': 42, + 'unique_import_id': 'hello', + }], + }], + ) + + +class TestSaveFile(TransactionCase): + def test_SaveFile(self): + HelloWorldParser._build_model(self.registry, self.cr) + import_wizard = self.env['account.bank.statement.import'] + import_wizard._prepare_setup() + import_wizard._setup_base(False) + import_wizard._setup_fields() + import_wizard._setup_complete() + import_wizard._auto_init() + journal_id = self.env['res.partner.bank'].search([ + ('acc_number', '=', acc_number), + ]).journal_id.id + if not journal_id: + account = import_wizard._create_bank_account(acc_number) + journal_id = self.env['account.journal']\ + .search([ + '|', + ('currency.name', '=', 'EUR'), + ('currency', '=', False) + ]).ids[0] + account.journal_id = journal_id + action = import_wizard.with_context(journal_id=journal_id)\ + .create({'data_file': base64.b64encode('hello world')})\ + .import_file() + for statement in self.env['account.bank.statement'].browse( + action['context']['statement_ids']): + self.assertEqual( + base64.b64decode(statement.import_file.datas), + 'hello world') 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 0000000..ab0ce41 --- /dev/null +++ b/account_bank_statement_import_save_file/views/account_bank_statement.xml @@ -0,0 +1,25 @@ + + + + + account.bank.statement + + + + + + + + + + + + + + + + + + + +