Skip to content

Commit

Permalink
[MIG] account_invoice_inter_company: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongnmtm committed Aug 10, 2024
1 parent 86f301f commit 67f8aef
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 148 deletions.
7 changes: 5 additions & 2 deletions account_invoice_inter_company/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ Configuration
To configure this module, you need to go to the menu *Settings > General
Settings*, go to the tab *Companies / Inter Company OCA features*

You now have access to other checks *Common Product Catalog* and
*Invoice Auto Validation*.
You now have access to other options *Intercompany user for invoices*
and *Invoice Auto Validation*.

To customize products sharing don't hesitate to override
\_compute_share_product() in res.company model.
Expand Down Expand Up @@ -106,6 +106,9 @@ Contributors
- Pedro M. Baeza

- Isaac Gallart <[email protected]>
- \`Komit <https://komit-consulting.com>\`:

- Cuong Nguyen Mtm <[email protected]>

Maintainers
-----------
Expand Down
2 changes: 1 addition & 1 deletion account_invoice_inter_company/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"name": "Inter Company Invoices",
"summary": "Intercompany invoice rules",
"version": "16.0.1.0.2",
"version": "17.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://github.com/OCA/multi-company",
"author": "Odoo SA, Akretion, Odoo Community Association (OCA)",
Expand Down
2 changes: 0 additions & 2 deletions account_invoice_inter_company/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ def action_post(self):

def _check_intercompany_product(self, dest_company):
self.ensure_one()
if dest_company.company_share_product:
return
domain = dest_company._get_user_domain()
dest_user = self.env["res.users"].search(domain, limit=1)
for line in self.invoice_line_ids:
Expand Down
16 changes: 0 additions & 16 deletions account_invoice_inter_company/models/res_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
class ResCompany(models.Model):
_inherit = "res.company"

company_share_product = fields.Boolean(
"Share product to all companies",
compute="_compute_share_product",
compute_sudo=True,
help="Share your product to all companies defined in your instance.\n"
" * Checked : Product are visible for every company, "
"even if a company is defined on the partner.\n"
" * Unchecked : Each company can see only its product "
"(product where company is defined). Product not related to a "
"company are visible for all companies.",
)
invoice_auto_validation = fields.Boolean(
help="When an invoice is created by a multi company rule "
"for this company, it will automatically validate it",
Expand All @@ -29,11 +18,6 @@ class ResCompany(models.Model):
"intercompany rules.",
)

def _compute_share_product(self):
product_rule = self.env.ref("product.product_comp_rule")
for company in self:
company.company_share_product = not bool(product_rule.active)

def _get_user_domain(self):
self.ensure_one()
group_account_invoice = self.env.ref("account.group_account_invoice")
Expand Down
26 changes: 1 addition & 25 deletions account_invoice_inter_company/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo import fields, models


class ResConfigSettings(models.TransientModel):
Expand All @@ -20,27 +20,3 @@ class ResConfigSettings(models.TransientModel):
"intercompany rules. If not set the user initiating the"
"transaction will be used",
)
company_share_product = fields.Boolean(
"Share product to all companies",
help="Share your product to all companies defined in your instance.\n"
" * Checked : Product are visible for every company, "
"even if a company is defined on the partner.\n"
" * Unchecked : Each company can see only its product "
"(product where company is defined). Product not related to a "
"company are visible for all companies.",
)

@api.model
def get_values(self):
res = super().get_values()
product_rule = self.env.ref("product.product_comp_rule")
res.update(
company_share_product=not bool(product_rule.active),
)
return res

def set_values(self):
res = super().set_values()
product_rule = self.env.ref("product.product_comp_rule")
product_rule.write({"active": not bool(self.company_share_product)})
return res
2 changes: 1 addition & 1 deletion account_invoice_inter_company/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ To configure this module, you need to go to the menu *Settings \>
General Settings*, go to the tab *Companies / Inter Company OCA
features*

You now have access to other checks *Common Product Catalog* and
You now have access to other options *Intercompany user for invoices* and
*Invoice Auto Validation*.

To customize products sharing don't hesitate to override
Expand Down
2 changes: 2 additions & 0 deletions account_invoice_inter_company/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
- David Vidal
- Pedro M. Baeza
- Isaac Gallart \<<[email protected]>\>
- \`Komit \<<https://komit-consulting.com>\>\`:
- Cuong Nguyen Mtm \<<[email protected]>\>
8 changes: 6 additions & 2 deletions account_invoice_inter_company/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ <h1 class="title">Inter Company Invoices</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<p>To configure this module, you need to go to the menu <em>Settings &gt; General
Settings</em>, go to the tab <em>Companies / Inter Company OCA features</em></p>
<p>You now have access to other checks <em>Common Product Catalog</em> and
<em>Invoice Auto Validation</em>.</p>
<p>You now have access to other options <em>Intercompany user for invoices</em>
and <em>Invoice Auto Validation</em>.</p>
<p>To customize products sharing don’t hesitate to override
_compute_share_product() in res.company model.</p>
</div>
Expand Down Expand Up @@ -453,6 +453,10 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
</ul>
</li>
<li>Isaac Gallart &lt;<a class="reference external" href="mailto:igallart&#64;puntsistemes.es">igallart&#64;puntsistemes.es</a>&gt;</li>
<li>`Komit &lt;<a class="reference external" href="https://komit-consulting.com">https://komit-consulting.com</a>&gt;`:<ul>
<li>Cuong Nguyen Mtm &lt;<a class="reference external" href="mailto:cuong.nmtm&#64;komit-consulting.com">cuong.nmtm&#64;komit-consulting.com</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
113 changes: 68 additions & 45 deletions account_invoice_inter_company/tests/test_inter_company_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,60 @@
# Copyright 2020 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _
from odoo.exceptions import UserError, ValidationError
from unittest.mock import patch

from odoo.exceptions import UserError
from odoo.tests import tagged
from odoo.tests.common import Form, TransactionCase

from odoo.addons.account.models.chart_template import AccountChartTemplate
from odoo.addons.account.tests.test_chart_template import test_get_data


def _get_chart_template_mapping(self, get_all=False):
return {
"test": {
"name": "test",
"country_id": None,
"country_code": None,
"modules": ["account"],
"parent": None,
}
}


@tagged("post_install", "-at_install")
@patch.object(
AccountChartTemplate, "_get_chart_template_mapping", _get_chart_template_mapping
)
class TestAccountInvoiceInterCompanyBase(TransactionCase):
@classmethod
@patch.object(
AccountChartTemplate, "_get_chart_template_mapping", _get_chart_template_mapping
)
def setUpClass(cls):
super().setUpClass()
cls.account_obj = cls.env["account.account"]
cls.account_move_obj = cls.env["account.move"]
cls.chart = cls.env["account.chart.template"].search([], limit=1)
if not cls.chart:
raise ValidationError(
# translation to avoid pylint warnings
_("No Chart of Account Template has been defined !")
)

cls.company_a = cls.env["res.company"].create(
{
"name": "Company A",
"currency_id": cls.env.ref("base.EUR").id,
"country_id": cls.env.ref("base.fr").id,
"parent_id": cls.env.ref("base.main_company").id,
# "parent_id": cls.env.ref("base.main_company").id,
"invoice_auto_validation": True,
}
)
cls.chart.try_loading(company=cls.company_a, install_demo=False)
with patch.object(
AccountChartTemplate,
"_get_chart_template_data",
side_effect=test_get_data,
autospec=True,
):
cls.env["account.chart.template"].try_loading(
"test", company=cls.company_a, install_demo=False
)
cls.partner_company_a = cls.env["res.partner"].create(
{"name": cls.company_a.name, "is_company": True}
)
Expand All @@ -42,11 +66,19 @@ def setUpClass(cls):
"name": "Company B",
"currency_id": cls.env.ref("base.EUR").id,
"country_id": cls.env.ref("base.fr").id,
"parent_id": cls.env.ref("base.main_company").id,
# "parent_id": cls.env.ref("base.main_company").id,
"invoice_auto_validation": True,
}
)
cls.chart.try_loading(company=cls.company_b, install_demo=False)
with patch.object(
AccountChartTemplate,
"_get_chart_template_data",
side_effect=test_get_data,
autospec=True,
):
cls.env["account.chart.template"].try_loading(
"test", company=cls.company_b, install_demo=False
)
cls.partner_company_b = cls.env["res.partner"].create(
{"name": cls.company_b.name, "is_company": True}
)
Expand Down Expand Up @@ -332,15 +364,6 @@ def setUpClass(cls):
"company_id": cls.company_a.id,
}
)
cls.pcg_X58 = cls.env["account.account.template"].create(
{
"name": "Internal Transfers",
"code": "X58",
"account_type": "asset_current",
"reconcile": True,
}
)

cls.a_recv_company_a = cls.account_obj.create(
{
"code": "X11002.A",
Expand All @@ -363,8 +386,18 @@ def setUpClass(cls):
cls.partner_company_a.property_account_receivable_id = cls.a_recv_company_a.id
cls.partner_company_a.property_account_payable_id = cls.a_pay_company_a.id

cls.partner_company_b.property_account_receivable_id = cls.a_recv_company_b.id
cls.partner_company_b.property_account_payable_id = cls.a_pay_company_b.id
cls.partner_company_b.with_user(
cls.user_company_a.id
).property_account_receivable_id = cls.a_recv_company_a.id
cls.partner_company_b.with_user(
cls.user_company_a.id
).property_account_payable_id = cls.a_pay_company_a.id
cls.partner_company_b.with_user(
cls.user_company_b.id
).property_account_receivable_id = cls.a_recv_company_b.id
cls.partner_company_b.with_user(
cls.user_company_b.id
).property_account_payable_id = cls.a_pay_company_b.id

cls.invoice_company_a = Form(
cls.account_move_obj.with_user(cls.user_company_a.id).with_context(
Expand All @@ -390,7 +423,7 @@ def setUpClass(cls):
cls.product_a = cls.invoice_line_a.product_id
cls.product_a.with_user(
cls.user_company_b.id
).property_account_expense_id = cls.a_expense_company_b.id
).sudo().property_account_expense_id = cls.a_expense_company_b.id


class TestAccountInvoiceInterCompany(TestAccountInvoiceInterCompanyBase):
Expand All @@ -415,7 +448,9 @@ def test03_confirm_invoice_and_cancel(self):
# ensure the catalog is shared
self.env.ref("product.product_comp_rule").write({"active": False})
# Make sure there are no taxes in target company for the used product
self.product_a.with_user(self.user_company_b.id).supplier_taxes_id = False
self.product_a.with_user(
self.user_company_b.id
).sudo().supplier_taxes_id = False
# Confirm the invoice of company A
self.invoice_company_a.with_user(self.user_company_a.id).action_post()
# Check destination invoice created in company B
Expand Down Expand Up @@ -443,9 +478,9 @@ def test03_confirm_invoice_and_cancel(self):
self.invoice_company_a.invoice_line_ids[0].product_id,
)
# Cancel the invoice of company A
invoice_origin = ("%s - Canceled Invoice: %s") % (
self.invoice_company_a.company_id.name,
self.invoice_company_a.name,
invoice_origin = "{company_name} - Canceled Invoice: {invoice_name}".format(
company_name=self.invoice_company_a.company_id.name,
invoice_name=self.invoice_company_a.name,
)
self.invoice_company_a.with_user(self.user_company_a.id).button_cancel()
# Check invoices after to cancel invoice of company A
Expand All @@ -454,6 +489,7 @@ def test03_confirm_invoice_and_cancel(self):
self.assertEqual(invoices[0].invoice_origin, invoice_origin)
# Check if keep the invoice number
invoice_number = self.invoice_company_a.name
self.invoice_company_a.with_user(self.user_company_a.id).button_draft()
self.invoice_company_a.with_user(self.user_company_a.id).action_post()
self.assertEqual(self.invoice_company_a.name, invoice_number)
# When the destination invoice is posted we can't modify the origin either
Expand Down Expand Up @@ -484,30 +520,17 @@ def test_confirm_invoice_with_child_partner(self):
)
self.assertEqual(len(invoices), 1)

def test_confirm_invoice_with_product_and_shared_catalog(self):
"""With no security rule, child company have access to any product.
Then child invoice can share the same product
"""
# ensure the catalog is shared even if product is in other company
self.env.ref("product.product_comp_rule").write({"active": False})
# Product is set to a specific company
self.product_a.write({"company_id": self.company_a.id})
invoices = self._confirm_invoice_with_product()
self.assertNotEqual(
invoices.invoice_line_ids[0].product_id, self.env["product.product"]
)

def test_confirm_invoice_with_native_product_rule_and_shared_product(self):
"""With native security rule, products with access in both companies
must be present in parent and child invoices.
"""
# ensure the catalog is shared even if product is in other company
self.env.ref("product.product_comp_rule").write({"active": True})
# Product is set to a specific company
self.product_a.write({"company_id": False})
self.product_a.sudo().write({"company_id": False})
# If product_multi_company is installed
if "company_ids" in dir(self.product_a):
self.product_a.write({"company_ids": [(5, 0, 0)]})
self.product_a.sudo().write({"company_ids": [(5, 0, 0)]})
invoices = self._confirm_invoice_with_product()
self.assertEqual(invoices.invoice_line_ids[0].product_id, self.product_a)

Expand All @@ -518,10 +541,10 @@ def test_confirm_invoice_with_native_product_rule_and_unshared_product(self):
# ensure the catalog is shared even if product is in other company
self.env.ref("product.product_comp_rule").write({"active": True})
# Product is set to a specific company
self.product_a.write({"company_id": self.company_a.id})
self.product_a.sudo().write({"company_id": self.company_a.id})
# If product_multi_company is installed
if "company_ids" in dir(self.product_a):
self.product_a.write({"company_ids": [(6, 0, [self.company_a.id])]})
self.product_a.sudo().write({"company_ids": [(6, 0, [self.company_a.id])]})
with self.assertRaises(UserError):
self._confirm_invoice_with_product()

Expand Down
Loading

0 comments on commit 67f8aef

Please sign in to comment.