【问题标题】:Read product qty on lines and on warehouses读取生产线和仓库的产品数量
【发布时间】:2017-10-25 05:50:54
【问题描述】:

我需要检查我的生产线,我拥有的产品,它们各自的数量,并知道这些产品在仓库中的可用性,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.order 上的order_lines One2many 字段,isbn 是一个产品,系统的所有位置都有多少可用,另外,将其与@987654328 进行比较@ 字段,所以,我可以从那里转到对象的另一个状态。

想想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 字段,如果不够,什么也不做,如果有,然后传递到下一个状态。

【问题讨论】:

  • 获取真实库存的诀窍是在特定的仓库或位置上使用一些上下文变量浏览产品。你真的必须调查this method。例如:如果您有 2 个仓库并且在销售订单上想要决定每个客户选择哪个仓库(可能是国家/地区),您需要在销售订单上提供一个仓库 many2one 字段并在上下文中提供此字段,以获取有关产品选择的警告订单行,如果没有足够的库存。

标签: python openerp odoo-8


【解决方案1】:

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

从所见 isbn is many2one to 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 .

【讨论】:

  • 非常感谢!还有一些其他问题,但我会为此提出一个新问题,再次感谢!
猜你喜欢
  • 1970-01-01
  • 2021-11-09
  • 2011-11-27
  • 1970-01-01
  • 2013-12-20
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多