mirror of
https://github.com/OCA/bank-statement-import.git
synced 2025-01-20 12:37:43 +02:00
migration script
and a module that takes care of saving imported files (+migration)
This commit is contained in:
committed by
Alexis de Lattre
parent
2908fb0f22
commit
64a21a9f10
30
account_bank_statement_import_save_file/README.rst
Normal file
30
account_bank_statement_import_save_file/README.rst
Normal file
@@ -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 <hbrunn@therp.nl>
|
||||
|
||||
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.
|
||||
22
account_bank_statement_import_save_file/__init__.py
Normal file
22
account_bank_statement_import_save_file/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
from . import models
|
||||
from .hooks import _post_init_hook
|
||||
45
account_bank_statement_import_save_file/__openerp__.py
Normal file
45
account_bank_statement_import_save_file/__openerp__.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
{
|
||||
"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': [],
|
||||
},
|
||||
}
|
||||
92
account_bank_statement_import_save_file/hooks.py
Normal file
92
account_bank_statement_import_save_file/hooks.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
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, '<unknown>'), 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)})
|
||||
22
account_bank_statement_import_save_file/models/__init__.py
Normal file
22
account_bank_statement_import_save_file/models/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV <http://therp.nl>.
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
from . import account_bank_statement
|
||||
from . import account_bank_statement_import
|
||||
@@ -0,0 +1,34 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
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)
|
||||
@@ -0,0 +1,60 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
##############################################################################
|
||||
#
|
||||
# OpenERP, Open Source Management Solution
|
||||
# This module copyright (C) 2015 Therp BV (<http://therp.nl>).
|
||||
#
|
||||
# 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 <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
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': '<unknown>',
|
||||
'res_model': 'account.bank.statement',
|
||||
'res_id': statement_id,
|
||||
'type': 'binary',
|
||||
'datas': base64.b64encode(data_file),
|
||||
'description': notifications,
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 6.7 KiB |
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record id="view_bank_statement_form" model="ir.ui.view">
|
||||
<field name="model">account.bank.statement</field>
|
||||
<field name="inherit_id" ref="account.view_bank_statement_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//page[@string='Transactions']" position="after">
|
||||
<page string="Imported file" attrs="{'invisible': [('import_file', '=', False)]}">
|
||||
<group>
|
||||
<group>
|
||||
<field name="import_file" />
|
||||
</group>
|
||||
<group>
|
||||
<field name="import_date" />
|
||||
<field name="import_user" />
|
||||
</group>
|
||||
</group>
|
||||
<field name="import_log" />
|
||||
</page>
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
||||
Reference in New Issue
Block a user