How to set record rule for account move to show user own records and their saleteams member records Odoo 15? - 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.

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).

Related

Add permission to group (or find where is the problem)

I have this message:
The requested operation can not be completed due to security
restrictions. Document type: Employee (hr.employee) Operation: read
User: 23 Fields: - contract_id (allowed for groups 'Employees /
Officer')
I would not like to add the user to the mentioned group because I want to restrict his actions, and this group has too many permissions. How can I know what permission is required for that specific field?
UPDATE
Create a module with just these lines. I am trying to overwrite the field by deleting the group but it doesn't work for me. What am I doing something wrong?
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo.osv import expression
class Employee(models.Model):
_inherit = "hr.employee"
contract_id = fields.Many2one('hr.contract', string='Current Contract',
domain="[('company_id', '=', company_id)]", help='Current contract of the employee')
Odoo is nice for once and is telling you which field on which model is restricted. So in this case you should look into the field definition and will find:
contract_id = fields.Many2one(
'hr.contract', string='Current Contract',
groups="hr.group_hr_user",
domain="[('company_id', '=', company_id), ('employee_id', '=', id)]",
help='Current contract of the employee')
You can see the groups parameter which will lead to a restriction of this field to the following groups. Here it is one: hr.group_hr_user which is created with the hr App and also mentioned in Odoo's Access Error: "Employees / Officer".
So you could change the field definition, but i don't advise to do that. I'm not sure why there is no possibility for an Employee to atleast see some of the current contract information of his own contract.

How to disable "You have been assigned" email when a user creates a sales order in Odoo 10

In Odoo 10, when user "A" creates a new sales order and assigns it to a different salesperson (user "B"), no matter what configuration you have applied to email templates/subtypes/send notifications, an email is automatically sent to the customer and the salesperson (I am still amazed on which business logic was follow to send internal notification emails to customers by default).
The email is the well known one with this format:
"You have been assigned to SOxxxx."
To make things worse the email is set to "Auto-delete", so you do not even know what your system is sending to customers (no comments).
Which module or function or setting in Odoo 10 CE shall be overwritten to avoid such default behaviour?
Overwrite _message_auto_subscribe_notify method for sale.order class and add to context mail_auto_subscribe_no_notify.
from odoo import models, api
class SaleOrder(models.Model):
_inherit = 'sale.order'
#api.multi
def _message_auto_subscribe_notify(self, partner_ids):
""" Notify newly subscribed followers of the last posted message.
:param partner_ids : the list of partner to add as needaction partner of the last message
(This excludes the current partner)
"""
return super(SaleOrder, self.with_context(mail_auto_subscribe_no_notify=True))\
._message_auto_subscribe_notify(partner_ids)
The original method will not send the message if that key is passed in the context
#api.multi
def _message_auto_subscribe_notify(self, partner_ids):
""" Notify newly subscribed followers of the last posted message.
:param partner_ids : the list of partner to add as needaction partner of the last message
(This excludes the current partner)
"""
if not partner_ids:
return
if self.env.context.get('mail_auto_subscribe_no_notify'): # Here
return
# send the email only to the current record and not all the ids matching active_domain !
# by default, send_mail for mass_mail use the active_domain instead of active_ids.
if 'active_domain' in self.env.context:
ctx = dict(self.env.context)
ctx.pop('active_domain')
self = self.with_context(ctx)
for record in self:
record.message_post_with_view(
'mail.message_user_assigned',
composition_mode='mass_mail',
partner_ids=[(4, pid) for pid in partner_ids],
auto_delete=True,
auto_delete_message=True,
parent_id=False, # override accidental context defaults
subtype_id=self.env.ref('mail.mt_note').id)
If this should only be disabled for SaleOrder which are generated through custom code (e.g. an developed API endpoint), you could use the with_context() method on each model:
sale_order = {
'partner_id': partner['id'],
'state': 'sent',
'user_id': 6,
'source_id': 3,
'currency_id': currency['id'],
'payment_term_id': payment_term['id'],
}
created_sale_order = request.env['sale.order'].with_context(mail_auto_subscribe_no_notify=True).create(sale_order)
In my example, the user with the ID 6 does not get the notification about the assignment of this sale order.

How to apply an domain on relational field odoo?

Here is my sale.order.line module, I do some modifications
I want to apply a domain on the product that not all product be displayed just products in Ligne contract field for example :
Here in my Contrat line, I Have just one Product so on the Sale order lines only this product (article) must be shown
Use onchane event for this in your sale.order.line
#api.onchange('contrat_id')
def set_domain(self):
# force the user to reselect the producg if he changes the contrat line
self.product_id = False
if self.contrat_id :
return {'domain': {'product_id': [('id', 'in', self.contrat_id.product_ids.ids)]}}
else:
# remove the domain if no contrat is selected
return {'domain': {'product_id': []}}
I'm using my phone sorry if i made a syntax error but I hope you get the idea
Edits
Okay in your contract model you don't have a many2many field to product model as I thought instead you have this one2many field ligne contract
So let suppose that the name of that field is ligne_ids in this one2many relation there is a many2one field to product model let us say its name is product_id.
Use the power of mapped to extract in one line all product ids in the contract lignes.
# many2one -> one2many -> mapped('many2one') this will collect of the records without duplication from the o2m field.
# contract -> contract lignes -> products
self.contrat_lignes_id.ligne_ids.mapped('product_id').ids
Hope this helps you

Functional change needed for "see own leads" option in Odoo 9

when i used this domain rulein "Personal leads" rule under "see own leads" group, so that users can also see those leads that they follow with leads they own i got an error,
domai rule :
['|',('user_id','=',user.id),('user_id','=',user.message_follower_ids)]
error:
"Invalid value %r in domain term %r" % (right, leaf)
AssertionError: Invalid value mail.followers(10,) in domain term ('user_id', '=', mail.followers(10,))
You're having this error ValueError: Invalid field 'user_id' in leaf "<osv.ExtendedLeaf: ('user_id', '=', 10) on mail_followers (ctx: )>" because the mail.followers model does not have a user_id field.
The domain that you're using is one that is used on crm.lead or sale.order (where user_id indicates the seller). Like #CZoellner points it out, you should read the examples he mentions.
Fields you filter records on must exist on the model.
Edit You should search with the current user's partner in the models followers, like:
['|', ('user_id', '=', user.id), ('message_partner_ids', 'in', [user.partner_id.id])]
Interesting example is one of Odoos default rules for project tasks "Project/Task: portal users: (portal and colleagues following) or (followers and following)"

Openerp Domain Filter Help on Tasks associated to Sales Team

I have been developing a lot of modules and implementing openerp for a wild. But I am stuck in a functional implementation.
I have installed the module crm_todo, it is for have tasks on crm, this module add "My Tasks" menu under Sales.
I need to create a new Menu with a Domain Filter called "Department Tasks" where will show all the task to all the members of an specific Sales Team. The task is assigned to User A; User A belongs to Sales Team A; Sales Team A has 2 more members. This new Menu have to list the task Assigned to User A to all the members of Sales Team A.
I was trying to do with field.function, but something is wrong. I am trying to apply the domain on act_window using the openerp Action Windows Menu and assigning it to the new Menu.
Specifying the login user sale team as domain parameter is not possible but there is another way in which we can achieve this. ie; in my view action i specify the domain as:
<field name="domain">[('user_id.default_section_id', 'in', user_sale_team())]</field>
where user_id is the responsible user of the task.
Now inherit read function of ir.actions.act_window and check if user_sale_team() is present in the domain of read result and replace this with the login user sale team id. This can be done as:
class ir_action_window(osv.osv):
_inherit = 'ir.actions.act_window'
def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
user_pool = self.pool.get('res.users')
obj_user = user_pool.browse(cr, uid, uid, context=context)
res = super(ir_action_window, self).read(cr, uid, ids, fields=fields, context=context, load=load)
if not isinstance(res, list):
res = [res]
sale_team_id = obj_user.default_section_id and obj_user.default_section_id.id or''
for r in res:
mystring = 'user_sale_team()'
if mystring in (r.get('domain', '[]') or ''):
r['domain'] = r['domain'].replace(mystring, str([sale_team_id]))
if isinstance(ids, (int, long)):
if res:
return res[0]
else:
return False
return res
ir_action_window()
This filters the result of the task to be displayed to each user based on his/her sale team.
Hope this helps.....