Browse Source

[MIG] account_bank_statement_import_save_file from v10 to v12

pull/202/head
Alexis de Lattre 6 years ago
parent
commit
3ce503fd86
  1. 5
      account_bank_statement_import_save_file/__init__.py
  2. 7
      account_bank_statement_import_save_file/__manifest__.py
  3. 76
      account_bank_statement_import_save_file/hooks.py
  4. 4
      account_bank_statement_import_save_file/models/__init__.py
  5. 13
      account_bank_statement_import_save_file/models/account_bank_statement.py
  6. 26
      account_bank_statement_import_save_file/models/account_bank_statement_import.py
  7. 3
      account_bank_statement_import_save_file/readme/CONTRIBUTORS.rst
  8. 1
      account_bank_statement_import_save_file/readme/DESCRIPTION.rst
  9. 1
      account_bank_statement_import_save_file/readme/USAGE.rst
  10. 18
      account_bank_statement_import_save_file/views/account_bank_statement.xml

5
account_bank_statement_import_save_file/__init__.py

@ -1,6 +1 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import models
from .hooks import _post_init_hook

7
account_bank_statement_import_save_file/__manifest__.py

@ -1,10 +1,9 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# Copyright 2015-2019 Therp BV (<http://therp.nl>).
# 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,
}

76
account_bank_statement_import_save_file/hooks.py

@ -1,76 +0,0 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# 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, '<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()]
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)})

4
account_bank_statement_import_save_file/models/__init__.py

@ -1,6 +1,2 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import account_bank_statement
from . import account_bank_statement_import

13
account_bank_statement_import_save_file/models/account_bank_statement.py

@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# Copyright 2015-2019 Therp BV (<http://therp.nl>).
# 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')

26
account_bank_statement_import_save_file/models/account_bank_statement_import.py

@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
# © 2015 Therp BV (<http://therp.nl>).
# Copyright 2015-2019 Therp BV (<http://therp.nl>).
# 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 '<unknown>',
'name': filename,
'res_model': 'account.bank.statement',
'res_id': statement_id,
'type': 'binary',
'datas': base64.b64encode(data_file),
'datas_fname': filename or '<unknown>',
'datas': data_file,
'datas_fname': filename,
'description': '\n'.join(
'%(type)s: %(message)s' % notification
for notification in notifications) or False,

3
account_bank_statement_import_save_file/readme/CONTRIBUTORS.rst

@ -0,0 +1,3 @@
* Holger Brunn <hbrunn@therp.nl>
* Mourad EL HADJ MIMOUNE <mourad.elhadj.mimoune@akretion.com>
* Alexis de Lattre <alexis.delattre@akretion.com>

1
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.

1
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.

18
account_bank_statement_import_save_file/views/account_bank_statement.xml

@ -4,20 +4,16 @@
<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[@name='statement_line_ids']" position="after">
<page string="Imported file" attrs="{'invisible': [('import_file', '=', False)]}">
<notebook position="inside">
<page string="Imported File" name="imported_file" attrs="{'invisible': [('import_file', '=', False)]}">
<group>
<group>
<field name="import_file" />
</group>
<group>
<field name="import_date" />
<field name="import_user" />
</group>
<field name="import_file"/>
<field name="import_date"/>
<field name="import_user"/>
<field name="import_log"/>
</group>
<field name="import_log" />
</page>
</xpath>
</notebook>
</field>
</record>
</odoo>
Loading…
Cancel
Save