From 00ea4a5c45d49b7d19224b2faa1b9a3714b57e12 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Wed, 15 May 2019 12:39:24 +0300 Subject: [PATCH] [ADD] account_bank_statement_import_online: online bank statements --- .../README.rst | 114 +++++ .../__init__.py | 5 + .../__manifest__.py | 30 ++ .../account_bank_statement_import_online.xml | 21 + .../models/__init__.py | 4 + .../models/account_journal.py | 96 ++++ .../models/online_bank_statement_provider.py | 388 +++++++++++++++ .../readme/CONFIGURE.rst | 23 + .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 1 + .../readme/USAGE.rst | 9 + .../security/ir.model.access.csv | 3 + .../online_bank_statement_provider.xml | 16 + .../static/description/index.html | 460 ++++++++++++++++++ .../tests/__init__.py | 3 + .../online_bank_statement_provider_dummy.py | 55 +++ ...st_account_bank_statement_import_online.py | 343 +++++++++++++ .../views/account_journal.xml | 120 +++++ .../views/online_bank_statement_provider.xml | 96 ++++ .../wizards/__init__.py | 3 + .../online_bank_statement_pull_wizard.py | 34 ++ .../online_bank_statement_pull_wizard.xml | 32 ++ oca_dependencies.txt | 1 + 23 files changed, 1858 insertions(+) create mode 100644 account_bank_statement_import_online/README.rst create mode 100644 account_bank_statement_import_online/__init__.py create mode 100644 account_bank_statement_import_online/__manifest__.py create mode 100644 account_bank_statement_import_online/data/account_bank_statement_import_online.xml create mode 100644 account_bank_statement_import_online/models/__init__.py create mode 100644 account_bank_statement_import_online/models/account_journal.py create mode 100644 account_bank_statement_import_online/models/online_bank_statement_provider.py create mode 100644 account_bank_statement_import_online/readme/CONFIGURE.rst create mode 100644 account_bank_statement_import_online/readme/CONTRIBUTORS.rst create mode 100644 account_bank_statement_import_online/readme/DESCRIPTION.rst create mode 100644 account_bank_statement_import_online/readme/USAGE.rst create mode 100644 account_bank_statement_import_online/security/ir.model.access.csv create mode 100644 account_bank_statement_import_online/security/online_bank_statement_provider.xml create mode 100644 account_bank_statement_import_online/static/description/index.html create mode 100644 account_bank_statement_import_online/tests/__init__.py create mode 100644 account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py create mode 100644 account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py create mode 100644 account_bank_statement_import_online/views/account_journal.xml create mode 100644 account_bank_statement_import_online/views/online_bank_statement_provider.xml create mode 100644 account_bank_statement_import_online/wizards/__init__.py create mode 100644 account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.py create mode 100644 account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.xml create mode 100644 oca_dependencies.txt diff --git a/account_bank_statement_import_online/README.rst b/account_bank_statement_import_online/README.rst new file mode 100644 index 0000000..9cbdefa --- /dev/null +++ b/account_bank_statement_import_online/README.rst @@ -0,0 +1,114 @@ +====================== +Online Bank Statements +====================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fbank--statement--import-lightgray.png?logo=github + :target: https://github.com/OCA/bank-statement-import/tree/12.0/account_bank_statement_import_online + :alt: OCA/bank-statement-import +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/bank-statement-import-12-0/bank-statement-import-12-0-account_bank_statement_import_online + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/174/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module provides base for building online bank statements providers. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure online bank statements provider: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Open bank account to configure and edit it +#. Set *Bank Feeds* to *Online* +#. Select online bank statements provider in *Online Bank Statements (OCA)* + section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +or, alternatively: + +#. Go to *Invoicing > Overview* +#. Open settings of the corresponding journal account +#. Switch to *Bank Account* tab +#. Set *Bank Feeds* to *Online* +#. Select online bank statements provider in *Online Bank Statements (OCA)* + section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +**NOTE**: To access these features, user needs to belong to +*Show Full Accounting Features* group. + +Usage +===== + +To pull historical bank statements: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Select specific bank accounts +#. Launch *Actions > Online Bank Statements Pull Wizard* +#. Configure date interval and click *Pull* + +**NOTE**: To access these features, user needs to belong to +*Show Full Accounting Features* group. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Brainbean Apps +* Dataplug + +Contributors +~~~~~~~~~~~~ + +* Alexey Pelykh + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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. + +This module is part of the `OCA/bank-statement-import `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_bank_statement_import_online/__init__.py b/account_bank_statement_import_online/__init__.py new file mode 100644 index 0000000..a82a91d --- /dev/null +++ b/account_bank_statement_import_online/__init__.py @@ -0,0 +1,5 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models +from . import wizards +from .tests import online_bank_statement_provider_dummy diff --git a/account_bank_statement_import_online/__manifest__.py b/account_bank_statement_import_online/__manifest__.py new file mode 100644 index 0000000..c614dd4 --- /dev/null +++ b/account_bank_statement_import_online/__manifest__.py @@ -0,0 +1,30 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + 'name': 'Online Bank Statements', + 'version': '12.0.1.0.0', + 'author': + 'Brainbean Apps, ' + 'Dataplug, ' + 'Odoo Community Association (OCA)', + 'website': 'https://github.com/OCA/bank-statement-import/', + 'license': 'AGPL-3', + 'category': 'Accounting', + 'summary': 'Online bank statements update', + 'depends': [ + 'account', + 'account_bank_statement_import', + 'web_widget_dropdown_dynamic', + ], + 'data': [ + 'data/account_bank_statement_import_online.xml', + 'security/ir.model.access.csv', + 'security/online_bank_statement_provider.xml', + 'views/account_journal.xml', + 'views/online_bank_statement_provider.xml', + 'wizards/online_bank_statement_pull_wizard.xml', + ], + 'installable': True, +} diff --git a/account_bank_statement_import_online/data/account_bank_statement_import_online.xml b/account_bank_statement_import_online/data/account_bank_statement_import_online.xml new file mode 100644 index 0000000..63add6d --- /dev/null +++ b/account_bank_statement_import_online/data/account_bank_statement_import_online.xml @@ -0,0 +1,21 @@ + + + + + + Pull Online Bank Statements + 1 + hours + -1 + code + 2019-01-01 00:10:00 + + + model._scheduled_pull() + + + diff --git a/account_bank_statement_import_online/models/__init__.py b/account_bank_statement_import_online/models/__init__.py new file mode 100644 index 0000000..56bd827 --- /dev/null +++ b/account_bank_statement_import_online/models/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import account_journal +from . import online_bank_statement_provider diff --git a/account_bank_statement_import_online/models/account_journal.py b/account_bank_statement_import_online/models/account_journal.py new file mode 100644 index 0000000..cca7974 --- /dev/null +++ b/account_bank_statement_import_online/models/account_journal.py @@ -0,0 +1,96 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +import logging + +from odoo import models, fields, api, _ + +_logger = logging.getLogger(__name__) + + +class AccountJournal(models.Model): + _inherit = 'account.journal' + + online_bank_statement_provider = fields.Selection( + selection=lambda self: self.env[ + 'account.journal' + ]._selection_online_bank_statement_provider(), + ) + online_bank_statement_provider_id = fields.Many2one( + string='Statement Provider', + comodel_name='online.bank.statement.provider', + ondelete='restrict', + copy=False, + ) + + def __get_bank_statements_available_sources(self): + result = super().__get_bank_statements_available_sources() + result.append(('online', _('Online (OCA)'))) + return result + + @api.model + def _selection_online_bank_statement_provider(self): + return self.env[ + 'online.bank.statement.provider' + ]._get_available_services() + [('dummy', 'Dummy')] + + @api.model + def values_online_bank_statement_provider(self): + return self.env[ + 'online.bank.statement.provider' + ]._get_available_services() + + @api.multi + def _update_online_bank_statement_provider_id(self): + OnlineBankStatementProvider = ( + self.env['online.bank.statement.provider'] + ) + for journal in self.filtered('id'): + provider_id = journal.online_bank_statement_provider_id + if journal.bank_statements_source != 'online': + journal.online_bank_statement_provider_id = False + if provider_id: + provider_id.unlink() + continue + if provider_id.service == journal.online_bank_statement_provider: + continue + journal.online_bank_statement_provider_id = False + if provider_id: + provider_id.unlink() + journal.online_bank_statement_provider_id = ( + OnlineBankStatementProvider.create({ + 'journal_id': journal.id, + 'service': journal.online_bank_statement_provider, + }) + ) + + @api.model + def create(self, vals): + rec = super().create(vals) + if 'bank_statements_source' in vals \ + or 'online_bank_statement_provider' in vals: + rec._update_online_bank_statement_provider_id() + return rec + + @api.multi + def write(self, vals): + res = super().write(vals) + if 'bank_statements_source' in vals \ + or 'online_bank_statement_provider' in vals: + self._update_online_bank_statement_provider_id() + return res + + @api.multi + def action_online_bank_statements_pull_wizard(self): + provider_ids = self.mapped('online_bank_statement_provider_id').ids + return { + 'name': _('Online Bank Statement Pull Wizard'), + 'type': 'ir.actions.act_window', + 'res_model': 'online.bank.statement.pull.wizard', + 'views': [[False, 'form']], + 'target': 'new', + 'context': { + 'default_provider_ids': [(6, False, provider_ids)], + }, + } diff --git a/account_bank_statement_import_online/models/online_bank_statement_provider.py b/account_bank_statement_import_online/models/online_bank_statement_provider.py new file mode 100644 index 0000000..6efb834 --- /dev/null +++ b/account_bank_statement_import_online/models/online_bank_statement_provider.py @@ -0,0 +1,388 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from dateutil.relativedelta import relativedelta, MO +from decimal import Decimal +import logging + +from odoo import models, fields, api, _ +from odoo.addons.base.models.res_bank import sanitize_account_number + +_logger = logging.getLogger(__name__) + + +class OnlineBankStatementProvider(models.Model): + _name = 'online.bank.statement.provider' + _inherit = ['mail.thread'] + _description = 'Online Bank Statement Provider' + + company_id = fields.Many2one( + related='journal_id.company_id', + store=True, + ) + active = fields.Boolean() + name = fields.Char( + string='Name', + compute='_compute_name', + store=True, + ) + journal_id = fields.Many2one( + comodel_name='account.journal', + required=True, + readonly=True, + ondelete='cascade', + domain=[ + ('type', '=', 'bank'), + ], + ) + currency_id = fields.Many2one( + related='journal_id.currency_id', + ) + account_number = fields.Char( + related='journal_id.bank_account_id.acc_number' + ) + service = fields.Selection( + selection=lambda self: self._selection_service(), + required=True, + readonly=True, + ) + interval_type = fields.Selection( + selection=[ + ('minutes', 'Minute(s)'), + ('hours', 'Hour(s)'), + ('days', 'Day(s)'), + ('weeks', 'Week(s)'), + ], + default='hours', + required=True, + ) + interval_number = fields.Integer( + string='Scheduled update interval', + default=1, + required=True, + ) + update_schedule = fields.Char( + string='Update Schedule', + compute='_compute_update_schedule', + ) + last_successful_run = fields.Datetime( + string='Last successful pull', + ) + next_run = fields.Datetime( + string='Next scheduled pull', + default=fields.Datetime.now, + required=True, + ) + statement_creation_mode = fields.Selection( + selection=[ + ('daily', 'Daily statements'), + ('weekly', 'Weekly statements'), + ('monthly', 'Monthly statements'), + ], + default='daily', + required=True, + ) + api_base = fields.Char() + origin = fields.Char() + username = fields.Char() + password = fields.Char() + key = fields.Binary() + certificate = fields.Binary() + passphrase = fields.Char() + certificate_public_key = fields.Text() + certificate_private_key = fields.Text() + certificate_chain = fields.Text() + + _sql_constraints = [ + ( + 'journal_id_uniq', + 'UNIQUE(journal_id)', + 'Only one online banking statement provider per journal!' + ), + ( + 'valid_interval_number', + 'CHECK(interval_number > 0)', + 'Scheduled update interval must be greater than zero!' + ) + ] + + @api.model + def _get_available_services(self): + """Hook for extension""" + return [] + + @api.model + def _selection_service(self): + return self._get_available_services() + [('dummy', 'Dummy')] + + @api.model + def values_service(self): + return self._get_available_services() + + @api.multi + @api.depends('service') + def _compute_name(self): + for provider in self: + provider.name = list(filter( + lambda x: x[0] == provider.service, + self._selection_service() + ))[0][1] + + @api.multi + @api.depends('active', 'interval_type', 'interval_number') + def _compute_update_schedule(self): + for provider in self: + if not provider.active: + provider.update_schedule = _('Inactive') + continue + + provider.update_schedule = _('%(number)s %(type)s') % { + 'number': provider.interval_number, + 'type': list(filter( + lambda x: x[0] == provider.interval_type, + self._fields['interval_type'].selection + ))[0][1], + } + + @api.multi + def _pull(self, date_since, date_until): + AccountBankStatement = self.env['account.bank.statement'] + is_scheduled = self.env.context.get('scheduled') + if is_scheduled: + AccountBankStatement = AccountBankStatement.with_context( + tracking_disable=True, + ) + AccountBankStatementLine = self.env['account.bank.statement.line'] + for provider in self: + statement_date_since = provider._get_statement_date_since( + date_since + ) + while statement_date_since < date_until: + statement_date_until = ( + statement_date_since + provider._get_statement_date_step() + ) + try: + data = provider._obtain_statement_data( + statement_date_since, + statement_date_until + ) + except Exception as e: + if is_scheduled: + _logger.warning( + 'Online Bank Statement Provider "%s" failed to' + ' obtain statement data since %s until %s' % ( + provider.name, + statement_date_since, + statement_date_until, + ), + exc_info=True, + ) + provider.message_post( + body=_( + 'Online Bank Statement Provider "%s" failed to' + ' obtain statement data since %s until %s:\n%s' + ) % ( + provider.name, + statement_date_since, + statement_date_until, + str(e), + ), + subject=_( + 'Online Bank Statement Provider failure' + ), + ) + break + raise + statement_date = provider._get_statement_date( + statement_date_since, + statement_date_until, + ) + if not data: + statement_date_since = statement_date_until + continue + lines_data, statement_values = data + statement = AccountBankStatement.search([ + ('journal_id', '=', provider.journal_id.id), + ('state', '=', 'open'), + ('date', '=', statement_date), + ], limit=1) + if not statement: + statement_values.update({ + 'name': provider.journal_id.sequence_id.with_context( + ir_sequence_date=statement_date, + ).next_by_id(), + 'journal_id': provider.journal_id.id, + 'date': statement_date, + }) + statement = AccountBankStatement.create( + # NOTE: This is needed since create() alters values + statement_values.copy() + ) + filtered_lines = [] + for line_values in lines_data: + date = fields.Datetime.from_string(line_values['date']) + if date < statement_date_since: + if 'balance_start' in statement_values: + statement_values['balance_start'] = ( + Decimal( + statement_values['balance_start'] + ) + Decimal( + line_values['amount'] + ) + ) + continue + elif date >= statement_date_until: + if 'balance_end_real' in statement_values: + statement_values['balance_end_real'] = ( + Decimal( + statement_values['balance_end_real'] + ) - Decimal( + line_values['amount'] + ) + ) + continue + elif date <= date_since or date > date_until: + continue + unique_import_id = line_values.get('unique_import_id') + if unique_import_id: + unique_import_id = provider._generate_unique_import_id( + unique_import_id + ) + line_values.update({ + 'unique_import_id': unique_import_id, + }) + if AccountBankStatementLine.sudo().search( + [('unique_import_id', '=', unique_import_id)], + limit=1): + continue + filtered_lines.append(line_values) + statement_values.update({ + 'line_ids': [[0, False, line] for line in filtered_lines], + }) + if 'balance_start' in statement_values: + statement_values['balance_start'] = float( + statement_values['balance_start'] + ) + if 'balance_start' in statement_values: + statement_values['balance_start'] = float( + statement_values['balance_start'] + ) + statement.write(statement_values) + statement_date_since = statement_date_until + if is_scheduled: + provider._schedule_next_run() + + @api.multi + def _schedule_next_run(self): + self.ensure_one() + self.last_successful_run = self.next_run + self.next_run += self._get_next_run_period() + + @api.multi + def _get_statement_date_since(self, date): + self.ensure_one() + date = date.replace( + hour=0, + minute=0, + second=0, + microsecond=0, + ) + if self.statement_creation_mode == 'daily': + return date + elif self.statement_creation_mode == 'weekly': + return date + relativedelta(weekday=MO(-1)) + elif self.statement_creation_mode == 'monthly': + return date.replace( + day=1, + ) + + @api.multi + def _get_statement_date_step(self): + self.ensure_one() + if self.statement_creation_mode == 'daily': + return relativedelta( + days=1, + hour=0, + minute=0, + second=0, + microsecond=0, + ) + elif self.statement_creation_mode == 'weekly': + return relativedelta( + weeks=1, + weekday=MO, + hour=0, + minute=0, + second=0, + microsecond=0, + ) + elif self.statement_creation_mode == 'monthly': + return relativedelta( + months=1, + day=1, + hour=0, + minute=0, + second=0, + microsecond=0, + ) + + @api.multi + def _get_statement_date(self, date_since, date_until): + self.ensure_one() + # NOTE: Statement date is treated by Odoo as start of period. Details + # - addons/account/models/account_journal_dashboard.py + # - def get_line_graph_datas() + return date_since.date() + + @api.multi + def _generate_unique_import_id(self, unique_import_id): + self.ensure_one() + sanitized_account_number = sanitize_account_number(self.account_number) + return ( + sanitized_account_number and sanitized_account_number + '-' or '' + ) + str(self.journal_id.id) + '-' + unique_import_id + + @api.multi + def _get_next_run_period(self): + self.ensure_one() + if self.interval_type == 'minutes': + return relativedelta(minutes=self.interval_number) + elif self.interval_type == 'hours': + return relativedelta(hours=self.interval_number) + elif self.interval_type == 'days': + return relativedelta(days=self.interval_number) + elif self.interval_type == 'weeks': + return relativedelta(weeks=self.interval_number) + + @api.model + def _scheduled_pull(self): + _logger.info('Scheduled pull of online bank statements...') + + providers = self.search([ + ('active', '=', True), + ('next_run', '<=', fields.Datetime.now()), + ]) + if providers: + _logger.info('Pulling online bank statements of: %s' % ', '.join( + providers.mapped('journal_id.name') + )) + for provider in providers.with_context({'scheduled': True}): + date_since = ( + provider.last_successful_run + ) if provider.last_successful_run else ( + provider.next_run - provider._get_next_run_period() + ) + date_until = provider.next_run + provider._pull(date_since, date_until) + + _logger.info('Scheduled pull of online bank statements complete.') + + @api.multi + def _obtain_statement_data( + self, date_since, date_until + ): + """Hook for extension""" + # Check tests/online_bank_statement_provider_dummy.py for reference + self.ensure_one() + return [] diff --git a/account_bank_statement_import_online/readme/CONFIGURE.rst b/account_bank_statement_import_online/readme/CONFIGURE.rst new file mode 100644 index 0000000..d2ae6f2 --- /dev/null +++ b/account_bank_statement_import_online/readme/CONFIGURE.rst @@ -0,0 +1,23 @@ +To configure online bank statements provider: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Open bank account to configure and edit it +#. Set *Bank Feeds* to *Online* +#. Select online bank statements provider in *Online Bank Statements (OCA)* + section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +or, alternatively: + +#. Go to *Invoicing > Overview* +#. Open settings of the corresponding journal account +#. Switch to *Bank Account* tab +#. Set *Bank Feeds* to *Online* +#. Select online bank statements provider in *Online Bank Statements (OCA)* + section +#. Save the bank account +#. Click on provider and configure provider-specific settings. + +**NOTE**: To access these features, user needs to belong to +*Show Full Accounting Features* group. diff --git a/account_bank_statement_import_online/readme/CONTRIBUTORS.rst b/account_bank_statement_import_online/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..1c6a35a --- /dev/null +++ b/account_bank_statement_import_online/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Alexey Pelykh diff --git a/account_bank_statement_import_online/readme/DESCRIPTION.rst b/account_bank_statement_import_online/readme/DESCRIPTION.rst new file mode 100644 index 0000000..cc0aaaf --- /dev/null +++ b/account_bank_statement_import_online/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module provides base for building online bank statements providers. diff --git a/account_bank_statement_import_online/readme/USAGE.rst b/account_bank_statement_import_online/readme/USAGE.rst new file mode 100644 index 0000000..bbc2fce --- /dev/null +++ b/account_bank_statement_import_online/readme/USAGE.rst @@ -0,0 +1,9 @@ +To pull historical bank statements: + +#. Go to *Invoicing > Configuration > Bank Accounts* +#. Select specific bank accounts +#. Launch *Actions > Online Bank Statements Pull Wizard* +#. Configure date interval and click *Pull* + +**NOTE**: To access these features, user needs to belong to +*Show Full Accounting Features* group. diff --git a/account_bank_statement_import_online/security/ir.model.access.csv b/account_bank_statement_import_online/security/ir.model.access.csv new file mode 100644 index 0000000..4e04821 --- /dev/null +++ b/account_bank_statement_import_online/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_online_bank_statement_provider_admin,online.bank.statement.provider:base.group_system,model_online_bank_statement_provider,base.group_system,1,1,1,1 +access_online_bank_statement_provider_user,online.bank.statement.provider:account.group_account_user,model_online_bank_statement_provider,account.group_account_user,1,1,1,1 diff --git a/account_bank_statement_import_online/security/online_bank_statement_provider.xml b/account_bank_statement_import_online/security/online_bank_statement_provider.xml new file mode 100644 index 0000000..521e7e9 --- /dev/null +++ b/account_bank_statement_import_online/security/online_bank_statement_provider.xml @@ -0,0 +1,16 @@ + + + + + + Online Bank Statement Provider multi-company + + + ['|', ('company_id', '=', False), ('company_id', 'child_of', [user.company_id.id])] + + + diff --git a/account_bank_statement_import_online/static/description/index.html b/account_bank_statement_import_online/static/description/index.html new file mode 100644 index 0000000..d4669d2 --- /dev/null +++ b/account_bank_statement_import_online/static/description/index.html @@ -0,0 +1,460 @@ + + + + + + +Online Bank Statements + + + +
+

Online Bank Statements

+ + +

Beta License: AGPL-3 OCA/bank-statement-import Translate me on Weblate Try me on Runbot

+

This module provides base for building online bank statements providers.

+

Table of contents

+ +
+

Configuration

+

To configure online bank statements provider:

+
    +
  1. Go to Invoicing > Configuration > Bank Accounts
  2. +
  3. Open bank account to configure and edit it
  4. +
  5. Set Bank Feeds to Online
  6. +
  7. Select online bank statements provider in Online Bank Statements (OCA) +section
  8. +
  9. Save the bank account
  10. +
  11. Click on provider and configure provider-specific settings.
  12. +
+

or, alternatively:

+
    +
  1. Go to Invoicing > Overview
  2. +
  3. Open settings of the corresponding journal account
  4. +
  5. Switch to Bank Account tab
  6. +
  7. Set Bank Feeds to Online
  8. +
  9. Select online bank statements provider in Online Bank Statements (OCA) +section
  10. +
  11. Save the bank account
  12. +
  13. Click on provider and configure provider-specific settings.
  14. +
+

NOTE: To access these features, user needs to belong to +Show Full Accounting Features group.

+
+
+

Usage

+

To pull historical bank statements:

+
    +
  1. Go to Invoicing > Configuration > Bank Accounts
  2. +
  3. Select specific bank accounts
  4. +
  5. Launch Actions > Online Bank Statements Pull Wizard
  6. +
  7. Configure date interval and click Pull
  8. +
+

NOTE: To access these features, user needs to belong to +Show Full Accounting Features group.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Brainbean Apps
  • +
  • Dataplug
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

This module is part of the OCA/bank-statement-import project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/account_bank_statement_import_online/tests/__init__.py b/account_bank_statement_import_online/tests/__init__.py new file mode 100644 index 0000000..c631b1f --- /dev/null +++ b/account_bank_statement_import_online/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_account_bank_statement_import_online diff --git a/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py b/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py new file mode 100644 index 0000000..04b96ca --- /dev/null +++ b/account_bank_statement_import_online/tests/online_bank_statement_provider_dummy.py @@ -0,0 +1,55 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from datetime import datetime, timedelta +from dateutil.relativedelta import relativedelta +from random import randrange + +from odoo import models, api + + +class OnlineBankStatementProviderDummy(models.Model): + _inherit = 'online.bank.statement.provider' + + @api.multi + def _obtain_statement_data(self, date_since, date_until): + self.ensure_one() + if self.service != 'dummy': + return super()._obtain_statement_data( + date_since, + date_until, + ) # pragma: no cover + + if self.env.context.get('crash', False): + raise Exception('Expected') + + line_step_options = self.env.context.get('step', { + 'minutes': 5, + }) + line_step = relativedelta(**line_step_options) + expand_by = self.env.context.get('expand_by', 0) + date_since -= expand_by * line_step + date_until += expand_by * line_step + + balance_start = randrange(-10000, 10000, 1) * 0.1 + balance = balance_start + lines = [] + date = date_since + while date < date_until: + amount = randrange(-100, 100, 1) * 0.1 + lines.append({ + 'name': 'payment', + 'amount': amount, + 'date': date, + 'unique_import_id': str(int( + (date - datetime(1970, 1, 1)) / timedelta(seconds=1) + )), + }) + balance += amount + date += line_step + balance_end = balance + return lines, { + 'balance_start': balance_start, + 'balance_end_real': balance_end, + } diff --git a/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py new file mode 100644 index 0000000..2eb25d1 --- /dev/null +++ b/account_bank_statement_import_online/tests/test_account_bank_statement_import_online.py @@ -0,0 +1,343 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from dateutil.relativedelta import relativedelta + +from psycopg2 import IntegrityError + +from odoo.tests import common +from odoo.tools import mute_logger +from odoo import fields + + +class TestAccountBankAccountStatementImportOnline(common.TransactionCase): + + def setUp(self): + super().setUp() + + self.now = fields.Datetime.now() + self.AccountJournal = self.env['account.journal'] + self.OnlineBankStatementProvider = self.env[ + 'online.bank.statement.provider' + ] + self.AccountBankStatement = self.env['account.bank.statement'] + self.AccountBankStatementLine = self.env['account.bank.statement.line'] + + def test_provider_unlink_restricted(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + }) + with common.Form(journal) as journal_form: + journal_form.bank_statements_source = 'online' + journal_form.online_bank_statement_provider = 'dummy' + journal_form.save() + + with self.assertRaises(IntegrityError), mute_logger('odoo.sql_db'): + journal.online_bank_statement_provider_id.unlink() + + def test_cascade_unlink(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + }) + with common.Form(journal) as journal_form: + journal_form.bank_statements_source = 'online' + journal_form.online_bank_statement_provider = 'dummy' + journal_form.save() + + self.assertTrue(journal.online_bank_statement_provider_id) + journal.unlink() + self.assertFalse(self.OnlineBankStatementProvider.search([])) + + def test_source_change_cleanup(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + }) + with common.Form(journal) as journal_form: + journal_form.bank_statements_source = 'online' + journal_form.online_bank_statement_provider = 'dummy' + journal_form.save() + + self.assertTrue(journal.online_bank_statement_provider_id) + + with common.Form(journal) as journal_form: + journal_form.bank_statements_source = 'undefined' + journal_form.save() + + self.assertFalse(journal.online_bank_statement_provider_id) + self.assertFalse(self.OnlineBankStatementProvider.search([])) + + def test_pull_boundary(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.with_context({ + 'expand_by': 1, + })._pull( + self.now - relativedelta(hours=1), + self.now, + ) + + statement = self.AccountBankStatement.search( + [('journal_id', '=', journal.id)], + ) + self.assertEquals(len(statement), 1) + self.assertEquals(len(statement.line_ids), 12) + + def test_pull_mode_daily(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'daily' + + provider.with_context(step={'hours': 2})._pull( + self.now - relativedelta(days=1), + self.now, + ) + self.assertEquals( + len(self.AccountBankStatement.search( + [('journal_id', '=', journal.id)] + )), + 2 + ) + + def test_pull_mode_weekly(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'weekly' + + provider.with_context(step={'hours': 8})._pull( + self.now - relativedelta(weeks=1), + self.now, + ) + self.assertEquals( + len(self.AccountBankStatement.search( + [('journal_id', '=', journal.id)] + )), + 2 + ) + + def test_pull_mode_monthly(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'monthly' + + provider.with_context(step={'hours': 8})._pull( + self.now - relativedelta(months=1), + self.now, + ) + self.assertEquals( + len(self.AccountBankStatement.search( + [('journal_id', '=', journal.id)] + )), + 2 + ) + + def test_pull_scheduled(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.next_run = ( + self.now - relativedelta(days=15) + ) + + self.assertFalse(self.AccountBankStatement.search( + [('journal_id', '=', journal.id)], + )) + + provider.with_context(step={'hours': 8})._scheduled_pull() + + statement = self.AccountBankStatement.search( + [('journal_id', '=', journal.id)], + ) + self.assertEquals(len(statement), 1) + + def test_pull_skip_duplicates_by_unique_import_id(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'weekly' + + provider.with_context(step={'hours': 8})._pull( + self.now - relativedelta(weeks=2), + self.now, + ) + self.assertEquals( + len(self.AccountBankStatementLine.search( + [('journal_id', '=', journal.id)] + )), + 14 * (24 / 8) + ) + + provider.with_context(step={'hours': 8})._pull( + self.now - relativedelta(weeks=3), + self.now - relativedelta(weeks=1), + ) + self.assertEquals( + len(self.AccountBankStatementLine.search( + [('journal_id', '=', journal.id)] + )), + 21 * (24 / 8) + ) + + provider.with_context(step={'hours': 8})._pull( + self.now - relativedelta(weeks=1), + self.now, + ) + self.assertEquals( + len(self.AccountBankStatementLine.search( + [('journal_id', '=', journal.id)] + )), + 21 * (24 / 8) + ) + + def test_interval_type_minutes(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.interval_type = 'minutes' + provider._compute_update_schedule() + + def test_interval_type_hours(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.interval_type = 'hours' + provider._compute_update_schedule() + + def test_interval_type_days(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.interval_type = 'days' + provider._compute_update_schedule() + + def test_interval_type_weeks(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.interval_type = 'weeks' + provider._compute_update_schedule() + + def test_pull_no_crash(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'weekly' + + provider.with_context( + crash=True, + scheduled=True, + )._pull( + self.now - relativedelta(hours=1), + self.now, + ) + self.assertFalse(self.AccountBankStatement.search( + [('journal_id', '=', journal.id)], + )) + + def test_pull_crash(self): + journal = self.AccountJournal.create({ + 'name': 'Bank', + 'type': 'bank', + 'code': 'BANK', + 'bank_statements_source': 'online', + 'online_bank_statement_provider': 'dummy', + }) + + provider = journal.online_bank_statement_provider_id + provider.active = True + provider.statement_creation_mode = 'weekly' + + with self.assertRaises(Exception): + provider.with_context( + crash=True, + )._pull( + self.now - relativedelta(hours=1), + self.now, + ) diff --git a/account_bank_statement_import_online/views/account_journal.xml b/account_bank_statement_import_online/views/account_journal.xml new file mode 100644 index 0000000..8cb76b5 --- /dev/null +++ b/account_bank_statement_import_online/views/account_journal.xml @@ -0,0 +1,120 @@ + + + + + + account.journal.form + account.journal + + + + + + + + + + + account.journal.form + account.journal + + + + + + + + + + + account.journal.dashboard.kanban + account.journal + + +
+ dashboard.bank_statements_source != 'online_sync' and dashboard.bank_statements_source != 'online' +
+
+
+ + + Online Bank Statements Pull Wizard + ir.actions.server + + + code + + if records: + action = records.action_online_bank_statements_pull_wizard() + + + +
diff --git a/account_bank_statement_import_online/views/online_bank_statement_provider.xml b/account_bank_statement_import_online/views/online_bank_statement_provider.xml new file mode 100644 index 0000000..232ba04 --- /dev/null +++ b/account_bank_statement_import_online/views/online_bank_statement_provider.xml @@ -0,0 +1,96 @@ + + + + + + online.bank.statement.provider.filter + online.bank.statement.provider + + + + + + + + + online.bank.statement.provider.tree + online.bank.statement.provider + + + + + + + + + + + + + + online.bank.statement.provider.form + online.bank.statement.provider + +
+ +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + +
+
+
+
+ +
diff --git a/account_bank_statement_import_online/wizards/__init__.py b/account_bank_statement_import_online/wizards/__init__.py new file mode 100644 index 0000000..a8ac662 --- /dev/null +++ b/account_bank_statement_import_online/wizards/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import online_bank_statement_pull_wizard diff --git a/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.py b/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.py new file mode 100644 index 0000000..6a081e0 --- /dev/null +++ b/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.py @@ -0,0 +1,34 @@ +# Copyright 2019 Brainbean Apps (https://brainbeanapps.com) +# Copyright 2019 Dataplug (https://dataplug.io) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models, api + + +class OnlineBankStatementPullWizard(models.TransientModel): + _name = 'online.bank.statement.pull.wizard' + _description = 'Online Bank Statement Pull Wizard' + + date_since = fields.Datetime( + string='Since', + required=True, + default=fields.Datetime.now, + ) + date_until = fields.Datetime( + string='Until', + required=True, + default=fields.Datetime.now, + ) + provider_ids = fields.Many2many( + string='Providers', + comodel_name='online.bank.statement.provider', + column1='wizard_id', + column2='provider_id', + relation='online_bank_statement_provider_pull_wizard_rel' + ) + + @api.multi + def action_pull(self): + self.ensure_one() + self.provider_ids._pull(self.date_since, self.date_until) + return {'type': 'ir.actions.act_window_close'} diff --git a/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.xml b/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.xml new file mode 100644 index 0000000..3fac2b8 --- /dev/null +++ b/account_bank_statement_import_online/wizards/online_bank_statement_pull_wizard.xml @@ -0,0 +1,32 @@ + + + + + + online.bank.statement.pull.wizard.form + online.bank.statement.pull.wizard + +
+ + + + + + + + + + + +
+
+
+ +
diff --git a/oca_dependencies.txt b/oca_dependencies.txt new file mode 100644 index 0000000..c077218 --- /dev/null +++ b/oca_dependencies.txt @@ -0,0 +1 @@ +web