This question has been flagged
4 Replies
5080 Views

Hi everyone,

My question started on a very simple operation: filter a combo-box field with information from another field. In my case: Lot/Serial Number from the current product.

Odoo13 on Manufaturing->Manufacture Orders->Selecting a order->Produce there is a window from wizard that shows the list of products (which are the components to produce a certain finished product) and Lot/Serial field.
Here is the problem: the field Lot/Serial Number shows all lots and serial numbers ever created for that product, does not matter if it has zero quantity. If you select one of those lots with zero quantity you may end up with negative quantities in your stock.
In order to avoid that, I am trying to filter only the lots which have quantity bigger than zero on stock. Here is my code:

class myproduceline(models.TransientModel):
_inherit = 'mrp.product.produce.line'
_check_company_auto = True

lot_id = fields.Many2one('stock.production.lot', 'Lot/Serial Number', domain=lambda self: self._get_lot_domain(), check_company=True)

@api.model
def _get_lot_domain(self):
lot_locations = self.env['stock.location'].search([('usage', '=', 'internal'), ('active', '=', True)]).ids
stock_id = self.env['stock.quant'].search([('product_id', '=', 39),('quantity', '>', 0),('location_id', '=', lot_locations)]).mapped("lot_id").ids
return [('id', 'in', stock_id)]

It worked very well for the hardcoded product id = 39. My question is how to get the current product id from the line that I just clicked on? I've tried to use self.product_id.id, but unfortunately it did not work.

Thanks.

Edit: It is not the case of using onchange, because nothing will change, this will happen basically when Odoo loads the window and the Lot/Serial Number is already in there.

http://www.bulma.com.br/img/produce screen.jpg

Avatar
Discard

Can you give some screen shot of wizard from where you are getting?

Author

Here is the screen shot of the wizard:

http://www.bulma.com.br/img/produce screen.jpg

It is a standard functionality of Odoo, I bet more people have the same problem.

Best Answer

In wizard, if product ID normal or hidden field available in that case can get self.product.id otherwise using context can get wizard current model id or ids,

rec_ids = self._context.get('active_ids', [])

 rec_ids contains  list of current model wizard selected record ids from that  can browse and get value. 

https://stackoverflow.com/questions/59457152/how-to-get-records-into-wizard-from-the-current-form-view-in-odoo

Avatar
Discard
Best Answer

In XML this should work:

'product_id', '=', product_id

I am not sure how to convert that to Python

Avatar
Discard
Author

Hi Chris TRINGHAM, thank you for your answer.

I just started learning Odoo and my knowledge is very basic. Could you please explain how do you know that 'product_id', '=', product_id would work in XML?

This form was building through wizard and once the wizard screen is loaded you cannot open Oddo Studio, so in that case did you look at the source code?

Yes, I looked at the XML for another example where Lots are selected for an item.

Author Best Answer

This might have a solution using context.

Avatar
Discard