Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] product_tax_multicompany_default: Extend ignored_company_ids context functionality #787

Draft
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions product_tax_multicompany_default/models/product.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2017 Carlos Dauden - Tecnativa <[email protected]>
# Copyright 2018 Vicent Cubells - Tecnativa <[email protected]>
# Copyright 2023 Eduardo de Miguel - Moduon <[email protected]>
# Copyright 2025 Sergio Bustamante - FactorLibre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from typing import List
Expand Down Expand Up @@ -101,6 +102,8 @@ def _delete_product_taxes(

def set_multicompany_taxes(self):
self.ensure_one()
ignored_company_ids = self.env.context.get("ignored_company_ids", [])
ignored_taxes_id = self._get_ignored_taxes_id(ignored_company_ids) or []
user_company = self.env.company
customer_tax = self.taxes_id
customer_tax_ids = customer_tax.ids
Expand All @@ -119,8 +122,8 @@ def set_multicompany_taxes(self):
)
# Clean taxes from other companies (cannot replace it with sudo)
self._delete_product_taxes(
excl_customer_tax_ids=customer_tax_ids,
excl_supplier_tax_ids=supplier_tax_ids,
excl_customer_tax_ids=customer_tax_ids + ignored_taxes_id,
excl_supplier_tax_ids=supplier_tax_ids + ignored_taxes_id,
)
# Use list() to copy list
match_customer_tax_ids = (
Expand All @@ -133,7 +136,8 @@ def set_multicompany_taxes(self):
if default_supplier_tax_ids != supplier_tax_ids
else None
)
for company in obj.env["res.company"].search([("id", "!=", user_company.id)]):
company_ids = ignored_company_ids + [user_company.id]
for company in obj.env["res.company"].search([("id", "not in", company_ids)]):
customer_tax_ids.extend(
obj.taxes_by_company(
"account_sale_tax_id", company, match_customer_tax_ids
Expand All @@ -146,8 +150,8 @@ def set_multicompany_taxes(self):
)
self.write(
{
"taxes_id": [(6, 0, customer_tax_ids)],
"supplier_taxes_id": [(6, 0, supplier_tax_ids)],
"taxes_id": [[4, tax] for tax in customer_tax_ids],
"supplier_taxes_id": [[4, tax] for tax in supplier_tax_ids],
}
)

Expand All @@ -158,6 +162,17 @@ def create(self, vals_list):
product.set_multicompany_taxes()
return new_products

def _get_ignored_taxes_id(self, ignored_company_ids):
ignored_taxes_ids = False
if ignored_company_ids:
ignored_taxes_ids = (
self.env["account.tax"]
.sudo()
.search([("company_id", "in", ignored_company_ids)])
.ids
)
return ignored_taxes_ids


class ProductProduct(models.Model):
_inherit = "product.product"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,49 @@
self.assertTrue(product.divergent_company_taxes)
product.set_multicompany_taxes()
self.assertFalse(product.divergent_company_taxes)

@users("user_12")
def test_set_multicompany_taxes_ignored_company_ids(self):
# If purchase module is installed
# add purchase manager group to user_12
# to access the supplier_taxes_id field in the product view
try:
self.env.ref(
"purchase.group_purchase_manager", raise_if_not_found=True
).sudo().users = [(4, self.user_12.id)]
except ValueError as e:
logging.info(e) # Skipping configuration of purchase module

Check warning on line 279 in product_tax_multicompany_default/tests/test_product_tax_multicompany.py

View check run for this annotation

Codecov / codecov/patch

product_tax_multicompany_default/tests/test_product_tax_multicompany.py#L278-L279

Added lines #L278 - L279 were not covered by tests
# # Create product with empty taxes
pf_u3_c1 = Form(self.env["product.product"].with_company(self.company_1))
pf_u3_c1.name = "Testing Product"
pf_u3_c1.taxes_id.clear()
pf_u3_c1.supplier_taxes_id.clear()
product = pf_u3_c1.save()
self.assertFalse(
product.sudo().taxes_id,
"Taxes not empty when initializing product",
)
# Fill taxes
pf_u3_c1.taxes_id.add(self.tax_30_cc1)
pf_u3_c1.supplier_taxes_id.add(self.tax_30_sc1)
product = pf_u3_c1.save()
self.assertEqual(
product.sudo().taxes_id,
self.tax_30_cc1,
"Taxes has been propagated before calling set_multicompany_taxes",
)
product.with_context(ignored_company_ids=self.company_2.ids).with_company(
self.company_1
).set_multicompany_taxes()
company_1_taxes_fill = product.sudo().taxes_id.filtered(
lambda t: t.company_id == self.company_1
)
company_2_taxes_fill = product.sudo().taxes_id.filtered(
lambda t: t.company_id == self.company_2
)
self.assertEqual(
company_1_taxes_fill,
self.tax_30_cc1,
"Incorrect taxes when setting it for the first time in Company 1",
)
self.assertFalse(company_2_taxes_fill)