[MIG] account_tax_balance: Migration to 14.0

pull/837/head
Francisco Ivan Anton Prieto 2020-10-23 15:30:27 +02:00 committed by Valentin Vinagre Urteaga
parent 4d16073ed9
commit 6ea0d1e03b
12 changed files with 90 additions and 52 deletions

View File

@ -41,7 +41,7 @@ Accounting --> Reporting --> Taxes Balance
Select the company, the date range, the target moves and 'open taxes' 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 Bug Tracker
=========== ===========
@ -73,6 +73,7 @@ Contributors
* Tecnativa - Pedro M. Baeza * Tecnativa - Pedro M. Baeza
* ACSONE SA/NV - Stéphane Bidoul * ACSONE SA/NV - Stéphane Bidoul
* Andrea Stirpe <a.stirpe@onestein.nl> * Andrea Stirpe <a.stirpe@onestein.nl>
* Iván Antón <ozono@ozonomultimedia.com>
Maintainers Maintainers
~~~~~~~~~~~ ~~~~~~~~~~~

View File

@ -6,7 +6,7 @@
{ {
"name": "Tax Balance", "name": "Tax Balance",
"summary": "Compute tax balances based on date range", "summary": "Compute tax balances based on date range",
"version": "13.0.1.0.1", "version": "14.0.1.0.0",
"category": "Invoices & Payments", "category": "Invoices & Payments",
"website": "https://github.com/OCA/account-financial-reporting", "website": "https://github.com/OCA/account-financial-reporting",
"author": "Agile Business Group, Therp BV, Tecnativa, ACSONE SA/NV, " "author": "Agile Business Group, Therp BV, Tecnativa, ACSONE SA/NV, "
@ -18,6 +18,7 @@
"wizard/open_tax_balances_view.xml", "wizard/open_tax_balances_view.xml",
"views/account_move_view.xml", "views/account_move_view.xml",
"views/account_tax_view.xml", "views/account_tax_view.xml",
"security/ir.model.access.csv",
], ],
"images": ["images/tax_balance.png"], "images": ["images/tax_balance.png"],
"pre_init_hook": "pre_init_hook", "pre_init_hook": "pre_init_hook",

View File

@ -7,14 +7,16 @@ from psycopg2 import sql
def pre_init_hook(cr): 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 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 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 important as one move can have move lines on accounts of multiple types
and the move type is set in the order of precedence.""" and the move type is set in the order of precedence."""
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.info("Add account_move.move_type column if it does not yet exist") 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 move_type VARCHAR") cr.execute(
"ALTER TABLE account_move ADD COLUMN IF NOT EXISTS financial_type VARCHAR"
)
MAPPING = [ MAPPING = [
("liquidity", "liquidity", False), ("liquidity", "liquidity", False),
("payable", "payable", "AND aml.balance < 0"), ("payable", "payable", "AND aml.balance < 0"),
@ -23,22 +25,22 @@ def pre_init_hook(cr):
("receivable_refund", "receivable", "AND aml.balance >= 0"), ("receivable_refund", "receivable", "AND aml.balance >= 0"),
("other", False, False), ("other", False, False),
] ]
for move_type, internal_type, extra_where in MAPPING: for financial_type, internal_type, extra_where in MAPPING:
args = [move_type] args = [financial_type]
query = sql.SQL("UPDATE account_move am SET move_type = %s") query = sql.SQL("UPDATE account_move am SET financial_type = %s")
if internal_type: if internal_type:
query += sql.SQL( query += sql.SQL(
"""FROM account_move_line aml """FROM account_move_line aml
WHERE aml.account_id IN ( WHERE aml.account_id IN (
SELECT id FROM account_account SELECT id FROM account_account
WHERE internal_type = %s) 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) args.append(internal_type)
else: else:
query += sql.SQL("WHERE am.move_type IS NULL") query += sql.SQL("WHERE am.financial_type IS NULL")
if extra_where: if extra_where:
query += sql.SQL(extra_where) query += sql.SQL(extra_where)
cr.execute(query, tuple(args)) 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)

View File

@ -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)

View File

@ -8,7 +8,7 @@ class AccountMove(models.Model):
_inherit = "account.move" _inherit = "account.move"
@api.model @api.model
def _selection_move_type(self): def _selection_financial_type(self):
return [ return [
("other", "Other"), ("other", "Other"),
("liquidity", "Liquidity"), ("liquidity", "Liquidity"),
@ -18,9 +18,9 @@ class AccountMove(models.Model):
("payable_refund", "Payable refund"), ("payable_refund", "Payable refund"),
] ]
move_type = fields.Selection( financial_type = fields.Selection(
selection="_selection_move_type", selection="_selection_financial_type",
compute="_compute_move_type", compute="_compute_financial_type",
store=True, store=True,
readonly=True, readonly=True,
) )
@ -30,7 +30,7 @@ class AccountMove(models.Model):
"line_ids.balance", "line_ids.balance",
"line_ids.account_id.user_type_id.type", "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): def _balance_get(line_ids, internal_type):
return sum( return sum(
line_ids.filtered( line_ids.filtered(
@ -41,12 +41,14 @@ class AccountMove(models.Model):
for move in self: for move in self:
internal_types = move.line_ids.mapped("account_id.internal_type") internal_types = move.line_ids.mapped("account_id.internal_type")
if "liquidity" in internal_types: if "liquidity" in internal_types:
move.move_type = "liquidity" move.financial_type = "liquidity"
elif "payable" in internal_types: elif "payable" in internal_types:
balance = _balance_get(move.line_ids, "payable") 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: elif "receivable" in internal_types:
balance = _balance_get(move.line_ids, "receivable") 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: else:
move.move_type = "other" move.financial_type = "other"

View File

@ -86,24 +86,24 @@ class AccountTax(models.Model):
def _compute_balance(self): def _compute_balance(self):
for tax in self: for tax in self:
tax.balance_regular = tax.compute_balance( 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.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.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.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.balance = tax.balance_regular + tax.balance_refund
tax.base_balance = tax.base_balance_regular + tax.base_balance_refund tax.base_balance = tax.base_balance_regular + tax.base_balance_refund
def get_target_type_list(self, move_type=None): def get_target_type_list(self, financial_type=None):
if move_type == "refund": if financial_type == "refund":
return ["receivable_refund", "payable_refund"] return ["receivable_refund", "payable_refund"]
elif move_type == "regular": elif financial_type == "regular":
return ["receivable", "payable", "liquidity", "other"] return ["receivable", "payable", "liquidity", "other"]
return [] return []
@ -123,10 +123,10 @@ class AccountTax(models.Model):
("company_id", "in", company_ids), ("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() self.ensure_one()
domain = self.get_move_lines_domain( 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 # balance is debit - credit whereas on tax return you want to see what
# vat has to be paid so: # vat has to be paid so:
@ -144,7 +144,7 @@ class AccountTax(models.Model):
("tax_exigible", "=", True), ("tax_exigible", "=", True),
] ]
if type_list: if type_list:
domain.append(("move_id.move_type", "in", type_list)) domain.append(("move_id.financial_type", "in", type_list))
return domain return domain
def get_base_balance_domain(self, state_list, type_list): def get_base_balance_domain(self, state_list, type_list):
@ -154,13 +154,13 @@ class AccountTax(models.Model):
("tax_exigible", "=", True), ("tax_exigible", "=", True),
] ]
if type_list: if type_list:
domain.append(("move_id.move_type", "in", type_list)) domain.append(("move_id.financial_type", "in", type_list))
return domain 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() from_date, to_date, company_ids, target_move = self.get_context_values()
state_list = self.get_target_state_list(target_move) 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) domain = self.get_move_line_partial_domain(from_date, to_date, company_ids)
balance_domain = [] balance_domain = []
if tax_or_base == "tax": if tax_or_base == "tax":
@ -170,12 +170,12 @@ class AccountTax(models.Model):
domain.extend(balance_domain) domain.extend(balance_domain)
return 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( 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") action = self.env.ref("account.action_account_moves_all_tree")
vals = action.read()[0] vals = action.sudo().read()[0]
vals["context"] = {} vals["context"] = {}
vals["domain"] = domain vals["domain"] = domain
return vals return vals
@ -190,16 +190,16 @@ class AccountTax(models.Model):
def view_tax_regular_lines(self): def view_tax_regular_lines(self):
self.ensure_one() 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): def view_base_regular_lines(self):
self.ensure_one() 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): def view_tax_refund_lines(self):
self.ensure_one() 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): def view_base_refund_lines(self):
self.ensure_one() 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")

View File

@ -4,3 +4,4 @@
* Tecnativa - Pedro M. Baeza * Tecnativa - Pedro M. Baeza
* ACSONE SA/NV - Stéphane Bidoul * ACSONE SA/NV - Stéphane Bidoul
* Andrea Stirpe <a.stirpe@onestein.nl> * Andrea Stirpe <a.stirpe@onestein.nl>
* Iván Antón <ozono@ozonomultimedia.com>

View File

@ -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
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_wizard_open_tax_balances_user access_wizard_open_tax_balances model_wizard_open_tax_balances account.group_account_user 1 1 1 1
3 access_wizard_open_tax_balances_manager access_wizard_open_tax_balances model_wizard_open_tax_balances account.group_account_manager 1 1 1 1

View File

@ -389,7 +389,7 @@ It depends on date_range module and exposes compute methods that can be ca
<p>Accounting &gt; Reporting &gt; Taxes Balance</p> <p>Accounting &gt; Reporting &gt; Taxes Balance</p>
<p>Select the company, the date range, the target moves and open taxes</p> <p>Select the company, the date range, the target moves and open taxes</p>
<div class="figure"> <div class="figure">
<img alt="https://raw.githubusercontent.com/account_tax_balance/static/description/tax_balance.png" src="https://raw.githubusercontent.com/account_tax_balance/static/description/tax_balance.png" /> <img alt="Taxes" src="https://user-images.githubusercontent.com/1336274/99388381-cd279300-28d5-11eb-9bb7-e8fe90d482af.png" />
</div> </div>
</div> </div>
<div class="section" id="bug-tracker"> <div class="section" id="bug-tracker">

View File

@ -16,8 +16,10 @@ from odoo.tests.common import HttpCase
class TestAccountTaxBalance(HttpCase): class TestAccountTaxBalance(HttpCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.company = self.env.user.company_id
self.range_type = self.env["date.range.type"].create( 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.range_generator = self.env["date.range.generator"]
self.current_year = datetime.now().year self.current_year = datetime.now().year
@ -69,7 +71,7 @@ class TestAccountTaxBalance(HttpCase):
invoice = self.env["account.move"].create( invoice = self.env["account.move"].create(
{ {
"partner_id": self.env.ref("base.res_partner_2").id, "partner_id": self.env.ref("base.res_partner_2").id,
"type": "out_invoice", "move_type": "out_invoice",
"invoice_line_ids": [ "invoice_line_ids": [
( (
0, 0,
@ -152,7 +154,7 @@ class TestAccountTaxBalance(HttpCase):
refund = self.env["account.move"].create( refund = self.env["account.move"].create(
{ {
"partner_id": self.env.ref("base.res_partner_2").id, "partner_id": self.env.ref("base.res_partner_2").id,
"type": "out_refund", "move_type": "out_refund",
"invoice_line_ids": [ "invoice_line_ids": [
( (
0, 0,
@ -193,15 +195,24 @@ class TestAccountTaxBalance(HttpCase):
) )
liquidity_account_id = ( liquidity_account_id = (
self.env["account.account"] self.env["account.account"]
.search([("internal_type", "=", "liquidity")], limit=1) .search(
[
("internal_type", "=", "liquidity"),
("company_id", "=", self.company.id),
],
limit=1,
)
.id .id
) )
move = self.env["account.move"].create( move = self.env["account.move"].create(
{ {
"type": "entry", "move_type": "entry",
"date": Date.context_today(self.env.user), "date": Date.context_today(self.env.user),
"journal_id": self.env["account.journal"] "journal_id": self.env["account.journal"]
.search([("type", "=", "bank")], limit=1) .search(
[("type", "=", "bank"), ("company_id", "=", self.company.id)],
limit=1,
)
.id, .id,
"name": "Test move", "name": "Test move",
"line_ids": [ "line_ids": [

View File

@ -8,7 +8,7 @@
<field name="inherit_id" ref="account.view_move_tree" /> <field name="inherit_id" ref="account.view_move_tree" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="state" position="after"> <field name="state" position="after">
<field name="move_type" /> <field name="financial_type" />
</field> </field>
</field> </field>
</record> </record>
@ -18,7 +18,7 @@
<field name="inherit_id" ref="account.view_move_form" /> <field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="ref" position="after"> <field name="ref" position="after">
<field name="move_type" /> <field name="financial_type" />
</field> </field>
</field> </field>
</record> </record>
@ -29,10 +29,10 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<group expand="0" position="inside"> <group expand="0" position="inside">
<filter <filter
name="move_type" name="financial_type"
string="Move type" string="Move type"
domain="[]" domain="[]"
context="{'group_by':'move_type'}" context="{'group_by':'financial_type'}"
/> />
</group> </group>
</field> </field>

View File

@ -41,7 +41,7 @@ class WizardOpenTaxBalances(models.TransientModel):
def open_taxes(self): def open_taxes(self):
self.ensure_one() self.ensure_one()
action = self.env.ref("account_tax_balance.action_tax_balances_tree") 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 # override action name doesn't work in v12 or v10
# we need to build a dynamic action on main keys # we need to build a dynamic action on main keys
vals = { vals = {