Skip to Content
Menu
This question has been flagged

I have a module that generate the sale order name.

I need to change the initials in the sequence according to the current company.

if self.env.company.name == 'example':

    rec.name = 'EXMP' + str(rec.country) + str(year_date) + '-' + str(rec.type) + '-' + str(source) + vendor_code + str(rec.seq_num)

else:

    rec.name = 'NTT' + str(rec.country) + str(year_date) + '-' + str(rec.type) + '-' + str(source) + vendor_code + str(rec.seq_num)

 

Case:

User (Dan) has access to two companies {example} and {example2}. {example} is user (Dan) default company.

User (Dan) switched to {example2} company.

When creating a sale order, everything seems fine and the name of the sale order will appear as [NTT********] when the current company is {example2} until I press [SAVE]. Instead, the name of the sale order will change to [EXMP********].

tried the same scenario with other user (Joe) who has the same access except that the default company is {example2}. This time the sale order name will stay as [NTT********] after saving.

It appears that the sale order is taking the user default company when saving (create)?

I wonder what should I add to insure that the code works properly?

haven’t found a similar case on the forum yet.


Avatar
Discard
Author Best Answer

Just in case someone wanted a workaround method.
I have created a new field [c-company].

In the python file you can add: 

c_company=fields.Many2one('res.company', 'c_company', required=False, index=True, default=lambdaself: self.env.company)

And in the view file add:

<fieldname="c_company"invisible="1"/>

Now you can use the following code:

if str(rec.c_company.name) == "company name":
​rec.name = 'EXMP' + str(rec.country) + str(year_date) + '-' + str(rec.type) + '-' + str(source) + vendor_code + str(rec.seq_num)
else:
​rec.name = 'NTT' + str(rec.country) + str(year_date) + '-' + str(rec.type) + '-' + str(source) + vendor_code + str(rec.seq_num)

I hope this can be a help for someone.

Avatar
Discard
Related Posts Replies Views Activity
2
Sep 17
21504
0
Mar 15
3821
1
Oct 18
5047
1
May 25
870
2
Nov 24
2074