OCA / multi-company

GNU Affero General Public License v3.0
100 stars 281 forks source link

[14.0][FIX] base_multi_company: Proper _check_company when multiple companies selected #535

Closed pedrobaeza closed 9 months ago

pedrobaeza commented 9 months ago

Steps to reproduce:

Result:

'Stock>Inventory adjustment' belongs to company 'C2' and 'Product' (product_id: 'Product 1') belongs to another company.

Explanation:

This is because the computed field company_id that is used for checking the product company vs the stock.move company (the error is not accurate, as it says the location, but it's the move), is returning the main company where the user is.

Solution:

Inject a context whenever we run _check_company for having available the company involved, and use that company as value for the record being checked if included in the allowed companies.

@Tecnativa

pedrobaeza commented 9 months ago

@francesco-ooops @ilyasProgrammer @renda-dev you can check if this one fulfills all the cases. I have tried with the 2 provided and everything is OK.

francesco-ooops commented 9 months ago

@moylop260 @MiquelRForgeFlow good for you?

pedrobaeza commented 9 months ago

/ocabot merge patch

OCA-git-bot commented 9 months ago

What a great day to merge this nice PR. Let's do it! Prepared branch 14.0-ocabot-merge-pr-535-by-pedrobaeza-bump-patch, awaiting test results.

OCA-git-bot commented 9 months ago

Congratulations, your PR was merged at 07a0d607a3fb2a2826b47a7b20dd122513962403. Thanks a lot for contributing to OCA. ❤️

pedrobaeza commented 9 months ago

Forward-port to v15 in #536

GSLabIt commented 9 months ago

@pedrobaeza, maybe you can help. We're experiencing a weird issue since this changes has been merged.

Here is the error:

2023-11-30 11:52:37,645 152 WARNING openerp_template odoo.modules.loading: Module stock demo data failed to install, installed without demo data 
Traceback (most recent call last):
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 677, in _tag_root
    f(rec)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 330, in _tag_function
    _eval_xml(self, rec, env)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 201, in _eval_xml
    return odoo.api.call_kw(model, method_name, args, kwargs)
  File "/.repo_requirements/odoo/odoo/api.py", line 404, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "/.repo_requirements/odoo/odoo/api.py", line 391, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/.repo_requirements/odoo/addons/stock/models/stock_picking.py", line 739, in action_confirm
    self._check_company()
  File "/home/odoo/dependencies/multi-company/base_multi_company/models/base.py", line 19, in _check_company
    company_source_id = self.company_id.id
  File "/.repo_requirements/odoo/odoo/fields.py", line 3825, in __get__
    raise ValueError("Expected singleton: %s" % record)
ValueError: Expected singleton: res.company(1, 2)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/.repo_requirements/odoo/odoo/modules/loading.py", line 86, in load_demo
    load_data(cr, idref, mode, kind='demo', package=package)
  File "/.repo_requirements/odoo/odoo/modules/loading.py", line 69, in load_data
    tools.convert_file(cr, package.name, filename, idref, mode, noupdate, kind)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 733, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 799, in convert_xml_import
    obj.parse(doc.getroot())
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 719, in parse
    self._tag_root(de)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 677, in _tag_root
    f(rec)
  File "/.repo_requirements/odoo/odoo/tools/convert.py", line 685, in _tag_root
    )) from e
odoo.tools.convert.ParseError: while parsing /.repo_requirements/odoo/addons/stock/data/stock_demo2.xml:462, near
<function model="stock.picking" name="action_confirm">
            <value model="stock.picking" eval="[                 obj().env.ref('stock.outgoing_shipment_main_warehouse').id,                 obj().env.ref('stock.outgoing_shipment_main_warehouse1').id,                 obj().env.ref('stock.outgoing_shipment_main_warehouse4').id,                 obj().env.ref('stock.outgoing_shipment_main_warehouse5').id,                 obj().env.ref('stock.outgoing_shipment_main_warehouse6').id,                 obj().env.ref('stock.incomming_shipment1').id,                 obj().env.ref('stock.incomming_shipment2').id,                 obj().env.ref('stock.incomming_shipment3').id,                 obj().env.ref('stock.outgoing_chicago_warehouse1').id,                 obj().env.ref('stock.outgoing_chicago_warehouse2').id,                 obj().env.ref('stock.outgoing_chicago_warehouse3').id,                 obj().env.ref('stock.incomming_chicago_warehouse1').id,                 obj().env.ref('stock.incomming_chicago_warehouse2').id,                 obj().env.ref('stock.incomming_chicago_warehouse3').id]"/>
        </function>

For some reason company_id having multiple values and it is raising singleton error.

Any hint on how to solve it?

pedrobaeza commented 9 months ago

@GSLabIt very weird steps to reproduce, but technically possible, so here you have the patch: #539

GSLabIt commented 9 months ago

@GSLabIt very weird steps to reproduce, but technically possible, so here you have the patch: #539

big thanks