mirror of
https://github.com/OCA/account-financial-tools.git
synced 2025-02-02 12:47:26 +02:00
[IMP] product_category_tax: copy taxes when creating products programatically
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user