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.
Can you give some screen shot of wizard from where you are getting?
Try this: https://learnopenerp.blogspot.com/2016/10/onchange-many2one-filed-in-odoo.html
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.