Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][IMP] l10n_es_vat_prorate: Use a proper account on investments and assets + Add hook for VAT prorate move line #3833

Open
wants to merge 2 commits into
base: 17.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions l10n_es_vat_prorate/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ def _process_aeat_tax_fee_info(self, res, tax, sign):
res[tax]["deductible_amount"] -= self.balance * sign
return result

def _prepare_vat_prorate_key(self):
self.ensure_one()
return {
"vat_prorate": True,
"account_id": self.company_id._get_tax_prorrate_account_map().get(
self.account_id.account_type
)
or self.account_id.id,
"analytic_distribution": self.analytic_distribution,
}

@api.depends("with_vat_prorate")
def _compute_all_tax(self):
"""After getting normal taxes dict that is dumped into this field, we loop
Expand Down Expand Up @@ -100,13 +111,7 @@ def _compute_all_tax(self):
new_vals[field] -= tax_vals[field]
new_vals["vat_prorate"] = True
new_key = dict(tax_key)
new_key.update(
{
"vat_prorate": True,
"account_id": line.account_id.id,
"analytic_distribution": line.analytic_distribution,
}
)
new_key.update(line._prepare_vat_prorate_key())
new_key = frozendict(new_key)
if prorate_tax_list.get(new_key):
for field in {"amount_currency", "balance"}:
Expand Down
47 changes: 46 additions & 1 deletion l10n_es_vat_prorate/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright 2023 Tecnativa Carolina Fernandez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError
from odoo.tools import ormcache

Expand All @@ -19,6 +19,36 @@ class ResCompany(models.Model):
vat_prorate_ids = fields.One2many(
"res.company.vat.prorate", inverse_name="company_id"
)
prorrate_asset_account_id = fields.Many2one(
"account.account",
domain="[('company_id', '=', id)]",
compute="_compute_prorrate_accounts",
store=True,
readonly=False,
)
prorrate_investment_account_id = fields.Many2one(
"account.account",
domain="[('company_id', '=', id)]",
compute="_compute_prorrate_accounts",
store=True,
readonly=False,
)

@api.depends("chart_template", "with_vat_prorate")
def _compute_prorrate_accounts(self):
for record in self:
if record.with_vat_prorate and record.chart_template:
record.prorrate_asset_account_id = self.env.ref(
"l10n_es.%s_account_common_6341" % record.id,
raise_if_not_found=False,
)
record.prorrate_investment_account_id = self.env.ref(
"l10n_es.%s_account_common_6342" % record.id,
raise_if_not_found=False,
)
else:
record.prorrate_asset_account_id = False
record.prorrate_investment_account_id = False

@ormcache("self")
def _get_prorate_accounts(self):
Expand Down Expand Up @@ -50,6 +80,21 @@ def _check_vat_prorate_ids(self):
if rec.with_vat_prorate and not rec.vat_prorate_ids:
raise ValidationError(_("You must complete VAT prorate information"))

@tools.ormcache(
"self.id",
"self.prorrate_asset_account_id.id",
"self.prorrate_investment_account_id.id",
)
def _get_tax_prorrate_account_map(self):
"""Get the account mapping according user type"""
return {
"asset_current": self.prorrate_asset_account_id.id,
"asset_non_current": self.prorrate_asset_account_id.id,
"asset_fixed": self.prorrate_asset_account_id.id,
"liability_current": self.prorrate_investment_account_id.id,
"liability_non_current": self.prorrate_investment_account_id.id,
}


class ResCompanyVatProrate(models.Model):
_name = "res.company.vat.prorate"
Expand Down
34 changes: 34 additions & 0 deletions l10n_es_vat_prorate/tests/test_vat_prorate.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def test_no_company_vat_prorate_information(self):
with self.assertRaises(exceptions.ValidationError):
self.env.company.write({"vat_prorate_ids": False})

def test_company_configuration(self):
self.assertTrue(self.env.company.with_vat_prorate)
self.assertTrue(self.env.company.prorrate_asset_account_id)
self.assertTrue(self.env.company.prorrate_investment_account_id)
self.env.company.write({"with_vat_prorate": False})
self.assertFalse(self.env.company.prorrate_asset_account_id)
self.assertFalse(self.env.company.prorrate_investment_account_id)

def test_no_prorate_in_invoice(self):
self.env.company.write(
{"with_vat_prorate": False}
Expand All @@ -87,6 +95,18 @@ def test_prorate_different_accounts_in_invoice(self):
)
self.assertEqual(6, len(invoice.line_ids))
self.assertEqual(3, len(invoice.line_ids.filtered(lambda r: r.tax_line_id)))
self.assertTrue(
invoice.line_ids.filtered(
lambda r: r.tax_line_id
and r.account_id == self.product_a.property_account_expense_id
)
)
self.assertTrue(
invoice.line_ids.filtered(
lambda r: r.tax_line_id
and r.account_id == self.product_b.property_account_expense_id
)
)
# Deal with analytics
invoice.line_ids[0].analytic_distribution = {self.analytic_account.id: 100}
self.assertEqual(6, len(invoice.line_ids))
Expand Down Expand Up @@ -124,6 +144,20 @@ def test_prorate_same_accounts_in_invoice(self):
# One of the tax lines should have expense account and the other the tax account
self.assertNotEqual(tax_lines[0].account_id, tax_lines[1].account_id)

def test_prorate_asset_in_invoice(self):
self.product_b.property_account_expense_id = self.company_data[
"default_account_assets"
]
invoice = self.init_invoice("in_invoice", products=[self.product_b])
self.assertEqual(4, len(invoice.line_ids))
self.assertEqual(2, len(invoice.line_ids.filtered(lambda r: r.tax_line_id)))
self.assertFalse(
invoice.line_ids.filtered(
lambda r: r.tax_line_id
and r.account_id == self.product_b.property_account_expense_id
)
)

def test_no_prorate_in_refund(self):
self.env.company.write(
{"with_vat_prorate": False}
Expand Down
8 changes: 8 additions & 0 deletions l10n_es_vat_prorate/views/res_company_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
</notebook>
<field name="company_registry" position="after">
<field name="with_vat_prorate" />
<field
name="prorrate_asset_account_id"
invisible="not with_vat_prorate"
/>
<field
name="prorrate_investment_account_id"
invisible="not with_vat_prorate"
/>
</field>
</field>
</record>
Expand Down
Loading