From a7c745fd828568775f837db0b669850c3e6a0f74 Mon Sep 17 00:00:00 2001 From: David Beal Date: Wed, 12 Jun 2024 15:55:48 +0200 Subject: [PATCH 1/7] FIX acc_fisc_classif: find or create a classif if none --- .../models/product_template.py | 56 +++++++++++++++---- .../tests/test_module.py | 42 +++++++++++++- 2 files changed, 86 insertions(+), 12 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index 350648e3f..63ddd54f9 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -4,11 +4,13 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). import json +import logging from lxml import etree -from odoo import _, api, fields, models -from odoo.exceptions import ValidationError +from odoo import api, fields, models + +_logger = logging.getLogger(__name__) class ProductTemplate(models.Model): @@ -62,7 +64,7 @@ def get_view(self, view_id=None, view_type="form", **options): # Custom Section def _update_vals_fiscal_classification(self, vals): FiscalClassification = self.env["account.product.fiscal.classification"] - if vals.get("fiscal_classification_id", False): + if vals.get("fiscal_classification_id"): # We use sudo to have access to all the taxes, even taxes that belong # to companies that the user can't access in the current context classification = FiscalClassification.sudo().browse( @@ -74,16 +76,48 @@ def _update_vals_fiscal_classification(self, vals): "taxes_id": [(6, 0, classification.sale_tax_ids.ids)], } ) - elif vals.get("supplier_taxes_id") or vals.get("taxes_id"): - raise ValidationError( - _( - "You can not create or write products with" - " 'Customer Taxes' or 'Supplier Taxes'\n." - "Please, use instead the 'Fiscal Classification' field." - ) - ) + elif ( + vals.get("supplier_taxes_id") + or vals.get("taxes_id") + or "fiscal_classification_id" not in vals + ): + self._find_or_create_classification(vals) return vals @api.constrains("categ_id", "fiscal_classification_id") def _check_rules_fiscal_classification(self): self.env["account.product.fiscal.rule"].check_product_templates_integrity(self) + + def _find_or_create_classification(self, vals): + """Find the correct Fiscal classification, + depending of the taxes, or create a new one, if no one are found.""" + # search for matching classication + domain = [] + purchase_tax_ids = vals.get("supplier_taxes_id", []) + sale_tax_ids = vals.get("taxes_id", []) + for elm in ("supplier_taxes_id", "taxes_id"): + if elm in vals: + del vals[elm] + if sale_tax_ids: + domain.append(("sale_tax_ids", "in", sale_tax_ids)) + if purchase_tax_ids: + domain.append(("purchase_tax_ids", "in", purchase_tax_ids)) + classification = self.env["account.product.fiscal.classification"].search( + domain, limit=1 + ) + if not classification: + # Create a dedicate classification for these taxes combination + classif_vals = self.env[ + "account.product.fiscal.classification" + ]._prepare_vals_from_taxes( + self.env["account.tax"].browse(purchase_tax_ids), + self.env["account.tax"].browse(sale_tax_ids), + ) + classification = self.env["account.product.fiscal.classification"].create( + classif_vals + ) + _logger.info( + f"Creating new Fiscal Classification '{classif_vals['name']}'" + f" for {self.display_name}" + ) + vals["fiscal_classification_id"] = classification.id diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index d4410a7a9..ba317b884 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -1,7 +1,6 @@ # Copyright (C) 2014-Today GRAP (http://www.grap.coop) # @author: Sylvain LE GAL (https://twitter.com/legalsylvain) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - from odoo.exceptions import ValidationError from odoo.tests.common import TransactionCase @@ -167,6 +166,47 @@ def test_30_rules(self): self.fiscal_classification_B_company_1, ) + def test_no_classification_and_find_one(self): + classif = self.env.ref( + "account_product_fiscal_classification.fiscal_classification_A_company_1" + ) + vals = { + "name": "Test Product", + "company_id": self.env.company.id, + "categ_id": self.category_all.id, + "taxes_id": classif.sale_tax_ids.ids, + "supplier_taxes_id": classif.purchase_tax_ids.ids, + } + product = self.ProductTemplate.with_user(self.env.user).create(vals) + self.assertEqual(product.fiscal_classification_id, classif) + + def test_no_classification_and_create_one(self): + classif_co = self.env["account.product.fiscal.classification"].search_count([]) + my_tax = self.env["account.tax"].create( + {"name": "my_tax", "type_tax_use": "sale", "amount": 9.99} + ) + vals = { + "name": "Test Product", + "company_id": self.env.company.id, + "categ_id": self.category_all.id, + "taxes_id": my_tax.id, + } + product = self.ProductTemplate.with_user(self.env.user).create(vals) + self.assertNotEquals(product.fiscal_classification_id, False) + classif_co_after = self.env[ + "account.product.fiscal.classification" + ].search_count([]) + self.assertEqual(classif_co_after, classif_co + 1) + + def test_no_tax_nor_classification_and_create_one(self): + vals = { + "name": "Test Product", + "company_id": self.env.company.id, + "categ_id": self.category_all.id, + } + product = self.ProductTemplate.with_user(self.env.user).create(vals) + self.assertNotEquals(product.fiscal_classification_id, False) + def _create_product(self, user, category, classification): vals = { "name": "Test Product", From cff21755a6ff05518024f2a43e81791c6efaed8c Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Wed, 12 Jun 2024 23:09:21 +0200 Subject: [PATCH 2/7] 16.0 fix create mode (#4) * [FIX] account_product_fiscal_classification : In a context of product creation, if classification is not provided (in import context for exemple), _find_or_create_classification should be allways called to ensure all templates have a classification * [FIX] account_product_fiscal_classification : if a code make a write vals={supplier_taxes_id: []} this should raise the call of _find_or_create_classification * [FIX] account_product_fiscal_classification/tests : assertNotEquals is deprecated * [FIX] account_product_fiscal_classification : explicit supplier and customer taxes in test, to avoid bug if default taxes are defined --- .../models/product_template.py | 10 +++------- .../tests/test_module.py | 9 ++++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index 63ddd54f9..faeecdf4c 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -34,7 +34,7 @@ class ProductTemplate(models.Model): @api.model_create_multi def create(self, vals_list): for vals in vals_list: - self._update_vals_fiscal_classification(vals) + self._update_vals_fiscal_classification(vals, create_mode=True) return super().create(vals_list) def write(self, vals): @@ -62,7 +62,7 @@ def get_view(self, view_id=None, view_type="form", **options): return result # Custom Section - def _update_vals_fiscal_classification(self, vals): + def _update_vals_fiscal_classification(self, vals, create_mode=False): FiscalClassification = self.env["account.product.fiscal.classification"] if vals.get("fiscal_classification_id"): # We use sudo to have access to all the taxes, even taxes that belong @@ -76,11 +76,7 @@ def _update_vals_fiscal_classification(self, vals): "taxes_id": [(6, 0, classification.sale_tax_ids.ids)], } ) - elif ( - vals.get("supplier_taxes_id") - or vals.get("taxes_id") - or "fiscal_classification_id" not in vals - ): + elif create_mode or {"supplier_taxes_id", "taxes_id"} & vals.keys(): self._find_or_create_classification(vals) return vals diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index ba317b884..14522cb20 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -189,10 +189,11 @@ def test_no_classification_and_create_one(self): "name": "Test Product", "company_id": self.env.company.id, "categ_id": self.category_all.id, - "taxes_id": my_tax.id, + "taxes_id": my_tax.ids, + "supplier_taxes_id": [], } product = self.ProductTemplate.with_user(self.env.user).create(vals) - self.assertNotEquals(product.fiscal_classification_id, False) + self.assertNotEqual(product.fiscal_classification_id, False) classif_co_after = self.env[ "account.product.fiscal.classification" ].search_count([]) @@ -203,9 +204,11 @@ def test_no_tax_nor_classification_and_create_one(self): "name": "Test Product", "company_id": self.env.company.id, "categ_id": self.category_all.id, + "taxes_id": [], + "supplier_taxes_id": [], } product = self.ProductTemplate.with_user(self.env.user).create(vals) - self.assertNotEquals(product.fiscal_classification_id, False) + self.assertNotEqual(product.fiscal_classification_id, False) def _create_product(self, user, category, classification): vals = { From a8db5d0bc827c0128e24891ed6800dd54f210594 Mon Sep 17 00:00:00 2001 From: David Beal Date: Thu, 13 Jun 2024 11:27:55 +0200 Subject: [PATCH 3/7] FIX acc_fisc_classif: test_no_tax_nor_classification_and_create_one --- .../models/product_template.py | 7 +++---- account_product_fiscal_classification/tests/test_module.py | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index faeecdf4c..b0fd3a91d 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -88,16 +88,15 @@ def _find_or_create_classification(self, vals): """Find the correct Fiscal classification, depending of the taxes, or create a new one, if no one are found.""" # search for matching classication - domain = [] purchase_tax_ids = vals.get("supplier_taxes_id", []) sale_tax_ids = vals.get("taxes_id", []) for elm in ("supplier_taxes_id", "taxes_id"): if elm in vals: del vals[elm] + domain = [("sale_tax_ids", "=", [])] if sale_tax_ids: - domain.append(("sale_tax_ids", "in", sale_tax_ids)) - if purchase_tax_ids: - domain.append(("purchase_tax_ids", "in", purchase_tax_ids)) + domain = [("sale_tax_ids", "in", sale_tax_ids)] + domain.append(("purchase_tax_ids", "in", purchase_tax_ids or [])) classification = self.env["account.product.fiscal.classification"].search( domain, limit=1 ) diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index 14522cb20..970475c2f 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -208,7 +208,9 @@ def test_no_tax_nor_classification_and_create_one(self): "supplier_taxes_id": [], } product = self.ProductTemplate.with_user(self.env.user).create(vals) - self.assertNotEqual(product.fiscal_classification_id, False) + classif = product.fiscal_classification_id + self.assertEqual(classif.purchase_tax_ids, self.env["account.tax"]) + self.assertEqual(classif.sale_tax_ids, self.env["account.tax"]) def _create_product(self, user, category, classification): vals = { From f8d0755e139edd173ef0111f6fb4d7e640a7e95d Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Thu, 13 Jun 2024 22:31:17 +0200 Subject: [PATCH 4/7] IMP acc_fisc_classif: domain definition --- .../models/product_template.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index b0fd3a91d..7187d6c4a 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -93,10 +93,10 @@ def _find_or_create_classification(self, vals): for elm in ("supplier_taxes_id", "taxes_id"): if elm in vals: del vals[elm] - domain = [("sale_tax_ids", "=", [])] - if sale_tax_ids: - domain = [("sale_tax_ids", "in", sale_tax_ids)] - domain.append(("purchase_tax_ids", "in", purchase_tax_ids or [])) + domain = [ + ("sale_tax_ids", "in", sale_tax_ids), + ("purchase_tax_ids", "in", purchase_tax_ids), + ] classification = self.env["account.product.fiscal.classification"].search( domain, limit=1 ) From 3adc341b52339cbcbcc55070d1f3e3a9049fe487 Mon Sep 17 00:00:00 2001 From: David Beal Date: Fri, 14 Jun 2024 17:44:15 +0200 Subject: [PATCH 5/7] UPD acc_prd_fiscal_classif: finally right domain --- .../models/product_template.py | 15 ++++--- .../tests/test_module.py | 41 +++++++++++++++---- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index 7187d6c4a..b9c748dcf 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -88,15 +88,18 @@ def _find_or_create_classification(self, vals): """Find the correct Fiscal classification, depending of the taxes, or create a new one, if no one are found.""" # search for matching classication - purchase_tax_ids = vals.get("supplier_taxes_id", []) - sale_tax_ids = vals.get("taxes_id", []) + purchase_tax_ids = vals.get("supplier_taxes_id") + sale_tax_ids = vals.get("taxes_id") + domain = [("sale_tax_ids", "=", False)] + if sale_tax_ids: + domain = [("sale_tax_ids", "=", sale_tax_ids)] + if purchase_tax_ids: + domain.append(("purchase_tax_ids", "=", purchase_tax_ids)) + else: + domain.append(("purchase_tax_ids", "=", False)) for elm in ("supplier_taxes_id", "taxes_id"): if elm in vals: del vals[elm] - domain = [ - ("sale_tax_ids", "in", sale_tax_ids), - ("purchase_tax_ids", "in", purchase_tax_ids), - ] classification = self.env["account.product.fiscal.classification"].search( domain, limit=1 ) diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index 970475c2f..ed6137335 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -167,6 +167,9 @@ def test_30_rules(self): ) def test_no_classification_and_find_one(self): + classif_count = self.env["account.product.fiscal.classification"].search_count( + [] + ) classif = self.env.ref( "account_product_fiscal_classification.fiscal_classification_A_company_1" ) @@ -178,26 +181,46 @@ def test_no_classification_and_find_one(self): "supplier_taxes_id": classif.purchase_tax_ids.ids, } product = self.ProductTemplate.with_user(self.env.user).create(vals) + # no other classification is created + self.assertEqual( + self.env["account.product.fiscal.classification"].search_count([]), + classif_count, + ) + # product is linked to created classification self.assertEqual(product.fiscal_classification_id, classif) def test_no_classification_and_create_one(self): - classif_co = self.env["account.product.fiscal.classification"].search_count([]) my_tax = self.env["account.tax"].create( {"name": "my_tax", "type_tax_use": "sale", "amount": 9.99} ) - vals = { - "name": "Test Product", - "company_id": self.env.company.id, - "categ_id": self.category_all.id, - "taxes_id": my_tax.ids, - "supplier_taxes_id": [], - } - product = self.ProductTemplate.with_user(self.env.user).create(vals) + classif_co = self.env["account.product.fiscal.classification"].search_count([]) + + def create_product(): + vals = { + "name": "Test Product", + "company_id": self.env.company.id, + "categ_id": self.category_all.id, + "taxes_id": my_tax.ids, + "supplier_taxes_id": [], + } + return self.ProductTemplate.with_user(self.env.user).create(vals) + + product = create_product() self.assertNotEqual(product.fiscal_classification_id, False) classif_co_after = self.env[ "account.product.fiscal.classification" ].search_count([]) self.assertEqual(classif_co_after, classif_co + 1) + # check other products with same tax combination + # doesn't create other classification + product2 = create_product() + classif_co_final = self.env[ + "account.product.fiscal.classification" + ].search_count([]) + self.assertEqual( + product.fiscal_classification_id, product2.fiscal_classification_id + ) + self.assertEqual(classif_co_final, classif_co_after) def test_no_tax_nor_classification_and_create_one(self): vals = { From 820a0148dc59803fbb8d4b333f11ab100ae3d24b Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Mon, 17 Jun 2024 12:16:27 +0200 Subject: [PATCH 6/7] [REF] account_product_fiscal_classification : less code (#5) --- .../tests/test_module.py | 144 +++++++----------- 1 file changed, 54 insertions(+), 90 deletions(-) diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index ed6137335..8594467c1 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -19,13 +19,13 @@ def setUp(self): self.company_2 = self.env.ref("account_product_fiscal_classification.company_2") self.user_demo = self.env.ref("base.user_demo") - self.fiscal_classification_A_company_1 = self.env.ref( + self.classification_A_company_1 = self.env.ref( "account_product_fiscal_classification.fiscal_classification_A_company_1" ) - self.fiscal_classification_B_company_1 = self.env.ref( + self.classification_B_company_1 = self.env.ref( "account_product_fiscal_classification.fiscal_classification_B_company_1" ) - self.fiscal_classification_D_global = self.env.ref( + self.classification_D_global = self.env.ref( "account_product_fiscal_classification.fiscal_classification_D_global" ) self.product_template_A_company_1 = self.env.ref( @@ -43,18 +43,23 @@ def setUp(self): self.chart_template = self.env.ref( "account_product_fiscal_classification.chart_template" ) - # self.sale_tax_2 = self.env.ref( - # "account_product_fiscal_classification.account_tax_sale_5_company_1" - # ) - self.category_all = self.env.ref("product.product_category_all") self.category_wine = self.env.ref( "account_product_fiscal_classification.category_wine" ) - # # Group to create product - # self.product_group = self.env.ref("account.group_account_manager") - # self.restricted_group = self.env.ref("base.group_system") + self.initial_classif_count = self.FiscalClassification.search_count([]) + + def _create_product(self, extra_vals, user=False): + if not user: + user = self.env.user + vals = { + "name": "Test Product", + "company_id": self.main_company.id, + "categ_id": self.category_all.id, + } + vals.update(extra_vals) + return self.ProductTemplate.with_user(user).create(vals) # Test Section def test_01_change_classification(self): @@ -62,39 +67,36 @@ def test_01_change_classification(self): products.""" wizard = self.WizardChange.create( { - "old_fiscal_classification_id": self.fiscal_classification_A_company_1.id, - "new_fiscal_classification_id": self.fiscal_classification_B_company_1.id, + "old_fiscal_classification_id": self.classification_A_company_1.id, + "new_fiscal_classification_id": self.classification_B_company_1.id, } ) wizard.button_change_fiscal_classification() self.assertEqual( self.product_template_A_company_1.fiscal_classification_id, - self.fiscal_classification_B_company_1, + self.classification_B_company_1, "Fiscal Classification change has failed for products via Wizard.", ) def test_02_create_product(self): """Test if creating a product with fiscal classification set correct taxes""" - vals = { - "name": "Product Product Name", - "company_id": self.main_company.id, - "fiscal_classification_id": self.fiscal_classification_D_global.id, - } - newTemplate = self.ProductTemplate.create(vals) + newTemplate = self._create_product( + {"fiscal_classification_id": self.classification_D_global.id} + ) # Test that all taxes are set (in sudo mode) self.assertEqual( set(newTemplate.sudo().taxes_id.ids), - set(self.fiscal_classification_D_global.sudo().sale_tax_ids.ids), + set(self.classification_D_global.sudo().sale_tax_ids.ids), ) self.assertEqual( set(newTemplate.sudo().supplier_taxes_id.ids), - set(self.fiscal_classification_D_global.sudo().purchase_tax_ids.ids), + set(self.classification_D_global.sudo().purchase_tax_ids.ids), ) def test_03_update_fiscal_classification(self): """Test if changing a Configuration of a Fiscal Classification changes the product.""" - self.fiscal_classification_A_company_1.write( + self.classification_A_company_1.write( {"sale_tax_ids": [(6, 0, [self.account_tax_sale_20_company_1.id])]} ) self.assertEqual( @@ -106,7 +108,7 @@ def test_03_update_fiscal_classification(self): def test_05_unlink_fiscal_classification(self): """Test if unlinking a Fiscal Classification with products fails.""" with self.assertRaises(ValidationError): - self.fiscal_classification_A_company_1.unlink() + self.classification_A_company_1.unlink() def test_10_chart_template(self): """Test if installing new CoA creates correct classification""" @@ -147,98 +149,60 @@ def test_30_rules(self): # Create a product without rules should success self._create_product( - self.env.user, self.category_all, self.fiscal_classification_B_company_1 + {"fiscal_classification_id": self.classification_B_company_1.id} ) self._create_product( - self.user_demo, self.category_all, self.fiscal_classification_B_company_1 + {"fiscal_classification_id": self.classification_B_company_1.id}, + user=self.user_demo, ) # create a product not respecting rules should succeed with accountant perfil self._create_product( - self.env.user, self.category_wine, self.fiscal_classification_B_company_1 + { + "categ_id": self.category_wine.id, + "fiscal_classification_id": self.classification_B_company_1.id, + } ) # create a product not respecting rules should fail without accountant perfil with self.assertRaises(ValidationError): self._create_product( - self.user_demo, - self.category_wine, - self.fiscal_classification_B_company_1, + { + "categ_id": self.category_wine.id, + "fiscal_classification_id": self.classification_B_company_1.id, + }, + user=self.user_demo, ) def test_no_classification_and_find_one(self): - classif_count = self.env["account.product.fiscal.classification"].search_count( - [] - ) - classif = self.env.ref( - "account_product_fiscal_classification.fiscal_classification_A_company_1" + product = self._create_product( + { + "taxes_id": self.classification_A_company_1.sale_tax_ids.ids, + "supplier_taxes_id": self.classification_A_company_1.purchase_tax_ids.ids, + } ) - vals = { - "name": "Test Product", - "company_id": self.env.company.id, - "categ_id": self.category_all.id, - "taxes_id": classif.sale_tax_ids.ids, - "supplier_taxes_id": classif.purchase_tax_ids.ids, - } - product = self.ProductTemplate.with_user(self.env.user).create(vals) # no other classification is created + classif_count_after = self.FiscalClassification.search_count([]) + self.assertEqual(classif_count_after, self.initial_classif_count) + # product is linked to created classification self.assertEqual( - self.env["account.product.fiscal.classification"].search_count([]), - classif_count, + product.fiscal_classification_id, self.classification_A_company_1 ) - # product is linked to created classification - self.assertEqual(product.fiscal_classification_id, classif) def test_no_classification_and_create_one(self): my_tax = self.env["account.tax"].create( {"name": "my_tax", "type_tax_use": "sale", "amount": 9.99} ) - classif_co = self.env["account.product.fiscal.classification"].search_count([]) - def create_product(): - vals = { - "name": "Test Product", - "company_id": self.env.company.id, - "categ_id": self.category_all.id, - "taxes_id": my_tax.ids, - "supplier_taxes_id": [], - } - return self.ProductTemplate.with_user(self.env.user).create(vals) - - product = create_product() - self.assertNotEqual(product.fiscal_classification_id, False) - classif_co_after = self.env[ - "account.product.fiscal.classification" - ].search_count([]) - self.assertEqual(classif_co_after, classif_co + 1) - # check other products with same tax combination - # doesn't create other classification - product2 = create_product() - classif_co_final = self.env[ - "account.product.fiscal.classification" - ].search_count([]) - self.assertEqual( - product.fiscal_classification_id, product2.fiscal_classification_id + product = self._create_product( + {"taxes_id": my_tax.ids, "supplier_taxes_id": []} ) - self.assertEqual(classif_co_final, classif_co_after) + self.assertNotEqual(product.fiscal_classification_id, False) + classif_count_after = self.FiscalClassification.search_count([]) + self.assertEqual(classif_count_after, self.initial_classif_count + 1) def test_no_tax_nor_classification_and_create_one(self): - vals = { - "name": "Test Product", - "company_id": self.env.company.id, - "categ_id": self.category_all.id, - "taxes_id": [], - "supplier_taxes_id": [], - } - product = self.ProductTemplate.with_user(self.env.user).create(vals) + product = self._create_product({"taxes_id": [], "supplier_taxes_id": []}) classif = product.fiscal_classification_id - self.assertEqual(classif.purchase_tax_ids, self.env["account.tax"]) - self.assertEqual(classif.sale_tax_ids, self.env["account.tax"]) - - def _create_product(self, user, category, classification): - vals = { - "name": "Test Product", - "categ_id": category.id, - "fiscal_classification_id": classification.id, - } - self.ProductTemplate.with_user(user).create(vals) + self.assertEqual(classif.purchase_tax_ids.ids, []) + self.assertEqual(classif.sale_tax_ids.ids, []) From c2cc6ffe60b8eb80d59f72e2e65c6fe38142ae83 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Mon, 17 Jun 2024 15:03:01 +0200 Subject: [PATCH 7/7] [REF] Add test that are failing (#6) --- .../tests/test_module.py | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index 8594467c1..c39909ae9 100644 --- a/account_product_fiscal_classification/tests/test_module.py +++ b/account_product_fiscal_classification/tests/test_module.py @@ -34,6 +34,9 @@ def setUp(self): self.account_tax_purchase_20_company_1 = self.env.ref( "account_product_fiscal_classification.account_tax_purchase_20_company_1" ) + self.account_tax_sale_5_company_1 = self.env.ref( + "account_product_fiscal_classification.account_tax_sale_5_company_1" + ) self.account_tax_sale_20_company_1 = self.env.ref( "account_product_fiscal_classification.account_tax_sale_20_company_1" ) @@ -177,7 +180,7 @@ def test_30_rules(self): def test_no_classification_and_find_one(self): product = self._create_product( { - "taxes_id": self.classification_A_company_1.sale_tax_ids.ids, + "taxes_id": self.classification_B_company_1.sale_tax_ids.ids, "supplier_taxes_id": self.classification_A_company_1.purchase_tax_ids.ids, } ) @@ -189,13 +192,29 @@ def test_no_classification_and_find_one(self): product.fiscal_classification_id, self.classification_A_company_1 ) - def test_no_classification_and_create_one(self): - my_tax = self.env["account.tax"].create( - {"name": "my_tax", "type_tax_use": "sale", "amount": 9.99} + def test_no_classification_one_more_tax_and_create_one(self): + """Create a product with fiscal settings that looks like + classification_B_company_1 but with an additional supplier tax. + """ + product = self._create_product( + { + "taxes_id": self.classification_B_company_1.sale_tax_ids.ids, + "supplier_taxes_id": self.account_tax_purchase_20_company_1.ids, + } ) + self.assertNotEqual(product.fiscal_classification_id, False) + classif_count_after = self.FiscalClassification.search_count([]) + self.assertEqual(classif_count_after, self.initial_classif_count + 1) + def test_no_classification_one_less_tax_and_create_one(self): + """Create a product with fiscal settings that looks like + classification_B_company_1 but with one less tax + """ product = self._create_product( - {"taxes_id": my_tax.ids, "supplier_taxes_id": []} + { + "taxes_id": self.account_tax_sale_5_company_1.ids, + "supplier_taxes_id": [], + } ) self.assertNotEqual(product.fiscal_classification_id, False) classif_count_after = self.FiscalClassification.search_count([])