Skip to content

Commit

Permalink
[MIG] product_tax_multicompany_default: Migration to 13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernesto Tejeda committed Oct 21, 2020
1 parent 2b4e1c6 commit 6043d3e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 86 deletions.
11 changes: 6 additions & 5 deletions product_tax_multicompany_default/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Product Tax Multi Company Default
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github
:target: https://github.com/OCA/multi-company/tree/12.0/product_tax_multicompany_default
:target: https://github.com/OCA/multi-company/tree/13.0/product_tax_multicompany_default
:alt: OCA/multi-company
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/multi-company-12-0/multi-company-12-0-product_tax_multicompany_default
:target: https://translation.odoo-community.org/projects/multi-company-13-0/multi-company-13-0-product_tax_multicompany_default
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/133/12.0
:target: https://runbot.odoo-community.org/runbot/133/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -59,7 +59,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/multi-company/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/multi-company/issues/new?body=module:%20product_tax_multicompany_default%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/multi-company/issues/new?body=module:%20product_tax_multicompany_default%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -79,6 +79,7 @@ Contributors
* Carlos Dauden
* Pedro M. Baeza
* Vicent Cubells
* Ernesto Tejeda

Maintainers
~~~~~~~~~~~
Expand All @@ -93,6 +94,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/multi-company <https://github.com/OCA/multi-company/tree/12.0/product_tax_multicompany_default>`_ project on GitHub.
This module is part of the `OCA/multi-company <https://github.com/OCA/multi-company/tree/13.0/product_tax_multicompany_default>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
10 changes: 5 additions & 5 deletions product_tax_multicompany_default/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

{
"name": "Product Tax Multi Company Default",
"version": "12.0.1.0.0",
"version": "13.0.1.0.0",
"category": "Account",
"website": "https://www.tecnativa.com",
"author": "Tecnativa, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/multi-company",
"author": "Tecnativa, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["account", "product",],
"data": ["views/product_template_view.xml",],
"depends": ["account", "product"],
"data": ["views/product_template_view.xml"],
}
34 changes: 18 additions & 16 deletions product_tax_multicompany_default/models/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@
class ProductTemplate(models.Model):
_inherit = "product.template"

def taxes_by_company(self, field, company_id, match_tax_ids=None):
def taxes_by_company(self, field, company, match_tax_ids=None):
taxes_ids = []
if match_tax_ids is None:
taxes_ids = self.env["ir.default"].get(
"product.template", field, company_id=company_id
)
# If None: return default taxes if []: return empty list
taxes_ids = company[field].ids
# If None: return default taxes
if not match_tax_ids:
return isinstance(taxes_ids, list) and taxes_ids or []
return taxes_ids
AccountTax = self.env["account.tax"]
for tax in AccountTax.browse(match_tax_ids):
taxes_ids.extend(
AccountTax.search(
[("name", "=", tax.name), ("company_id", "=", company_id)]
[("name", "=", tax.name), ("company_id", "=", company.id)]
).ids
)
return taxes_ids

@api.multi
def set_multicompany_taxes(self):
self.ensure_one()
customer_tax_ids = self.taxes_id.ids
supplier_tax_ids = self.supplier_taxes_id.ids
company_id = self.env.user.company_id.id
user_company = self.env.company
obj = self.sudo()
default_customer_tax_ids = obj.taxes_by_company("taxes_id", company_id)
default_supplier_tax_ids = obj.taxes_by_company("supplier_taxes_id", company_id)
default_customer_tax_ids = obj.taxes_by_company(
"account_sale_tax_id", user_company
)
default_supplier_tax_ids = obj.taxes_by_company(
"account_purchase_tax_id", user_company
)
# Use list() to copy list
match_customer_tax_ids = (
default_customer_tax_ids != customer_tax_ids
Expand All @@ -46,13 +47,15 @@ def set_multicompany_taxes(self):
and list(supplier_tax_ids)
or None
)
for company in obj.env["res.company"].search([("id", "!=", company_id)]):
for company in obj.env["res.company"].search([("id", "!=", user_company.id)]):
customer_tax_ids.extend(
obj.taxes_by_company("taxes_id", company.id, match_customer_tax_ids)
obj.taxes_by_company(
"account_sale_tax_id", company, match_customer_tax_ids
)
)
supplier_tax_ids.extend(
obj.taxes_by_company(
"supplier_taxes_id", company.id, match_suplier_tax_ids
"account_purchase_tax_id", company, match_suplier_tax_ids
)
)
self.write(
Expand All @@ -64,14 +67,13 @@ def set_multicompany_taxes(self):

@api.model
def create(self, vals):
res = super(ProductTemplate, self).create(vals)
res = super().create(vals)
res.set_multicompany_taxes()
return res


class ProductProduct(models.Model):
_inherit = "product.product"

@api.multi
def set_multicompany_taxes(self):
self.product_tmpl_id.set_multicompany_taxes()
1 change: 1 addition & 0 deletions product_tax_multicompany_default/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Carlos Dauden
* Pedro M. Baeza
* Vicent Cubells
* Ernesto Tejeda
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ <h1 class="title">Product Tax Multi Company Default</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/multi-company/tree/12.0/product_tax_multicompany_default"><img alt="OCA/multi-company" src="https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/multi-company-12-0/multi-company-12-0-product_tax_multicompany_default"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/133/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/multi-company/tree/13.0/product_tax_multicompany_default"><img alt="OCA/multi-company" src="https://img.shields.io/badge/github-OCA%2Fmulti--company-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/multi-company-13-0/multi-company-13-0-product_tax_multicompany_default"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/133/13.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module sets the default company taxes for all the existing companies when
a product is created. It also adds a button in product view to set all the
taxes from other companies matching them by tax code.</p>
Expand Down Expand Up @@ -411,7 +411,7 @@ <h1><a class="toc-backref" href="#id3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/multi-company/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/multi-company/issues/new?body=module:%20product_tax_multicompany_default%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/multi-company/issues/new?body=module:%20product_tax_multicompany_default%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -429,6 +429,7 @@ <h2><a class="toc-backref" href="#id6">Contributors</a></h2>
<li>Carlos Dauden</li>
<li>Pedro M. Baeza</li>
<li>Vicent Cubells</li>
<li>Ernesto Tejeda</li>
</ul>
</li>
</ul>
Expand All @@ -440,7 +441,7 @@ <h2><a class="toc-backref" href="#id7">Maintainers</a></h2>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/multi-company/tree/12.0/product_tax_multicompany_default">OCA/multi-company</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/multi-company/tree/13.0/product_tax_multicompany_default">OCA/multi-company</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,58 +40,33 @@ def setUpClass(cls):
"amount_type": "percent",
"type_tax_use": "sale",
}
cls.tax_10_cc1 = AccountTax.sudo(cls.user_1.id).create(tax_vals)
cls.tax_10_cc2 = AccountTax.sudo(cls.user_2.id).create(tax_vals)
tax_vals.update(
{"name": "Test Customer Tax 20%", "amount": 20.0,}
)
cls.tax_20_cc1 = AccountTax.sudo(cls.user_1.id).create(tax_vals)
cls.tax_20_cc2 = AccountTax.sudo(cls.user_2.id).create(tax_vals)
cls.tax_10_cc1 = AccountTax.with_user(cls.user_1.id).create(tax_vals)
cls.tax_10_cc2 = AccountTax.with_user(cls.user_2.id).create(tax_vals)
tax_vals.update({"name": "Test Customer Tax 20%", "amount": 20.0})
cls.tax_20_cc1 = AccountTax.with_user(cls.user_1.id).create(tax_vals)
cls.tax_20_cc2 = AccountTax.with_user(cls.user_2.id).create(tax_vals)
tax_vals.update(
{
"name": "Test Supplier Tax 10%",
"amount": 10.0,
"type_tax_use": "purchase",
}
)
cls.tax_10_sc1 = AccountTax.sudo(cls.user_1.id).create(tax_vals)
cls.tax_10_sc2 = AccountTax.sudo(cls.user_2.id).create(tax_vals)
tax_vals.update(
{"name": "Test Supplier Tax 20%", "amount": 20.0,}
)
cls.tax_20_sc1 = AccountTax.sudo(cls.user_1.id).create(tax_vals)
cls.tax_20_sc2 = AccountTax.sudo(cls.user_2.id).create(tax_vals)
IrDefault = cls.env["ir.default"]
IrDefault.set(
model_name="product.template",
field_name="taxes_id",
value=[cls.tax_10_cc1.id],
company_id=cls.company_1.id,
)
IrDefault.set(
model_name="product.template",
field_name="supplier_taxes_id",
value=[cls.tax_10_sc1.id],
company_id=cls.company_1.id,
)
IrDefault.set(
model_name="product.template",
field_name="taxes_id",
value=[cls.tax_20_cc2.id],
company_id=cls.company_2.id,
)
IrDefault.set(
model_name="product.template",
field_name="supplier_taxes_id",
value=[cls.tax_20_sc2.id],
company_id=cls.company_2.id,
)
cls.tax_10_sc1 = AccountTax.with_user(cls.user_1.id).create(tax_vals)
cls.tax_10_sc2 = AccountTax.with_user(cls.user_2.id).create(tax_vals)
tax_vals.update({"name": "Test Supplier Tax 20%", "amount": 20.0})
cls.tax_20_sc1 = AccountTax.with_user(cls.user_1.id).create(tax_vals)
cls.tax_20_sc2 = AccountTax.with_user(cls.user_2.id).create(tax_vals)
cls.company_1.account_sale_tax_id = cls.tax_10_cc1.id
cls.company_1.account_purchase_tax_id = cls.tax_10_sc1.id
cls.company_2.account_sale_tax_id = cls.tax_20_cc2.id
cls.company_2.account_purchase_tax_id = cls.tax_20_sc2.id

def test_multicompany_default_tax(self):
product = (
self.env["product.product"]
.sudo(self.user_1.id)
.create({"name": "Test Product", "company_id": False,})
.with_user(self.user_1.id)
.create({"name": "Test Product", "company_id": False})
)
product = product.sudo()
self.assertIn(self.tax_10_cc1, product.taxes_id)
Expand All @@ -102,7 +77,7 @@ def test_multicompany_default_tax(self):
def test_not_default_tax_if_set(self):
product = (
self.env["product.product"]
.sudo(self.user_1.id)
.with_user(self.user_1.id)
.create(
{
"name": "Test Product",
Expand All @@ -119,7 +94,7 @@ def test_not_default_tax_if_set(self):
def test_default_tax_if_set_match(self):
product = (
self.env["product.product"]
.sudo(self.user_2.id)
.with_user(self.user_2.id)
.create(
{
"name": "Test Product",
Expand All @@ -134,22 +109,11 @@ def test_default_tax_if_set_match(self):
self.assertIn(self.tax_10_sc1, product.supplier_taxes_id)

def test_tax_not_default_set_match(self):
IrDefault = self.env["ir.default"]
IrDefault.set(
model_name="product.template",
field_name="taxes_id",
value=[self.tax_20_cc1.id],
company_id=self.company_1.id,
)
IrDefault.set(
model_name="product.template",
field_name="supplier_taxes_id",
value=[self.tax_20_sc1.id],
company_id=self.company_1.id,
)
self.company_1.account_sale_tax_id = self.tax_20_cc1.id
self.company_1.account_purchase_tax_id = self.tax_20_sc1.id
product = (
self.env["product.product"]
.sudo(self.user_1.id)
.with_user(self.user_1.id)
.create(
{
"name": "Test Product",
Expand Down

0 comments on commit 6043d3e

Please sign in to comment.