Getting the Employee id of the Log User in Odoo - 8 - odoo

Good Day I have a problem in filtering of Odoo 8 Field
I just want to Select only the Employee id of the Log User i just add this in my XML
<field name="employee_id" style = "width:500px" domain = "[('id', '=', user.id.employee_id.id)]" options="{'no_create': True}"/>
But I have an Error it says
Uncaught Error: NameError: name 'user' is not defined.
Is there a right way to get the Employee Id of a Log User here in Odoo 8 ?

To obtain the employee of the logged user I do this way:
resource = self.env['resource.resource'].search([('user_id','=',self.env.user.id)])
employee = self.env['hr.employee'].search([('resource_id','=',resource.id)])
Of course it is not easy to put it inside a domain, thus maybe you can use a stored computed field to save the user_id of the employee inside your table, then you can write the domain this way:
[('user_id', '=', uid)]

you may try with below domain:
Replace your code
[('id', '=', user.id.employee_id.id)]
with
[('employee_id.user_id','=',user.id)]
This will filter the user of employee. Based on Logged User, it filter for the Employee's User.

It can be done in much simpler way.
Try this
.py file
employee_id = fields.Many2one('hr.employee', string=u"Employé", readonly=True, store=True, default=_employee_get)
def _employee_get(self):
record = self.env['hr.employee'].search([('user_id', '=', self.env.user.login)])
return record[0]

Related

Show field based on value of another field

I've been trying to show order confirmation date in sale order tree view.
To show it I used 'date_order' like this:
<field name="date_order" string="Confirmation Date" optional="show" attrs="{'invisible':[['state','not in',['sale', 'done']]]}" />
In our setup orders are created manually but also synced from Woocommerce and in those synced orders from web shop date_order is always before the create date.
For example order is created (create_date) 10.08.2022 17:10:20 and confirmed automatically (date_order) 10.08.2022 17:10:11
Somehow, it is confirmed 9s before it is created. Now, i'd like to show date_order only when it's value is > create_date. In other cases i'd like to display create_date in it's place.
Tryed to use attrs for this, I'm not sure if it's even possible:
<field name="date_order" string="Confirmation Date" optional="show" attrs="{'invisible':['&', ['state','not in',['sale', 'done']], ['date_order'], '>', ['create_date']]}" />
The code above gives XMLSyntaxError. Not sure if it's possible to compare values in that manner - and finally how to get one or the other value - I guess my second approach is maybe better.
In second approach I tried to create compute field, like this:
date_order_mine = fields.Char("Potvrdjeeeno", compute="comp_date")
#api.depends('create_date', 'date_order', 'order_line')
def comp_date(self):
for order in self:
for line in order.order_line:
if line.create_date < line.date_order:
return line.date_order
else:
return line.create_date
This code gives me AttributeError: 'sale.order.line' object has no attribute 'date_order'
Since I'm so new to Odoo and Python dev I'm not sure what should I do here to compare values of this fields and to return one or other based on conditions - if someone can help I will appreciate it.
The domain used in attrs is not valid, domains should be a list of criteria, each criterion being a triple (either a list or a tuple) of:
(field_name, operator, value)
In your second approach, the date_order field is on the parent model sale.order (order) and to access the date order from order lines , use the order_id field like following:
line.order_id.date_order
To set the value of date_order_mine to create_date or date_order you do not need the order lines and also the computed method should assign the compute value:
Computed Fields
Fields can be computed (instead of read straight from the database) using the compute parameter. It must assign the computed value to the field.
Example:
date_order_mine = fields.Datetime("Potvrdjeeeno", compute="comp_date")
#api.depends('create_date', 'date_order')
def comp_date(self):
for order in self:
if order.create_date < order.date_order:
order.date_order_mine = order.date_order
else:
order.date_order_mine = order.create_date

How to add a domain to a Many2one field in Odoo?

I have a custom module to manage students and groups, each group has a list of members (student_ids) and a president (manager_id). The manager should be selected among the list of members. So was traying to add a domain to manager_id field domain="[('id','in',self.student_ids.ids)], so the user can only select a manager from the existing list of students. But that code is giving me an error. What should I do to add that domain to the manager_id field?
class Groups(models.Model):
_name = 'estudiantes.group'
_description = 'Permite manejar los grupos a los que pertenecen los estudiantes'
manager_id = fields.Many2one('estudiantes.student',string='Responsable', domain="[('id','in',self.student_ids.ids)]")
student_ids = fields.Many2many('estudiantes.student', string='Estudiantes')
self is undefined, you can't use it. You should see the following error:
NameError: name 'self' is not defined
You do not need to specify the ids attribute when using an x2many field in the domain, it will be evaluated to a list of ids and if you do that Odoo will raise the following error message:
AttributeError: object has no attribute 'ids'
You have just to specify the field name:
domain="[('id', 'in', student_ids)]"

Load invoices of partner,parent and all childs

I have selected partner and I have field invoice_ids. when I click add object I want that only invoices of partner, parent and all his child_ids invoice would be loaded. I tried to add the domain as you see above but I get an error
Uncaught Error: AttributeError: object has no attribute 'parent_id'
and I tried it without domain but with onchange but problem is that when i create record and chose partner onchnage starts to work but the is no partner_id yet when i select partner it's still no partner because it is not saved.. so need some help here.
'invoice_ids': fields.many2many(
'account.invoice', 'cash_receipt_invoce_rel',
'cash_receipt_id', 'invoice_id', "Invoices",
domain="[('partner_id','in', partner_id.parent_id.child_ids)]",
def onchange_field_id(self, cr, uid, ids, name, context=None):
cash_rep = self.browse(cr,uid, ids,context=context)
relation_ids = [x.id for x in cash_rep.partner_id.child_ids]
return {'domain': {'invoice_ids': [('partner_id', 'in', relation_ids)]}}
To get invoices of selected partner, parent of that partner and child of that partner then you can write as following.
'invoice_ids': fields.many2many(
'account.invoice', 'cash_receipt_invoce_rel',
'cash_receipt_id', 'invoice_id', "Invoices",
domain="['|','|',('partner_id.child_ids','in',[partner_id]),('partner_id','=', partner_id),('partner_id','child_of',partner_id)]",

Odoo 8 : how to link a Many2one field automatically with the parent model?

I created a new model 'sale.order.category' in order to group Sale Order Lines in specific subcategories (allowing to display subtotals, etc.)
class SaleOrderCategory(models.Model):
_name = 'sale.order.category'
name = fields.Char('Name', required=True)
line_ids = fields.One2many('sale.order.line', 'category_id', 'Order Lines in this category')
order_id = fields.Many2one('sale.order', 'Order', required=True, readonly=True)
class SaleOrder(models.Model):
_name = 'sale.order'
_inherit = 'sale.order'
order_category_ids = fields.One2many('sale.order.category', 'order_id', 'Categories in this order', readonly=True, copy=True)
Just for info, here is my Order lines tree view modification to add the Category column :
<!-- adds a category column in the order lines list -->
<xpath expr="//field[#name='order_line']/tree/field[#name='name']" position="after">
<field name="category_id"/>
</xpath>
My question is : how can I automatically populate the order_id field with the current Sales Order ID when I create a new Category through the Order Lines Tree (inside a Sales Order) ?
Many thanks,
Max
Preliminary remark: your use case seems related to what the official sale_layout module does, so you might want to have a look at it before going any further. Perhaps you could extend it instead of starting from scratch.
Next, the most basic answer to your question is to pass a default value for the order_id field of your sale.order.category model when you create it from the view. You can do that by setting a context with an appropriate default value on the many2one field from which you will create the value:
<xpath expr="//field[#name='order_line']/tree/field[#name='name']" position="after">
<field name="category_id" context="{'default_order_id': parent.id}"/>
</xpath>
Your category_id field is defined on the sale.order.line tree view, so parent will dynamically refer to the parent record inside the web client interface, here the sale.order.
However this option will not work well:
When you're creating a new sales order, you will have to create your categories before the sales order is even saved, so there is no possible value for order_id yet. For this reason, you cannot make order_id required, and you will have to set its value again later, or you will need to save your orders before starting to add the categories.
You already have an order_lines one2many field in your sale.order.category model. The order_id field is redundant with the line_ids field, because all lines presumably belong to the same order.
A simple alternative would be to entirely omit the order_id field (use lines_id[0].order_id when you need it), or to replace it with a related field that will be automatically computed from the lines (it will take the value from the first order line):
order_id = fields.Many2one('sale.order', related='line_ids.order_id', readonly=True)
What you should do depends on your requirements, it's difficult to say based only on your question.

Odoo custom filter domain field on load view

I'm making a module for reservations in Odoo 9 and one field of my model is populated based if it's reserved or no. Basically my model is:
class Reservation(models.Model):
....
room_id = fields.Many2one('reservation.room', string="Room")
I've defined an onchange function that return a domain to filter the room_ids that aren't reserved:
#api.onchange('date')
def _set_available_room(self):
.....
return {'domain': {'room_id': [('id', 'in', res)]}}
This works fine and when I set the date, the rooms are filtered ok. My problem is when I save a reservation and enter again to edit it. The room_id field show all values and only when I change the date the room_id is filtered.
I've tried using the domain attribute in the field definition like this, but it doesn't works:
room_id = fields.Many2one('reservation.room', string="Room", domain=lambda self: self._get_available_slots())
How can I filter this field on the load view using my function than search for available rooms?