请将以下约束添加到
MRP.生产
如果原材料产品不足以生产,则限制节省生产订单的模型。
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.'))