跳至内容
菜单
此问题已终结
1 回复
5324 查看

Hi,

In my module, I have multi-company. For example, One main company and multiple sub companies. I have accounts for only main company. Sub companies also comes under main company's account.  Each sub companies have different users called sales users. I need to access 2 many2 one field (property_account_payable_id, property_account_receivable_id ) in res.partner.

I added record rule for account model for sales user. Domain also added in this record rule.

<field name="domain_force">[('company_id','in',[user.company_id.id,user.company_id.parent_id.id,False])]</field>

Partner created by admin. If I access these fields by admin, there will be no issues. But if I access these fields by sales user, I can't access these field's value. I used sudo() to access these account fields. But it never works.

account_id = partner.sudo().property_account_payable_id.id

I need to access these fields by sales user.

形象
丢弃
最佳答案

This is too late, but anyways, the issue is probably that the `property_account_payable_id` is a company specific value, meaning that accessing that property will always return the value for the current user's company.

Even if that user is the superuser aka OdooBot, it will return the value for that user's company.


A workaround is to read the value from the `ir.property` directly, maybe something like:


sudo_self = self.sudo()
ir_property = sudo_self.env['ir.property'].search([('name', '=', 'property_account_payable_id'), ('res_id', '=', 'res.partner,{}'.format(partner_id.id)), ('company_id', '=', company_id.id)])
target_account_id = (ir_property.value_reference.split(',')[1])
target_account = sudo_self.env['account.account'].browse(target_account_id)

More details:

The `property_account_payable_id` is not a normal field, it is defined with something like this:


    property_account_payable_id = fields.Many2one('account.account', company_dependent=True,
string="Account Payable", oldname="property_account_payable",
domain="[('internal_type', '=', 'payable'), ('deprecated', '=', False)]",
help="This account will be used instead of the default one as the payable account for the current partner",

required=True)

the `company_dependent` argument means that this field can be set for each company with a different value. Thus it will not be recorded as a normal column in the table in the database. Instead it will create a new record in a separate table called `ir_property`.

The table `ir_property` includes columns that can help to identify the value for each company, see these columns in the database (name, res_id, value_reference, company_id).

You can retrieve the value you want, but it will just need more effort.


BTW, don't try to change the company of the superuser to be able to read the fields you want. This is an opinion not a fact, but it is probably dangerous to do so.


形象
丢弃
相关帖文 回复 查看 活动
0
9月 22
2090
0
2月 21
2136
1
1月 20
1404
2
3月 24
7504
1
5月 21
4796