diff --git a/product_category_tax/models/product_product.py b/product_category_tax/models/product_product.py index 1b7f70a33..aca1fea36 100644 --- a/product_category_tax/models/product_product.py +++ b/product_category_tax/models/product_product.py @@ -14,3 +14,15 @@ class ProductProduct(models.Model): def set_tax_from_category(self): return self.product_tmpl_id.set_tax_from_category() + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if vals.get("categ_id"): + if "taxes_id" not in vals: + categ = self.env["product.category"].browse(vals["categ_id"]) + vals["taxes_id"] = [(6, 0, categ.taxes_id.ids)] + if "supplier_taxes_id" not in vals: + categ = self.env["product.category"].browse(vals["categ_id"]) + vals["supplier_taxes_id"] = [(6, 0, categ.supplier_taxes_id.ids)] + return super().create(vals_list) diff --git a/product_category_tax/models/product_template.py b/product_category_tax/models/product_template.py index 2a7c4b836..083ca1c09 100644 --- a/product_category_tax/models/product_template.py +++ b/product_category_tax/models/product_template.py @@ -28,3 +28,15 @@ class ProductTemplate(models.Model): } ) return True + + @api.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if vals.get("categ_id"): + if "taxes_id" not in vals: + categ = self.env["product.category"].browse(vals["categ_id"]) + vals["taxes_id"] = [(6, 0, categ.taxes_id.ids)] + if "supplier_taxes_id" not in vals: + categ = self.env["product.category"].browse(vals["categ_id"]) + vals["supplier_taxes_id"] = [(6, 0, categ.supplier_taxes_id.ids)] + return super().create(vals_list) diff --git a/product_category_tax/tests/test_product_category_tax.py b/product_category_tax/tests/test_product_category_tax.py index fcc71721b..70ed6b786 100644 --- a/product_category_tax/tests/test_product_category_tax.py +++ b/product_category_tax/tests/test_product_category_tax.py @@ -108,3 +108,57 @@ class ProductCategoryTax(common.SavepointCase): test_categ.update_product_taxes() self.assertEquals(self.product_test3.supplier_taxes_id, self.tax_purchase) self.assertNotEquals(self.product_test4.supplier_taxes_id, self.tax_purchase) + + def test_04_copy_taxes_during_create_product_product(self): + """Default taxes taken from the category during product create""" + category = self.env["product.category"].create( + { + "name": "Super Category", + "taxes_id": [(6, 0, self.tax_sale.ids)], + "supplier_taxes_id": [(6, 0, self.tax_purchase.ids)], + } + ) + # Case 1: Creating product.product with category + product = self.env["product.product"].create( + {"name": "Test Product", "categ_id": category.id} + ) + self.assertEqual(product.taxes_id, category.taxes_id) + self.assertEqual(product.supplier_taxes_id, category.supplier_taxes_id) + # Case 2: Creating product.product with category and values + product = self.env["product.product"].create( + { + "name": "Test Product", + "categ_id": category.id, + "taxes_id": False, + "supplier_taxes_id": [(6, 0, self.tax_purchase2.ids)], + } + ) + self.assertFalse(product.taxes_id, False) + self.assertEqual(product.supplier_taxes_id, self.tax_purchase2) + + def test_04_copy_taxes_during_create_product_template(self): + """Default taxes taken from the category during product create""" + category = self.env["product.category"].create( + { + "name": "Super Category", + "taxes_id": [(6, 0, self.tax_sale.ids)], + "supplier_taxes_id": [(6, 0, self.tax_purchase.ids)], + } + ) + # Case 1: Creating product.product with category + product = self.env["product.template"].create( + {"name": "Test Product", "categ_id": category.id} + ) + self.assertEqual(product.taxes_id, category.taxes_id) + self.assertEqual(product.supplier_taxes_id, category.supplier_taxes_id) + # Case 2: Creating product.product with category and values + product = self.env["product.template"].create( + { + "name": "Test Product", + "categ_id": category.id, + "taxes_id": False, + "supplier_taxes_id": [(6, 0, self.tax_purchase2.ids)], + } + ) + self.assertFalse(product.taxes_id, False) + self.assertEqual(product.supplier_taxes_id, self.tax_purchase2)