1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- # -*- coding: utf-8 -*-
- from openerp import api, fields, models
- from collections import deque
- class StockQuantity(models.Model):
- _inherit = 'stock.quant'
- @api.model
- def get_qty_available(self, location_id, location_ids=None, product_ids=None):
- if location_id:
- root_location = self.env['stock.location'].search([('id', '=', location_id)])
- all_location = [root_location.id]
- queue = deque([])
- self.location_traversal(queue, all_location, root_location)
- stock_quant = self.search_read([('location_id', 'in', all_location)], ['product_id', 'qty', 'location_id'])
- return stock_quant
- else:
- stock_quant = self.search_read([('location_id', 'in', location_ids), ('product_id', 'in', product_ids)],
- ['product_id', 'qty', 'location_id'])
- return stock_quant
- def location_traversal(self, queue, res, root):
- for child in root.child_ids:
- if child.usage == 'internal':
- queue.append(child)
- res.append(child.id)
- while queue:
- pick = queue.popleft()
- res.append(pick.id)
- self.location_traversal(queue, res, pick)
- @api.model
- def create(self, vals):
- res = super(StockQuantity, self).create(vals)
- if res.location_id.usage == 'internal':
- self.env['pos.stock.channel'].broadcast(res)
- return res
- @api.multi
- def write(self, vals):
- res = super(StockQuantity, self).write(vals)
- records = self.filtered(lambda x: x.location_id.usage == 'internal')
- if len(records) > 0:
- self.env['pos.stock.channel'].broadcast(records)
- return res
- class PosConfig(models.Model):
- _inherit = 'pos.config'
- show_qty_available = fields.Boolean(string='Display Stock in POS')
- location_only = fields.Boolean(string='Only in POS Location')
- allow_out_of_stock = fields.Boolean(string='Allow Out-of-Stock')
- limit_qty = fields.Integer(string='Deny Order when Quantity Available lower than')
- hide_product = fields.Boolean(string='Hide Products not in POS Location')
- class PosStockChannel(models.TransientModel):
- _name = 'pos.stock.channel'
- def broadcast(self, stock_quant):
- data = stock_quant.read(['product_id', 'location_id', 'qty'])
- self.env['bus.bus'].sendone((self._cr.dbname, 'pos.stock.channel'), data)
|