-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
[15.0][FIX] partner_company_default: multi company creation #1544
Conversation
One tests from base_location_nuts is failed. I think this should be handled by another PR and I will let this PR alone. |
self = self.with_context(creating_from_company=True) | ||
company = super(ResCompany, self).create(vals) | ||
# Add partner's company_id to new company | ||
company.partner_id.sudo().write({"company_id": company.id}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a potential problem. Companies' partner should have company_id = False.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I agree.
af5808e
to
03e0aba
Compare
c1a79aa
to
8475bc7
Compare
company = super(ResCompany, self).create(vals) | ||
return company |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
company = super(ResCompany, self).create(vals) | |
return company | |
return super().create(vals) |
user = self.env["res.users"].create( | ||
{ | ||
"name": "Test User", | ||
"login": "Test User", | ||
"email": "test@yourcompany.com", | ||
"groups_id": [ | ||
( | ||
6, | ||
0, | ||
[ | ||
self.env.ref("base.group_system").id, | ||
self.env.ref("base.group_partner_manager").id, | ||
], | ||
) | ||
], | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As talked, let's use the admin user from the demo data.
) | ||
self.assertEqual(partner.company_id, self.user.company_id) | ||
|
||
# Test partners of multi company |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Test partners of multi company | |
# Check company of the partner of newly created company |
.with_user(self.user.id) | ||
.create({"name": "Test Partner 2"}) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cls.user = cls.env.ref("base.user_admin") | ||
|
||
def test_partner_company_default(self): | ||
# Test created partner company |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Test created partner company | |
# Check company of newly created partner |
3f78f90
to
53f535b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the updates. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 LGTM
459455b
to
72a77b3
Compare
This PR has the |
company_id = fields.Many2one(default=lambda self: self.env.company) | ||
|
||
@api.model | ||
def create(self, vals): | ||
# The context value is set in the create method of res.company | ||
if self.env.context.get("creating_from_company"): | ||
vals["company_id"] = False | ||
return super(ResPartner, self).create(vals) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous code is not multi-record creation aware, but I have tried and it's not always correct depending on when the create override in the stack is executed, so better to put company_id=False from the beginning this way:
company_id = fields.Many2one(default=lambda self: self.env.company) | |
@api.model | |
def create(self, vals): | |
# The context value is set in the create method of res.company | |
if self.env.context.get("creating_from_company"): | |
vals["company_id"] = False | |
return super(ResPartner, self).create(vals) | |
company_id = fields.Many2one(default=lambda self: self._default_company_id()) | |
@api.model | |
def _default_company_id(self): | |
"""Return False for other tests or if creating a company.""" | |
context = self.env.context | |
if ( | |
context.get("creating_from_company") | |
or config["test_enable"] | |
and not context.get("test_partner_company_default") | |
): | |
return False | |
return self.env.company |
and on tests, pass the corresponding context.
72a77b3
to
9ed87bf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/ocabot merge patch
This PR looks fantastic, let's merge it! |
Congratulations, your PR was merged at 8abf090. Thanks a lot for contributing to OCA. ❤️ |
Prior to this commit, the system would generate a partner before the creation of a new company and subsequently assign that partner to the company. As per
partner_company_default
module's behavior, the system would automatically assign the current company to the partner during creation, leading to a mismatch between the partner's company_id and its company.In this Pull Request (PR), when a new company is created, the company_id of the associated partner will no longer be assigned during the partner's creation.
@qrtl