3

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.

Chaban33
  • 1,362
  • 11
  • 38
  • You know, that these constrains trigger on save? – CZoellner Jun 12 '18 at 07:56
  • Yes but i still can save record – Chaban33 Jun 12 '18 at 08:01
  • Then just dont use self.qty_available=123. Use self.write({'wty_available':123}) –  Jun 12 '18 at 08:24
  • qty_available is field that is in move_lines.qty_available that's why i'm struggling with answer – Chaban33 Jun 12 '18 at 08:26
  • Could you please write down your preferred process behind this "protection"? Because i would write an onchange trigger on the bom field, which should check all product availability of its bom line products. If something isn't available, just give out a warning und set the bom to False (empty) afterwards. – CZoellner Jun 12 '18 at 08:47
  • The goal is to make Warning appear if there is no raw materials to make product from. it was just my try to solve the problem. i would be really appreciated if you can post your solution – Chaban33 Jun 12 '18 at 09:02
  • But does constrains work with onchange when you call self.write? Then just call method move_lines_check in onchange method. Then it should work well. –  Jun 12 '18 at 12:39
  • Guys any suggestions? – Chaban33 Jun 14 '18 at 08:27

1 Answers1

1

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.'))
Nilesh Sheliya
  • 591
  • 2
  • 10