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

[16.0][ADD] account_multi_foreign_vat #387

Closed
Closed
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
Empty file.
1 change: 1 addition & 0 deletions account_multi_foreign_vat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
16 changes: 16 additions & 0 deletions account_multi_foreign_vat/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2023 - ForgeFlow S.L.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Account fiscal position - Multi foreign VAT",
"summary": "Allow having multiple foreign vat with same location",
"version": "16.0.1.0.0",
"author": "ForgeFlow S.L.,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-fiscal-rule",
"license": "AGPL-3",
"depends": ["account", "base_vat", "partner_identification"],
"data": [
"views/account_fiscal_position.xml",
"views/account_move.xml",
],
}
2 changes: 2 additions & 0 deletions account_multi_foreign_vat/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import account_fiscal_position
from . import account_move
54 changes: 54 additions & 0 deletions account_multi_foreign_vat/models/account_fiscal_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging

from odoo import _, api, models
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)


class AccountFiscalPosition(models.Model):
_inherit = "account.fiscal.position"

@api.constrains("country_id", "foreign_vat")
def _validate_foreign_vat(self):
for record in self:
try:
super()._validate_foreign_vat()
except Exception as e:
checked_country_code = self.env["res.partner"]._run_vat_test(
record.foreign_vat, record.country_id
)

if (
checked_country_code
and record.country_id
and checked_country_code != record.country_id.code.lower()
):
raise ValidationError(

Check warning on line 27 in account_multi_foreign_vat/models/account_fiscal_position.py

View check run for this annotation

Codecov / codecov/patch

account_multi_foreign_vat/models/account_fiscal_position.py#L27

Added line #L27 was not covered by tests
_(
"The country detected for this foreign VAT "
"number does not match the one set on this fiscal position."
)
) from e
if not checked_country_code and not record.country_id:
raise ValidationError(

Check warning on line 34 in account_multi_foreign_vat/models/account_fiscal_position.py

View check run for this annotation

Codecov / codecov/patch

account_multi_foreign_vat/models/account_fiscal_position.py#L34

Added line #L34 was not covered by tests
_("The foreign VAT number is not correct.")
) from e
Comment on lines +33 to +36

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

if not checked_country_code:
fp_label = _("fiscal position [%s]", record.name)
error_message = self.env["res.partner"]._build_vat_error_message(
record.country_id.code.lower(), record.foreign_vat, fp_label
)
raise ValidationError(error_message) from e
return True

@api.constrains("country_id", "state_ids", "foreign_vat")
def _validate_foreign_vat_country(self):
for _record in self:
try:
super()._validate_foreign_vat_country()
except ValidationError:
_logger.info("Ignored foreign vat country constrains")
except Exception as e:
raise e

Check warning on line 53 in account_multi_foreign_vat/models/account_fiscal_position.py

View check run for this annotation

Codecov / codecov/patch

account_multi_foreign_vat/models/account_fiscal_position.py#L52-L53

Added lines #L52 - L53 were not covered by tests
return True
28 changes: 28 additions & 0 deletions account_multi_foreign_vat/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models


class AccountMove(models.Model):
_inherit = "account.move"

@api.constrains("line_ids", "fiscal_position_id", "company_id")
def _validate_taxes_country(self):
filtered_self = self.browse()
for record in self:
amls = record.line_ids
impacted_countries = amls.tax_ids.country_id | amls.tax_line_id.country_id
if record.fiscal_position_id.country_group_id and all(
ic in record.fiscal_position_id.country_group_id.country_ids
for ic in impacted_countries
):
continue

Check warning on line 20 in account_multi_foreign_vat/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_multi_foreign_vat/models/account_move.py#L20

Added line #L20 was not covered by tests
if (
not record.fiscal_position_id.country_group_id
and not record.fiscal_position_id.country_id
):
continue
filtered_self |= record

return super(AccountMove, filtered_self)._validate_taxes_country()
2 changes: 2 additions & 0 deletions account_multi_foreign_vat/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* ForgeFlow, S.L. (https://www.forgeflow.com)
* Arnau Cruz <arnau.cruz@forgeflow.com>
1 change: 1 addition & 0 deletions account_multi_foreign_vat/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* This module allows the user to have multiple fiscal positions for the same country with identical foreign VAT. With this feature, users can efficiently manage distinct fiscal positions within a country, even when they share the same foreign VAT.
1 change: 1 addition & 0 deletions account_multi_foreign_vat/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* To use this module, users simply need to create two fiscal positions with the same country and foreign VAT.
1 change: 1 addition & 0 deletions account_multi_foreign_vat/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_account_multi_foreign_vat
47 changes: 47 additions & 0 deletions account_multi_foreign_vat/tests/test_account_multi_foreign_vat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from odoo.exceptions import ValidationError
from odoo.tests.common import TransactionCase


class TestAccountMultiForeignVat(TransactionCase):
def setUp(self):
super().setUp()
self.country_dk = self.env["res.country"].search([("code", "=", "DK")])
self.valid_vat_dk = "DK12345674"
self.invalid_vat_dk = "INVALID VAT"
self.fiscal_position_1 = self.env["account.fiscal.position"].create(
{
"name": "Denmark Fiscal Position 1",
"country_id": self.country_dk.id,
"foreign_vat": self.valid_vat_dk,
}
)
self.fiscal_position_2 = self.env["account.fiscal.position"].create(
{
"name": "Denmark Fiscal Position 2",
"country_id": self.country_dk.id,
"foreign_vat": self.valid_vat_dk,
}
)

def test_account_multi_foreign_vat(self):
# Check that 2 fiscal position are created successfully
self.assertTrue(self.fiscal_position_1)
self.assertTrue(self.fiscal_position_2)

# Check that both fiscal positions have the same foreign_vat and country_id
self.assertEqual(
self.fiscal_position_2.foreign_vat, self.fiscal_position_1.foreign_vat
)
self.assertEqual(
self.fiscal_position_2.country_id, self.fiscal_position_1.country_id
)

# Check that you cannot enter an invalid vat
with self.assertRaises(ValidationError):
self.env["account.fiscal.position"].create(
{
"name": "Denmark Fiscal Position 3",
"country_id": self.country_dk.id,
"foreign_vat": self.invalid_vat_dk,
}
)
20 changes: 20 additions & 0 deletions account_multi_foreign_vat/views/account_fiscal_position.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="account_fiscal_position_form_view" model="ir.ui.view">
<field
name="name"
>account.fiscal.position.form (in account_multi_foreign_vat)</field>
<field name="model">account.fiscal.position</field>
<field name="inherit_id" ref="account.view_account_position_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='country_id']" position="attributes">
<attribute name="attrs">{}</attribute>
<attribute
name="options"
>{'no_open': True, 'no_create': True}</attribute>
</xpath>
</field>
</record>

</odoo>
20 changes: 20 additions & 0 deletions account_multi_foreign_vat/views/account_move.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_move_form" model="ir.ui.view">
<field name="name">account.move.form (in account_multi_foreign_vat)</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form" />
<field name="arch" type="xml">
<xpath
expr="//field[@name='invoice_line_ids']/tree/field[@name='tax_ids']"
position="attributes"
>
<attribute
name="domain"
>[('type_tax_use', '=?', parent.invoice_filter_type_domain), ('company_id', '=', parent.company_id)]</attribute>
</xpath>
</field>
</record>

</odoo>
6 changes: 6 additions & 0 deletions setup/account_multi_foreign_vat/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
Loading