读取生产线和仓库的产品数量

Read product qty on lines and on warehouses

我需要检查我的生产线、我拥有的产品、它们各自的数量,并了解仓库中此类产品的可用性,stock.movestock.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.orderorder_lines One2many 字段中检查 isbn 是一种产品,在系统的所有位置上有多少可用,另外,将它与 qty 字段进行比较,因此,从那里我可以转到对象的另一个状态。

想想 stock.pickingstock.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 字段够了,什么都不做,如果有,再进入下一个状态。

首先,如果你想检查数据是否正确,请使用@api.constrains而不是@api.depends,如果用于计算则使用@api.depends

从什么是看到 isbn 是 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'))

关于数量,我不明白你到底需要什么,但我认为这个答案会对你有很大帮助。

你需要做的就是这样。

如果您只想向用户显示警告而不阻止他工作,请使用 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 .