stock_move.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. from openerp.tools import float_compare
  5. class stock_move(models.Model):
  6. _inherit = "stock.move"
  7. @api.multi
  8. def action_done_button(self):
  9. for move in self:
  10. if move.product_id.type == 'product' and move.location_id.usage == 'internal':
  11. uom_record = move.product_id.uom_id
  12. qty_available = self.get_location_qty(move.product_id.id, move.location_id.id)
  13. compare_qty = float_compare(qty_available, move.product_uom_qty, precision_rounding=uom_record.rounding)
  14. if compare_qty == -1:
  15. warn_msg = _('Estas intentando transferir %.2f %s de %s pero el almacén de origen posee %.2f %s disponible!') % \
  16. (move.product_uom_qty, uom_record.name,
  17. move.product_id.name,
  18. max(0,qty_available), uom_record.name)
  19. raise Warning(_("No hay stock suficiente: "),_(warn_msg))
  20. return super(stock_move, self).action_done()
  21. @api.multi
  22. def get_location_qty(self, product_id, location_id):
  23. suma = 0
  24. quant_ids = self.env['stock.quant'].search([('product_id','=', product_id),('location_id','=',location_id)])
  25. if quant_ids:
  26. for quant_id in quant_ids:
  27. suma = suma + quant_id.qty
  28. return suma