diff --git a/account_tax_balance/README.rst b/account_tax_balance/README.rst index 2a6cffbc..ed8ac532 100644 --- a/account_tax_balance/README.rst +++ b/account_tax_balance/README.rst @@ -41,7 +41,7 @@ Accounting --> Reporting --> Taxes Balance Select the company, the date range, the target moves and 'open taxes' -.. figure:: https://raw.githubusercontent.com/account_tax_balance/static/description/tax_balance.png +.. figure:: https://user-images.githubusercontent.com/1336274/99388381-cd279300-28d5-11eb-9bb7-e8fe90d482af.png Bug Tracker =========== @@ -73,6 +73,7 @@ Contributors * Tecnativa - Pedro M. Baeza * ACSONE SA/NV - Stéphane Bidoul * Andrea Stirpe +* Iván Antón Maintainers ~~~~~~~~~~~ diff --git a/account_tax_balance/__manifest__.py b/account_tax_balance/__manifest__.py index 03993cb6..a179f5df 100644 --- a/account_tax_balance/__manifest__.py +++ b/account_tax_balance/__manifest__.py @@ -6,7 +6,7 @@ { "name": "Tax Balance", "summary": "Compute tax balances based on date range", - "version": "13.0.1.0.1", + "version": "14.0.1.0.0", "category": "Invoices & Payments", "website": "https://github.com/OCA/account-financial-reporting", "author": "Agile Business Group, Therp BV, Tecnativa, ACSONE SA/NV, " @@ -18,6 +18,7 @@ "wizard/open_tax_balances_view.xml", "views/account_move_view.xml", "views/account_tax_view.xml", + "security/ir.model.access.csv", ], "images": ["images/tax_balance.png"], "pre_init_hook": "pre_init_hook", diff --git a/account_tax_balance/hooks.py b/account_tax_balance/hooks.py index bdc693a5..55e5a1cd 100644 --- a/account_tax_balance/hooks.py +++ b/account_tax_balance/hooks.py @@ -7,14 +7,16 @@ from psycopg2 import sql def pre_init_hook(cr): - """Precreate move_type and fill with appropriate values to prevent + """Precreate financial_type and fill with appropriate values to prevent a MemoryError when the ORM attempts to call its compute method on a large amount of preexisting moves. Note that the order of the mapping is important as one move can have move lines on accounts of multiple types and the move type is set in the order of precedence.""" logger = logging.getLogger(__name__) - logger.info("Add account_move.move_type column if it does not yet exist") - cr.execute("ALTER TABLE account_move ADD COLUMN IF NOT EXISTS move_type VARCHAR") + logger.info("Add account_move.financial_type column if it does not yet exist") + cr.execute( + "ALTER TABLE account_move ADD COLUMN IF NOT EXISTS financial_type VARCHAR" + ) MAPPING = [ ("liquidity", "liquidity", False), ("payable", "payable", "AND aml.balance < 0"), @@ -23,22 +25,22 @@ def pre_init_hook(cr): ("receivable_refund", "receivable", "AND aml.balance >= 0"), ("other", False, False), ] - for move_type, internal_type, extra_where in MAPPING: - args = [move_type] - query = sql.SQL("UPDATE account_move am SET move_type = %s") + for financial_type, internal_type, extra_where in MAPPING: + args = [financial_type] + query = sql.SQL("UPDATE account_move am SET financial_type = %s") if internal_type: query += sql.SQL( """FROM account_move_line aml WHERE aml.account_id IN ( SELECT id FROM account_account WHERE internal_type = %s) - AND aml.move_id = am.id AND am.move_type IS NULL + AND aml.move_id = am.id AND am.financial_type IS NULL """ ) args.append(internal_type) else: - query += sql.SQL("WHERE am.move_type IS NULL") + query += sql.SQL("WHERE am.financial_type IS NULL") if extra_where: query += sql.SQL(extra_where) cr.execute(query, tuple(args)) - logger.info("%s move set to type %s", move_type, cr.rowcount) + logger.info("%s move set to type %s", financial_type, cr.rowcount) diff --git a/account_tax_balance/migrations/14.0.1.0.0/pre-migration.py b/account_tax_balance/migrations/14.0.1.0.0/pre-migration.py new file mode 100644 index 00000000..3d7a40e6 --- /dev/null +++ b/account_tax_balance/migrations/14.0.1.0.0/pre-migration.py @@ -0,0 +1,17 @@ +# Copyright 2020 Ozono Multimedia S.L.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from openupgradelib import openupgrade + +field_renames = [ + ( + "account.move", + "account_move", + "move_type", + "financial_type", + ), +] + + +@openupgrade.migrate() +def migrate(env, version): + openupgrade.rename_fields(env, field_renames) diff --git a/account_tax_balance/models/account_move.py b/account_tax_balance/models/account_move.py index bc54a4d0..e0919bfa 100644 --- a/account_tax_balance/models/account_move.py +++ b/account_tax_balance/models/account_move.py @@ -8,7 +8,7 @@ class AccountMove(models.Model): _inherit = "account.move" @api.model - def _selection_move_type(self): + def _selection_financial_type(self): return [ ("other", "Other"), ("liquidity", "Liquidity"), @@ -18,9 +18,9 @@ class AccountMove(models.Model): ("payable_refund", "Payable refund"), ] - move_type = fields.Selection( - selection="_selection_move_type", - compute="_compute_move_type", + financial_type = fields.Selection( + selection="_selection_financial_type", + compute="_compute_financial_type", store=True, readonly=True, ) @@ -30,7 +30,7 @@ class AccountMove(models.Model): "line_ids.balance", "line_ids.account_id.user_type_id.type", ) - def _compute_move_type(self): + def _compute_financial_type(self): def _balance_get(line_ids, internal_type): return sum( line_ids.filtered( @@ -41,12 +41,14 @@ class AccountMove(models.Model): for move in self: internal_types = move.line_ids.mapped("account_id.internal_type") if "liquidity" in internal_types: - move.move_type = "liquidity" + move.financial_type = "liquidity" elif "payable" in internal_types: balance = _balance_get(move.line_ids, "payable") - move.move_type = "payable" if balance < 0 else "payable_refund" + move.financial_type = "payable" if balance < 0 else "payable_refund" elif "receivable" in internal_types: balance = _balance_get(move.line_ids, "receivable") - move.move_type = "receivable" if balance > 0 else "receivable_refund" + move.financial_type = ( + "receivable" if balance > 0 else "receivable_refund" + ) else: - move.move_type = "other" + move.financial_type = "other" diff --git a/account_tax_balance/models/account_tax.py b/account_tax_balance/models/account_tax.py index 243bf65e..60f86b42 100644 --- a/account_tax_balance/models/account_tax.py +++ b/account_tax_balance/models/account_tax.py @@ -86,24 +86,24 @@ class AccountTax(models.Model): def _compute_balance(self): for tax in self: tax.balance_regular = tax.compute_balance( - tax_or_base="tax", move_type="regular" + tax_or_base="tax", financial_type="regular" ) tax.base_balance_regular = tax.compute_balance( - tax_or_base="base", move_type="regular" + tax_or_base="base", financial_type="regular" ) tax.balance_refund = tax.compute_balance( - tax_or_base="tax", move_type="refund" + tax_or_base="tax", financial_type="refund" ) tax.base_balance_refund = tax.compute_balance( - tax_or_base="base", move_type="refund" + tax_or_base="base", financial_type="refund" ) tax.balance = tax.balance_regular + tax.balance_refund tax.base_balance = tax.base_balance_regular + tax.base_balance_refund - def get_target_type_list(self, move_type=None): - if move_type == "refund": + def get_target_type_list(self, financial_type=None): + if financial_type == "refund": return ["receivable_refund", "payable_refund"] - elif move_type == "regular": + elif financial_type == "regular": return ["receivable", "payable", "liquidity", "other"] return [] @@ -123,10 +123,10 @@ class AccountTax(models.Model): ("company_id", "in", company_ids), ] - def compute_balance(self, tax_or_base="tax", move_type=None): + def compute_balance(self, tax_or_base="tax", financial_type=None): self.ensure_one() domain = self.get_move_lines_domain( - tax_or_base=tax_or_base, move_type=move_type + tax_or_base=tax_or_base, financial_type=financial_type ) # balance is debit - credit whereas on tax return you want to see what # vat has to be paid so: @@ -144,7 +144,7 @@ class AccountTax(models.Model): ("tax_exigible", "=", True), ] if type_list: - domain.append(("move_id.move_type", "in", type_list)) + domain.append(("move_id.financial_type", "in", type_list)) return domain def get_base_balance_domain(self, state_list, type_list): @@ -154,13 +154,13 @@ class AccountTax(models.Model): ("tax_exigible", "=", True), ] if type_list: - domain.append(("move_id.move_type", "in", type_list)) + domain.append(("move_id.financial_type", "in", type_list)) return domain - def get_move_lines_domain(self, tax_or_base="tax", move_type=None): + def get_move_lines_domain(self, tax_or_base="tax", financial_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) + type_list = self.get_target_type_list(financial_type) domain = self.get_move_line_partial_domain(from_date, to_date, company_ids) balance_domain = [] if tax_or_base == "tax": @@ -170,12 +170,12 @@ class AccountTax(models.Model): domain.extend(balance_domain) return domain - def get_lines_action(self, tax_or_base="tax", move_type=None): + def get_lines_action(self, tax_or_base="tax", financial_type=None): domain = self.get_move_lines_domain( - tax_or_base=tax_or_base, move_type=move_type + tax_or_base=tax_or_base, financial_type=financial_type ) action = self.env.ref("account.action_account_moves_all_tree") - vals = action.read()[0] + vals = action.sudo().read()[0] vals["context"] = {} vals["domain"] = domain return vals @@ -190,16 +190,16 @@ class AccountTax(models.Model): def view_tax_regular_lines(self): self.ensure_one() - return self.get_lines_action(tax_or_base="tax", move_type="regular") + return self.get_lines_action(tax_or_base="tax", financial_type="regular") def view_base_regular_lines(self): self.ensure_one() - return self.get_lines_action(tax_or_base="base", move_type="regular") + return self.get_lines_action(tax_or_base="base", financial_type="regular") def view_tax_refund_lines(self): self.ensure_one() - return self.get_lines_action(tax_or_base="tax", move_type="refund") + return self.get_lines_action(tax_or_base="tax", financial_type="refund") def view_base_refund_lines(self): self.ensure_one() - return self.get_lines_action(tax_or_base="base", move_type="refund") + return self.get_lines_action(tax_or_base="base", financial_type="refund") diff --git a/account_tax_balance/readme/CONTRIBUTORS.rst b/account_tax_balance/readme/CONTRIBUTORS.rst index 2712f89b..9d50d806 100644 --- a/account_tax_balance/readme/CONTRIBUTORS.rst +++ b/account_tax_balance/readme/CONTRIBUTORS.rst @@ -4,3 +4,4 @@ * Tecnativa - Pedro M. Baeza * ACSONE SA/NV - Stéphane Bidoul * Andrea Stirpe +* Iván Antón diff --git a/account_tax_balance/security/ir.model.access.csv b/account_tax_balance/security/ir.model.access.csv new file mode 100644 index 00000000..282f560f --- /dev/null +++ b/account_tax_balance/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_wizard_open_tax_balances_user,access_wizard_open_tax_balances,model_wizard_open_tax_balances,account.group_account_user,1,1,1,1 +access_wizard_open_tax_balances_manager,access_wizard_open_tax_balances,model_wizard_open_tax_balances,account.group_account_manager,1,1,1,1 diff --git a/account_tax_balance/static/description/index.html b/account_tax_balance/static/description/index.html index b89550d5..22329ea1 100644 --- a/account_tax_balance/static/description/index.html +++ b/account_tax_balance/static/description/index.html @@ -389,7 +389,7 @@ It depends on date_range module and exposes ‘compute’ methods that can be ca

Accounting –> Reporting –> Taxes Balance

Select the company, the date range, the target moves and ‘open taxes’

-https://raw.githubusercontent.com/account_tax_balance/static/description/tax_balance.png +Taxes
diff --git a/account_tax_balance/tests/test_account_tax_balance.py b/account_tax_balance/tests/test_account_tax_balance.py index 440857f3..0f11a27c 100644 --- a/account_tax_balance/tests/test_account_tax_balance.py +++ b/account_tax_balance/tests/test_account_tax_balance.py @@ -16,8 +16,10 @@ from odoo.tests.common import HttpCase class TestAccountTaxBalance(HttpCase): def setUp(self): super().setUp() + + self.company = self.env.user.company_id self.range_type = self.env["date.range.type"].create( - {"name": "Fiscal year", "company_id": False, "allow_overlap": False} + {"name": "Fiscal year", "allow_overlap": False} ) self.range_generator = self.env["date.range.generator"] self.current_year = datetime.now().year @@ -69,7 +71,7 @@ class TestAccountTaxBalance(HttpCase): invoice = self.env["account.move"].create( { "partner_id": self.env.ref("base.res_partner_2").id, - "type": "out_invoice", + "move_type": "out_invoice", "invoice_line_ids": [ ( 0, @@ -152,7 +154,7 @@ class TestAccountTaxBalance(HttpCase): refund = self.env["account.move"].create( { "partner_id": self.env.ref("base.res_partner_2").id, - "type": "out_refund", + "move_type": "out_refund", "invoice_line_ids": [ ( 0, @@ -193,15 +195,24 @@ class TestAccountTaxBalance(HttpCase): ) liquidity_account_id = ( self.env["account.account"] - .search([("internal_type", "=", "liquidity")], limit=1) + .search( + [ + ("internal_type", "=", "liquidity"), + ("company_id", "=", self.company.id), + ], + limit=1, + ) .id ) move = self.env["account.move"].create( { - "type": "entry", + "move_type": "entry", "date": Date.context_today(self.env.user), "journal_id": self.env["account.journal"] - .search([("type", "=", "bank")], limit=1) + .search( + [("type", "=", "bank"), ("company_id", "=", self.company.id)], + limit=1, + ) .id, "name": "Test move", "line_ids": [ diff --git a/account_tax_balance/views/account_move_view.xml b/account_tax_balance/views/account_move_view.xml index 2d96548b..39272005 100644 --- a/account_tax_balance/views/account_move_view.xml +++ b/account_tax_balance/views/account_move_view.xml @@ -8,7 +8,7 @@ - + @@ -18,7 +18,7 @@ - + @@ -29,10 +29,10 @@ diff --git a/account_tax_balance/wizard/open_tax_balances.py b/account_tax_balance/wizard/open_tax_balances.py index 3147641e..a878959a 100644 --- a/account_tax_balance/wizard/open_tax_balances.py +++ b/account_tax_balance/wizard/open_tax_balances.py @@ -41,7 +41,7 @@ class WizardOpenTaxBalances(models.TransientModel): def open_taxes(self): self.ensure_one() action = self.env.ref("account_tax_balance.action_tax_balances_tree") - act_vals = action.read()[0] + act_vals = action.sudo().read()[0] # override action name doesn't work in v12 or v10 # we need to build a dynamic action on main keys vals = {