From 5b07b6fb1f77eb45682afb545298cadb2859e7d7 Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Wed, 8 Feb 2023 10:08:30 +0100 Subject: [PATCH] intrastat_base: intrastat field is now a selection field intrastat field on account.fiscal.position is now a selection field with 3 values : b2b/b2c/no Add views on account.fiscal.position.template --- intrastat_base/__manifest__.py | 1 + .../models/account_fiscal_position.py | 43 +++++++++++++++-- .../account_fiscal_position_template.py | 13 +++-- intrastat_base/models/account_move.py | 2 +- intrastat_base/tests/test_all.py | 18 +++++++ .../views/account_fiscal_position.xml | 11 ++++- .../account_fiscal_position_template.xml | 48 +++++++++++++++++++ 7 files changed, 127 insertions(+), 9 deletions(-) create mode 100644 intrastat_base/views/account_fiscal_position_template.xml diff --git a/intrastat_base/__manifest__.py b/intrastat_base/__manifest__.py index d574ac5..8bd97fb 100644 --- a/intrastat_base/__manifest__.py +++ b/intrastat_base/__manifest__.py @@ -20,6 +20,7 @@ "views/res_config_settings.xml", "views/intrastat.xml", "views/account_fiscal_position.xml", + "views/account_fiscal_position_template.xml", ], "demo": ["demo/intrastat_demo.xml"], "installable": True, diff --git a/intrastat_base/models/account_fiscal_position.py b/intrastat_base/models/account_fiscal_position.py index dfb9be3..f7d5ba8 100644 --- a/intrastat_base/models/account_fiscal_position.py +++ b/intrastat_base/models/account_fiscal_position.py @@ -2,13 +2,50 @@ # @author: # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class AccountFiscalPosition(models.Model): _inherit = "account.fiscal.position" - intrastat = fields.Boolean( - help="Set to True if the invoices with this fiscal position should " + intrastat = fields.Selection( + "_intrastat_selection", + help="When set to B2B or B2C, the invoices with this fiscal position will " "be taken into account for the generation of the intrastat reports.", ) + + @api.model + def _intrastat_selection(self): + return [ + ("b2b", _("B2B")), + ("b2c", _("B2C")), + ("no", _("No")), + ] + + @api.constrains("intrastat", "vat_required") + def _check_intrastat(self): + for position in self: + if position.intrastat == "b2b" and not position.vat_required: + raise ValidationError( + _( + "The fiscal position '%s' has intrastat set to B2B, " + "so the option 'VAT Required' must be enabled." + ) + % position.display_name + ) + elif position.intrastat == "b2c" and position.vat_required: + raise ValidationError( + _( + "The fiscal position '%s' has intrastat set to B2C, " + "so the option 'VAT Required' mustn't be enabled." + ) + % position.display_name + ) + + @api.onchange("intrastat", "vat_required") + def intrastat_change(self): + if self.intrastat == "b2b" and not self.vat_required: + self.vat_required = True + elif self.intrastat == "b2c" and self.vat_required: + self.vat_required = False diff --git a/intrastat_base/models/account_fiscal_position_template.py b/intrastat_base/models/account_fiscal_position_template.py index 8b6c51e..e4dd231 100644 --- a/intrastat_base/models/account_fiscal_position_template.py +++ b/intrastat_base/models/account_fiscal_position_template.py @@ -1,13 +1,18 @@ # Copyright 2020-2022 Akretion France (http://www.akretion.com/) # @author: # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models class AccountFiscalPositionTemplate(models.Model): _inherit = "account.fiscal.position.template" - intrastat = fields.Boolean( - help="Check this if you want to generate intrastat declarations with" - "the created fiscal position", + intrastat = fields.Selection( + "_intrastat_selection", + help="When set to B2B or B2C, the invoices with this fiscal position will " + "be taken into account for the generation of the intrastat reports.", ) + + @api.model + def _intrastat_selection(self): + return self.env["account.fiscal.position"]._intrastat_selection() diff --git a/intrastat_base/models/account_move.py b/intrastat_base/models/account_move.py index 83c13cb..8ef5269 100644 --- a/intrastat_base/models/account_move.py +++ b/intrastat_base/models/account_move.py @@ -8,7 +8,7 @@ from odoo import fields, models class AccountMove(models.Model): _inherit = "account.move" - intrastat_fiscal_position = fields.Boolean( + intrastat_fiscal_position = fields.Selection( related="fiscal_position_id.intrastat", store=True, string="Intrastat Fiscal Position", diff --git a/intrastat_base/tests/test_all.py b/intrastat_base/tests/test_all.py index 40b414f..70913a6 100644 --- a/intrastat_base/tests/test_all.py +++ b/intrastat_base/tests/test_all.py @@ -28,3 +28,21 @@ class TestIntrastatBase(IntrastatCommon): def test_accessory(self): with self.assertRaises(ValidationError): self.shipping_cost.type = "consu" + + def test_fiscal_position(self): + with self.assertRaises(ValidationError): + self.env["account.fiscal.position"].create( + { + "name": "TestB2B", + "vat_required": False, + "intrastat": "b2b", + } + ) + with self.assertRaises(ValidationError): + self.env["account.fiscal.position"].create( + { + "name": "TestB2C", + "vat_required": True, + "intrastat": "b2c", + } + ) diff --git a/intrastat_base/views/account_fiscal_position.xml b/intrastat_base/views/account_fiscal_position.xml index a52b5b4..7c7dbfb 100644 --- a/intrastat_base/views/account_fiscal_position.xml +++ b/intrastat_base/views/account_fiscal_position.xml @@ -32,11 +32,20 @@ + + + + + diff --git a/intrastat_base/views/account_fiscal_position_template.xml b/intrastat_base/views/account_fiscal_position_template.xml new file mode 100644 index 0000000..ca8111d --- /dev/null +++ b/intrastat_base/views/account_fiscal_position_template.xml @@ -0,0 +1,48 @@ + + + + + intrastat.account.fiscal.position.form + account.fiscal.position.template + + + + + + + + + intrastat.account.fiscal.position.tree + account.fiscal.position.template + + + + + + + + + account.fiscal.position.template + + + + + + + + + + +