diff --git a/account_bank_statement_import_save_file/__init__.py b/account_bank_statement_import_save_file/__init__.py index d0b326e..0650744 100644 --- a/account_bank_statement_import_save_file/__init__.py +++ b/account_bank_statement_import_save_file/__init__.py @@ -1,6 +1 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from . import models -from .hooks import _post_init_hook diff --git a/account_bank_statement_import_save_file/__manifest__.py b/account_bank_statement_import_save_file/__manifest__.py index 91347c2..58b38d1 100644 --- a/account_bank_statement_import_save_file/__manifest__.py +++ b/account_bank_statement_import_save_file/__manifest__.py @@ -1,10 +1,9 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). +# Copyright 2015-2019 Therp BV (). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { 'name': 'Save imported bank statements', - 'version': '10.0.1.0.0', + 'version': '12.0.1.0.0', 'author': 'Odoo Community Association (OCA), Therp BV', 'license': 'AGPL-3', 'category': 'Banking addons', @@ -15,8 +14,6 @@ 'data': [ 'views/account_bank_statement.xml', ], - 'post_init_hook': '_post_init_hook', - 'auto_install': False, 'installable': True, 'application': False, } diff --git a/account_bank_statement_import_save_file/hooks.py b/account_bank_statement_import_save_file/hooks.py deleted file mode 100644 index f2b8fd3..0000000 --- a/account_bank_statement_import_save_file/hooks.py +++ /dev/null @@ -1,76 +0,0 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - - -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()] - - if not attachment_ids: - return - - # 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'].sudo().write( - [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 index 9413d5c..b0eb256 100644 --- a/account_bank_statement_import_save_file/models/__init__.py +++ b/account_bank_statement_import_save_file/models/__init__.py @@ -1,6 +1,2 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - 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 index a221c13..4b94dc4 100644 --- a/account_bank_statement_import_save_file/models/account_bank_statement.py +++ b/account_bank_statement_import_save_file/models/account_bank_statement.py @@ -1,8 +1,7 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). +# Copyright 2015-2019 Therp BV (). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import models, fields +from odoo import fields, models class AccountBankStatement(models.Model): @@ -10,9 +9,7 @@ class AccountBankStatement(models.Model): 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_date = fields.Datetime(related='import_file.create_date') + import_user = fields.Many2one(related='import_file.create_uid') import_log = fields.Text( - related=['import_file', 'description'], readonly=True) + related='import_file.description', string='Import Warnings') 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 index e5e216a..7405c6a 100644 --- 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 @@ -1,9 +1,7 @@ -# -*- coding: utf-8 -*- -# © 2015 Therp BV (). +# Copyright 2015-2019 Therp BV (). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -import base64 -from odoo import models, api +from odoo import api, models class AccountBankStatementImport(models.TransientModel): @@ -15,26 +13,24 @@ class AccountBankStatementImport(models.TransientModel): super(AccountBankStatementImport, self).import_file() statement_ids = action.get('context', {}).get('statement_ids') notifications = action.get('context', {}).get('notifications') - data_file = base64.b64decode(self.data_file) if statement_ids: + attach_vals = self._prepare_import_file_attachment( + self.data_file, statement_ids[0], notifications, self.filename) + attach = self.env['ir.attachment'].create(attach_vals) 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, - self.filename)).id, - }) + 'import_file': attach.id}) return action @api.model - def _create_import_file_attachment_data(self, data_file, statement_id, - notifications, filename=None): + def _prepare_import_file_attachment(self, data_file, statement_id, + notifications, filename): return { - 'name': filename or '', + 'name': filename, 'res_model': 'account.bank.statement', 'res_id': statement_id, 'type': 'binary', - 'datas': base64.b64encode(data_file), - 'datas_fname': filename or '', + 'datas': data_file, + 'datas_fname': filename, 'description': '\n'.join( '%(type)s: %(message)s' % notification for notification in notifications) or False, diff --git a/account_bank_statement_import_save_file/readme/CONTRIBUTORS.rst b/account_bank_statement_import_save_file/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..a1b7115 --- /dev/null +++ b/account_bank_statement_import_save_file/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* Holger Brunn +* Mourad EL HADJ MIMOUNE +* Alexis de Lattre diff --git a/account_bank_statement_import_save_file/readme/DESCRIPTION.rst b/account_bank_statement_import_save_file/readme/DESCRIPTION.rst new file mode 100644 index 0000000..7744de0 --- /dev/null +++ b/account_bank_statement_import_save_file/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +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. diff --git a/account_bank_statement_import_save_file/readme/USAGE.rst b/account_bank_statement_import_save_file/readme/USAGE.rst new file mode 100644 index 0000000..634adb1 --- /dev/null +++ b/account_bank_statement_import_save_file/readme/USAGE.rst @@ -0,0 +1 @@ +On a successful import, the form view of the bank statement will have an additional tab *Imported File* which contains the original file. 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 index c1375ac..070a437 100644 --- a/account_bank_statement_import_save_file/views/account_bank_statement.xml +++ b/account_bank_statement_import_save_file/views/account_bank_statement.xml @@ -1,23 +1,19 @@ - + account.bank.statement - - + + - - - - - - - + + + + - - +