From cabfd7976e29f02375c5b1fce1623cbd1877aa70 Mon Sep 17 00:00:00 2001 From: Wesley Oliveira Date: Tue, 7 May 2024 14:47:43 -0300 Subject: [PATCH] [IMP] account_financial_report: general ledger account range --- .../tests/test_general_ledger.py | 18 +++++++++++++ .../wizard/general_ledger_wizard.py | 25 +++++++++++-------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/account_financial_report/tests/test_general_ledger.py b/account_financial_report/tests/test_general_ledger.py index c086a5a98fb..09be4d9e3c8 100644 --- a/account_financial_report/tests/test_general_ledger.py +++ b/account_financial_report/tests/test_general_ledger.py @@ -742,3 +742,21 @@ def test_account_type_filter(self): wizard.account_type_ids = False wizard._onchange_account_type_ids() self.assertEqual(wizard.account_ids, account_model) + + def test_validate_account_range(self): + accounts = self.env["account.account"].search([]) + wizard = self.env["general.ledger.report.wizard"].create( + { + "company_id": False, + "account_code_from": accounts[0].id, + "account_code_to": accounts[9].id, + } + ) + wizard.on_change_account_range() + self.assertEqual(wizard.account_ids, accounts[0:10]) + + company_id = self.env.user.company_id + company_accounts = accounts[0:10].filtered(lambda a: a.company_id == company_id) + wizard.company_id = company_id + wizard.on_change_account_range() + self.assertEqual(wizard.account_ids, company_accounts) diff --git a/account_financial_report/wizard/general_ledger_wizard.py b/account_financial_report/wizard/general_ledger_wizard.py index 2da09c981c9..ca8cd6b443b 100644 --- a/account_financial_report/wizard/general_ledger_wizard.py +++ b/account_financial_report/wizard/general_ledger_wizard.py @@ -108,17 +108,20 @@ def _get_account_move_lines_domain(self): @api.onchange("account_code_from", "account_code_to") def on_change_account_range(self): - if ( - self.account_code_from - and self.account_code_from.code.isdigit() - and self.account_code_to - and self.account_code_to.code.isdigit() - ): - start_range = int(self.account_code_from.code) - end_range = int(self.account_code_to.code) - self.account_ids = self.env["account.account"].search( - [("code", ">=", start_range), ("code", "<=", end_range)] - ) + if self.account_code_from and self.account_code_to: + start_range = self.account_code_from.code + end_range = self.account_code_to.code + + accounts = self.env["account.account"].search([]) + account_codes = [account.code for account in accounts] + + start_index = account_codes.index(start_range) + reverse_account_codes = account_codes[::-1] + reverse_end_index = reverse_account_codes.index(end_range) + end_index = len(account_codes) - 1 - reverse_end_index + + self.account_ids = accounts[start_index : end_index + 1] + if self.company_id: self.account_ids = self.account_ids.filtered( lambda a: a.company_id == self.company_id