Reputation: 1382
I want to forbid making product if there is no "qty_available". But this code is not working.
It works only if i change @api.constrains to @api.onchange('move_lines') but if i do it with onchange there is still possibility to save record.
as api.constrains ingores doted names, how can i make this work
class mrp_production(osv.osv):
_inherit = 'mrp.production'
@api.constrains('qty_available', 'move_lines.qty_available')
def move_lines_check(self):
for line in self.move_lines:
if line.qty_available < 1:
raise ValidationError(_('There is not enough raw material, check Quantity on hand'))
UPDATE goal
So again goal is to make Warning appear if there is no raw materials to make product from (we can't manufacture from nothing) and it should be impossible to make product if there is not enough materials.
Upvotes: 3
Views: 212
Reputation: 591
Please add below constraint to mrp.production model to restrict saving Manufacturing order, if raw material product is not enough for production.
from openerp import api
from openerp.exceptions import Warning
@api.one
@api.constrains('move_lines','bom_id')
def _check_product_stock_availability(self):
if self.move_lines:
for move in self.move_lines:
qty_available = move.product_id.with_context(location=move.location_id.id).qty_available
if qty_available < move.product_uom_qty:
raise Warning(_('There is not enough raw material, check Quantity on hand.'))
elif self.bom_id:
factor = self.product_uom._compute_qty(self.product_uom.id,self.product_qty, self.bom_id.product_uom.id)
result, result2 = self.bom_id._bom_explode(self.bom_id,self.product_id, factor / self.bom_id.product_qty, None, routing_id=self.routing_id.id)
product_obj = self.env['product.product']
for line in result:
qty_available = product_obj.browse(line.get('product_id')).with_context(location=self.location_src_id.id).qty_available
#qty_available = line.product_id.with_context(location=self.location_src_id.id).qty_available
if qty_available < line.get('product_qty'):
raise Warning(_('There is not enough raw material, check Quantity on hand for products in BOM.'))
Upvotes: 1