From b26c207c733f6cf52566dd5c38b7661f7979c69b Mon Sep 17 00:00:00 2001 From: David Beal Date: Fri, 14 Jun 2024 12:57:51 +0200 Subject: [PATCH] UPD acc_prd_fiscal_classif: finally right domain --- .../models/product_template.py | 9 ++-- .../tests/test_module.py | 41 +++++++++++++++---- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index 7187d6c4a..3ec11eb49 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -88,14 +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 - purchase_tax_ids = vals.get("supplier_taxes_id", []) - sale_tax_ids = vals.get("taxes_id", []) + purchase_tax_ids = vals.get("supplier_taxes_id", False) + # it doesn't work properly with [] instead of False, + sale_tax_ids = vals.get("taxes_id", 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), + ("sale_tax_ids", "=", sale_tax_ids), + ("purchase_tax_ids", "=", 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 = {