我需要检查我的产品线,我拥有的产品,各自的数量,并了解仓库中此类产品的可用性,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
One2many字段order_lines
上检查,isbn
这是一个产品,系统的所有位置有多少可用,同样,将它与qty
字段进行比较,因此,从那里我可以转到该对象的另一个状态。
考虑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回购中的其他一些模块,虽然有类似的例程,我找不到真正适合的东西。
有这种方法,这似乎很有可能我需要:
@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
字段,什么都不做,如果有的话,那就转到下一个州。
答案 0 :(得分:1)
首先,如果要检查数据是否正确,请使用@api.constrains
而不是@api.depends
,@api.depends
进行计算。
从看到isbn is many2one到product.product所以只需要填写该字段并检查order_lines是否为空。
@api.constrains('order_lines', 'order_lines.isbn')
def checkit(self):
#actual_stock = self.env['product.product'].browse(qty_available)
for record in self:
# inside the loop use record not self
if self.order_lines:continue # if the order_lines contains one record go back and check the second record
# no need for other instruction because if the field is empty this will full
# another thing if you return the program will exit the function but you only
# checked one record what if someone user write with mutliple record
else: # here order_line is empty
raise Warning(('Enter? ?at least? ?1? ?ISBN to produce'))
但是,如果你需要保持它不需要的一些,我认为会更快。
@api.constrains('order_lines', 'order_lines.isbn')
def checkit(self):
for record in self:
# inside the loop use record not self
if self.order_lines:
found_isbn = False
for line in self.order_lines:
if line.isbn:
found_isbn = True
break # no need to check other lines.
if not found_isbn: # after the looping the lines check if the isbn is found
raise Warning(('Enter at least one ISBN to produce'))
else: # here order_line is empty
raise Warning(('Enter? ?at least? ?1? ?ISBN to produce'))
关于数量,我不明白你需要什么,但我认为这个答案会对你有所帮助。
how to get available quantity of Lot number
你需要做的就是这样。
如果您只想向用户显示警告,并且不要阻止他使用onchange
@api.onchange('order_lines.qty')
def check_quantity(self):
if self.order_lines:
for line in rec.order_lines:
if line.qty > line.isbn.qty_available:
# return warning or validation error if it's restricted .
return {'warning': {
'title': _('Warning'),
'message': _('Quantity is invalid.')
}
但是如果此操作受到限制且不应保存在数据库使用约束中:
@api.constrains('order_lines.qty')
def check_quantity(self):
for rec in self:
if rec.order_lines:
for line in rec.order_lines:
if line.qty > line.isbn.qty_available:
# raise validation error to user .