From 37359a22be8496ce8c152fbeaf716cc1cea3cbd2 Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Sun, 11 Apr 2021 09:36:41 +0200 Subject: [PATCH 1/7] [12.0][imp][account_tax_balance] improve performance The tax balance report searches using a m2m field, which kills performance in large databases. We need direct SQL queries in order to work around this issue. See odoo/odoo#30350 --- account_tax_balance/models/account_tax.py | 139 ++++++++++++++-------- 1 file changed, 92 insertions(+), 47 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index e4c47356..481cf39e 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -121,58 +121,103 @@ class AccountTax(models.Model): state = [] return state - def get_move_line_partial_domain(self, from_date, to_date, company_id): - return [ - ('date', '<=', to_date), - ('date', '>=', from_date), - ('company_id', '=', company_id), - ] - - def compute_balance(self, tax_or_base='tax', move_type=None): + def get_move_line_partial_where(self, from_date, to_date, company_ids): + query = "aml.date <= %s AND aml.date >= %s AND aml.company_id IN %s" + params = [to_date, from_date, tuple(company_ids)] + return query, params + + def compute_balance(self, tax_or_base="tax", move_type=None): + # There's really bad performace in m2m fields. + # So we better do a direct query. + # See https://github.com/odoo/odoo/issues/30350 self.ensure_one() - domain = self.get_move_lines_domain( - tax_or_base=tax_or_base, move_type=move_type) - # balance is debit - credit whereas on tax return you want to see what - # vat has to be paid so: - # VAT on sales (credit) - VAT on purchases (debit). - - balance = self.env['account.move.line'].\ - read_group(domain, ['balance'], [])[0]['balance'] - return balance and -balance or 0 - - def get_balance_domain(self, state_list, type_list): - domain = [ - ('move_id.state', 'in', state_list), - ('tax_line_id', '=', self.id), - ('tax_exigible', '=', True) - ] + query, params = self.get_move_lines_query( + tax_or_base=tax_or_base, move_type=move_type + ) + _select = "sum(aml.balance)" + query = query.format(select_clause=_select) + self.env.cr.execute(query, params) # pylint: disable=E8103 + res = self.env.cr.fetchone() + balance = 0.0 + if res: + balance = res[0] + return balance and -balance or 0.0 + + def get_move_lines_query(self, tax_or_base="tax", move_type=None): + from_date, to_date, company_ids, target_move = self.get_context_values() + state_list = self.get_target_state_list(target_move) + type_list = self.get_target_type_list(move_type) + base_query = self.get_move_lines_base_query() + _where = "" + _joins = "" + _params = [] + where, params = self.get_move_line_partial_where( + from_date, to_date, company_ids + ) + _where += where + _params += params + if tax_or_base == "tax": + where, params = self.get_balance_where(state_list, type_list) + _where += where + _params += params + elif tax_or_base == "base": + joins, where, params = self.get_base_balance_where(state_list, type_list) + _where += where + _joins += joins + _params += params + query = base_query.format( + select_clause="{select_clause}", + where_clause=_where, + additional_joins=_joins, + ) + return query, _params + + def get_move_lines_base_query(self): + return ( + "SELECT {select_clause} FROM account_move_line AS aml " + "INNER JOIN account_move AS am ON aml.move_id = am.id " + "{additional_joins}" + " WHERE {where_clause}" + ) + + def get_balance_where(self, state_list, type_list): + where = ( + " AND am.state IN %s AND " + "aml.tax_line_id = %s AND " + "aml.tax_exigible = True" + ) + params = [tuple(state_list), self.id] if type_list: - domain.append(('move_id.move_type', 'in', type_list)) - return domain + where += " AND am.move_type IN %s" + params += [tuple(type_list)] + return where, params + + def get_base_balance_where(self, state_list, type_list): + joins = ( + " INNER JOIN account_move_line_account_tax_rel AS rel " + "ON aml.id = rel.account_move_line_id" + " INNER JOIN account_tax as tax " + "ON tax.id = rel.account_tax_id" + ) - def get_base_balance_domain(self, state_list, type_list): - domain = [ - ('move_id.state', 'in', state_list), - ('tax_ids', 'in', self.id), - ('tax_exigible', '=', True) - ] + where = " AND am.state IN %s" " AND tax.id = %s" " AND aml.tax_exigible = True " + params = [tuple(state_list), self.id] if type_list: - domain.append(('move_id.move_type', 'in', type_list)) - return domain + where += " AND am.move_type IN %s" + params += [tuple(type_list)] + return joins, where, params - def get_move_lines_domain(self, tax_or_base='tax', move_type=None): - from_date, to_date, company_id, target_move = self.get_context_values() - state_list = self.get_target_state_list(target_move) - type_list = self.get_target_type_list(move_type) - domain = self.get_move_line_partial_domain( - from_date, to_date, company_id) - balance_domain = [] - if tax_or_base == 'tax': - balance_domain = self.get_balance_domain(state_list, type_list) - elif tax_or_base == 'base': - balance_domain = self.get_base_balance_domain( - state_list, type_list) - domain.extend(balance_domain) + def get_move_lines_domain(self, tax_or_base="tax", move_type=None): + query, params = self.get_move_lines_query( + tax_or_base=tax_or_base, move_type=move_type + ) + _select = "aml.id" + query = query.format(select_clause=_select) + self.env.cr.execute(query, params) # pylint: disable=E8103 + amls = [] + for (aml_id,) in self.env.cr.fetchall(): + amls.append(aml_id) + domain = [("id", "in", amls)] return domain def get_lines_action(self, tax_or_base='tax', move_type=None): From 883d7dbbd33b17b99e5ffed223d42b280288594a Mon Sep 17 00:00:00 2001 From: Jordi Ballester Date: Wed, 14 Apr 2021 16:32:38 +0200 Subject: [PATCH 2/7] fixup! --- account_tax_balance/models/account_tax.py | 50 +++++++++++++++-------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 481cf39e..b77b9590 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -131,17 +131,16 @@ class AccountTax(models.Model): # So we better do a direct query. # See https://github.com/odoo/odoo/issues/30350 self.ensure_one() - query, params = self.get_move_lines_query( + _select, _group_by, query, params = self.get_move_lines_query( tax_or_base=tax_or_base, move_type=move_type ) - _select = "sum(aml.balance)" - query = query.format(select_clause=_select) + _select = "SUM(aml.balance)" + query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 - res = self.env.cr.fetchone() - balance = 0.0 - if res: - balance = res[0] - return balance and -balance or 0.0 + total_balance = {} + for balance, tax_id in self.env.cr.fetchall(): + total_balance[tax_id] = balance + return False # TODO def get_move_lines_query(self, tax_or_base="tax", move_type=None): from_date, to_date, company_ids, target_move = self.get_context_values() @@ -150,27 +149,39 @@ class AccountTax(models.Model): base_query = self.get_move_lines_base_query() _where = "" _joins = "" + _group_by = "" _params = [] + _select = "SELECT SUM(balance)" + _group_by = "GROUP BY " where, params = self.get_move_line_partial_where( from_date, to_date, company_ids ) _where += where _params += params if tax_or_base == "tax": - where, params = self.get_balance_where(state_list, type_list) + select, where, group_by, params = self.get_balance_where( + state_list, type_list + ) _where += where _params += params + _select += select + _group_by += group_by elif tax_or_base == "base": - joins, where, params = self.get_base_balance_where(state_list, type_list) + select, joins, where, group_by, params = self.get_base_balance_where( + state_list, type_list + ) _where += where _joins += joins _params += params + _select += select + _group_by += group_by query = base_query.format( select_clause="{select_clause}", where_clause=_where, additional_joins=_joins, + group_by="{group_by_clause}", ) - return query, _params + return _select, _group_by, query, _params def get_move_lines_base_query(self): return ( @@ -178,41 +189,44 @@ class AccountTax(models.Model): "INNER JOIN account_move AS am ON aml.move_id = am.id " "{additional_joins}" " WHERE {where_clause}" + "{group_by_clause}" ) def get_balance_where(self, state_list, type_list): + select = ", tax_line_id as tax_id" where = ( " AND am.state IN %s AND " "aml.tax_line_id = %s AND " "aml.tax_exigible = True" ) + group_by = "aml.tax_line_id" params = [tuple(state_list), self.id] if type_list: where += " AND am.move_type IN %s" params += [tuple(type_list)] - return where, params + return select, where, group_by, params def get_base_balance_where(self, state_list, type_list): + select = ", rel.account_tax_id as tax_id" joins = ( " INNER JOIN account_move_line_account_tax_rel AS rel " "ON aml.id = rel.account_move_line_id" - " INNER JOIN account_tax as tax " - "ON tax.id = rel.account_tax_id" ) - + group_by = "rel.account_tax_id" where = " AND am.state IN %s" " AND tax.id = %s" " AND aml.tax_exigible = True " params = [tuple(state_list), self.id] if type_list: where += " AND am.move_type IN %s" params += [tuple(type_list)] - return joins, where, params + return select, joins, where, group_by, params def get_move_lines_domain(self, tax_or_base="tax", move_type=None): - query, params = self.get_move_lines_query( + _select, _group_by, query, params = self.get_move_lines_query( tax_or_base=tax_or_base, move_type=move_type ) _select = "aml.id" - query = query.format(select_clause=_select) + _group_by = "" + query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 amls = [] for (aml_id,) in self.env.cr.fetchall(): From 2970b2e44724fb97f02da5d2c5bab3aca36954ba Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Thu, 15 Apr 2021 15:18:13 +0200 Subject: [PATCH 3/7] fixup! --- account_tax_balance/models/account_tax.py | 73 ++++++++++++++++------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index b77b9590..15312a80 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -92,15 +92,39 @@ class AccountTax(models.Model): return [('id', 'in', ids_with_moves)] def _compute_balance(self): + total_balance_regular = self.compute_balance( + tax_or_base="tax", move_type="regular" + ) + total_base_balance_regular = self.compute_balance( + tax_or_base="base", move_type="regular" + ) + total_balance_refund = self.compute_balance( + tax_or_base="tax", move_type="refund" + ) + total_base_balance_refund = self.compute_balance( + tax_or_base="base", move_type="refund" + ) for tax in self: - tax.balance_regular = tax.compute_balance( - tax_or_base='tax', move_type='regular') - tax.base_balance_regular = tax.compute_balance( - tax_or_base='base', move_type='regular') - tax.balance_refund = tax.compute_balance( - tax_or_base='tax', move_type='refund') - tax.base_balance_refund = tax.compute_balance( - tax_or_base='base', move_type='refund') + tax.balance_regular = ( + total_balance_regular[tax.id] + if tax.id in total_balance_regular.keys() + else 0.0 + ) + tax.base_balance_regular = ( + total_base_balance_regular[tax.id] + if tax.id in total_base_balance_regular.keys() + else 0.0 + ) + tax.balance_refund = ( + total_balance_refund[tax.id] + if tax.id in total_balance_refund.keys() + else 0.0 + ) + tax.base_balance_refund = ( + total_base_balance_refund[tax.id] + if tax.id in total_base_balance_refund.keys() + else 0.0 + ) tax.balance = tax.balance_regular + tax.balance_refund tax.base_balance = ( tax.base_balance_regular + tax.base_balance_refund) @@ -130,17 +154,16 @@ class AccountTax(models.Model): # There's really bad performace in m2m fields. # So we better do a direct query. # See https://github.com/odoo/odoo/issues/30350 - self.ensure_one() _select, _group_by, query, params = self.get_move_lines_query( tax_or_base=tax_or_base, move_type=move_type ) - _select = "SUM(aml.balance)" query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 + results = self.env.cr.fetchall() total_balance = {} - for balance, tax_id in self.env.cr.fetchall(): + for balance, tax_id in results: total_balance[tax_id] = balance - return False # TODO + return total_balance def get_move_lines_query(self, tax_or_base="tax", move_type=None): from_date, to_date, company_ids, target_move = self.get_context_values() @@ -152,7 +175,7 @@ class AccountTax(models.Model): _group_by = "" _params = [] _select = "SELECT SUM(balance)" - _group_by = "GROUP BY " + _group_by = " GROUP BY " where, params = self.get_move_line_partial_where( from_date, to_date, company_ids ) @@ -179,13 +202,13 @@ class AccountTax(models.Model): select_clause="{select_clause}", where_clause=_where, additional_joins=_joins, - group_by="{group_by_clause}", + group_by_clause="{group_by_clause}", ) return _select, _group_by, query, _params def get_move_lines_base_query(self): return ( - "SELECT {select_clause} FROM account_move_line AS aml " + "{select_clause} FROM account_move_line AS aml " "INNER JOIN account_move AS am ON aml.move_id = am.id " "{additional_joins}" " WHERE {where_clause}" @@ -193,14 +216,14 @@ class AccountTax(models.Model): ) def get_balance_where(self, state_list, type_list): - select = ", tax_line_id as tax_id" + select = ", aml.tax_line_id as tax_id" where = ( - " AND am.state IN %s AND " - "aml.tax_line_id = %s AND " - "aml.tax_exigible = True" + " AND am.state IN %s" + " AND aml.tax_line_id IS NOT NULL" + " AND aml.tax_exigible = True" ) group_by = "aml.tax_line_id" - params = [tuple(state_list), self.id] + params = [tuple(state_list)] if type_list: where += " AND am.move_type IN %s" params += [tuple(type_list)] @@ -213,8 +236,8 @@ class AccountTax(models.Model): "ON aml.id = rel.account_move_line_id" ) group_by = "rel.account_tax_id" - where = " AND am.state IN %s" " AND tax.id = %s" " AND aml.tax_exigible = True " - params = [tuple(state_list), self.id] + where = " AND am.state IN %s" " AND aml.tax_exigible = True " + params = [tuple(state_list)] if type_list: where += " AND am.move_type IN %s" params += [tuple(type_list)] @@ -224,8 +247,12 @@ class AccountTax(models.Model): _select, _group_by, query, params = self.get_move_lines_query( tax_or_base=tax_or_base, move_type=move_type ) - _select = "aml.id" + _select = "SELECT aml.id" _group_by = "" + if tax_or_base == "tax": + query += " AND aml.tax_line_id = " + str(self.id) + elif tax_or_base == "base": + query += " AND rel.account_tax_id = " + str(self.id) query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 amls = [] From 532cf7fc64a6f41611ffefa6a72b5a395457b2be Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Thu, 15 Apr 2021 17:11:58 +0200 Subject: [PATCH 4/7] [IMP] remove unused field has_moves --- account_tax_balance/models/account_tax.py | 53 +------------------ .../views/account_tax_view.xml | 1 - 2 files changed, 1 insertion(+), 53 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 15312a80..dc39ed3b 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -2,7 +2,7 @@ # © 2016 Antonio Espinosa # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import _, api, fields, models +from odoo import api, fields, models class AccountTax(models.Model): @@ -26,11 +26,6 @@ class AccountTax(models.Model): base_balance_refund = fields.Float( string="Base Balance Refund", compute="_compute_balance", ) - has_moves = fields.Boolean( - string="Has balance in period", - compute="_compute_has_moves", - search="_search_has_moves", - ) def get_context_values(self): context = self.env.context @@ -41,56 +36,10 @@ class AccountTax(models.Model): context.get('target_move', 'posted'), ) - def _account_tax_ids_with_moves(self): - """ Return all account.tax ids for which there is at least - one account.move.line in the context period - for the user company. - - Caveat: this ignores record rules and ACL but it is good - enough for filtering taxes with activity during the period. - """ - req = """ - SELECT id - FROM account_tax at - WHERE - company_id = %s AND - EXISTS ( - SELECT 1 FROM account_move_Line aml - WHERE - date >= %s AND - date <= %s AND - company_id = %s AND ( - tax_line_id = at.id OR - EXISTS ( - SELECT 1 FROM account_move_line_account_tax_rel - WHERE account_move_line_id = aml.id AND - account_tax_id = at.id - ) - ) - ) - """ - from_date, to_date, company_id, target_move = self.get_context_values() - self.env.cr.execute( - req, (company_id, from_date, to_date, company_id)) - return [r[0] for r in self.env.cr.fetchall()] - - @api.multi - def _compute_has_moves(self): - ids_with_moves = set(self._account_tax_ids_with_moves()) - for tax in self: - tax.has_moves = tax.id in ids_with_moves - @api.model def _is_unsupported_search_operator(self, operator): return operator != '=' - @api.model - def _search_has_moves(self, operator, value): - if self._is_unsupported_search_operator(operator) or not value: - raise ValueError(_("Unsupported search operator")) - ids_with_moves = self._account_tax_ids_with_moves() - return [('id', 'in', ids_with_moves)] - def _compute_balance(self): total_balance_regular = self.compute_balance( tax_or_base="tax", move_type="regular" diff --git a/account_tax_balance/views/account_tax_view.xml b/account_tax_balance/views/account_tax_view.xml index fdfd092b..b5ea3f86 100644 --- a/account_tax_balance/views/account_tax_view.xml +++ b/account_tax_balance/views/account_tax_view.xml @@ -60,7 +60,6 @@ account.tax form tree - [('has_moves', '=', True)] From fd07aa48489b8f4d4e72cf8f92f16d658b934e49 Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Wed, 21 Apr 2021 10:51:35 +0200 Subject: [PATCH 5/7] [IMP] account_tax_balance: reestructure to reduce number of queries --- account_tax_balance/models/account_tax.py | 89 +++++++++++++---------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index dc39ed3b..73b9f1d4 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -40,20 +40,36 @@ class AccountTax(models.Model): def _is_unsupported_search_operator(self, operator): return operator != '=' + def _compute_regular_and_refund(self, total): + tax_ids = total.keys() + total_refund = {} + total_regular = {} + for tax_id in tax_ids: + total_refund[tax_id] = 0.0 + total_regular[tax_id] = 0.0 + move_types = total[tax_id].keys() + for move_type in move_types: + if move_type in ["receivable_refund", "payable_refund"]: + total_refund[tax_id] += total[tax_id][move_type] + else: + total_regular[tax_id] += total[tax_id][move_type] + return total_regular, total_refund + def _compute_balance(self): - total_balance_regular = self.compute_balance( - tax_or_base="tax", move_type="regular" - ) - total_base_balance_regular = self.compute_balance( - tax_or_base="base", move_type="regular" - ) - total_balance_refund = self.compute_balance( - tax_or_base="tax", move_type="refund" + total_balance_tax = self.compute_balance(tax_or_base="tax") + total_balance_base = self.compute_balance(tax_or_base="base") + total_balance_regular, total_balance_refund = self._compute_regular_and_refund( + total_balance_tax ) - total_base_balance_refund = self.compute_balance( - tax_or_base="base", move_type="refund" + ( + total_base_balance_regular, + total_base_balance_refund, + ) = self._compute_regular_and_refund(total_balance_base) + founded_taxes_ids = set(list(total_balance_tax.keys())).union( + set(list(total_balance_base.keys())) ) - for tax in self: + for tax_id in list(founded_taxes_ids): + tax = self.browse(tax_id) tax.balance_regular = ( total_balance_regular[tax.id] if tax.id in total_balance_regular.keys() @@ -94,53 +110,52 @@ class AccountTax(models.Model): state = [] return state - def get_move_line_partial_where(self, from_date, to_date, company_ids): - query = "aml.date <= %s AND aml.date >= %s AND aml.company_id IN %s" - params = [to_date, from_date, tuple(company_ids)] + def get_move_line_partial_where(self, from_date, to_date, company_id): + query = "aml.date <= %s AND aml.date >= %s AND aml.company_id = %s" + params = [to_date, from_date, company_id] return query, params - def compute_balance(self, tax_or_base="tax", move_type=None): + def compute_balance(self, tax_or_base="tax"): # There's really bad performace in m2m fields. # So we better do a direct query. # See https://github.com/odoo/odoo/issues/30350 _select, _group_by, query, params = self.get_move_lines_query( - tax_or_base=tax_or_base, move_type=move_type + tax_or_base=tax_or_base ) query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 results = self.env.cr.fetchall() total_balance = {} - for balance, tax_id in results: - total_balance[tax_id] = balance + for balance, tax_id, move_type in results: + if tax_id not in total_balance.keys(): + total_balance[tax_id] = {} + total_balance[tax_id][move_type] = balance return total_balance - def get_move_lines_query(self, tax_or_base="tax", move_type=None): - from_date, to_date, company_ids, target_move = self.get_context_values() + def get_move_lines_query(self, tax_or_base="tax"): + from_date, to_date, company_id, target_move = self.get_context_values() state_list = self.get_target_state_list(target_move) - type_list = self.get_target_type_list(move_type) base_query = self.get_move_lines_base_query() _where = "" _joins = "" _group_by = "" _params = [] _select = "SELECT SUM(balance)" - _group_by = " GROUP BY " + _group_by = " GROUP BY am.move_type, " where, params = self.get_move_line_partial_where( - from_date, to_date, company_ids + from_date, to_date, company_id ) _where += where _params += params if tax_or_base == "tax": - select, where, group_by, params = self.get_balance_where( - state_list, type_list - ) + select, where, group_by, params = self.get_balance_where(state_list) _where += where _params += params _select += select _group_by += group_by elif tax_or_base == "base": select, joins, where, group_by, params = self.get_base_balance_where( - state_list, type_list + state_list ) _where += where _joins += joins @@ -164,8 +179,8 @@ class AccountTax(models.Model): "{group_by_clause}" ) - def get_balance_where(self, state_list, type_list): - select = ", aml.tax_line_id as tax_id" + def get_balance_where(self, state_list): + select = ", aml.tax_line_id as tax_id, am.move_type" where = ( " AND am.state IN %s" " AND aml.tax_line_id IS NOT NULL" @@ -173,13 +188,10 @@ class AccountTax(models.Model): ) group_by = "aml.tax_line_id" params = [tuple(state_list)] - if type_list: - where += " AND am.move_type IN %s" - params += [tuple(type_list)] return select, where, group_by, params - def get_base_balance_where(self, state_list, type_list): - select = ", rel.account_tax_id as tax_id" + def get_base_balance_where(self, state_list): + select = ", rel.account_tax_id as tax_id, am.move_type" joins = ( " INNER JOIN account_move_line_account_tax_rel AS rel " "ON aml.id = rel.account_move_line_id" @@ -187,14 +199,11 @@ class AccountTax(models.Model): group_by = "rel.account_tax_id" where = " AND am.state IN %s" " AND aml.tax_exigible = True " params = [tuple(state_list)] - if type_list: - where += " AND am.move_type IN %s" - params += [tuple(type_list)] return select, joins, where, group_by, params def get_move_lines_domain(self, tax_or_base="tax", move_type=None): _select, _group_by, query, params = self.get_move_lines_query( - tax_or_base=tax_or_base, move_type=move_type + tax_or_base=tax_or_base ) _select = "SELECT aml.id" _group_by = "" @@ -202,6 +211,10 @@ class AccountTax(models.Model): query += " AND aml.tax_line_id = " + str(self.id) elif tax_or_base == "base": query += " AND rel.account_tax_id = " + str(self.id) + type_list = self.get_target_type_list(move_type) + if type_list: + query += " AND am.move_type IN %s" + params += [tuple(type_list)] query = query.format(select_clause=_select, group_by_clause=_group_by) self.env.cr.execute(query, params) # pylint: disable=E8103 amls = [] From 409aff365800004db7a0957c64049cfe9a15786a Mon Sep 17 00:00:00 2001 From: Joan Sisquella Date: Thu, 22 Apr 2021 10:22:36 +0200 Subject: [PATCH 6/7] [IMP] account_tax_balance: show only taxes with amls --- account_tax_balance/models/account_tax.py | 20 +++++++++++++------ .../views/account_tax_view.xml | 2 ++ .../wizard/open_tax_balances.py | 1 + 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 73b9f1d4..21c8a55d 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -26,6 +26,11 @@ class AccountTax(models.Model): base_balance_refund = fields.Float( string="Base Balance Refund", compute="_compute_balance", ) + has_moves = fields.Boolean( + compute="_compute_balance", + search="_search_has_moves", + string="Has balance in period", + ) def get_context_values(self): context = self.env.context @@ -37,8 +42,11 @@ class AccountTax(models.Model): ) @api.model - def _is_unsupported_search_operator(self, operator): - return operator != '=' + def _search_has_moves(self, operator, value): + assert isinstance(value, bool), "Not implemented" + assert operator == "=", "Not implemented" + ids_with_moves = self.search([]).filtered(lambda t: t.has_moves == value) + return [('id', 'in', ids_with_moves.ids)] def _compute_regular_and_refund(self, total): tax_ids = total.keys() @@ -50,9 +58,9 @@ class AccountTax(models.Model): move_types = total[tax_id].keys() for move_type in move_types: if move_type in ["receivable_refund", "payable_refund"]: - total_refund[tax_id] += total[tax_id][move_type] + total_refund[tax_id] += (-1) * total[tax_id][move_type] else: - total_regular[tax_id] += total[tax_id][move_type] + total_regular[tax_id] += (-1) * total[tax_id][move_type] return total_regular, total_refund def _compute_balance(self): @@ -68,8 +76,8 @@ class AccountTax(models.Model): founded_taxes_ids = set(list(total_balance_tax.keys())).union( set(list(total_balance_base.keys())) ) - for tax_id in list(founded_taxes_ids): - tax = self.browse(tax_id) + for tax in self: + tax.has_moves = tax.id in list(founded_taxes_ids) tax.balance_regular = ( total_balance_regular[tax.id] if tax.id in total_balance_regular.keys() diff --git a/account_tax_balance/views/account_tax_view.xml b/account_tax_balance/views/account_tax_view.xml index b5ea3f86..7ade7524 100644 --- a/account_tax_balance/views/account_tax_view.xml +++ b/account_tax_balance/views/account_tax_view.xml @@ -46,6 +46,7 @@ + @@ -60,6 +61,7 @@ account.tax form tree + {"search_default_filter_has_moves":1} diff --git a/account_tax_balance/wizard/open_tax_balances.py b/account_tax_balance/wizard/open_tax_balances.py index 6063a9db..b5dedb8b 100644 --- a/account_tax_balance/wizard/open_tax_balances.py +++ b/account_tax_balance/wizard/open_tax_balances.py @@ -55,5 +55,6 @@ class WizardOpenTaxBalances(models.TransientModel): 'to_date': self.to_date, 'target_move': self.target_move, 'company_id': self.company_id.id, + 'search_default_filter_has_moves': True, } return vals From aa5601b057bd9bf181f36ef0e0b10fff0696a425 Mon Sep 17 00:00:00 2001 From: Mateu Griful Date: Fri, 9 Jul 2021 10:02:05 +0200 Subject: [PATCH 7/7] [IMP] Add option to display all TAX --- account_tax_balance/models/account_tax.py | 11 +++++++++-- account_tax_balance/wizard/open_tax_balances.py | 5 +++++ account_tax_balance/wizard/open_tax_balances_view.xml | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 21c8a55d..81d5ead2 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -39,13 +39,19 @@ class AccountTax(models.Model): context.get('to_date', fields.Date.context_today(self)), context.get('company_id', self.env.user.company_id.id), context.get('target_move', 'posted'), + context.get('display_all', False), ) @api.model def _search_has_moves(self, operator, value): assert isinstance(value, bool), "Not implemented" assert operator == "=", "Not implemented" - ids_with_moves = self.search([]).filtered(lambda t: t.has_moves == value) + from_date, to_date, company_id, target_move, display_all \ + = self.get_context_values() + if display_all: + ids_with_moves = self.env['account.tax'].search([]) + else: + ids_with_moves = self.search([]).filtered(lambda t: t.has_moves == value) return [('id', 'in', ids_with_moves.ids)] def _compute_regular_and_refund(self, total): @@ -141,7 +147,8 @@ class AccountTax(models.Model): return total_balance def get_move_lines_query(self, tax_or_base="tax"): - from_date, to_date, company_id, target_move = self.get_context_values() + from_date, to_date, company_id, target_move, display_all \ + = self.get_context_values() state_list = self.get_target_state_list(target_move) base_query = self.get_move_lines_base_query() _where = "" diff --git a/account_tax_balance/wizard/open_tax_balances.py b/account_tax_balance/wizard/open_tax_balances.py index b5dedb8b..1bdbec07 100644 --- a/account_tax_balance/wizard/open_tax_balances.py +++ b/account_tax_balance/wizard/open_tax_balances.py @@ -18,6 +18,10 @@ class WizardOpenTaxBalances(models.TransientModel): ('posted', 'All Posted Entries'), ('all', 'All Entries'), ], 'Target Moves', required=True, default='posted') + display_all = fields.Boolean( + string="Display all tax accounts", + default=False + ) @api.onchange('date_range_id') def onchange_date_range_id(self): @@ -56,5 +60,6 @@ class WizardOpenTaxBalances(models.TransientModel): 'target_move': self.target_move, 'company_id': self.company_id.id, 'search_default_filter_has_moves': True, + 'display_all': self.display_all, } return vals diff --git a/account_tax_balance/wizard/open_tax_balances_view.xml b/account_tax_balance/wizard/open_tax_balances_view.xml index 1cd5b0ef..cf965640 100644 --- a/account_tax_balance/wizard/open_tax_balances_view.xml +++ b/account_tax_balance/wizard/open_tax_balances_view.xml @@ -13,6 +13,7 @@ +