From fccd9c542df049a26e68c8dfdf0d7bc657da8552 Mon Sep 17 00:00:00 2001 From: David Beal Date: Wed, 12 Jun 2024 15:33:55 +0200 Subject: [PATCH] fixup --- .../models/product_template.py | 59 ++++++++++--------- .../tests/test_module.py | 36 ++++++++--- 2 files changed, 59 insertions(+), 36 deletions(-) diff --git a/account_product_fiscal_classification/models/product_template.py b/account_product_fiscal_classification/models/product_template.py index 4a3291cda..af4525650 100644 --- a/account_product_fiscal_classification/models/product_template.py +++ b/account_product_fiscal_classification/models/product_template.py @@ -8,8 +8,7 @@ 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__) @@ -79,13 +78,6 @@ def _update_vals_fiscal_classification(self, vals): ) elif vals.get("supplier_taxes_id") or vals.get("taxes_id"): self._find_or_create_classification(vals) - raise ValidationError( - _( - "You can not create or write products with" - " 'Customer Taxes' or 'Supplier Taxes'\n." - "Please, use instead the 'Fiscal Classification' field." - ) - ) return vals @api.constrains("categ_id", "fiscal_classification_id") @@ -97,33 +89,46 @@ def _find_or_create_classification(self, vals): depending of the taxes, or create a new one, if no one are found.""" # search for matching classication domain = [] - purchase_tax_ids = vals["supplier_taxes_id"] - sale_tax_ids = vals["taxes_id"] - del vals["taxes_id"] - del vals["supplier_taxes_id"] - if vals.get("taxes_id"): - domain.append(("sale_tax_ids", "=", sale_tax_ids)) - if vals.get("supplier_taxes_id"): - domain.append(("purchase_tax_ids", "=", purchase_tax_ids)) + 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] + taxe_ids = [] + if sale_tax_ids: + if isinstance(sale_tax_ids, int): + sale_tax_ids = [sale_tax_ids] + domain.append(("sale_tax_ids", "in", sale_tax_ids)) + taxe_ids.extend(sale_tax_ids) + if purchase_tax_ids: + if isinstance(purchase_tax_ids, int): + purchase_tax_ids = [purchase_tax_ids] + domain.append(("purchase_tax_ids", "in", purchase_tax_ids)) + taxe_ids.extend(purchase_tax_ids) classification = self.env["account.product.fiscal.classification"].search( domain ) - if classification: - vals["fiscal_classification_id"] = classification[0].id - else: - # Create a dedicate classification for this taxes combination - taxe_ids = purchase_tax_ids.ids - taxe_ids.extend(sale_tax_ids.ids) + if not classification: + # Create a dedicate classification for these taxes combination classif_vals = { "name": " ".join( [x.name for x in self.env["account.tax"].browse(taxe_ids)] ), - "purchase_taxes_ids": [(6, 0, [x.id for x in purchase_tax_ids])], - "sale_tax_ids": [(6, 0, [x.id for x in sale_tax_ids])], - "company_id": vals.get("company_id", self.env.company.id), + "company_id": vals.get( + "company_id", self.company_id and self.company_id.id or False + ), } - self.env["account.product.fiscal.classification"].create(classif_vals) + if purchase_tax_ids: + classif_vals["purchase_tax_ids"] = [ + (6, 0, [x for x in purchase_tax_ids]) + ] + if sale_tax_ids: + classif_vals["sale_tax_ids"] = [(6, 0, [x for x in 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[0].id diff --git a/account_product_fiscal_classification/tests/test_module.py b/account_product_fiscal_classification/tests/test_module.py index 4e70500ec..27375fb40 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,18 +166,37 @@ def test_30_rules(self): self.fiscal_classification_B_company_1, ) - def test_no_classification(self): + 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[0].id, + "supplier_taxes_id": classif.purchase_tax_ids[0].id, + } + 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": self.env["account.tax"] - .search([("type_tax_use", "=", "sale")])[0] - .id, - "supplier_taxes_id": self.env["account.tax"] - .search([("type_tax_use", "=", "purchase")])[0] - .id, + "taxes_id": my_tax.id, } - self.ProductTemplate.with_user(self.env.user).create(vals) + 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 _create_product(self, user, category, classification): vals = {