pos_stock.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models
  3. from collections import deque
  4. class StockQuantity(models.Model):
  5. _inherit = 'stock.quant'
  6. @api.model
  7. def get_qty_available(self, location_id, location_ids=None, product_ids=None):
  8. if location_id:
  9. root_location = self.env['stock.location'].search([('id', '=', location_id)])
  10. all_location = [root_location.id]
  11. queue = deque([])
  12. self.location_traversal(queue, all_location, root_location)
  13. stock_quant = self.search_read([('location_id', 'in', all_location)], ['product_id', 'qty', 'location_id'])
  14. return stock_quant
  15. else:
  16. stock_quant = self.search_read([('location_id', 'in', location_ids), ('product_id', 'in', product_ids)],
  17. ['product_id', 'qty', 'location_id'])
  18. return stock_quant
  19. def location_traversal(self, queue, res, root):
  20. for child in root.child_ids:
  21. if child.usage == 'internal':
  22. queue.append(child)
  23. res.append(child.id)
  24. while queue:
  25. pick = queue.popleft()
  26. res.append(pick.id)
  27. self.location_traversal(queue, res, pick)
  28. @api.model
  29. def create(self, vals):
  30. res = super(StockQuantity, self).create(vals)
  31. if res.location_id.usage == 'internal':
  32. self.env['pos.stock.channel'].broadcast(res)
  33. return res
  34. @api.multi
  35. def write(self, vals):
  36. res = super(StockQuantity, self).write(vals)
  37. records = self.filtered(lambda x: x.location_id.usage == 'internal')
  38. if len(records) > 0:
  39. self.env['pos.stock.channel'].broadcast(records)
  40. return res
  41. class PosConfig(models.Model):
  42. _inherit = 'pos.config'
  43. show_qty_available = fields.Boolean(string='Display Stock in POS')
  44. location_only = fields.Boolean(string='Only in POS Location')
  45. allow_out_of_stock = fields.Boolean(string='Allow Out-of-Stock')
  46. limit_qty = fields.Integer(string='Deny Order when Quantity Available lower than')
  47. hide_product = fields.Boolean(string='Hide Products not in POS Location')
  48. class PosStockChannel(models.TransientModel):
  49. _name = 'pos.stock.channel'
  50. def broadcast(self, stock_quant):
  51. data = stock_quant.read(['product_id', 'location_id', 'qty'])
  52. self.env['bus.bus'].sendone((self._cr.dbname, 'pos.stock.channel'), data)