Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

库存明细报表的实现



  • OE当前的功能里,查询库存要么是所有产品所有库位一张表,要么是一个产品在不同库位的数量分布,无法在一张表上实现多个产品多个库位的明细。
    于是就有了如下的代码:
    [code]# -- coding: utf-8 --
    from openerp.osv import osv,fields
    from lxml import etree
    from openerp import tools


    class product_product(osv.osv):
        _inherit = "product.product"

        def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
            res = super(product_product, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
            if context.get('inventory_list') == True and view_type == 'tree':
                loc_obj = self.pool.get('stock.location')
                locations = loc_obj.browse(cr, uid, loc_obj.search(cr, uid, [('child_ids', '=', False), ('usage', '=', 'internal')]))
                view = etree.fromstring(res['arch'])
                fields_list = res['fields']
                for location in locations:
                    fields_list[tools.ustr(location.id)+'-real'] = {'type': 'float', 'string': location.name+'-实际库存'}
                    fields_list[tools.ustr(location.id)+'-virtual'] = {'type': 'float', 'string': location.name+'-账面库存'}
                    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real'})
                    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual'})
                res['arch'] = etree.tostring(view)
            return res

        def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
            result = super(product_product, self).read(cr, uid, ids, fields=fields, context=context)
            if context.get('inventory_list') == True:
                locations = []
                for field in fields:
                    s = field.split('-')
                    if len(s) == 2:
                        if s[1] == 'real':
                            locations.append(int(s[0]))
                i = 0
                for id in ids:
                    for location in locations:
                        context['location'] = location
                        val = super(product_product, self).read(cr, uid, id, ['qty_available', 'virtual_available'], context, )
                        result[i][tools.ustr(location)+'-real'] = val['qty_available']
                        result[i][tools.ustr(location)+'-virtual'] = val['virtual_available']
                    i += 1
            return result[/code]

    实现的效果附件




  • OE当前的功能里,查询库存要么是所有产品所有库位一张表,要么是一个产品在不同库位的数量分布,无法在一张表上实现多个产品多个库位的明细。
    于是就有了如下的代码:
    [code]# -- coding: utf-8 --
    from openerp.osv import osv,fields
    from lxml import etree
    from openerp import tools


    class product_product(osv.osv):
        _inherit = "product.product"

        def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
            res = super(product_product, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar=toolbar, submenu=submenu)
            if context.get('inventory_list') == True and view_type == 'tree':
                loc_obj = self.pool.get('stock.location')
                locations = loc_obj.browse(cr, uid, loc_obj.search(cr, uid, [('child_ids', '=', False), ('usage', '=', 'internal')]))
                view = etree.fromstring(res['arch'])
                fields_list = res['fields']
                for location in locations:
                    fields_list[tools.ustr(location.id)+'-real'] = {'type': 'float', 'string': location.name+'-实际库存'}
                    fields_list[tools.ustr(location.id)+'-virtual'] = {'type': 'float', 'string': location.name+'-账面库存'}
                    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real'})
                    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual'})
                res['arch'] = etree.tostring(view)
            return res

        def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
            result = super(product_product, self).read(cr, uid, ids, fields=fields, context=context)
            if context.get('inventory_list') == True:
                locations = []
                for field in fields:
                    s = field.split('-')
                    if len(s) == 2:
                        if s[1] == 'real':
                            locations.append(int(s[0]))
                i = 0
                for id in ids:
                    for location in locations:
                        context['location'] = location
                        val = super(product_product, self).read(cr, uid, id, ['qty_available', 'virtual_available'], context, )
                        result[i][tools.ustr(location)+'-real'] = val['qty_available']
                        result[i][tools.ustr(location)+'-virtual'] = val['virtual_available']
                    i += 1
            return result[/code]

    实现的效果附件




  • 谢谢分享,

    又学了一招



  • 这个是怎么用啊。。没看懂



  • 能否讲解下怎么用呢



  • 后台代码在附件中。说明下,前台显示的二级表头不是我做的,不能上传,见谅。
    目前这个版本的效率不高,希望高手能优化下。

    模块设计思路:

    1、库存明细表是通过 fields_view_get 动态生成的。
    2、不同库位的库存根据系统stock里product.product 中的字段 qty_available,virtual_available 读出。这两个字段的值是依据context里不同库位读出。我是通过这个功能循环产品id和库位id读出的。(这个方法挺笨的)
    3、功能入口通过向导进入。向导生成的菜单如下图:
    [attachimg=2]

    如果安装出错,可能需要把 product.py里 fields_view_get()中下几句代码:
    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-real', 'th_string':location.name, 'colspan':'2'})
    etree.SubElement(view, 'field', {'name': tools.ustr(location.id)+'-virtual','colspan':'1'})
    etree.SubElement(view, 'field', {'name': 'qty_available', 'string': u'实际库存', 'th_string': u'汇总', 'colspan':'2'})
    etree.SubElement(view, 'field', {'name': 'virtual_available', 'string': u'账面库存', 'colspan':'1'})
    中的'th_string' 和'colspan' 标签去掉。



  • 感谢分享!


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待