Search code examples
odoo-15record-rules

How to set record rule for account move to show user own records and their saleteams member records Odoo 15?


Is this possible to do this by record rule?

Example:

user A have saleteam with member B,C,D. A is the leader of this team.

so if we login to A, and go to account.move, we can see records of A,B,C,D.

if we login to b, we only see B records.

Thanks you.

Note: Other solution are good too, no need to be record rule.


Solution

  • Thanks to Jainesh Shah(Aktiv Software)

    I've found the answer, which is use the search_read() function:

    # -*- coding: utf-8 -*-
    
    from odoo import fields, models, api, _
    
    
    class AccountMove(models.Model):
        _inherit = 'account.move'
    
        def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None):
            # find list members of sale team which leaded by current user (current user is leader of this sale team)
            sale_teams = self.env['crm.team'].search([('user_id', '=', self.env.user.id)]).mapped('member_ids')
    
            # if current user is in group crm_account_move_restrict_records and also the leader of a team
            # then we will show all data of this user and members of the team that this user is leading
            if self.env.user.has_group('z_crm_contract_for_baan.crm_account_move_restrict_records'):
                if sale_teams:
                    # add domain
                    # get data by team members
                    domain += ['|', ('user_id', 'in', sale_teams.ids)]
                # add domain
                # get data by current user
                domain += [('user_id', '=', self.env.user.id)]
            return super(AccountMove, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order)
    

    Thanks you all for helping, especially Jainesh Shah(Aktiv Software).