Skip to content
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

Merged
merged 1 commit into from
Dec 28, 2023

Conversation

AungKoKoLin1997
Copy link
Contributor

@AungKoKoLin1997 AungKoKoLin1997 commented Jun 21, 2023

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

@AungKoKoLin1997 AungKoKoLin1997 changed the title [FIX] multi company creation [15.0][FIX] partner_company_default: multi company creation Jun 21, 2023
@AungKoKoLin1997
Copy link
Contributor Author

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})
Copy link
Member

@pedrobaeza pedrobaeza Jun 21, 2023

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I agree.

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 15.0-fix-partner_company_default branch from af5808e to 03e0aba Compare June 22, 2023 03:37
@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 15.0-fix-partner_company_default branch 2 times, most recently from c1a79aa to 8475bc7 Compare August 10, 2023 03:10
@rafaelbn rafaelbn added this to the 15.0 milestone Aug 11, 2023
Comment on lines 13 to 14
company = super(ResCompany, self).create(vals)
return company
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
company = super(ResCompany, self).create(vals)
return company
return super().create(vals)

Comment on lines 12 to 28
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,
],
)
],
}
)
Copy link
Sponsor Member

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
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Test partners of multi company
# Check company of the partner of newly created company

.with_user(self.user.id)
.create({"name": "Test Partner 2"})
)

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

cls.user = cls.env.ref("base.user_admin")

def test_partner_company_default(self):
# Test created partner company
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Test created partner company
# Check company of newly created partner

Copy link
Sponsor Member

@yostashiro yostashiro left a 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. 👍

Copy link
Contributor

@NL66278 NL66278 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM

@AungKoKoLin1997 AungKoKoLin1997 force-pushed the 15.0-fix-partner_company_default branch 2 times, most recently from 459455b to 72a77b3 Compare December 1, 2023 10:08
@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

Comment on lines 10 to 17
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)
Copy link
Member

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:

Suggested change
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.

Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ocabot merge patch

@OCA-git-bot
Copy link
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 15.0-ocabot-merge-pr-1544-by-pedrobaeza-bump-patch, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit b46a241 into OCA:15.0 Dec 28, 2023
7 checks passed
@OCA-git-bot
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants