Skip to content

Commit

Permalink
[18.0][MIG] partner_multi_company: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BertVGroenendael committed Nov 19, 2024
1 parent 895442f commit bf6c495
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions partner_multi_company/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import res_company
from . import res_partner
from . import res_users
23 changes: 23 additions & 0 deletions partner_multi_company/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from odoo import Command, api, models


class ResCompany(models.Model):
_inherit = "res.company"

@api.model_create_multi
def create(self, vals_list):
companies = super().create(vals_list)
for company in companies:
for user in company.user_ids:
if company not in user.partner_id.company_ids:
user.partner_id.company_ids = [Command.link(company.id)]

Check warning on line 13 in partner_multi_company/models/res_company.py

View check run for this annotation

Codecov / codecov/patch

partner_multi_company/models/res_company.py#L13

Added line #L13 was not covered by tests
return companies

def write(self, vals):
res = super().write(vals)
if "user_ids" in vals:
for company in self.sudo():
for user in company.user_ids:
if company not in user.partner_id.company_ids:
user.partner_id.company_ids = [Command.link(company.id)]
return res
21 changes: 21 additions & 0 deletions partner_multi_company/tests/test_partner_multi_company.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,24 @@ def test_partner_check_company_id(self):
self.user_company_1.partner_id.write(
{"company_ids": [Command.set(self.company_2.ids)]}
)

def test_partner_create_write_company(self):
self.user_company_1.groups_id += self.env.ref("base.group_system")
self.assertEqual(len(self.user_company_1.company_ids), 1)
self.assertEqual(self.user_company_1.company_ids, self.company_1)

company_3 = (
self.env["res.company"]
.with_user(self.user_company_1)
.create([{"name": "Test company 3"}])
)

self.assertEqual(len(self.user_company_1.company_ids), 2)
self.assertEqual(self.user_company_1.company_ids, self.company_1 + company_3)
self.assertEqual(len(self.user_company_2.company_ids), 1)
self.assertEqual(self.user_company_2.company_ids, self.company_2)

company_3.user_ids += self.user_company_2

self.assertEqual(len(self.user_company_2.company_ids), 2)
self.assertEqual(self.user_company_2.company_ids, self.company_2 + company_3)

0 comments on commit bf6c495

Please sign in to comment.