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

标签 python odoo odoo-8

我需要检查我的生产线、我拥有的产品、它们各自的数量,并了解仓库中此类产品的可用性、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部分进行了tuck。

查了库存管理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'))

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

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 .

关于python - 读取生产线和仓库的产品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924850/

相关文章:

python - 脱脂图像 : how to combine RGB channels?

python - 在 QscrollArea 上禁用鼠标滚轮滚动

javascript - Axios 未传递 Content-Type header

odoo - 警告 @class 的使用容易出错

html - 如何在odoo报告上添加静态字段而不循环?

odoo - 如何在 Odoo 中查看营销事件的电子邮件地址?

odoo - @api.constrains 无法正常工作

Python:由(不同的)n个空格分割

Python 没有从 arduino 串行输出中读取正确的值

python - Odoo - 如何读取/获取另一个模块中的字段值