【发布时间】:2017-10-25 05:50:54
【问题描述】:
我需要检查我的生产线,我拥有的产品,它们各自的数量,并知道这些产品在仓库中的可用性,stock.move 和 stock.picking 做类似的事情,但它是旧的 api,我需要一个自定义方法。
这是我的方法:
class bsi_production_order(models.Model):
_name = 'bsi.production.order'
name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Production Date")
production_type = fields.Selection([
('budgeted','Budgeted'),
('nonbudgeted','Non Budgeted'),
('direct','Direct Order'),
], string='Type of Order', index=True,
track_visibility='onchange', copy=False,
help=" ")
notes = fields.Text(string="Notes")
order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")
warehouse_quantity = fields.Char(compute='quantity', string='Quantity per warehouse')
class bsi_production_order_lines(models.Model):
_name = 'bsi.production.order.lines'
production_order = fields.Many2one('bsi.production.order', string="Production Orders")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Integer(string="Quantity")
consumed_qty = fields.Float(string="Consumed quantity")
remaining_qty = fields.Float(string="Remaining quantity")
我需要检查bsi.production.order 上的order_lines One2many 字段,isbn 是一个产品,系统的所有位置都有多少可用,另外,将其与@987654328 进行比较@ 字段,所以,我可以从那里转到对象的另一个状态。
想想stock.picking 或stock.move 对象。基本上是一样的逻辑。
到目前为止,我已经尝试过这种方法,以检查 One2many 对象上是否有任何线条。
@api.multi
@api.depends('order_lines', 'order_lines.isbn')
def checkit(self):
#actual_stock = self.env['product.product'].browse(qty_available)
for record in self:
if self.order_lines:
for line in self.order_lines:
if line.isbn:
return line.isbn
else:
raise Warning(('Enter at least 1 ISBN to produce'))
到目前为止,这个工作,检查是否有isbn在线,我还需要检查仓库是否有足够的计算,如果有,然后继续下一阶段,我只关注stock.location 部分。
我检查了库存管理 OCA repo 上的其他一些模块,虽然有类似的例程,但我找不到真正适合这个的。
有这个方法,看来很可能是我需要的:
@api.multi
@api.depends('order_lines', 'order_lines.isbn')
def quantity(self):
for record in self:
warehouse_quantity_text = ''
isbn = self.env['product.product'].sudo().search([('product_tmpl_id', '=', record.id)])
if isbn:
quant_ids = self.env['stock.quant'].sudo().search([('isbn','=',isbn[0].id),('location_id.usage','=','internal')])
t_warehouses = {}
for quant in quant_ids:
if quant.location_id:
if quant.location_id not in t_warehouses:
t_warehouses.update({quant.location_id:0})
t_warehouses[quant.location_id] += quant.qty
tt_warehouses = {}
for location in t_warehouses:
warehouse = False
location1 = location
while (not warehouse and location1):
warehouse_id = self.env['stock.warehouse'].sudo().search([('lot_stock_id','=',location1.id)])
if len(warehouse_id) > 0:
warehouse = True
else:
warehouse = False
location1 = location1.location_id
if warehouse_id:
if warehouse_id.name not in tt_warehouses:
tt_warehouses.update({warehouse_id.name:0})
tt_warehouses[warehouse_id.name] += t_warehouses[location]
for item in tt_warehouses:
if tt_warehouses[item] != 0:
warehouse_quantity_text = warehouse_quantity_text + ' ** ' + item + ': ' + str(tt_warehouses[item])
record.warehouse_quantity = warehouse_quantity_text
但它不起作用,因为它需要一个字段,而且我认为它非常复杂,必须有一种更简单的方法来进行此检查。
简而言之:我需要检查系统上的数量,将其与线上的每个isbn(产品)进行比较,这将是qty 字段,如果不够,什么也不做,如果有,然后传递到下一个状态。
【问题讨论】:
-
获取真实库存的诀窍是在特定的仓库或位置上使用一些上下文变量浏览产品。你真的必须调查this method。例如:如果您有 2 个仓库并且在销售订单上想要决定每个客户选择哪个仓库(可能是国家/地区),您需要在销售订单上提供一个仓库 many2one 字段并在上下文中提供此字段,以获取有关产品选择的警告订单行,如果没有足够的库存。