Skip to content

Commit

Permalink
[ADD] accounbt_bank_statement_import_file_hook
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiBForgeFlow authored and JaumeBforgeFlow committed May 24, 2023
1 parent 1fa1500 commit 42bc964
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 0 deletions.
2 changes: 2 additions & 0 deletions account_bank_statement_import_file_hook/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import models
from .hooks import post_load_hook
14 changes: 14 additions & 0 deletions account_bank_statement_import_file_hook/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
{
"name": "Account Bank Statement Import File Hook",
"version": "13.0.1.0.0",
# see https://odoo-community.org/page/development-status
"development_status": "Production/Stable",
"category": "Invoicing Management",
"website": "https://github.com/OCA/bank-statement-import",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"license": "LGPL-3",
"depends": ["account_bank_statement_import"],
"post_load": "post_load_hook",
}
86 changes: 86 additions & 0 deletions account_bank_statement_import_file_hook/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2023 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
import base64

from odoo import _
from odoo.exceptions import UserError

from odoo.addons.account_bank_statement_import.account_bank_statement_import import (
AccountBankStatementImport,
)


def post_load_hook():
def import_file_new(self):
""" Process the file chosen in the wizard, create bank
statement(s) and go to reconciliation. """
if not hasattr(self, "_post_process_statements"):
return self.import_file_original()

Check warning on line 18 in account_bank_statement_import_file_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

account_bank_statement_import_file_hook/hooks.py#L18

Added line #L18 was not covered by tests
self.ensure_one()
statement_line_ids_all = []
notifications_all = []
# Let the appropriate implementation module parse the file and
# return the required data
# The active_id is passed in context in case an implementation
# module requires information about the wizard state (see QIF)
for data_file in self.attachment_ids:
currency_code, account_number, stmts_vals = self.with_context(
active_id=self.ids[0]
)._parse_file(base64.b64decode(data_file.datas))
# Check raw data
self._check_parsed_data(stmts_vals, account_number)
# Try to find the currency and journal in odoo
currency, journal = self._find_additional_data(
currency_code, account_number
)
# If no journal found, ask the user about creating one
if not journal:
# The active_id is passed in context so the wizard can call
# import_file again once the journal is created
return self.with_context(

Check warning on line 40 in account_bank_statement_import_file_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

account_bank_statement_import_file_hook/hooks.py#L40

Added line #L40 was not covered by tests
active_id=self.ids[0]
)._journal_creation_wizard(currency, account_number)
if (
not journal.default_debit_account_id
or not journal.default_credit_account_id
):
raise UserError(

Check warning on line 47 in account_bank_statement_import_file_hook/hooks.py

View check run for this annotation

Codecov / codecov/patch

account_bank_statement_import_file_hook/hooks.py#L47

Added line #L47 was not covered by tests
_(
"You have to set a Default Debit Account and a Default "
"Credit Account for the journal: %s"
)
% (journal.name,)
)
# Prepare statement data to be used for bank statements creation
stmts_vals = self._complete_stmts_vals(stmts_vals, journal, account_number)
# Create the bank statements
statement_line_ids, notifications = self._create_bank_statements(stmts_vals)
statement_line_ids_all.extend(statement_line_ids)
notifications_all.extend(notifications)
# START HOOK
# Link attachment in bank statement chatter
self._post_process_statements(data_file, statement_line_ids_all)
# END HOOK
# Now that the import worked out, set it as the bank_statements_source
# of the journal
if journal.bank_statements_source != "file_import":
# Use sudo() because only 'account.group_account_manager'
# has write access on 'account.journal', but 'account.group_account_user'
# must be able to import bank statement files
journal.sudo().bank_statements_source = "file_import"
# Finally dispatch to reconciliation interface
return {
"type": "ir.actions.client",
"tag": "bank_statement_reconciliation_view",
"context": {
"statement_line_ids": statement_line_ids_all,
"company_ids": self.env.user.company_ids.ids,
"notifications": notifications_all,
},
}

if not hasattr(AccountBankStatementImport, "import_file_original"):
AccountBankStatementImport.import_file_original = (
AccountBankStatementImport.import_file
)
AccountBankStatementImport.import_file = import_file_new
1 change: 1 addition & 0 deletions account_bank_statement_import_file_hook/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_bank_statement_import
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2023 ForgeFlow, S.L.
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

from odoo import models


class AccountBankStatementImport(models.TransientModel):
_inherit = "account.bank.statement.import"

def _post_process_statements(self, data_file, statement_line_ids):
"""To be inherited"""
return False
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Jaume Bernaus <jaume.bernaus@forgeflow.com>
* Jordi Ballester <jordi.ballester@forgeflow.com>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Module to add a hook to the import_file method of bank statement import.
6 changes: 6 additions & 0 deletions setup/account_bank_statement_import_file_hook/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 42bc964

Please sign in to comment.