From 47968a866baf8f8e62f826529dc90fea5348604a Mon Sep 17 00:00:00 2001 From: Iryna Vyshnevska Date: Thu, 14 Jan 2021 13:38:05 +0200 Subject: [PATCH] [IMP] add option to create addtional final line --- .../models/__init__.py | 1 + .../models/account_journal.py | 14 ++++ .../models/account_statement_import.py | 29 +++++--- .../readme/CREDITS.rst | 1 + .../readme/DESCRIPTION.rst | 5 ++ .../tests/__init__.py | 1 + .../tests/test_statement.py | 73 +++++++++++++++++++ 7 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 account_statement_import_camt54/models/account_journal.py create mode 100644 account_statement_import_camt54/readme/CREDITS.rst create mode 100644 account_statement_import_camt54/tests/test_statement.py diff --git a/account_statement_import_camt54/models/__init__.py b/account_statement_import_camt54/models/__init__.py index 11ac1bbc..7a228b17 100644 --- a/account_statement_import_camt54/models/__init__.py +++ b/account_statement_import_camt54/models/__init__.py @@ -1,2 +1,3 @@ from . import account_statement_import +from . import account_journal from . import parser diff --git a/account_statement_import_camt54/models/account_journal.py b/account_statement_import_camt54/models/account_journal.py new file mode 100644 index 00000000..752187a6 --- /dev/null +++ b/account_statement_import_camt54/models/account_journal.py @@ -0,0 +1,14 @@ +# Copyright 2020 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class AccountBankStatementImport(models.Model): + + _inherit = "account.journal" + + transfer_line = fields.Boolean( + string="Add balance Line", + help="Generate balance line on total of bank statement import", + ) diff --git a/account_statement_import_camt54/models/account_statement_import.py b/account_statement_import_camt54/models/account_statement_import.py index ae863028..6dc15ec3 100644 --- a/account_statement_import_camt54/models/account_statement_import.py +++ b/account_statement_import_camt54/models/account_statement_import.py @@ -12,18 +12,29 @@ class AccountStatementImport(models.TransientModel): _inherit = "account.statement.import" - def _create_bank_statements(self, stmts_vals): - """ Set balance_end_real if not already provided by the file.""" + def _create_bank_statements(self, stmts_vals, result): + """Create additional line in statement to set bank statement statement + to 0 balance""" - statement_line_ids, notifications = super()._create_bank_statements(stmts_vals) - statements = self.env["account.bank.statement"].search( - [("line_ids", "in", statement_line_ids)] - ) + super()._create_bank_statements(stmts_vals, result) + statements = self.env["account.bank.statement"].browse(result["statement_ids"]) for statement in statements: - if not statement.balance_end_real: - amount = sum(statement.line_ids.mapped("amount")) + amount = sum(statement.line_ids.mapped("amount")) + if statement.journal_id.transfer_line: + if amount != 0: + amount = -amount + statement.line_ids.create( + { + "name": statement.name, + "amount": amount, + "statement_id": statement.id, + "date": statement.date, + "payment_ref": "/", + } + ) + statement.balance_end_real = statement.balance_start + else: statement.balance_end_real = statement.balance_start + amount - return statement_line_ids, notifications def _complete_stmts_vals(self, stmts_vals, journal, account_number): """Search partner from partner reference""" diff --git a/account_statement_import_camt54/readme/CREDITS.rst b/account_statement_import_camt54/readme/CREDITS.rst new file mode 100644 index 00000000..5ee91fa4 --- /dev/null +++ b/account_statement_import_camt54/readme/CREDITS.rst @@ -0,0 +1 @@ +* Digital4efficiency.ch diff --git a/account_statement_import_camt54/readme/DESCRIPTION.rst b/account_statement_import_camt54/readme/DESCRIPTION.rst index 63b1f4cf..4b97b805 100644 --- a/account_statement_import_camt54/readme/DESCRIPTION.rst +++ b/account_statement_import_camt54/readme/DESCRIPTION.rst @@ -4,6 +4,11 @@ Customer invoices will be reconciled/Paid. Payment entries will be posted into a After this first step, import normally your CAMT.053 (full bank statement) into the bank journal. You will be able to clear the internal transfer account to end up the accounting flow. +Optionally we can activate add generation of additional line in bank statement which will balance your bank statement total to 0. +This line can be consolidated later with different account. +To enable option of final statement line you need properly set flag on Account Journal +Configuration -> Journals -> tab Advanced Settings -> Bank statement configuration + Switzerland localisation ------------------------ diff --git a/account_statement_import_camt54/tests/__init__.py b/account_statement_import_camt54/tests/__init__.py index c2592574..002dfdba 100644 --- a/account_statement_import_camt54/tests/__init__.py +++ b/account_statement_import_camt54/tests/__init__.py @@ -1 +1,2 @@ from . import test_get_partner_ref +from . import test_statement diff --git a/account_statement_import_camt54/tests/test_statement.py b/account_statement_import_camt54/tests/test_statement.py new file mode 100644 index 00000000..b7a16fd3 --- /dev/null +++ b/account_statement_import_camt54/tests/test_statement.py @@ -0,0 +1,73 @@ +# Copyright 2020 Camptocamp SA +# Copyright 2020 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import base64 + +import mock + +from odoo.modules.module import get_module_resource +from odoo.tests.common import SavepointCase + + +class TestGenerateBankStatement(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + bank = cls.env["res.partner.bank"].create( + { + "acc_number": "NL77ABNA0574908765", + "partner_id": cls.env.ref("base.main_partner").id, + "company_id": cls.env.ref("base.main_company").id, + "bank_id": cls.env.ref("base.res_bank_1").id, + } + ) + cls.env["res.partner.bank"].create( + { + "acc_number": "NL46ABNA0499998748", + "partner_id": cls.env.ref("base.main_partner").id, + "company_id": cls.env.ref("base.main_company").id, + "bank_id": cls.env.ref("base.res_bank_1").id, + } + ) + cls.journal = cls.env["account.journal"].create( + { + "name": "Bank Journal - (test camt)", + "code": "TBNKCAMT", + "type": "bank", + "bank_account_id": bank.id, + "currency_id": cls.env.ref("base.EUR").id, + } + ) + + @mock.patch( + "odoo.addons.account.models.sequence_mixin." + "SequenceMixin._constrains_date_sequence", + side_effect=False, + ) + def _load_statement(self, constraint): + testfile = get_module_resource( + "account_statement_import_camt", "test_files", "test-camt053" + ) + with open(testfile, "rb") as datafile: + camt_file = base64.b64encode(datafile.read()) + self.env["account.statement.import"].create( + { + "statement_filename": "test import", + "statement_file": camt_file, + } + ).import_file_button() + bank_st_record = self.env["account.bank.statement"].search( + [("name", "=", "1234Test/1")], limit=1 + ) + statement_lines = bank_st_record.line_ids + return statement_lines + + def test_statement_import(self): + self.journal.transfer_line = True + lines = self._load_statement() + self.assertEqual(len(lines), 5) + self.assertAlmostEqual(sum(lines.mapped("amount")), 0) + self.journal.transfer_line = False + lines = self._load_statement() + self.assertEqual(len(lines), 4) + self.assertAlmostEqual(sum(lines.mapped("amount")), -12.99)